From 8e6ed1e678a94032ae2197869cd0ab527cbadacf Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Tue, 22 Oct 2024 11:01:03 +0200 Subject: [PATCH 1/2] update the node controler unit test --- .../controller/node/node_controller_test.go | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/operator/staticpod/controller/node/node_controller_test.go b/pkg/operator/staticpod/controller/node/node_controller_test.go index 8536b03e46..78fe7e7292 100644 --- a/pkg/operator/staticpod/controller/node/node_controller_test.go +++ b/pkg/operator/staticpod/controller/node/node_controller_test.go @@ -82,11 +82,11 @@ func validateJSONPatch(expected, actual *jsonpatch.PatchSet) error { func TestNodeControllerDegradedConditionType(t *testing.T) { scenarios := []struct { - name string - masterNodes []runtime.Object - existingNodeStatuses []operatorv1.NodeStatus - existingConditions []operatorv1.OperatorCondition - triggerStatusApplyError error + name string + masterNodes []runtime.Object + existingNodeStatuses []operatorv1.NodeStatus + existingConditions []operatorv1.OperatorCondition + triggerStatusApplyErrorFn func(rv string, spec *operatorv1.StaticPodOperatorStatus) error verifyNodeStatus func([]operatorv1.OperatorCondition) error verifyJSONPatch func(*jsonpatch.PatchSet) error @@ -197,7 +197,16 @@ func TestNodeControllerDegradedConditionType(t *testing.T) { NodeName: "test-node-4", }, }, - triggerStatusApplyError: fmt.Errorf("nasty err"), + triggerStatusApplyErrorFn: func() func(rv string, spec *operatorv1.StaticPodOperatorStatus) error { + var counter int + return func(rv string, spec *operatorv1.StaticPodOperatorStatus) error { + if counter == 0 { + counter++ + return fmt.Errorf("nasty err") + } + return nil + } + }(), verifyNodeStatus: func(conditions []operatorv1.OperatorCondition) error { var expectedCondition operatorv1.OperatorCondition expectedCondition.Type = condition.NodeControllerDegradedConditionType @@ -442,11 +451,9 @@ func TestNodeControllerDegradedConditionType(t *testing.T) { kubeClient := fake.NewSimpleClientset(scenario.masterNodes...) fakeLister := v1helpers.NewFakeNodeLister(kubeClient) - var triggerStatusUpdateError func(rv string, spec *operatorv1.StaticPodOperatorStatus) error - if scenario.triggerStatusApplyError != nil { - triggerStatusUpdateError = func(rv string, spec *operatorv1.StaticPodOperatorStatus) error { - return scenario.triggerStatusApplyError - } + var triggerStatusUpdateErrorFn func(rv string, spec *operatorv1.StaticPodOperatorStatus) error + if scenario.triggerStatusApplyErrorFn != nil { + triggerStatusUpdateErrorFn = scenario.triggerStatusApplyErrorFn } fakeStaticPodOperatorClient := v1helpers.NewFakeStaticPodOperatorClient( &operatorv1.StaticPodOperatorSpec{ @@ -461,7 +468,7 @@ func TestNodeControllerDegradedConditionType(t *testing.T) { }, NodeStatuses: scenario.existingNodeStatuses, }, - triggerStatusUpdateError, + triggerStatusUpdateErrorFn, nil, ) From 2913b2c8fee3a004c90c07c5b546051ab31ea12b Mon Sep 17 00:00:00 2001 From: Lukasz Szaszkiewicz Date: Tue, 22 Oct 2024 12:52:39 +0200 Subject: [PATCH 2/2] test_helpers: add ability to trigger an error on appling operator's status --- pkg/operator/v1helpers/test_helpers.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/operator/v1helpers/test_helpers.go b/pkg/operator/v1helpers/test_helpers.go index 5f37e5255c..60dcb09bc0 100644 --- a/pkg/operator/v1helpers/test_helpers.go +++ b/pkg/operator/v1helpers/test_helpers.go @@ -183,6 +183,12 @@ func (c *fakeStaticPodOperatorClient) ApplyStaticPodOperatorSpec(ctx context.Con } func (c *fakeStaticPodOperatorClient) ApplyStaticPodOperatorStatus(ctx context.Context, fieldManager string, applyConfiguration *applyoperatorv1.StaticPodOperatorStatusApplyConfiguration) (err error) { + if c.triggerStatusUpdateError != nil { + operatorStatus := convertStaticPodOperatorStatusApplyConfiguration(applyConfiguration) + if err := c.triggerStatusUpdateError("", operatorStatus); err != nil { + return err + } + } c.fakeStaticPodOperatorStatus = convertStaticPodOperatorStatusApplyConfiguration(applyConfiguration) return nil }