diff --git a/pkg/common/testing/assertion.go b/pkg/common/testing/assertion.go new file mode 100644 index 000000000..7afa98acf --- /dev/null +++ b/pkg/common/testing/assertion.go @@ -0,0 +1,167 @@ +package testing + +import ( + "github.com/davecgh/go-spew/spew" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" + clienttesting "k8s.io/client-go/testing" + "strings" + "testing" +) + +// AssertError asserts the actual error representation is the same with the expected, +// if the expected error representation is empty, the actual should be nil +func AssertError(t *testing.T, actual error, expectedErr string) { + t.Helper() + if len(expectedErr) > 0 && actual == nil { + t.Errorf("expected %q error", expectedErr) + return + } + if len(expectedErr) > 0 && actual != nil && actual.Error() != expectedErr { + t.Errorf("expected %q error, but got %q", expectedErr, actual.Error()) + return + } + if len(expectedErr) == 0 && actual != nil { + t.Errorf("unexpected err: %v", actual) + return + } +} + +// AssertError asserts the actual error representation starts with the expected prerfix, +// if the expected error prefix is empty, the actual should be nil +func AssertErrorWithPrefix(t *testing.T, actual error, expectedErrorPrefix string) { + t.Helper() + if len(expectedErrorPrefix) > 0 && actual == nil { + t.Errorf("expected error with prefix %q", expectedErrorPrefix) + return + } + if len(expectedErrorPrefix) > 0 && actual != nil && !strings.HasPrefix(actual.Error(), expectedErrorPrefix) { + t.Errorf("expected error with prefix %q, but got %q", expectedErrorPrefix, actual.Error()) + return + } + if len(expectedErrorPrefix) == 0 && actual != nil { + t.Errorf("unexpected err: %v", actual) + return + } +} + +// AssertActions asserts the actual actions have the expected action verb +func AssertActions(t *testing.T, actualActions []clienttesting.Action, expectedVerbs ...string) { + t.Helper() + if len(actualActions) != len(expectedVerbs) { + t.Fatalf("expected %d call but got: %#v", len(expectedVerbs), actualActions) + } + for i, expected := range expectedVerbs { + if actualActions[i].GetVerb() != expected { + t.Errorf("expected %s action but got: %#v", expected, actualActions[i]) + } + } +} + +// AssertNoActions asserts no actions are happened +func AssertNoActions(t *testing.T, actualActions []clienttesting.Action) { + t.Helper() + AssertActions(t, actualActions) +} + +func AssertAction(t *testing.T, actual clienttesting.Action, expected string) { + t.Helper() + if actual.GetVerb() != expected { + t.Errorf("expected %s action but got: %#v", expected, actual) + } +} + +func AssertGet(t *testing.T, actual clienttesting.Action, group, version, resource string) { + t.Helper() + if actual.GetVerb() != "get" { + t.Error(spew.Sdump(actual)) + } + if actual.GetResource() != (schema.GroupVersionResource{Group: group, Version: version, Resource: resource}) { + t.Error(spew.Sdump(actual)) + } +} + +func AssertDelete(t *testing.T, actual clienttesting.Action, resource, namespace, name string) { + t.Helper() + deleteAction, ok := actual.(clienttesting.DeleteAction) + if !ok { + t.Error(spew.Sdump(actual)) + } + if deleteAction.GetResource().Resource != resource { + t.Error(spew.Sdump(actual)) + } + if deleteAction.GetNamespace() != namespace { + t.Error(spew.Sdump(actual)) + } + if deleteAction.GetName() != name { + t.Error(spew.Sdump(actual)) + } +} + +// AssertUpdateActions asserts the actions are get-then-update action +func AssertUpdateActions(t *testing.T, actions []clienttesting.Action) { + t.Helper() + for i := 0; i < len(actions); i = i + 2 { + if actions[i].GetVerb() != "get" { + t.Errorf("expected action %d is get, but %v", i, actions[i]) + } + if actions[i+1].GetVerb() != "update" { + t.Errorf("expected action %d is update, but %v", i, actions[i+1]) + } + } +} + +// AssertNoMoreUpdates asserts only one update action in given actions +func AssertNoMoreUpdates(t *testing.T, actions []clienttesting.Action) { + t.Helper() + updateActions := 0 + for _, action := range actions { + if action.GetVerb() == "update" { + updateActions++ + } + } + if updateActions != 1 { + t.Errorf("expected there is only one update action, but failed") + } +} + +// AssertCondition asserts the actual conditions has +// the expected condition +func AssertCondition( + t *testing.T, + actualConditions []metav1.Condition, + expectedCondition metav1.Condition) { + t.Helper() + cond := meta.FindStatusCondition(actualConditions, expectedCondition.Type) + if cond == nil { + t.Errorf("expected condition %s but got: %s", expectedCondition.Type, cond.Type) + } + if cond.Status != expectedCondition.Status { + t.Errorf("expected status %s but got: %s", expectedCondition.Status, cond.Status) + } + if cond.Reason != expectedCondition.Reason { + t.Errorf("expected reason %s but got: %s", expectedCondition.Reason, cond.Reason) + } + if cond.Message != expectedCondition.Message { + t.Errorf("expected message %s but got: %s", expectedCondition.Message, cond.Message) + } +} + +func AssertEqualNumber(t *testing.T, actual, expected int) { + t.Helper() + if actual != expected { + t.Errorf("expected %d number of actions but got: %d", expected, actual) + } +} + +func AssertEqualNameNamespace(t *testing.T, actualName, actualNamespace, name, namespace string) { + t.Helper() + if actualName != name { + t.Errorf("Name of the object does not match, expected %s, actual %s", name, actualName) + } + + if actualNamespace != namespace { + t.Errorf("Namespace of the object does not match, expected %s, actual %s", namespace, actualNamespace) + } +} diff --git a/pkg/common/testing/fake_sync_context.go b/pkg/common/testing/fake_sync_context.go new file mode 100644 index 000000000..e6966e1d5 --- /dev/null +++ b/pkg/common/testing/fake_sync_context.go @@ -0,0 +1,26 @@ +package testing + +import ( + "github.com/openshift/library-go/pkg/operator/events" + "github.com/openshift/library-go/pkg/operator/events/eventstesting" + "k8s.io/client-go/util/workqueue" + "testing" +) + +type FakeSyncContext struct { + spokeName string + recorder events.Recorder + queue workqueue.RateLimitingInterface +} + +func (f FakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue } +func (f FakeSyncContext) QueueKey() string { return f.spokeName } +func (f FakeSyncContext) Recorder() events.Recorder { return f.recorder } + +func NewFakeSyncContext(t *testing.T, clusterName string) *FakeSyncContext { + return &FakeSyncContext{ + spokeName: clusterName, + recorder: eventstesting.NewTestingEventRecorder(t), + queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), + } +} diff --git a/pkg/placement/controllers/scheduling/cluster_event_handler_test.go b/pkg/placement/controllers/scheduling/cluster_event_handler_test.go index aae24c962..5490d0b56 100644 --- a/pkg/placement/controllers/scheduling/cluster_event_handler_test.go +++ b/pkg/placement/controllers/scheduling/cluster_event_handler_test.go @@ -14,6 +14,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/placement/helpers/testing" ) @@ -96,7 +97,7 @@ func TestOnClusterChange(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), @@ -255,7 +256,7 @@ func TestOnClusterUpdate(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), @@ -355,7 +356,7 @@ func TestOnClusterDelete(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), diff --git a/pkg/placement/controllers/scheduling/enqueue_test.go b/pkg/placement/controllers/scheduling/enqueue_test.go index 0181a38c3..736a8fcd4 100644 --- a/pkg/placement/controllers/scheduling/enqueue_test.go +++ b/pkg/placement/controllers/scheduling/enqueue_test.go @@ -17,6 +17,7 @@ import ( clusterapiv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1" clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1" clusterapiv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/placement/helpers/testing" ) @@ -174,7 +175,7 @@ func TestEnqueuePlacementsByClusterSet(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), @@ -281,7 +282,7 @@ func TestEnqueuePlacementsByClusterSetBinding(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), @@ -369,7 +370,7 @@ func TestEnqueuePlacementsByScore(t *testing.T) { clusterClient := clusterfake.NewSimpleClientset(c.initObjs...) clusterInformerFactory := newClusterInformerFactory(clusterClient, c.initObjs...) - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") q := newEnqueuer( syncCtx.Queue(), clusterInformerFactory.Cluster().V1().ManagedClusters(), diff --git a/pkg/placement/controllers/scheduling/scheduling_controller_test.go b/pkg/placement/controllers/scheduling/scheduling_controller_test.go index 411520f16..1530cbe6f 100644 --- a/pkg/placement/controllers/scheduling/scheduling_controller_test.go +++ b/pkg/placement/controllers/scheduling/scheduling_controller_test.go @@ -3,6 +3,8 @@ package scheduling import ( "context" "fmt" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" + "open-cluster-management.io/ocm/test/integration/util" "sort" "strings" "testing" @@ -62,7 +64,7 @@ func TestSchedulingController_sync(t *testing.T) { }, validateActions: func(t *testing.T, actions []clienttesting.Action) { // check if PlacementDecision has been updated - testinghelpers.AssertActions(t, actions, "create", "update", "update") + testingcommon.AssertActions(t, actions, "create", "update", "update") // check if Placement has been updated actual := actions[2].(clienttesting.UpdateActionImpl).Object placement, ok := actual.(*clusterapiv1beta1.Placement) @@ -73,7 +75,7 @@ func TestSchedulingController_sync(t *testing.T) { if placement.Status.NumberOfSelectedClusters != int32(3) { t.Errorf("expecte %d cluster selected, but got %d", 3, placement.Status.NumberOfSelectedClusters) } - testinghelpers.HasCondition( + util.HasCondition( placement.Status.Conditions, clusterapiv1beta1.PlacementConditionSatisfied, "AllDecisionsScheduled", @@ -104,7 +106,7 @@ func TestSchedulingController_sync(t *testing.T) { }, validateActions: func(t *testing.T, actions []clienttesting.Action) { // check if PlacementDecision has been updated - testinghelpers.AssertActions(t, actions, "create", "update", "update") + testingcommon.AssertActions(t, actions, "create", "update", "update") // check if Placement has been updated actual := actions[2].(clienttesting.UpdateActionImpl).Object placement, ok := actual.(*clusterapiv1beta1.Placement) @@ -115,7 +117,7 @@ func TestSchedulingController_sync(t *testing.T) { if placement.Status.NumberOfSelectedClusters != int32(3) { t.Errorf("expecte %d cluster selected, but got %d", 3, placement.Status.NumberOfSelectedClusters) } - testinghelpers.HasCondition( + util.HasCondition( placement.Status.Conditions, clusterapiv1beta1.PlacementConditionSatisfied, "NotAllDecisionsScheduled", @@ -133,7 +135,7 @@ func TestSchedulingController_sync(t *testing.T) { }, validateActions: func(t *testing.T, actions []clienttesting.Action) { // check if PlacementDecision has been updated - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") // check if emtpy PlacementDecision has been created actual := actions[0].(clienttesting.CreateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) @@ -154,7 +156,7 @@ func TestSchedulingController_sync(t *testing.T) { if placement.Status.NumberOfSelectedClusters != int32(0) { t.Errorf("expecte %d cluster selected, but got %d", 0, placement.Status.NumberOfSelectedClusters) } - testinghelpers.HasCondition( + util.HasCondition( placement.Status.Conditions, clusterapiv1beta1.PlacementConditionSatisfied, "NoManagedClusterSetBindings", @@ -176,7 +178,7 @@ func TestSchedulingController_sync(t *testing.T) { }, validateActions: func(t *testing.T, actions []clienttesting.Action) { // check if PlacementDecision has been updated - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") // check if emtpy PlacementDecision has been created actual := actions[0].(clienttesting.CreateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) @@ -197,7 +199,7 @@ func TestSchedulingController_sync(t *testing.T) { if placement.Status.NumberOfSelectedClusters != int32(0) { t.Errorf("expecte %d cluster selected, but got %d", 0, placement.Status.NumberOfSelectedClusters) } - testinghelpers.HasCondition( + util.HasCondition( placement.Status.Conditions, clusterapiv1beta1.PlacementConditionSatisfied, "AllManagedClusterSetsEmpty", @@ -222,7 +224,7 @@ func TestSchedulingController_sync(t *testing.T) { }, validateActions: func(t *testing.T, actions []clienttesting.Action) { // check if PlacementDecision has been updated - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") // check if emtpy PlacementDecision has been created actual := actions[0].(clienttesting.CreateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) @@ -243,7 +245,7 @@ func TestSchedulingController_sync(t *testing.T) { if placement.Status.NumberOfSelectedClusters != int32(0) { t.Errorf("expecte %d cluster selected, but got %d", 0, placement.Status.NumberOfSelectedClusters) } - testinghelpers.HasCondition( + util.HasCondition( placement.Status.Conditions, clusterapiv1beta1.PlacementConditionSatisfied, "NoManagedClusterMatched", @@ -276,7 +278,7 @@ func TestSchedulingController_sync(t *testing.T) { }, unscheduledDecisions: 0, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "placement schedule controller is disabled", @@ -292,7 +294,7 @@ func TestSchedulingController_sync(t *testing.T) { }, scheduledDecisions: []clusterapiv1beta1.ClusterDecision{}, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, } @@ -314,7 +316,7 @@ func TestSchedulingController_sync(t *testing.T) { recorder: kevents.NewFakeRecorder(100), } - sysCtx := testinghelpers.NewFakeSyncContext(t, c.placement.Namespace+"/"+c.placement.Name) + sysCtx := testingcommon.NewFakeSyncContext(t, c.placement.Namespace+"/"+c.placement.Name) syncErr := ctrl.sync(context.TODO(), sysCtx) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) @@ -737,7 +739,7 @@ func TestBind(t *testing.T) { name: "create single placementdecision", clusterDecisions: newClusterDecisions(10), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") actual := actions[1].(clienttesting.UpdateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) if !ok { @@ -750,7 +752,7 @@ func TestBind(t *testing.T) { name: "create multiple placementdecisions", clusterDecisions: newClusterDecisions(101), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create", "update", "create", "update") + testingcommon.AssertActions(t, actions, "create", "update", "create", "update") selectedClusters := newSelectedClusters(101) actual := actions[1].(clienttesting.UpdateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) @@ -771,7 +773,7 @@ func TestBind(t *testing.T) { name: "create empty placementdecision", clusterDecisions: newClusterDecisions(0), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create") + testingcommon.AssertActions(t, actions, "create") actual := actions[0].(clienttesting.CreateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) if !ok { @@ -793,7 +795,7 @@ func TestBind(t *testing.T) { WithLabel(placementLabel, placementName). WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "update one of placementdecisions", @@ -804,7 +806,7 @@ func TestBind(t *testing.T) { WithDecisions(newSelectedClusters(128)[:100]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") selectedClusters := newSelectedClusters(128) actual := actions[1].(clienttesting.UpdateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) @@ -826,7 +828,7 @@ func TestBind(t *testing.T) { WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update", "delete") + testingcommon.AssertActions(t, actions, "update", "delete") actual := actions[0].(clienttesting.UpdateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) if !ok { @@ -847,7 +849,7 @@ func TestBind(t *testing.T) { WithDecisions(newSelectedClusters(128)[100:]...).Build(), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update", "delete") + testingcommon.AssertActions(t, actions, "update", "delete") actual := actions[0].(clienttesting.UpdateActionImpl).Object placementDecision, ok := actual.(*clusterapiv1beta1.PlacementDecision) if !ok { diff --git a/pkg/placement/helpers/testing/helpers.go b/pkg/placement/helpers/testing/helpers.go index b3a4662c8..e47262a34 100644 --- a/pkg/placement/helpers/testing/helpers.go +++ b/pkg/placement/helpers/testing/helpers.go @@ -3,13 +3,8 @@ package testing import ( "testing" - "github.com/openshift/library-go/pkg/operator/events" - "github.com/openshift/library-go/pkg/operator/events/eventstesting" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - clienttesting "k8s.io/client-go/testing" kevents "k8s.io/client-go/tools/events" - "k8s.io/client-go/util/workqueue" clusterclient "open-cluster-management.io/api/client/cluster/clientset/versioned" clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterlisterv1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1" @@ -17,24 +12,6 @@ import ( clusterlisterv1beta1 "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1" ) -type FakeSyncContext struct { - queueKey string - queue workqueue.RateLimitingInterface - recorder events.Recorder -} - -func (f FakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue } -func (f FakeSyncContext) QueueKey() string { return f.queueKey } -func (f FakeSyncContext) Recorder() events.Recorder { return f.recorder } - -func NewFakeSyncContext(t *testing.T, queueKey string) *FakeSyncContext { - return &FakeSyncContext{ - queueKey: queueKey, - queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), - recorder: eventstesting.NewTestingEventRecorder(t), - } -} - type FakePluginHandle struct { recorder kevents.EventRecorder placementDecisionLister clusterlisterv1beta1.PlacementDecisionLister @@ -68,42 +45,3 @@ func NewFakePluginHandle( clusterLister: informers.Cluster().V1().ManagedClusters().Lister(), } } - -// AssertActions asserts the actual actions have the expected action verb -func AssertActions(t *testing.T, actualActions []clienttesting.Action, expectedVerbs ...string) { - if len(actualActions) != len(expectedVerbs) { - t.Fatalf("expected %d call but got: %#v", len(expectedVerbs), actualActions) - } - for i, expected := range expectedVerbs { - if actualActions[i].GetVerb() != expected { - t.Errorf("expected %s action but got: %#v", expected, actualActions[i]) - } - } -} - -// AssertNoActions asserts no actions are happened -func AssertNoActions(t *testing.T, actualActions []clienttesting.Action) { - AssertActions(t, actualActions) -} - -func HasCondition(conditions []metav1.Condition, expectedType, expectedReason string, expectedStatus metav1.ConditionStatus) bool { - found := false - for _, condition := range conditions { - if condition.Type != expectedType { - continue - } - found = true - - if condition.Status != expectedStatus { - return false - } - - if condition.Reason != expectedReason { - return false - } - - return true - } - - return found -} diff --git a/pkg/registration-operator/helpers/testing/assertion.go b/pkg/registration-operator/helpers/testing/assertion.go index c55a84cb9..9d8f12488 100644 --- a/pkg/registration-operator/helpers/testing/assertion.go +++ b/pkg/registration-operator/helpers/testing/assertion.go @@ -8,46 +8,10 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" - clienttesting "k8s.io/client-go/testing" - opratorapiv1 "open-cluster-management.io/api/operator/v1" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" ) -func AssertAction(t *testing.T, actual clienttesting.Action, expected string) { - if actual.GetVerb() != expected { - t.Errorf("expected %s action but got: %#v", expected, actual) - } -} - -func AssertGet(t *testing.T, actual clienttesting.Action, group, version, resource string) { - t.Helper() - if actual.GetVerb() != "get" { - t.Error(spew.Sdump(actual)) - } - if actual.GetResource() != (schema.GroupVersionResource{Group: group, Version: version, Resource: resource}) { - t.Error(spew.Sdump(actual)) - } -} - -func AssertDelete(t *testing.T, actual clienttesting.Action, resource, namespace, name string) { - t.Helper() - deleteAction, ok := actual.(clienttesting.DeleteAction) - if !ok { - t.Error(spew.Sdump(actual)) - } - if deleteAction.GetResource().Resource != resource { - t.Error(spew.Sdump(actual)) - } - if deleteAction.GetNamespace() != namespace { - t.Error(spew.Sdump(actual)) - } - if deleteAction.GetName() != name { - t.Error(spew.Sdump(actual)) - } -} - func NamedCondition(name, reason string, status metav1.ConditionStatus) metav1.Condition { return metav1.Condition{Type: name, Status: status, Reason: reason} } @@ -119,19 +83,3 @@ func AssertOnlyGenerationStatuses(t *testing.T, actual runtime.Object, expectedG } } - -func AssertEqualNumber(t *testing.T, actual, expected int) { - if actual != expected { - t.Errorf("expected %d number of actions but got: %d", expected, actual) - } -} - -func AssertEqualNameNamespace(t *testing.T, actualName, actualNamespace, name, namespace string) { - if actualName != name { - t.Errorf("Name of the object does not match, expected %s, actual %s", name, actualName) - } - - if actualNamespace != namespace { - t.Errorf("Namespace of the object does not match, expected %s, actual %s", namespace, actualNamespace) - } -} diff --git a/pkg/registration-operator/helpers/testing/helpers.go b/pkg/registration-operator/helpers/testing/helpers.go deleted file mode 100644 index 9a2b86564..000000000 --- a/pkg/registration-operator/helpers/testing/helpers.go +++ /dev/null @@ -1,27 +0,0 @@ -package testing - -import ( - "testing" - - "github.com/openshift/library-go/pkg/operator/events" - "github.com/openshift/library-go/pkg/operator/events/eventstesting" - "k8s.io/client-go/util/workqueue" -) - -type fakeSyncContext struct { - key string - queue workqueue.RateLimitingInterface - recorder events.Recorder -} - -func (f fakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue } -func (f fakeSyncContext) QueueKey() string { return f.key } -func (f fakeSyncContext) Recorder() events.Recorder { return f.recorder } - -func NewFakeSyncContext(t *testing.T, key string) *fakeSyncContext { - return &fakeSyncContext{ - key: key, - queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), - recorder: eventstesting.NewTestingEventRecorder(t), - } -} diff --git a/pkg/registration-operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller_test.go b/pkg/registration-operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller_test.go index 08bd590d7..1bc5f7a22 100644 --- a/pkg/registration-operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller_test.go +++ b/pkg/registration-operator/operators/clustermanager/controllers/certrotationcontroller/certrotation_controller_test.go @@ -21,8 +21,8 @@ import ( fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) const ( @@ -179,7 +179,7 @@ func TestCertRotation(t *testing.T) { } } - syncContext := testinghelper.NewFakeSyncContext(t, c.queueKey) + syncContext := testingcommon.NewFakeSyncContext(t, c.queueKey) recorder := syncContext.Recorder() controller := NewCertRotationController(kubeClient, secretInformers, configmapInformer, operatorInformers.Operator().V1().ClusterManagers(), recorder) diff --git a/pkg/registration-operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller_test.go b/pkg/registration-operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller_test.go index c52dd2c67..0e5288587 100644 --- a/pkg/registration-operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller_test.go +++ b/pkg/registration-operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller_test.go @@ -2,6 +2,7 @@ package clustermanagercontroller import ( "context" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "strings" "testing" "time" @@ -30,7 +31,6 @@ import ( migrationclient "sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset/typed/migration/v1alpha1" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) var ( @@ -272,7 +272,7 @@ func ensureObject(t *testing.T, object runtime.Object, hubCore *operatorapiv1.Cl switch o := object.(type) { case *corev1.Namespace: - testinghelper.AssertEqualNameNamespace(t, access.GetName(), "", helpers.ClusterManagerNamespace(hubCore.Name, hubCore.Spec.DeployOption.Mode), "") + testingcommon.AssertEqualNameNamespace(t, access.GetName(), "", helpers.ClusterManagerNamespace(hubCore.Name, hubCore.Spec.DeployOption.Mode), "") case *appsv1.Deployment: if strings.Contains(o.Name, "registration") && hubCore.Spec.RegistrationImagePullSpec != o.Spec.Template.Spec.Containers[0].Image { t.Errorf("Registration image does not match to the expected.") @@ -294,7 +294,7 @@ func TestSyncDeploy(t *testing.T) { cd := setDeployment(clusterManager.Name, clusterManagerNamespace) setup(t, tc, cd) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") err := tc.clusterManagerController.sync(ctx, syncContext) if err != nil { @@ -312,7 +312,7 @@ func TestSyncDeploy(t *testing.T) { // Check if resources are created as expected // We expect create the namespace twice respectively in the management cluster and the hub cluster. - testinghelper.AssertEqualNumber(t, len(createKubeObjects), 27) + testingcommon.AssertEqualNumber(t, len(createKubeObjects), 27) for _, object := range createKubeObjects { ensureObject(t, object, clusterManager) } @@ -326,7 +326,7 @@ func TestSyncDeploy(t *testing.T) { } } // Check if resources are created as expected - testinghelper.AssertEqualNumber(t, len(createCRDObjects), 11) + testingcommon.AssertEqualNumber(t, len(createCRDObjects), 11) } func TestSyncDeployNoWebhook(t *testing.T) { @@ -334,7 +334,7 @@ func TestSyncDeployNoWebhook(t *testing.T) { tc := newTestController(t, clusterManager) setup(t, tc, nil) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") err := tc.clusterManagerController.sync(ctx, syncContext) if err != nil { @@ -352,7 +352,7 @@ func TestSyncDeployNoWebhook(t *testing.T) { // Check if resources are created as expected // We expect create the namespace twice respectively in the management cluster and the hub cluster. - testinghelper.AssertEqualNumber(t, len(createKubeObjects), 28) + testingcommon.AssertEqualNumber(t, len(createKubeObjects), 28) for _, object := range createKubeObjects { ensureObject(t, object, clusterManager) } @@ -366,7 +366,7 @@ func TestSyncDeployNoWebhook(t *testing.T) { } } // Check if resources are created as expected - testinghelper.AssertEqualNumber(t, len(createCRDObjects), 11) + testingcommon.AssertEqualNumber(t, len(createCRDObjects), 11) } // TestSyncDelete test cleanup hub deploy @@ -378,7 +378,7 @@ func TestSyncDelete(t *testing.T) { tc := newTestController(t, clusterManager) setup(t, tc, nil) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") clusterManagerNamespace := helpers.ClusterManagerNamespace(clusterManager.Name, clusterManager.Spec.DeployOption.Mode) err := tc.clusterManagerController.sync(ctx, syncContext) @@ -394,7 +394,7 @@ func TestSyncDelete(t *testing.T) { deleteKubeActions = append(deleteKubeActions, deleteKubeAction) } } - testinghelper.AssertEqualNumber(t, len(deleteKubeActions), 27) // delete namespace both from the hub cluster and the mangement cluster + testingcommon.AssertEqualNumber(t, len(deleteKubeActions), 27) // delete namespace both from the hub cluster and the mangement cluster deleteCRDActions := []clienttesting.DeleteActionImpl{} crdActions := tc.apiExtensionClient.Actions() @@ -405,12 +405,12 @@ func TestSyncDelete(t *testing.T) { } } // Check if resources are created as expected - testinghelper.AssertEqualNumber(t, len(deleteCRDActions), 15) + testingcommon.AssertEqualNumber(t, len(deleteCRDActions), 15) for _, action := range deleteKubeActions { switch action.Resource.Resource { case "namespaces": - testinghelper.AssertEqualNameNamespace(t, action.Name, "", clusterManagerNamespace, "") + testingcommon.AssertEqualNameNamespace(t, action.Name, "", clusterManagerNamespace, "") } } } @@ -440,7 +440,7 @@ func TestDeleteCRD(t *testing.T) { apiextensionsv1.Resource("customresourcedefinitions"), "clustermanagementaddons.addon.open-cluster-management.io") }) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") err := tc.clusterManagerController.sync(ctx, syncContext) if err == nil { t.Fatalf("Expected error when sync at first time") diff --git a/pkg/registration-operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller_test.go b/pkg/registration-operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller_test.go index 46b4c5f8f..f26d4d7b7 100644 --- a/pkg/registration-operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller_test.go +++ b/pkg/registration-operator/operators/clustermanager/controllers/crdstatuccontroller/crd_status_controller_test.go @@ -18,7 +18,7 @@ import ( fakeoperatorlient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration-operator/operators/clustermanager/controllers/migrationcontroller" ) @@ -26,7 +26,7 @@ func TestSync(t *testing.T) { clusterManager := newClusterManager("testhub") tc := newTestController(t, clusterManager) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") //Do not support migration err := tc.sync(context.Background(), syncContext) if err != nil { diff --git a/pkg/registration-operator/operators/clustermanager/controllers/migrationcontroller/migration_controller_test.go b/pkg/registration-operator/operators/clustermanager/controllers/migrationcontroller/migration_controller_test.go index 818db01de..6d4f20a2f 100644 --- a/pkg/registration-operator/operators/clustermanager/controllers/migrationcontroller/migration_controller_test.go +++ b/pkg/registration-operator/operators/clustermanager/controllers/migrationcontroller/migration_controller_test.go @@ -6,8 +6,6 @@ import ( "testing" "time" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" - fakeoperatorlient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" @@ -23,6 +21,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/rest" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" migrationv1alpha1 "sigs.k8s.io/kube-storage-version-migrator/pkg/apis/migration/v1alpha1" fakemigrationclient "sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset/fake" migrationv1alpha1client "sigs.k8s.io/kube-storage-version-migrator/pkg/clients/clientset/typed/migration/v1alpha1" @@ -395,7 +394,7 @@ func TestSync(t *testing.T) { clusterManager := newClusterManager("testhub") tc := newTestController(t, clusterManager) - syncContext := testinghelper.NewFakeSyncContext(t, "testhub") + syncContext := testingcommon.NewFakeSyncContext(t, "testhub") //Do not support migration err := tc.sync(context.Background(), syncContext) if err != nil { diff --git a/pkg/registration-operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller_test.go b/pkg/registration-operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller_test.go index 71bd1a803..d44cfd16e 100644 --- a/pkg/registration-operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller_test.go +++ b/pkg/registration-operator/operators/clustermanager/controllers/statuscontroller/clustermanager_status_controller_test.go @@ -16,6 +16,7 @@ import ( fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) @@ -76,7 +77,7 @@ func TestSyncStatus(t *testing.T) { clusterManagers: []runtime.Object{}, deployments: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertEqualNumber(t, len(actions), 0) + testingcommon.AssertEqualNumber(t, len(actions), 0) }, }, { @@ -85,7 +86,7 @@ func TestSyncStatus(t *testing.T) { clusterManagers: []runtime.Object{}, deployments: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertEqualNumber(t, len(actions), 0) + testingcommon.AssertEqualNumber(t, len(actions), 0) }, }, { @@ -96,9 +97,9 @@ func TestSyncStatus(t *testing.T) { newPlacementDeployment(3, 0), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertEqualNumber(t, len(actions), 2) - testinghelper.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") - testinghelper.AssertAction(t, actions[1], "update") + testingcommon.AssertEqualNumber(t, len(actions), 2) + testingcommon.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") + testingcommon.AssertAction(t, actions[1], "update") expectedCondition1 := testinghelper.NamedCondition(registrationDegraded, "GetRegistrationDeploymentFailed", metav1.ConditionTrue) expectedCondition2 := testinghelper.NamedCondition(placementDegraded, "UnavailablePlacementPod", metav1.ConditionTrue) testinghelper.AssertOnlyConditions(t, actions[1].(clienttesting.UpdateActionImpl).Object, expectedCondition1, expectedCondition2) @@ -113,9 +114,9 @@ func TestSyncStatus(t *testing.T) { newPlacementDeployment(3, 3), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertEqualNumber(t, len(actions), 2) - testinghelper.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") - testinghelper.AssertAction(t, actions[1], "update") + testingcommon.AssertEqualNumber(t, len(actions), 2) + testingcommon.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") + testingcommon.AssertAction(t, actions[1], "update") expectedCondition1 := testinghelper.NamedCondition(registrationDegraded, "UnavailableRegistrationPod", metav1.ConditionTrue) expectedCondition2 := testinghelper.NamedCondition(placementDegraded, "PlacementFunctional", metav1.ConditionFalse) testinghelper.AssertOnlyConditions(t, actions[1].(clienttesting.UpdateActionImpl).Object, expectedCondition1, expectedCondition2) @@ -127,9 +128,9 @@ func TestSyncStatus(t *testing.T) { clusterManagers: []runtime.Object{newClusterManager()}, deployments: []runtime.Object{newRegistrationDeployment(3, 3)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertEqualNumber(t, len(actions), 2) - testinghelper.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") - testinghelper.AssertAction(t, actions[1], "update") + testingcommon.AssertEqualNumber(t, len(actions), 2) + testingcommon.AssertGet(t, actions[0], "operator.open-cluster-management.io", "v1", "clustermanagers") + testingcommon.AssertAction(t, actions[1], "update") expectedCondition1 := testinghelper.NamedCondition(registrationDegraded, "RegistrationFunctional", metav1.ConditionFalse) expectedCondition2 := testinghelper.NamedCondition(placementDegraded, "GetPlacementDeploymentFailed", metav1.ConditionTrue) testinghelper.AssertOnlyConditions(t, actions[1].(clienttesting.UpdateActionImpl).Object, expectedCondition1, expectedCondition2) @@ -163,7 +164,7 @@ func TestSyncStatus(t *testing.T) { clusterManagerLister: operatorInformers.Operator().V1().ClusterManagers().Lister(), } - syncContext := testinghelper.NewFakeSyncContext(t, c.queueKey) + syncContext := testingcommon.NewFakeSyncContext(t, c.queueKey) err := controller.sync(context.TODO(), syncContext) if err != nil { t.Errorf("Expected no error when update status: %v", err) diff --git a/pkg/registration-operator/operators/crdmanager/manager_test.go b/pkg/registration-operator/operators/crdmanager/manager_test.go index 107659e91..8dfe61c31 100644 --- a/pkg/registration-operator/operators/crdmanager/manager_test.go +++ b/pkg/registration-operator/operators/crdmanager/manager_test.go @@ -19,7 +19,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" versionutil "k8s.io/apimachinery/pkg/util/version" clienttesting "k8s.io/client-go/testing" - testinghelpers "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestApplyV1CRD(t *testing.T) { @@ -36,10 +36,7 @@ func TestApplyV1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") }, }, { @@ -48,10 +45,7 @@ func TestApplyV1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "v0.8.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object assertCRDVersion(t, obj, "0.9.0-16-g889bd8b") }, @@ -62,10 +56,7 @@ func TestApplyV1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object assertCRDVersion(t, obj, "0.9.0-16-g889bd8b") }, @@ -76,10 +67,7 @@ func TestApplyV1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "v0.9.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "get") + testingcommon.AssertActions(t, actions, "get") }, }, { @@ -88,10 +76,7 @@ func TestApplyV1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "0.0.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "get") + testingcommon.AssertActions(t, actions, "get") }, }, } @@ -134,10 +119,7 @@ func TestApplyV1Beta1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1Beta1CRD("foo", "")}, existingCRDs: []runtime.Object{}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") }, }, { @@ -146,10 +128,7 @@ func TestApplyV1Beta1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1Beta1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1Beta1CRD("foo", "v0.8.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object assertCRDVersion(t, obj, "0.9.0-16-g889bd8b") }, @@ -160,10 +139,7 @@ func TestApplyV1Beta1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1Beta1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1Beta1CRD("foo", "")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object assertCRDVersion(t, obj, "0.9.0-16-g889bd8b") }, @@ -174,10 +150,7 @@ func TestApplyV1Beta1CRD(t *testing.T) { requiredCRDs: []runtime.Object{newV1Beta1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1Beta1CRD("foo", "v0.9.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "get") + testingcommon.AssertActions(t, actions, "get") }, }, } @@ -222,10 +195,7 @@ func TestClean(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -235,10 +205,7 @@ func TestClean(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -248,10 +215,7 @@ func TestClean(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "0.9.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object accessor, _ := meta.Accessor(obj) if len(accessor.GetAnnotations()) != 0 { @@ -266,10 +230,7 @@ func TestClean(t *testing.T) { requiredCRDs: []runtime.Object{newV1CRD("foo", "")}, existingCRDs: []runtime.Object{newV1CRD("foo", "0.10.0")}, verify: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Fatalf("actions are not expected: %v", actions) - } - testinghelpers.AssertAction(t, actions[0], "get") + testingcommon.AssertActions(t, actions, "get") }, }, } diff --git a/pkg/registration-operator/operators/klusterlet/controllers/addonsecretcontroller/controller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/addonsecretcontroller/controller_test.go index ab15a802d..746989509 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/addonsecretcontroller/controller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/addonsecretcontroller/controller_test.go @@ -11,7 +11,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/informers" kubefake "k8s.io/client-go/kubernetes/fake" - testinghelpers "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestSync(t *testing.T) { @@ -98,7 +98,7 @@ func TestSync(t *testing.T) { namespaceInformer: kubeInformer.Core().V1().Namespaces(), } - err := controller.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, tc.queueKey)) + err := controller.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, tc.queueKey)) if err != nil { t.Errorf("%s: unexpected error: %v", tc.name, err) } diff --git a/pkg/registration-operator/operators/klusterlet/controllers/bootstrapcontroller/bootstrapcontroller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/bootstrapcontroller/bootstrapcontroller_test.go index a3bb20ae0..c3fb3b0cc 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/bootstrapcontroller/bootstrapcontroller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/bootstrapcontroller/bootstrapcontroller_test.go @@ -11,11 +11,6 @@ import ( "testing" "time" - fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" - operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" - operatorapiv1 "open-cluster-management.io/api/operator/v1" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" - appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -26,6 +21,10 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest" certutil "k8s.io/client-go/util/cert" + fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" + operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" + operatorapiv1 "open-cluster-management.io/api/operator/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestSync(t *testing.T) { @@ -54,9 +53,9 @@ func TestSync(t *testing.T) { newDeployment("test-work-agent", "test"), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertDelete(t, actions[0], "secrets", "test", "hub-kubeconfig-secret") - testinghelper.AssertDelete(t, actions[1], "deployments", "test", "test-registration-agent") - testinghelper.AssertDelete(t, actions[2], "deployments", "test", "test-work-agent") + testingcommon.AssertDelete(t, actions[0], "secrets", "test", "hub-kubeconfig-secret") + testingcommon.AssertDelete(t, actions[1], "deployments", "test", "test-registration-agent") + testingcommon.AssertDelete(t, actions[2], "deployments", "test", "test-work-agent") }, }, { @@ -92,9 +91,9 @@ func TestSync(t *testing.T) { newDeployment("test-work-agent", "test"), }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelper.AssertDelete(t, actions[0], "secrets", "test", "hub-kubeconfig-secret") - testinghelper.AssertDelete(t, actions[1], "deployments", "test", "test-registration-agent") - testinghelper.AssertDelete(t, actions[2], "deployments", "test", "test-work-agent") + testingcommon.AssertDelete(t, actions[0], "secrets", "test", "hub-kubeconfig-secret") + testingcommon.AssertDelete(t, actions[1], "deployments", "test", "test-registration-agent") + testingcommon.AssertDelete(t, actions[2], "deployments", "test", "test-work-agent") }, }, } @@ -126,7 +125,7 @@ func TestSync(t *testing.T) { secretLister: kubeInformers.Core().V1().Secrets().Lister(), } - syncContext := testinghelper.NewFakeSyncContext(t, c.queueKey) + syncContext := testingcommon.NewFakeSyncContext(t, c.queueKey) if err := controller.sync(context.TODO(), syncContext); err != nil { t.Errorf("Expected no errors, but got %v", err) } diff --git a/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller_test.go index 5552a05c6..4ae163544 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_cleanup_controller_test.go @@ -10,8 +10,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" clienttesting "k8s.io/client-go/testing" "k8s.io/klog/v2" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" - testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) // TestSyncDelete test cleanup hub deploy @@ -28,7 +28,7 @@ func TestSyncDelete(t *testing.T) { newAppliedManifestWorks("testhost-2", []string{appliedManifestWorkFinalizer}, false), } controller := newTestController(t, klusterlet, appliedManifestWorks, namespace, bootstrapKubeConfigSecret) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.cleanupController.sync(context.TODO(), syncContext) if err != nil { @@ -86,7 +86,7 @@ func TestSyncDeleteHosted(t *testing.T) { newAppliedManifestWorks("testhost-2", []string{appliedManifestWorkFinalizer}, false), } controller := newTestControllerHosted(t, klusterlet, appliedManifestWorks, bootstrapKubeConfigSecret, namespace /*externalManagedSecret*/) - syncContext := testinghelper.NewFakeSyncContext(t, klusterlet.Name) + syncContext := testingcommon.NewFakeSyncContext(t, klusterlet.Name) err := controller.cleanupController.sync(context.TODO(), syncContext) if err != nil { @@ -150,7 +150,7 @@ func TestSyncDeleteHostedDeleteAgentNamespace(t *testing.T) { now := metav1.Now() klusterlet.ObjectMeta.SetDeletionTimestamp(&now) controller := newTestControllerHosted(t, klusterlet, nil).setDefaultManagedClusterClientsBuilder() - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.cleanupController.sync(context.TODO(), syncContext) if err != nil { @@ -159,7 +159,7 @@ func TestSyncDeleteHostedDeleteAgentNamespace(t *testing.T) { kubeActions := controller.kubeClient.Actions() // assert there last action is deleting the klusterlet agent namespace on the management cluster - testinghelper.AssertDelete(t, kubeActions[len(kubeActions)-1], "namespaces", "", "klusterlet") + testingcommon.AssertDelete(t, kubeActions[len(kubeActions)-1], "namespaces", "", "klusterlet") } func TestSyncDeleteHostedDeleteWaitKubeconfig(t *testing.T) { @@ -167,7 +167,7 @@ func TestSyncDeleteHostedDeleteWaitKubeconfig(t *testing.T) { now := metav1.Now() klusterlet.ObjectMeta.SetDeletionTimestamp(&now) controller := newTestControllerHosted(t, klusterlet, nil).setDefaultManagedClusterClientsBuilder() - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.cleanupController.sync(context.TODO(), syncContext) if err != nil { @@ -188,7 +188,7 @@ func TestSyncAddHostedFinalizerWhenKubeconfigReady(t *testing.T) { klusterletHostedFinalizer) c := newTestControllerHosted(t, klusterlet, nil) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := c.cleanupController.sync(context.TODO(), syncContext) if err != nil { diff --git a/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller_test.go index 37166c03e..1bbb0f579 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/klusterletcontroller/klusterlet_controller_test.go @@ -33,6 +33,7 @@ import ( fakeworkclient "open-cluster-management.io/api/client/work/clientset/versioned/fake" operatorapiv1 "open-cluster-management.io/api/operator/v1" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) @@ -425,7 +426,7 @@ func ensureObject(t *testing.T, object runtime.Object, klusterlet *operatorapiv1 switch o := object.(type) { case *appsv1.Deployment: if strings.Contains(access.GetName(), "registration") { - testinghelper.AssertEqualNameNamespace( + testingcommon.AssertEqualNameNamespace( t, access.GetName(), access.GetNamespace(), fmt.Sprintf("%s-registration-agent", klusterlet.Name), namespace) if klusterlet.Spec.RegistrationImagePullSpec != o.Spec.Template.Spec.Containers[0].Image { @@ -433,7 +434,7 @@ func ensureObject(t *testing.T, object runtime.Object, klusterlet *operatorapiv1 return } } else if strings.Contains(access.GetName(), "work") { - testinghelper.AssertEqualNameNamespace( + testingcommon.AssertEqualNameNamespace( t, access.GetName(), access.GetNamespace(), fmt.Sprintf("%s-work-agent", klusterlet.Name), namespace) if klusterlet.Spec.WorkImagePullSpec != o.Spec.Template.Spec.Containers[0].Image { @@ -455,7 +456,7 @@ func TestSyncDeploy(t *testing.T) { hubKubeConfigSecret.Data["kubeconfig"] = []byte("dummuykubeconnfig") namespace := newNamespace("testns") controller := newTestController(t, klusterlet, nil, bootStrapSecret, hubKubeConfigSecret, namespace) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -498,8 +499,8 @@ func TestSyncDeploy(t *testing.T) { t.Errorf("Expect 4 actions in the sync loop, actual %#v", operatorAction) } - testinghelper.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorAction[1], "update") + testingcommon.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorAction[1], "update") testinghelper.AssertOnlyConditions( t, operatorAction[1].(clienttesting.UpdateActionImpl).Object, testinghelper.NamedCondition(klusterletApplied, "KlusterletApplied", metav1.ConditionTrue), @@ -524,7 +525,7 @@ func TestSyncDeployHosted(t *testing.T) { pullSecret := newSecret(imagePullSecret, "open-cluster-management") controller := newTestControllerHosted(t, klusterlet, nil, bootStrapSecret, hubKubeConfigSecret, namespace, pullSecret /*externalManagedSecret*/) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -599,9 +600,9 @@ func TestSyncDeployHosted(t *testing.T) { t.Errorf("Expect 3 actions in the sync loop, actual %#v", len(operatorAction)) } - testinghelper.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertGet(t, operatorAction[1], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorAction[2], "update") + testingcommon.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertGet(t, operatorAction[1], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorAction[2], "update") conditionReady := testinghelper.NamedCondition(klusterletReadyToApply, "KlusterletPrepared", metav1.ConditionTrue) conditionApplied := testinghelper.NamedCondition(klusterletApplied, "KlusterletApplied", metav1.ConditionTrue) @@ -619,7 +620,7 @@ func TestSyncDeployHostedCreateAgentNamespace(t *testing.T) { Message: fmt.Sprintf("Failed to build managed cluster clients: secrets \"external-managed-kubeconfig\" not found"), }) controller := newTestControllerHosted(t, klusterlet, nil).setDefaultManagedClusterClientsBuilder() - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if !errors.IsNotFound(err) { @@ -627,8 +628,8 @@ func TestSyncDeployHostedCreateAgentNamespace(t *testing.T) { } kubeActions := controller.kubeClient.Actions() - testinghelper.AssertGet(t, kubeActions[0], "", "v1", "namespaces") - testinghelper.AssertAction(t, kubeActions[1], "create") + testingcommon.AssertGet(t, kubeActions[0], "", "v1", "namespaces") + testingcommon.AssertAction(t, kubeActions[1], "create") if kubeActions[1].GetResource().Resource != "namespaces" { t.Errorf("expect object namespaces, but got %v", kubeActions[2].GetResource().Resource) } @@ -690,7 +691,7 @@ func TestReplica(t *testing.T) { } controller := newTestController(t, klusterlet, nil, objects...) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -754,7 +755,7 @@ func TestClusterNameChange(t *testing.T) { hubSecret.Data["kubeconfig"] = []byte("dummuykubeconnfig") hubSecret.Data["cluster-name"] = []byte("cluster1") controller := newTestController(t, klusterlet, nil, bootStrapSecret, hubSecret, namespace) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -769,8 +770,8 @@ func TestClusterNameChange(t *testing.T) { t.Errorf("Expect 2 actions in the sync loop, actual %#v", operatorAction) } - testinghelper.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorAction[1], "update") + testingcommon.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorAction[1], "update") updatedKlusterlet := operatorAction[1].(clienttesting.UpdateActionImpl).Object.(*operatorapiv1.Klusterlet) testinghelper.AssertOnlyGenerationStatuses( t, updatedKlusterlet, @@ -841,7 +842,7 @@ func TestSyncWithPullSecret(t *testing.T) { namespace := newNamespace("testns") pullSecret := newSecret(imagePullSecret, "open-cluster-management") controller := newTestController(t, klusterlet, nil, bootStrapSecret, hubKubeConfigSecret, namespace, pullSecret) - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -873,7 +874,7 @@ func TestDeployOnKube111(t *testing.T) { kubeVersion, _ := version.ParseGeneric("v1.11.0") controller.controller.kubeVersion = kubeVersion controller.cleanupController.kubeVersion = kubeVersion - syncContext := testinghelper.NewFakeSyncContext(t, "klusterlet") + syncContext := testingcommon.NewFakeSyncContext(t, "klusterlet") ctx := context.TODO() err := controller.controller.sync(ctx, syncContext) @@ -904,8 +905,8 @@ func TestDeployOnKube111(t *testing.T) { t.Errorf("Expect 4 actions in the sync loop, actual %#v", operatorAction) } - testinghelper.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorAction[1], "update") + testingcommon.AssertGet(t, operatorAction[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorAction[1], "update") testinghelper.AssertOnlyConditions( t, operatorAction[1].(clienttesting.UpdateActionImpl).Object, testinghelper.NamedCondition(klusterletApplied, "KlusterletApplied", metav1.ConditionTrue), diff --git a/pkg/registration-operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller_test.go index 7b156302d..bc41b0374 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/ssarcontroller/klusterlet_ssar_controller_test.go @@ -22,6 +22,7 @@ import ( fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration-operator/helpers" testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) @@ -244,7 +245,7 @@ func TestSync(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { controller := newTestController(t, c.klusterlet, c.object...) - syncContext := testinghelper.NewFakeSyncContext(t, c.klusterlet.Name) + syncContext := testingcommon.NewFakeSyncContext(t, c.klusterlet.Name) response.allowToOperateManagedClusters = c.allowToOperateManagedClusters response.allowToOperateManagedClusterStatus = c.allowToOperateManagedClusterStatus @@ -262,9 +263,9 @@ func TestSync(t *testing.T) { operatorActions := controller.operatorClient.Actions() - testinghelper.AssertEqualNumber(t, len(operatorActions), 2) - testinghelper.AssertGet(t, operatorActions[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorActions[1], "update") + testingcommon.AssertEqualNumber(t, len(operatorActions), 2) + testingcommon.AssertGet(t, operatorActions[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorActions[1], "update") testinghelper.AssertOnlyConditions(t, operatorActions[1].(clienttesting.UpdateActionImpl).Object, c.expectedConditions...) }) } diff --git a/pkg/registration-operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller_test.go b/pkg/registration-operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller_test.go index 83e04d438..37f886030 100644 --- a/pkg/registration-operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller_test.go +++ b/pkg/registration-operator/operators/klusterlet/controllers/statuscontroller/klusterlet_status_controller_test.go @@ -15,6 +15,7 @@ import ( fakeoperatorclient "open-cluster-management.io/api/client/operator/clientset/versioned/fake" operatorinformers "open-cluster-management.io/api/client/operator/informers/externalversions" operatorapiv1 "open-cluster-management.io/api/operator/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelper "open-cluster-management.io/ocm/pkg/registration-operator/helpers/testing" ) @@ -156,7 +157,7 @@ func TestSync(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { controller := newTestController(t, c.klusterlet, c.object...) - syncContext := testinghelper.NewFakeSyncContext(t, c.klusterlet.Name) + syncContext := testingcommon.NewFakeSyncContext(t, c.klusterlet.Name) err := controller.controller.sync(context.TODO(), syncContext) if err != nil { @@ -164,9 +165,9 @@ func TestSync(t *testing.T) { } operatorActions := controller.operatorClient.Actions() - testinghelper.AssertEqualNumber(t, len(operatorActions), 2) - testinghelper.AssertGet(t, operatorActions[0], "operator.open-cluster-management.io", "v1", "klusterlets") - testinghelper.AssertAction(t, operatorActions[1], "update") + testingcommon.AssertEqualNumber(t, len(operatorActions), 2) + testingcommon.AssertGet(t, operatorActions[0], "operator.open-cluster-management.io", "v1", "klusterlets") + testingcommon.AssertAction(t, operatorActions[1], "update") testinghelper.AssertOnlyConditions(t, operatorActions[1].(clienttesting.UpdateActionImpl).Object, c.expectedConditions...) }) } diff --git a/pkg/registration/clientcert/certificate_test.go b/pkg/registration/clientcert/certificate_test.go index 94971cb91..04042017b 100644 --- a/pkg/registration/clientcert/certificate_test.go +++ b/pkg/registration/clientcert/certificate_test.go @@ -13,6 +13,7 @@ import ( "k8s.io/client-go/tools/cache" certutil "k8s.io/client-go/util/cert" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" ) @@ -217,7 +218,7 @@ func TestGetCertValidityPeriod(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { notBefore, notAfter, err := getCertValidityPeriod(c.secret) - testinghelpers.AssertError(t, err, c.expectedErr) + testingcommon.AssertError(t, err, c.expectedErr) if err != nil { return } diff --git a/pkg/registration/clientcert/controller_test.go b/pkg/registration/clientcert/controller_test.go index d171439a6..307c00beb 100644 --- a/pkg/registration/clientcert/controller_test.go +++ b/pkg/registration/clientcert/controller_test.go @@ -18,6 +18,7 @@ import ( clienttesting "k8s.io/client-go/testing" "k8s.io/client-go/tools/cache" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "open-cluster-management.io/ocm/pkg/registration/hub/user" ) @@ -54,12 +55,12 @@ func TestSync(t *testing.T) { keyDataExpected: true, csrNameExpected: true, validateActions: func(t *testing.T, hubActions, agentActions []clienttesting.Action) { - testinghelpers.AssertActions(t, hubActions, "create") + testingcommon.AssertActions(t, hubActions, "create") actual := hubActions[0].(clienttesting.CreateActionImpl).Object if _, ok := actual.(*unstructured.Unstructured); !ok { t.Errorf("expected csr was created, but failed") } - testinghelpers.AssertActions(t, agentActions, "get") + testingcommon.AssertActions(t, agentActions, "get") }, }, { @@ -78,8 +79,8 @@ func TestSync(t *testing.T) { }, approvedCSRCert: testinghelpers.NewTestCert(commonName, 10*time.Second), validateActions: func(t *testing.T, hubActions, agentActions []clienttesting.Action) { - testinghelpers.AssertActions(t, hubActions, "get", "get") - testinghelpers.AssertActions(t, agentActions, "get", "update") + testingcommon.AssertActions(t, hubActions, "get", "get") + testingcommon.AssertActions(t, agentActions, "get", "update") actual := agentActions[1].(clienttesting.UpdateActionImpl).Object secret := actual.(*corev1.Secret) valid, err := IsCertificateValid(secret.Data[TLSCertFile], testSubject) @@ -102,8 +103,8 @@ func TestSync(t *testing.T) { }), }, validateActions: func(t *testing.T, hubActions, agentActions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, hubActions) - testinghelpers.AssertActions(t, agentActions, "get") + testingcommon.AssertNoActions(t, hubActions) + testingcommon.AssertActions(t, agentActions, "get") }, }, { @@ -119,12 +120,12 @@ func TestSync(t *testing.T) { keyDataExpected: true, csrNameExpected: true, validateActions: func(t *testing.T, hubActions, agentActions []clienttesting.Action) { - testinghelpers.AssertActions(t, hubActions, "create") + testingcommon.AssertActions(t, hubActions, "create") actual := hubActions[0].(clienttesting.CreateActionImpl).Object if _, ok := actual.(*unstructured.Unstructured); !ok { t.Errorf("expected csr was created, but failed") } - testinghelpers.AssertActions(t, agentActions, "get") + testingcommon.AssertActions(t, agentActions, "get") }, }, { @@ -140,12 +141,12 @@ func TestSync(t *testing.T) { csrNameExpected: true, additonalSecretDataSensitive: true, validateActions: func(t *testing.T, hubActions, agentActions []clienttesting.Action) { - testinghelpers.AssertActions(t, hubActions, "create") + testingcommon.AssertActions(t, hubActions, "create") actual := hubActions[0].(clienttesting.CreateActionImpl).Object if _, ok := actual.(*unstructured.Unstructured); !ok { t.Errorf("expected csr was created, but failed") } - testinghelpers.AssertActions(t, agentActions, "get") + testingcommon.AssertActions(t, agentActions, "get") }, }, } @@ -207,7 +208,7 @@ func TestSync(t *testing.T) { controller.keyData = c.approvedCSRCert.Key } - err := controller.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, c.queueKey)) + err := controller.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, c.queueKey)) if err != nil { t.Errorf("unexpected error %v", err) } diff --git a/pkg/registration/helpers/helpers_test.go b/pkg/registration/helpers/helpers_test.go index 277e0dd46..c7216a869 100644 --- a/pkg/registration/helpers/helpers_test.go +++ b/pkg/registration/helpers/helpers_test.go @@ -12,6 +12,7 @@ import ( addonfake "open-cluster-management.io/api/client/addon/clientset/versioned/fake" clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterv1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "github.com/openshift/library-go/pkg/operator/events/eventstesting" @@ -282,7 +283,7 @@ func TestCleanUpManagedClusterManifests(t *testing.T) { }, applyFiles: applyFiles, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, expectedActions...) + testingcommon.AssertActions(t, actions, expectedActions...) }, }, { @@ -290,7 +291,7 @@ func TestCleanUpManagedClusterManifests(t *testing.T) { applyObject: []runtime.Object{}, applyFiles: applyFiles, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, expectedActions...) + testingcommon.AssertActions(t, actions, expectedActions...) }, }, { @@ -298,7 +299,7 @@ func TestCleanUpManagedClusterManifests(t *testing.T) { applyObject: []runtime.Object{}, applyFiles: map[string]runtime.Object{"secret": testinghelpers.NewUnstructuredObj("v1", "Secret", "n1", "s1")}, expectedErr: "unhandled type *v1.Secret", - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, } for _, c := range cases { @@ -316,7 +317,7 @@ func TestCleanUpManagedClusterManifests(t *testing.T) { }, getApplyFileNames(c.applyFiles)..., ) - testinghelpers.AssertError(t, cleanUpErr, c.expectedErr) + testingcommon.AssertError(t, cleanUpErr, c.expectedErr) c.validateActions(t, kubeClient.Actions()) }) } diff --git a/pkg/registration/helpers/testing/assertion.go b/pkg/registration/helpers/testing/assertion.go index 34607e01b..43e5f4857 100644 --- a/pkg/registration/helpers/testing/assertion.go +++ b/pkg/registration/helpers/testing/assertion.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "reflect" - "strings" "testing" clusterv1 "open-cluster-management.io/api/cluster/v1" @@ -17,86 +16,9 @@ import ( "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - clienttesting "k8s.io/client-go/testing" "k8s.io/utils/diff" ) -// AssertError asserts the actual error representation is the same with the expected, -// if the expected error representation is empty, the actual should be nil -func AssertError(t *testing.T, actual error, expectedErr string) { - if len(expectedErr) > 0 && actual == nil { - t.Errorf("expected %q error", expectedErr) - return - } - if len(expectedErr) > 0 && actual != nil && actual.Error() != expectedErr { - t.Errorf("expected %q error, but got %q", expectedErr, actual.Error()) - return - } - if len(expectedErr) == 0 && actual != nil { - t.Errorf("unexpected err: %v", actual) - return - } -} - -// AssertError asserts the actual error representation starts with the expected prerfix, -// if the expected error prefix is empty, the actual should be nil -func AssertErrorWithPrefix(t *testing.T, actual error, expectedErrorPrefix string) { - if len(expectedErrorPrefix) > 0 && actual == nil { - t.Errorf("expected error with prefix %q", expectedErrorPrefix) - return - } - if len(expectedErrorPrefix) > 0 && actual != nil && !strings.HasPrefix(actual.Error(), expectedErrorPrefix) { - t.Errorf("expected error with prefix %q, but got %q", expectedErrorPrefix, actual.Error()) - return - } - if len(expectedErrorPrefix) == 0 && actual != nil { - t.Errorf("unexpected err: %v", actual) - return - } -} - -// AssertActions asserts the actual actions have the expected action verb -func AssertActions(t *testing.T, actualActions []clienttesting.Action, expectedVerbs ...string) { - if len(actualActions) != len(expectedVerbs) { - t.Fatalf("expected %d call but got: %#v", len(expectedVerbs), actualActions) - } - for i, expected := range expectedVerbs { - if actualActions[i].GetVerb() != expected { - t.Errorf("expected %s action but got: %#v", expected, actualActions[i]) - } - } -} - -// AssertNoActions asserts no actions are happened -func AssertNoActions(t *testing.T, actualActions []clienttesting.Action) { - AssertActions(t, actualActions) -} - -// AssertUpdateActions asserts the actions are get-then-update action -func AssertUpdateActions(t *testing.T, actions []clienttesting.Action) { - for i := 0; i < len(actions); i = i + 2 { - if actions[i].GetVerb() != "get" { - t.Errorf("expected action %d is get, but %v", i, actions[i]) - } - if actions[i+1].GetVerb() != "update" { - t.Errorf("expected action %d is update, but %v", i, actions[i+1]) - } - } -} - -// AssertNoMoreUpdates asserts only one update action in given actions -func AssertNoMoreUpdates(t *testing.T, actions []clienttesting.Action) { - updateActions := 0 - for _, action := range actions { - if action.GetVerb() == "update" { - updateActions++ - } - } - if updateActions != 1 { - t.Errorf("expected there is only one update action, but failed") - } -} - // AssertFinalizers asserts the given runtime object has the expected finalizers func AssertFinalizers(t *testing.T, obj runtime.Object, finalizers []string) { accessor, _ := meta.Accessor(obj) @@ -109,27 +31,6 @@ func AssertFinalizers(t *testing.T, obj runtime.Object, finalizers []string) { } } -// AssertCondition asserts the actual conditions has -// the expected condition -func AssertCondition( - t *testing.T, - actualConditions []metav1.Condition, - expectedCondition metav1.Condition) { - cond := meta.FindStatusCondition(actualConditions, expectedCondition.Type) - if cond == nil { - t.Errorf("expected condition %s but got: %s", expectedCondition.Type, cond.Type) - } - if cond.Status != expectedCondition.Status { - t.Errorf("expected status %s but got: %s", expectedCondition.Status, cond.Status) - } - if cond.Reason != expectedCondition.Reason { - t.Errorf("expected reason %s but got: %s", expectedCondition.Reason, cond.Reason) - } - if cond.Message != expectedCondition.Message { - t.Errorf("expected message %s but got: %s", expectedCondition.Message, cond.Message) - } -} - // AssertManagedClusterClientConfigs asserts the actual managed cluster client configs are the // same wiht the expected func AssertManagedClusterClientConfigs(t *testing.T, actual, expected []clusterv1.ClientConfig) { diff --git a/pkg/registration/helpers/testing/testinghelpers.go b/pkg/registration/helpers/testing/testinghelpers.go index 79473ff14..cfb58972e 100644 --- a/pkg/registration/helpers/testing/testinghelpers.go +++ b/pkg/registration/helpers/testing/testinghelpers.go @@ -12,15 +12,11 @@ import ( "math/big" "math/rand" "net" - "testing" "time" clusterv1 "open-cluster-management.io/api/cluster/v1" workapiv1 "open-cluster-management.io/api/work/v1" - "github.com/openshift/library-go/pkg/operator/events" - "github.com/openshift/library-go/pkg/operator/events/eventstesting" - certv1 "k8s.io/api/certificates/v1" certv1beta1 "k8s.io/api/certificates/v1beta1" coordv1 "k8s.io/api/coordination/v1" @@ -34,7 +30,6 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" certutil "k8s.io/client-go/util/cert" "k8s.io/client-go/util/keyutil" - "k8s.io/client-go/util/workqueue" ) const ( @@ -42,24 +37,6 @@ const ( TestManagedClusterName = "testmanagedcluster" ) -type FakeSyncContext struct { - spokeName string - recorder events.Recorder - queue workqueue.RateLimitingInterface -} - -func (f FakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue } -func (f FakeSyncContext) QueueKey() string { return f.spokeName } -func (f FakeSyncContext) Recorder() events.Recorder { return f.recorder } - -func NewFakeSyncContext(t *testing.T, clusterName string) *FakeSyncContext { - return &FakeSyncContext{ - spokeName: clusterName, - recorder: eventstesting.NewTestingEventRecorder(t), - queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), - } -} - func NewManagedCluster() *clusterv1.ManagedCluster { return &clusterv1.ManagedCluster{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/registration/hub/addon/discovery_controller_test.go b/pkg/registration/hub/addon/discovery_controller_test.go index b1b5824f8..4b1469404 100644 --- a/pkg/registration/hub/addon/discovery_controller_test.go +++ b/pkg/registration/hub/addon/discovery_controller_test.go @@ -16,7 +16,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" - testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestGetAddOnLabelValue(t *testing.T) { @@ -100,7 +100,7 @@ func TestDiscoveryController_SyncAddOn(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object assertNoAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon1") }, @@ -119,7 +119,7 @@ func TestDiscoveryController_SyncAddOn(t *testing.T) { DeletionTimestamp: &deleteTime, }, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "new addon is added", @@ -136,7 +136,7 @@ func TestDiscoveryController_SyncAddOn(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object assertAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon1", addOnStatusUnreachable) }, @@ -159,7 +159,7 @@ func TestDiscoveryController_SyncAddOn(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object assertAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon1", addOnStatusUnreachable) }, @@ -179,7 +179,7 @@ func TestDiscoveryController_SyncAddOn(t *testing.T) { Namespace: clusterName, }, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, } @@ -257,7 +257,7 @@ func TestDiscoveryController_Sync(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object assertAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon1", addOnStatusUnreachable) }, @@ -265,7 +265,7 @@ func TestDiscoveryController_Sync(t *testing.T) { { name: "cluster not found", queueKey: clusterName, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "cluster is deleting", @@ -276,7 +276,7 @@ func TestDiscoveryController_Sync(t *testing.T) { DeletionTimestamp: &deleteTime, }, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "no change", @@ -286,7 +286,7 @@ func TestDiscoveryController_Sync(t *testing.T) { Name: clusterName, }, }, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "cluster synced", @@ -329,7 +329,7 @@ func TestDiscoveryController_Sync(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object assertAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon1", addOnStatusUnreachable) assertAddonLabel(t, actual.(*clusterv1.ManagedCluster), "addon3", addOnStatusAvailable) @@ -374,7 +374,7 @@ func TestDiscoveryController_Sync(t *testing.T) { addOnLister: addOnInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Lister(), } - err := controller.sync(context.Background(), testinghelpers.NewFakeSyncContext(t, c.queueKey)) + err := controller.sync(context.Background(), testingcommon.NewFakeSyncContext(t, c.queueKey)) if err != nil { t.Errorf("unexpected err: %v", err) } diff --git a/pkg/registration/hub/addon/healthcheck_controller_test.go b/pkg/registration/hub/addon/healthcheck_controller_test.go index 55f185b18..42fc5da34 100644 --- a/pkg/registration/hub/addon/healthcheck_controller_test.go +++ b/pkg/registration/hub/addon/healthcheck_controller_test.go @@ -6,17 +6,17 @@ import ( "testing" "time" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + clienttesting "k8s.io/client-go/testing" addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" addonfake "open-cluster-management.io/api/client/addon/clientset/versioned/fake" addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" - - "k8s.io/apimachinery/pkg/api/meta" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - clienttesting "k8s.io/client-go/testing" ) func TestSync(t *testing.T) { @@ -31,7 +31,7 @@ func TestSync(t *testing.T) { managedClusters: []runtime.Object{}, addOns: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -39,7 +39,7 @@ func TestSync(t *testing.T) { managedClusters: []runtime.Object{testinghelpers.NewManagedCluster()}, addOns: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -47,7 +47,7 @@ func TestSync(t *testing.T) { managedClusters: []runtime.Object{testinghelpers.NewAvailableManagedCluster()}, addOns: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -57,7 +57,7 @@ func TestSync(t *testing.T) { ObjectMeta: metav1.ObjectMeta{Namespace: testinghelpers.TestManagedClusterName, Name: "test"}, }}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} @@ -103,7 +103,7 @@ func TestSync(t *testing.T) { clusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/clusterrole/controller_test.go b/pkg/registration/hub/clusterrole/controller_test.go index 8eb8a443d..3f39f389f 100644 --- a/pkg/registration/hub/clusterrole/controller_test.go +++ b/pkg/registration/hub/clusterrole/controller_test.go @@ -8,6 +8,7 @@ import ( "github.com/openshift/library-go/pkg/operator/resource/resourceapply" clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "github.com/openshift/library-go/pkg/operator/events/eventstesting" @@ -31,7 +32,7 @@ func TestSyncManagedClusterClusterRole(t *testing.T) { clusters: []runtime.Object{testinghelpers.NewManagedCluster()}, clusterroles: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "create", "get", "create") + testingcommon.AssertActions(t, actions, "get", "create", "get", "create") registrationClusterRole := (actions[1].(clienttesting.CreateActionImpl).Object).(*rbacv1.ClusterRole) if registrationClusterRole.Name != "open-cluster-management:managedcluster:registration" { t.Errorf("expected registration clusterrole, but failed") @@ -50,7 +51,7 @@ func TestSyncManagedClusterClusterRole(t *testing.T) { &rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "open-cluster-management:managedcluster:work"}}, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "delete", "delete") + testingcommon.AssertActions(t, actions, "delete", "delete") if actions[0].(clienttesting.DeleteActionImpl).Name != "open-cluster-management:managedcluster:registration" { t.Errorf("expected registration clusterrole, but failed") } @@ -81,7 +82,7 @@ func TestSyncManagedClusterClusterRole(t *testing.T) { eventRecorder: eventstesting.NewTestingEventRecorder(t), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "testmangedclsuterclusterrole")) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "testmangedclsuterclusterrole")) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/csr/controller_beta_test.go b/pkg/registration/hub/csr/controller_beta_test.go index 2c2f0a80b..df3e1d7bf 100644 --- a/pkg/registration/hub/csr/controller_beta_test.go +++ b/pkg/registration/hub/csr/controller_beta_test.go @@ -14,6 +14,7 @@ import ( "k8s.io/client-go/informers" kubefake "k8s.io/client-go/kubernetes/fake" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "open-cluster-management.io/ocm/pkg/registration/hub/user" ) @@ -40,21 +41,21 @@ func Test_v1beta1CSRApprovingController_sync(t *testing.T) { name: "sync a deleted csr", startingCSRs: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "sync a denied csr", startingCSRs: []runtime.Object{testinghelpers.NewDeniedV1beta1CSR(validV1beta1CSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "sync an approved csr", startingCSRs: []runtime.Object{testinghelpers.NewApprovedV1beta1CSR(validV1beta1CSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -69,14 +70,14 @@ func Test_v1beta1CSRApprovingController_sync(t *testing.T) { ReqBlockType: validV1beta1CSR.ReqBlockType, })}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "deny an auto approving csr", startingCSRs: []runtime.Object{testinghelpers.NewV1beta1CSR(validV1beta1CSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create") + testingcommon.AssertActions(t, actions, "create") testinghelpers.AssertSubjectAccessReviewObj(t, actions[0].(clienttesting.CreateActionImpl).Object) }, }, @@ -91,7 +92,7 @@ func Test_v1beta1CSRApprovingController_sync(t *testing.T) { Reason: "AutoApprovedByHubCSRApprovingController", Message: "Auto approving Managed cluster agent certificate after SubjectAccessReview.", } - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") actual := actions[1].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertV1beta1CSRCondition(t, actual.(*certificatesv1beta1.CertificateSigningRequest).Status.Conditions, expectedCondition) }, @@ -115,7 +116,7 @@ func Test_v1beta1CSRApprovingController_sync(t *testing.T) { Reason: "AutoApprovedByHubCSRApprovingController", Message: "Auto approving Managed cluster agent certificate after SubjectAccessReview.", } - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") actual := actions[1].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertV1beta1CSRCondition(t, actual.(*certificatesv1beta1.CertificateSigningRequest).Status.Conditions, expectedCondition) }, @@ -154,7 +155,7 @@ func Test_v1beta1CSRApprovingController_sync(t *testing.T) { }, }, } - if err := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, validV1beta1CSR.Name)); (err != nil) != tt.wantErr { + if err := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, validV1beta1CSR.Name)); (err != nil) != tt.wantErr { t.Errorf("v1beta1CSRApprovingController.sync() error = %v, wantErr %v", err, tt.wantErr) } tt.validateActions(t, kubeClient.Actions()) diff --git a/pkg/registration/hub/csr/controller_test.go b/pkg/registration/hub/csr/controller_test.go index 0aea8ea49..bb20d01c8 100644 --- a/pkg/registration/hub/csr/controller_test.go +++ b/pkg/registration/hub/csr/controller_test.go @@ -8,6 +8,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "open-cluster-management.io/ocm/pkg/registration/hub/user" @@ -50,7 +51,7 @@ func TestSync(t *testing.T) { startingClusters: []runtime.Object{}, startingCSRs: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -58,7 +59,7 @@ func TestSync(t *testing.T) { startingClusters: []runtime.Object{}, startingCSRs: []runtime.Object{testinghelpers.NewDeniedCSR(validCSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -66,7 +67,7 @@ func TestSync(t *testing.T) { startingClusters: []runtime.Object{}, startingCSRs: []runtime.Object{testinghelpers.NewApprovedCSR(validCSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -82,7 +83,7 @@ func TestSync(t *testing.T) { ReqBlockType: validCSR.ReqBlockType, })}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -90,7 +91,7 @@ func TestSync(t *testing.T) { startingClusters: []runtime.Object{}, startingCSRs: []runtime.Object{testinghelpers.NewCSR(validCSR)}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create") + testingcommon.AssertActions(t, actions, "create") testinghelpers.AssertSubjectAccessReviewObj(t, actions[0].(clienttesting.CreateActionImpl).Object) }, }, @@ -106,7 +107,7 @@ func TestSync(t *testing.T) { Reason: "AutoApprovedByHubCSRApprovingController", Message: "Auto approving Managed cluster agent certificate after SubjectAccessReview.", } - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") actual := actions[1].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertCSRCondition(t, actual.(*certificatesv1.CertificateSigningRequest).Status.Conditions, expectedCondition) }, @@ -131,7 +132,7 @@ func TestSync(t *testing.T) { Reason: "AutoApprovedByHubCSRApprovingController", Message: "Auto approving Managed cluster agent certificate after SubjectAccessReview.", } - testinghelpers.AssertActions(t, actions, "create", "update") + testingcommon.AssertActions(t, actions, "create", "update") actual := actions[1].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertCSRCondition(t, actual.(*certificatesv1.CertificateSigningRequest).Status.Conditions, expectedCondition) }, @@ -159,7 +160,7 @@ func TestSync(t *testing.T) { Reason: "AutoApprovedByHubCSRApprovingController", Message: "Auto approving Managed cluster agent certificate after SubjectAccessReview.", } - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") actual := actions[0].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertCSRCondition(t, actual.(*certificatesv1.CertificateSigningRequest).Status.Conditions, expectedCondition) }, @@ -217,7 +218,7 @@ func TestSync(t *testing.T) { ), }, } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, validCSR.Name)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, validCSR.Name)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/lease/controller_test.go b/pkg/registration/hub/lease/controller_test.go index b1b001500..b01d48916 100644 --- a/pkg/registration/hub/lease/controller_test.go +++ b/pkg/registration/hub/lease/controller_test.go @@ -11,6 +11,7 @@ import ( clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" v1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -34,8 +35,8 @@ func TestSync(t *testing.T) { clusters: []runtime.Object{testinghelpers.NewManagedCluster()}, clusterLeases: []runtime.Object{}, validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, leaseActions) - testinghelpers.AssertNoActions(t, clusterActions) + testingcommon.AssertNoActions(t, leaseActions) + testingcommon.AssertNoActions(t, clusterActions) }, }, { @@ -43,8 +44,8 @@ func TestSync(t *testing.T) { clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()}, clusterLeases: []runtime.Object{}, validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) { - testinghelpers.AssertActions(t, leaseActions, "create") - testinghelpers.AssertNoActions(t, clusterActions) + testingcommon.AssertActions(t, leaseActions, "create") + testingcommon.AssertNoActions(t, clusterActions) }, }, { @@ -61,14 +62,14 @@ func TestSync(t *testing.T) { Reason: "ManagedClusterLeaseUpdateStopped", Message: "Registration agent stopped updating its lease.", } - testinghelpers.AssertActions(t, clusterActions, "get", "patch") + testingcommon.AssertActions(t, clusterActions, "get", "patch") patch := clusterActions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expected) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expected) }, }, { @@ -76,7 +77,7 @@ func TestSync(t *testing.T) { clusters: []runtime.Object{testinghelpers.NewAvailableManagedCluster()}, clusterLeases: []runtime.Object{testinghelpers.NewManagedClusterLease("managed-cluster-lease", now)}, validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, clusterActions) + testingcommon.AssertNoActions(t, clusterActions) }, }, { @@ -89,14 +90,14 @@ func TestSync(t *testing.T) { Reason: "ManagedClusterLeaseUpdateStopped", Message: "Registration agent stopped updating its lease.", } - testinghelpers.AssertActions(t, clusterActions, "get", "patch") + testingcommon.AssertActions(t, clusterActions, "get", "patch") patch := clusterActions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expected) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expected) }, }, { @@ -104,7 +105,7 @@ func TestSync(t *testing.T) { clusters: []runtime.Object{testinghelpers.NewUnknownManagedCluster()}, clusterLeases: []runtime.Object{testinghelpers.NewManagedClusterLease("managed-cluster-lease", now.Add(-5*time.Minute))}, validateActions: func(t *testing.T, leaseActions, clusterActions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, clusterActions) + testingcommon.AssertNoActions(t, clusterActions) }, }, } @@ -129,7 +130,7 @@ func TestSync(t *testing.T) { } } - syncCtx := testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName) + syncCtx := testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName) ctrl := &leaseController{ kubeClient: leaseClient, diff --git a/pkg/registration/hub/managedcluster/controller_test.go b/pkg/registration/hub/managedcluster/controller_test.go index b32fca893..be2527ea1 100644 --- a/pkg/registration/hub/managedcluster/controller_test.go +++ b/pkg/registration/hub/managedcluster/controller_test.go @@ -18,6 +18,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" kubefake "k8s.io/client-go/kubernetes/fake" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestSyncManagedCluster(t *testing.T) { @@ -30,14 +31,14 @@ func TestSyncManagedCluster(t *testing.T) { name: "sync a deleted spoke cluster", startingObjects: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "create a new spoke cluster", startingObjects: []runtime.Object{testinghelpers.NewManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "patch") + testingcommon.AssertActions(t, actions, "patch") patch := actions[0].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) @@ -57,21 +58,21 @@ func TestSyncManagedCluster(t *testing.T) { Reason: "HubClusterAdminAccepted", Message: "Accepted by hub cluster admin", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, { name: "sync an accepted spoke cluster", startingObjects: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get") + testingcommon.AssertActions(t, actions, "get") }, }, { @@ -84,21 +85,21 @@ func TestSyncManagedCluster(t *testing.T) { Reason: "HubClusterAdminDenied", Message: "Denied by hub cluster admin", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, { name: "delete a spoke cluster", startingObjects: []runtime.Object{testinghelpers.NewDeletingManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "patch") + testingcommon.AssertActions(t, actions, "patch") patch := actions[0].(clienttesting.PatchAction).GetPatch() managedCluster := &v1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) @@ -123,7 +124,7 @@ func TestSyncManagedCluster(t *testing.T) { } ctrl := managedClusterController{kubeClient, clusterClient, clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(), resourceapply.NewResourceCache(), eventstesting.NewTestingEventRecorder(t)} - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/managedclusterset/controller_test.go b/pkg/registration/hub/managedclusterset/controller_test.go index dcffdfc37..0d36a8c4f 100644 --- a/pkg/registration/hub/managedclusterset/controller_test.go +++ b/pkg/registration/hub/managedclusterset/controller_test.go @@ -2,6 +2,7 @@ package managedclusterset import ( "context" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "reflect" "testing" "time" @@ -14,7 +15,6 @@ import ( clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" - testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" ) func TestSyncClusterSet(t *testing.T) { @@ -377,7 +377,7 @@ func TestEnqueueUpdateClusterClusterSet(t *testing.T) { t.Errorf("Failed to add clusterset: %v, error: %v", clusterset, err) } } - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") ctrl := managedClusterSetController{ clusterClient: clusterClient, diff --git a/pkg/registration/hub/managedclusterset/default_managedclusterset_controller_test.go b/pkg/registration/hub/managedclusterset/default_managedclusterset_controller_test.go index beeb1cb04..aefad0dac 100644 --- a/pkg/registration/hub/managedclusterset/default_managedclusterset_controller_test.go +++ b/pkg/registration/hub/managedclusterset/default_managedclusterset_controller_test.go @@ -13,6 +13,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" ) @@ -33,7 +34,7 @@ func TestSyncDefaultClusterSet(t *testing.T) { name: "sync default cluster set", existingClusterSet: newDefaultManagedClusterSet(DefaultManagedClusterSetName, DefaultManagedClusterSet.Spec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -41,7 +42,7 @@ func TestSyncDefaultClusterSet(t *testing.T) { existingClusterSet: newDefaultManagedClusterSet(DefaultManagedClusterSetName, editedDefaultManagedClusterSetSpec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") clusterset := actions[0].(clienttesting.UpdateAction).GetObject().(*clusterv1beta2.ManagedClusterSet) // if spec not rollbacked, error if !equality.Semantic.DeepEqual(clusterset.Spec, DefaultManagedClusterSet.Spec) { @@ -53,14 +54,14 @@ func TestSyncDefaultClusterSet(t *testing.T) { name: "sync deleting default cluster set", existingClusterSet: newDefaultManagedClusterSet(DefaultManagedClusterSetName, DefaultManagedClusterSet.Spec, true), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "sync deleted default cluster set", // default cluster set should be created if it is deleted. validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create") + testingcommon.AssertActions(t, actions, "create") clusterset := actions[0].(clienttesting.CreateAction).GetObject().(*clusterv1beta2.ManagedClusterSet) if clusterset.ObjectMeta.Name != DefaultManagedClusterSetName { t.Errorf("Failed to create default managed cluster set") @@ -71,7 +72,7 @@ func TestSyncDefaultClusterSet(t *testing.T) { name: "sync default cluster set with disabled annotation", existingClusterSet: newDefaultManagedClusterSetWithAnnotation(DefaultManagedClusterSetName, autoUpdateAnnotation, "false", DefaultManagedClusterSet.Spec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, } @@ -99,7 +100,7 @@ func TestSyncDefaultClusterSet(t *testing.T) { eventRecorder: eventstesting.NewTestingEventRecorder(t), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/managedclusterset/global_managedclusterset_controller_test.go b/pkg/registration/hub/managedclusterset/global_managedclusterset_controller_test.go index 23d3301b7..ee6857b80 100644 --- a/pkg/registration/hub/managedclusterset/global_managedclusterset_controller_test.go +++ b/pkg/registration/hub/managedclusterset/global_managedclusterset_controller_test.go @@ -13,6 +13,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" ) @@ -33,7 +34,7 @@ func TestSyncGlobalClusterSet(t *testing.T) { name: "sync global cluster set", existingClusterSet: newGlobalManagedClusterSet(GlobalManagedClusterSetName, GlobalManagedClusterSet.Spec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { @@ -41,7 +42,7 @@ func TestSyncGlobalClusterSet(t *testing.T) { existingClusterSet: newGlobalManagedClusterSet(GlobalManagedClusterSetName, editedGlobalManagedClusterSetSpec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") clusterset := actions[0].(clienttesting.UpdateAction).GetObject().(*clusterv1beta2.ManagedClusterSet) // if spec not rollbacked, error if !equality.Semantic.DeepEqual(clusterset.Spec, GlobalManagedClusterSet.Spec) { @@ -53,7 +54,7 @@ func TestSyncGlobalClusterSet(t *testing.T) { name: "sync deleted global cluster set", // global cluster set should be created if it is deleted. validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "create") + testingcommon.AssertActions(t, actions, "create") clusterset := actions[0].(clienttesting.CreateAction).GetObject().(*clusterv1beta2.ManagedClusterSet) if clusterset.ObjectMeta.Name != GlobalManagedClusterSetName { t.Errorf("Failed to create global managed cluster set") @@ -64,7 +65,7 @@ func TestSyncGlobalClusterSet(t *testing.T) { name: "sync global cluster set with disabled annotation", existingClusterSet: newGlobalManagedClusterSetWithAnnotation(GlobalManagedClusterSetName, autoUpdateAnnotation, "false", GlobalManagedClusterSet.Spec, false), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, } @@ -92,7 +93,7 @@ func TestSyncGlobalClusterSet(t *testing.T) { eventRecorder: eventstesting.NewTestingEventRecorder(t), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/hub/managedclustersetbinding/controller_test.go b/pkg/registration/hub/managedclustersetbinding/controller_test.go index 5ded7e414..0392f07fd 100644 --- a/pkg/registration/hub/managedclustersetbinding/controller_test.go +++ b/pkg/registration/hub/managedclustersetbinding/controller_test.go @@ -15,7 +15,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2" - testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestSync(t *testing.T) { @@ -29,14 +29,14 @@ func TestSync(t *testing.T) { name: "wrong clustersetbinding", clusterSets: []runtime.Object{}, clusterSetBinding: newManagedClusterSetBinding("test", ""), - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "no clusterset", clusterSets: []runtime.Object{}, clusterSetBinding: newManagedClusterSetBinding("test", "testns"), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "patch") + testingcommon.AssertActions(t, actions, "patch") patchData := actions[0].(clienttesting.PatchActionImpl).Patch binding := &clusterv1beta2.ManagedClusterSetBinding{} err := json.Unmarshal(patchData, binding) @@ -44,7 +44,7 @@ func TestSync(t *testing.T) { t.Fatal(err) } - testinghelpers.AssertCondition(t, binding.Status.Conditions, metav1.Condition{ + testingcommon.AssertCondition(t, binding.Status.Conditions, metav1.Condition{ Type: clusterv1beta2.ClusterSetBindingBoundType, Status: metav1.ConditionFalse, Reason: "ClusterSetNotFound", @@ -56,7 +56,7 @@ func TestSync(t *testing.T) { clusterSets: []runtime.Object{newManagedClusterSet("test")}, clusterSetBinding: newManagedClusterSetBinding("test", "testns"), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "patch") + testingcommon.AssertActions(t, actions, "patch") patchData := actions[0].(clienttesting.PatchActionImpl).Patch binding := &clusterv1beta2.ManagedClusterSetBinding{} err := json.Unmarshal(patchData, binding) @@ -64,7 +64,7 @@ func TestSync(t *testing.T) { t.Fatal(err) } - testinghelpers.AssertCondition(t, binding.Status.Conditions, metav1.Condition{ + testingcommon.AssertCondition(t, binding.Status.Conditions, metav1.Condition{ Type: clusterv1beta2.ClusterSetBindingBoundType, Status: metav1.ConditionTrue, Reason: "ClusterSetBound", @@ -83,7 +83,7 @@ func TestSync(t *testing.T) { }) return binding }(), - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, } @@ -113,7 +113,7 @@ func TestSync(t *testing.T) { key, _ := cache.MetaNamespaceKeyFunc(c.clusterSetBinding) - syncErr := ctrl.sync(context.Background(), testinghelpers.NewFakeSyncContext(t, key)) + syncErr := ctrl.sync(context.Background(), testingcommon.NewFakeSyncContext(t, key)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } @@ -172,7 +172,7 @@ func TestEnqueue(t *testing.T) { } } - syncCtx := testinghelpers.NewFakeSyncContext(t, "fake") + syncCtx := testingcommon.NewFakeSyncContext(t, "fake") ctrl := managedClusterSetBindingController{ clusterSetBindingIndexers: informerFactory.Cluster().V1beta2().ManagedClusterSetBindings().Informer().GetIndexer(), diff --git a/pkg/registration/hub/rbacfinalizerdeletion/controller_test.go b/pkg/registration/hub/rbacfinalizerdeletion/controller_test.go index cb6090ca5..92d878bf3 100644 --- a/pkg/registration/hub/rbacfinalizerdeletion/controller_test.go +++ b/pkg/registration/hub/rbacfinalizerdeletion/controller_test.go @@ -23,6 +23,7 @@ import ( kubeinformers "k8s.io/client-go/informers" fakeclient "k8s.io/client-go/kubernetes/fake" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) var roleName = fmt.Sprintf("%s:spoke-work", testinghelpers.TestManagedClusterName) @@ -102,8 +103,8 @@ func TestSync(t *testing.T) { rbacClient: kubeClient.RbacV1(), eventRecorder: events.NewInMemoryRecorder(""), } - err := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, c.key)) - testinghelpers.AssertError(t, err, c.expectedErr) + err := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, c.key)) + testingcommon.AssertError(t, err, c.expectedErr) }) } } @@ -127,7 +128,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { cluster: testinghelpers.NewManagedCluster(), namespace: testinghelpers.NewNamespace(testinghelpers.TestManagedClusterName, false), work: testinghelpers.NewManifestWork(testinghelpers.TestManagedClusterName, "work1", nil, nil), - validateRbacActions: testinghelpers.AssertNoActions, + validateRbacActions: testingcommon.AssertNoActions, }, { name: "skip if neither role nor rolebinding has finalizer", @@ -137,7 +138,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { namespace: testinghelpers.NewNamespace(testinghelpers.TestManagedClusterName, false), work: testinghelpers.NewManifestWork(testinghelpers.TestManagedClusterName, "work1", []string{manifestWorkFinalizer}, nil), expectedWorkFinalizers: []string{manifestWorkFinalizer}, - validateRbacActions: testinghelpers.AssertNoActions, + validateRbacActions: testingcommon.AssertNoActions, }, { name: "remove finalizer from deleting role within non-terminating namespace", @@ -149,7 +150,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { expectedRoleBindingFinalizers: []string{manifestWorkFinalizer}, expectedWorkFinalizers: []string{manifestWorkFinalizer}, validateRbacActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") }, }, { @@ -159,7 +160,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { cluster: testinghelpers.NewDeletingManagedCluster(), namespace: testinghelpers.NewNamespace(testinghelpers.TestManagedClusterName, false), validateRbacActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update", "update") + testingcommon.AssertActions(t, actions, "update", "update") }, }, { @@ -168,7 +169,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { roleBinding: testinghelpers.NewRoleBinding(testinghelpers.TestManagedClusterName, roleName, []string{manifestWorkFinalizer}, true), namespace: testinghelpers.NewNamespace(testinghelpers.TestManagedClusterName, true), validateRbacActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update", "update") + testingcommon.AssertActions(t, actions, "update", "update") }, }, } @@ -200,7 +201,7 @@ func TestSyncRoleAndRoleBinding(t *testing.T) { rbacClient: fakeClient.RbacV1(), } - controllerContext := testinghelpers.NewFakeSyncContext(t, "") + controllerContext := testingcommon.NewFakeSyncContext(t, "") func() { ctx, cancel := context.WithTimeout(context.TODO(), 15*time.Second) diff --git a/pkg/registration/hub/taint/controller_test.go b/pkg/registration/hub/taint/controller_test.go index 712a014f3..bf449f71e 100644 --- a/pkg/registration/hub/taint/controller_test.go +++ b/pkg/registration/hub/taint/controller_test.go @@ -10,6 +10,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "github.com/openshift/library-go/pkg/operator/events/eventstesting" @@ -28,14 +29,14 @@ func TestSyncTaintCluster(t *testing.T) { name: "ManagedClusterConditionAvailable conditionStatus is True", startingObjects: []runtime.Object{testinghelpers.NewAvailableManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, { name: "ManagedClusterConditionAvailable conditionStatus is False", startingObjects: []runtime.Object{testinghelpers.NewUnAvailableManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") managedCluster := (actions[0].(clienttesting.UpdateActionImpl).Object).(*v1.ManagedCluster) taints := []v1.Taint{UnavailableTaint} if !reflect.DeepEqual(managedCluster.Spec.Taints, taints) { @@ -47,7 +48,7 @@ func TestSyncTaintCluster(t *testing.T) { name: "There is no ManagedClusterConditionAvailable", startingObjects: []runtime.Object{testinghelpers.NewManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") managedCluster := (actions[0].(clienttesting.UpdateActionImpl).Object).(*v1.ManagedCluster) taints := []v1.Taint{UnreachableTaint} if !reflect.DeepEqual(managedCluster.Spec.Taints, taints) { @@ -59,7 +60,7 @@ func TestSyncTaintCluster(t *testing.T) { name: "ManagedClusterConditionAvailable conditionStatus is Unknown", startingObjects: []runtime.Object{testinghelpers.NewUnknownManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "update") + testingcommon.AssertActions(t, actions, "update") managedCluster := (actions[0].(clienttesting.UpdateActionImpl).Object).(*v1.ManagedCluster) taints := []v1.Taint{UnreachableTaint} if !reflect.DeepEqual(managedCluster.Spec.Taints, taints) { @@ -71,7 +72,7 @@ func TestSyncTaintCluster(t *testing.T) { name: "sync a deleted spoke cluster", startingObjects: []runtime.Object{}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + testingcommon.AssertNoActions(t, actions) }, }, } @@ -88,7 +89,7 @@ func TestSyncTaintCluster(t *testing.T) { } ctrl := taintController{clusterClient, clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(), eventstesting.NewTestingEventRecorder(t)} - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, testinghelpers.TestManagedClusterName)) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/spoke/addon/lease_controller_test.go b/pkg/registration/spoke/addon/lease_controller_test.go index 2bee2f87b..0c87f3818 100644 --- a/pkg/registration/spoke/addon/lease_controller_test.go +++ b/pkg/registration/spoke/addon/lease_controller_test.go @@ -9,6 +9,7 @@ import ( addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" addonfake "open-cluster-management.io/api/client/addon/clientset/versioned/fake" addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "k8s.io/apimachinery/pkg/api/meta" @@ -103,7 +104,7 @@ func TestSync(t *testing.T) { hubLeases []runtime.Object managementLeases []runtime.Object spokeLeases []runtime.Object - validateActions func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) + validateActions func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) }{ { name: "bad queue key", @@ -111,8 +112,8 @@ func TestSync(t *testing.T) { addOns: []runtime.Object{}, hubLeases: []runtime.Object{}, spokeLeases: []runtime.Object{}, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertNoActions(t, actions) }, }, { @@ -121,8 +122,8 @@ func TestSync(t *testing.T) { addOns: []runtime.Object{}, spokeLeases: []runtime.Object{}, hubLeases: []runtime.Object{}, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertNoActions(t, actions) }, }, { @@ -139,8 +140,8 @@ func TestSync(t *testing.T) { }}, hubLeases: []runtime.Object{}, spokeLeases: []runtime.Object{}, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} err := json.Unmarshal(patch, addOn) @@ -173,8 +174,8 @@ func TestSync(t *testing.T) { spokeLeases: []runtime.Object{ testinghelpers.NewAddOnLease("test", "test", now.Add(-5*time.Minute)), }, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} err := json.Unmarshal(patch, addOn) @@ -207,8 +208,8 @@ func TestSync(t *testing.T) { spokeLeases: []runtime.Object{ testinghelpers.NewAddOnLease("test", "test", now), }, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} err := json.Unmarshal(patch, addOn) @@ -251,8 +252,8 @@ func TestSync(t *testing.T) { spokeLeases: []runtime.Object{ testinghelpers.NewAddOnLease("test", "test", now), }, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertNoActions(t, actions) }, }, { @@ -279,7 +280,7 @@ func TestSync(t *testing.T) { spokeLeases: []runtime.Object{ testinghelpers.NewAddOnLease("test1", "test1", now.Add(-5*time.Minute)), }, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { if ctx.Queue().Len() != 2 { t.Errorf("expected two addons in queue, but failed") } @@ -304,8 +305,8 @@ func TestSync(t *testing.T) { managementLeases: []runtime.Object{ testinghelpers.NewAddOnLease("test", "test", now), }, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} err := json.Unmarshal(patch, addOn) @@ -333,8 +334,8 @@ func TestSync(t *testing.T) { }}, hubLeases: []runtime.Object{testinghelpers.NewAddOnLease(testinghelpers.TestManagedClusterName, "test", now)}, spokeLeases: []runtime.Object{}, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() addOn := &addonv1alpha1.ManagedClusterAddOn{} err := json.Unmarshal(patch, addOn) @@ -367,8 +368,8 @@ func TestSync(t *testing.T) { }}, hubLeases: []runtime.Object{}, spokeLeases: []runtime.Object{}, - validateActions: func(t *testing.T, ctx *testinghelpers.FakeSyncContext, actions []clienttesting.Action) { - testinghelpers.AssertNoActions(t, actions) + validateActions: func(t *testing.T, ctx *testingcommon.FakeSyncContext, actions []clienttesting.Action) { + testingcommon.AssertNoActions(t, actions) }, }, } @@ -397,7 +398,7 @@ func TestSync(t *testing.T) { managementLeaseClient: managementLeaseClient.CoordinationV1(), spokeLeaseClient: spokeLeaseClient.CoordinationV1(), } - syncCtx := testinghelpers.NewFakeSyncContext(t, c.queueKey) + syncCtx := testingcommon.NewFakeSyncContext(t, c.queueKey) syncErr := ctrl.sync(context.TODO(), syncCtx) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) diff --git a/pkg/registration/spoke/addon/registration_controller_test.go b/pkg/registration/spoke/addon/registration_controller_test.go index ac6b92291..140f11d68 100644 --- a/pkg/registration/spoke/addon/registration_controller_test.go +++ b/pkg/registration/spoke/addon/registration_controller_test.go @@ -18,7 +18,7 @@ import ( addonv1alpha1 "open-cluster-management.io/api/addon/v1alpha1" addonfake "open-cluster-management.io/api/client/addon/clientset/versioned/fake" addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions" - testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestFilterCSREvents(t *testing.T) { @@ -146,7 +146,7 @@ func TestRegistrationSync(t *testing.T) { if len(actions) != 1 { t.Errorf("expect 1 actions but got %d", len(actions)) } - testinghelpers.AssertActions(t, actions, "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -171,7 +171,7 @@ func TestRegistrationSync(t *testing.T) { if len(actions) != 1 { t.Errorf("expect 1 actions but got %d", len(actions)) } - testinghelpers.AssertActions(t, actions, "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -191,7 +191,7 @@ func TestRegistrationSync(t *testing.T) { if len(actions) != 1 { t.Errorf("expect 1 actions but got %d", len(actions)) } - testinghelpers.AssertActions(t, actions, "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -238,7 +238,7 @@ func TestRegistrationSync(t *testing.T) { if len(managementActions) != 1 { t.Errorf("expect 1 management actions but got %d", len(managementActions)) } - testinghelpers.AssertActions(t, managementActions, "delete") + testingcommon.AssertActions(t, managementActions, "delete") }, }, { @@ -267,7 +267,7 @@ func TestRegistrationSync(t *testing.T) { if len(managementActions) != 1 { t.Errorf("expect 1 management actions but got %d", len(managementActions)) } - testinghelpers.AssertActions(t, managementActions, "delete") + testingcommon.AssertActions(t, managementActions, "delete") }, }, { @@ -297,7 +297,7 @@ func TestRegistrationSync(t *testing.T) { if len(actions) != 1 { t.Errorf("expect 1 management actions but got %d", len(actions)) } - testinghelpers.AssertActions(t, actions, "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -318,7 +318,7 @@ func TestRegistrationSync(t *testing.T) { if len(managementActions) != 1 { t.Errorf("expect 1 actions but got %d", len(managementActions)) } - testinghelpers.AssertActions(t, managementActions, "delete") + testingcommon.AssertActions(t, managementActions, "delete") }, }, { @@ -351,7 +351,7 @@ func TestRegistrationSync(t *testing.T) { if len(actions) != 1 { t.Errorf("expect 1 actions but got %d", len(actions)) } - testinghelpers.AssertActions(t, actions, "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, } @@ -390,7 +390,7 @@ func TestRegistrationSync(t *testing.T) { addOnRegistrationConfigs: c.addOnRegistrationConfigs, } - err := controller.sync(context.Background(), testinghelpers.NewFakeSyncContext(t, c.queueKey)) + err := controller.sync(context.Background(), testingcommon.NewFakeSyncContext(t, c.queueKey)) if err != nil { t.Errorf("unexpected error: %v", err) } diff --git a/pkg/registration/spoke/managedcluster/claim_controller_test.go b/pkg/registration/spoke/managedcluster/claim_controller_test.go index 24333d0f1..8918036fd 100644 --- a/pkg/registration/spoke/managedcluster/claim_controller_test.go +++ b/pkg/registration/spoke/managedcluster/claim_controller_test.go @@ -11,6 +11,7 @@ import ( clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -28,13 +29,13 @@ func TestSync(t *testing.T) { }{ { name: "sync no managed cluster", - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, expectedErr: "unable to get managed cluster with name \"testmanagedcluster\" from hub: managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found", }, { name: "skip when managed cluster does not join the hub yet", cluster: testinghelpers.NewManagedCluster(), - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "sync a joined managed cluster", @@ -50,7 +51,7 @@ func TestSync(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() cluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, cluster) @@ -100,8 +101,8 @@ func TestSync(t *testing.T) { claimLister: clusterInformerFactory.Cluster().V1alpha1().ClusterClaims().Lister(), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "")) - testinghelpers.AssertError(t, syncErr, c.expectedErr) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "")) + testingcommon.AssertError(t, syncErr, c.expectedErr) c.validateActions(t, clusterClient.Actions()) }) @@ -131,7 +132,7 @@ func TestExposeClaims(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() cluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, cluster) @@ -189,7 +190,7 @@ func TestExposeClaims(t *testing.T) { }, maxCustomClusterClaims: 2, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() cluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, cluster) @@ -225,7 +226,7 @@ func TestExposeClaims(t *testing.T) { }, }), validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() cluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, cluster) @@ -261,7 +262,7 @@ func TestExposeClaims(t *testing.T) { }, }, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() cluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, cluster) @@ -315,8 +316,8 @@ func TestExposeClaims(t *testing.T) { claimLister: clusterInformerFactory.Cluster().V1alpha1().ClusterClaims().Lister(), } - syncErr := ctrl.exposeClaims(context.TODO(), testinghelpers.NewFakeSyncContext(t, c.cluster.Name), c.cluster) - testinghelpers.AssertError(t, syncErr, c.expectedErr) + syncErr := ctrl.exposeClaims(context.TODO(), testingcommon.NewFakeSyncContext(t, c.cluster.Name), c.cluster) + testingcommon.AssertError(t, syncErr, c.expectedErr) c.validateActions(t, clusterClient.Actions()) }) diff --git a/pkg/registration/spoke/managedcluster/creating_controller_test.go b/pkg/registration/spoke/managedcluster/creating_controller_test.go index de75addb3..d2b20e7e0 100644 --- a/pkg/registration/spoke/managedcluster/creating_controller_test.go +++ b/pkg/registration/spoke/managedcluster/creating_controller_test.go @@ -6,6 +6,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterv1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "k8s.io/apimachinery/pkg/runtime" @@ -30,7 +31,7 @@ func TestCreateSpokeCluster(t *testing.T) { CABundle: []byte("testcabundle"), }, } - testinghelpers.AssertActions(t, actions, "get", "create") + testingcommon.AssertActions(t, actions, "get", "create") actual := actions[1].(clienttesting.CreateActionImpl).Object actualClientConfigs := actual.(*clusterv1.ManagedCluster).Spec.ManagedClusterClientConfigs testinghelpers.AssertManagedClusterClientConfigs(t, actualClientConfigs, expectedClientConfigs) @@ -40,7 +41,7 @@ func TestCreateSpokeCluster(t *testing.T) { name: "create an existed cluster", startingObjects: []runtime.Object{testinghelpers.NewManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertActions(t, actions, "get", "update") + testingcommon.AssertActions(t, actions, "get", "update") }, }, } @@ -55,7 +56,7 @@ func TestCreateSpokeCluster(t *testing.T) { hubClusterClient: clusterClient, } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "")) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "")) if syncErr != nil { t.Errorf("unexpected err: %v", syncErr) } diff --git a/pkg/registration/spoke/managedcluster/joining_controller_test.go b/pkg/registration/spoke/managedcluster/joining_controller_test.go index 6ec1ba387..45f14f6c1 100644 --- a/pkg/registration/spoke/managedcluster/joining_controller_test.go +++ b/pkg/registration/spoke/managedcluster/joining_controller_test.go @@ -9,6 +9,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -26,13 +27,13 @@ func TestSyncManagedCluster(t *testing.T) { { name: "sync no managed cluster", startingObjects: []runtime.Object{}, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, expectedErr: "unable to get managed cluster with name \"testmanagedcluster\" from hub: managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found", }, { name: "sync an unaccepted managed cluster", startingObjects: []runtime.Object{testinghelpers.NewManagedCluster()}, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, }, { name: "sync an accepted managed cluster", @@ -44,14 +45,14 @@ func TestSyncManagedCluster(t *testing.T) { Reason: "ManagedClusterJoined", Message: "Managed cluster joined", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, } @@ -73,8 +74,8 @@ func TestSyncManagedCluster(t *testing.T) { hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "")) - testinghelpers.AssertError(t, syncErr, c.expectedErr) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "")) + testingcommon.AssertError(t, syncErr, c.expectedErr) c.validateActions(t, clusterClient.Actions()) }) diff --git a/pkg/registration/spoke/managedcluster/lease_controller_test.go b/pkg/registration/spoke/managedcluster/lease_controller_test.go index 20f51b5aa..34e071e6a 100644 --- a/pkg/registration/spoke/managedcluster/lease_controller_test.go +++ b/pkg/registration/spoke/managedcluster/lease_controller_test.go @@ -7,6 +7,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" "github.com/openshift/library-go/pkg/operator/events/eventstesting" @@ -29,7 +30,7 @@ func TestLeaseUpdate(t *testing.T) { name: "start lease update routine", clusters: []runtime.Object{testinghelpers.NewAcceptedManagedCluster()}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - testinghelpers.AssertUpdateActions(t, actions) + testingcommon.AssertUpdateActions(t, actions) leaseObj := actions[1].(clienttesting.UpdateActionImpl).Object lastLeaseObj := actions[len(actions)-1].(clienttesting.UpdateActionImpl).Object testinghelpers.AssertLeaseUpdated(t, leaseObj.(*coordinationv1.Lease), lastLeaseObj.(*coordinationv1.Lease)) @@ -39,14 +40,14 @@ func TestLeaseUpdate(t *testing.T) { name: "delete a managed cluster after lease update routine is started", clusters: []runtime.Object{}, needToStartUpdateBefore: true, - validateActions: testinghelpers.AssertNoMoreUpdates, + validateActions: testingcommon.AssertNoMoreUpdates, expectedErr: "unable to get managed cluster \"testmanagedcluster\" from hub: managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found", }, { name: "unaccept a managed cluster after lease update routine is started", clusters: []runtime.Object{testinghelpers.NewManagedCluster()}, needToStartUpdateBefore: true, - validateActions: testinghelpers.AssertNoMoreUpdates, + validateActions: testingcommon.AssertNoMoreUpdates, }, } @@ -81,8 +82,8 @@ func TestLeaseUpdate(t *testing.T) { hubClusterLister: clusterInformerFactory.Cluster().V1().ManagedClusters().Lister(), leaseUpdater: leaseUpdater, } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "")) - testinghelpers.AssertError(t, syncErr, c.expectedErr) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "")) + testingcommon.AssertError(t, syncErr, c.expectedErr) // wait one cycle time.Sleep(1200 * time.Millisecond) diff --git a/pkg/registration/spoke/managedcluster/registration_test.go b/pkg/registration/spoke/managedcluster/registration_test.go index d2725f7bd..0588d0de7 100644 --- a/pkg/registration/spoke/managedcluster/registration_test.go +++ b/pkg/registration/spoke/managedcluster/registration_test.go @@ -4,6 +4,7 @@ import ( "testing" "time" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" ) @@ -34,7 +35,7 @@ func TestGetClusterAgentNamesFromCertificate(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { clusterName, agentName, err := GetClusterAgentNamesFromCertificate(c.certData) - testinghelpers.AssertErrorWithPrefix(t, err, c.expectedErrorPrefix) + testingcommon.AssertErrorWithPrefix(t, err, c.expectedErrorPrefix) if clusterName != c.expectedClusterName { t.Errorf("expect %v, but got %v", c.expectedClusterName, clusterName) diff --git a/pkg/registration/spoke/managedcluster/status_controller_test.go b/pkg/registration/spoke/managedcluster/status_controller_test.go index 066fd2af1..edebc2f51 100644 --- a/pkg/registration/spoke/managedcluster/status_controller_test.go +++ b/pkg/registration/spoke/managedcluster/status_controller_test.go @@ -11,6 +11,7 @@ import ( clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake" clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" clusterv1 "open-cluster-management.io/api/cluster/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" corev1 "k8s.io/api/core/v1" @@ -75,7 +76,7 @@ func TestHealthCheck(t *testing.T) { { name: "there are no managed clusters", clusters: []runtime.Object{}, - validateActions: testinghelpers.AssertNoActions, + validateActions: testingcommon.AssertNoActions, expectedErr: "unable to get managed cluster \"testmanagedcluster\" from hub: managedcluster.cluster.open-cluster-management.io \"testmanagedcluster\" not found", }, { @@ -90,14 +91,14 @@ func TestHealthCheck(t *testing.T) { Reason: "ManagedClusterKubeAPIServerUnavailable", Message: "The kube-apiserver is not ok, status code: 500, an error on the server (\"internal server error\") has prevented the request from succeeding", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, { @@ -127,14 +128,14 @@ func TestHealthCheck(t *testing.T) { clusterv1.ResourceMemory: *resource.NewQuantity(int64(1024*1024*32), resource.BinarySI), }, } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) testinghelpers.AssertManagedClusterStatus(t, managedCluster.Status, expectedStatus) }, }, @@ -150,14 +151,14 @@ func TestHealthCheck(t *testing.T) { Reason: "ManagedClusterAvailable", Message: "Managed cluster is available", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, { @@ -172,14 +173,14 @@ func TestHealthCheck(t *testing.T) { Reason: "ManagedClusterAvailable", Message: "Managed cluster is available", } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) }, }, { @@ -219,14 +220,14 @@ func TestHealthCheck(t *testing.T) { clusterv1.ResourceMemory: *resource.NewQuantity(int64(1024*1024*64), resource.BinarySI), }, } - testinghelpers.AssertActions(t, actions, "get", "patch") + testingcommon.AssertActions(t, actions, "get", "patch") patch := actions[1].(clienttesting.PatchAction).GetPatch() managedCluster := &clusterv1.ManagedCluster{} err := json.Unmarshal(patch, managedCluster) if err != nil { t.Fatal(err) } - testinghelpers.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) + testingcommon.AssertCondition(t, managedCluster.Status.Conditions, expectedCondition) testinghelpers.AssertManagedClusterStatus(t, managedCluster.Status, expectedStatus) }, }, @@ -261,8 +262,8 @@ func TestHealthCheck(t *testing.T) { managedClusterDiscoveryClient: discoveryClient, nodeLister: kubeInformerFactory.Core().V1().Nodes().Lister(), } - syncErr := ctrl.sync(context.TODO(), testinghelpers.NewFakeSyncContext(t, "")) - testinghelpers.AssertError(t, syncErr, c.expectedErr) + syncErr := ctrl.sync(context.TODO(), testingcommon.NewFakeSyncContext(t, "")) + testingcommon.AssertError(t, syncErr, c.expectedErr) c.validateActions(t, clusterClient.Actions()) }) diff --git a/pkg/registration/spoke/spokeagent_test.go b/pkg/registration/spoke/spokeagent_test.go index afe29b10f..595243cde 100644 --- a/pkg/registration/spoke/spokeagent_test.go +++ b/pkg/registration/spoke/spokeagent_test.go @@ -10,6 +10,7 @@ import ( "time" "github.com/openshift/library-go/pkg/operator/events/eventstesting" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/registration/clientcert" testinghelpers "open-cluster-management.io/ocm/pkg/registration/helpers/testing" @@ -218,7 +219,7 @@ func TestValidate(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { err := c.options.Validate() - testinghelpers.AssertError(t, err, c.expectedErr) + testingcommon.AssertError(t, err, c.expectedErr) }) } } @@ -406,7 +407,7 @@ func TestGetSpokeClusterCABundle(t *testing.T) { restConig.CAFile = path.Join(tempDir, c.caFile) } caData, err := c.options.getSpokeClusterCABundle(restConig) - testinghelpers.AssertError(t, err, c.expectedErr) + testingcommon.AssertError(t, err, c.expectedErr) if c.expectedCAData == nil && caData == nil { return } diff --git a/pkg/work/spoke/apply/create_only_apply_test.go b/pkg/work/spoke/apply/create_only_apply_test.go index b8cca46d3..8ab367968 100644 --- a/pkg/work/spoke/apply/create_only_apply_test.go +++ b/pkg/work/spoke/apply/create_only_apply_test.go @@ -11,6 +11,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" fakedynamic "k8s.io/client-go/dynamic/fake" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" ) @@ -30,12 +31,7 @@ func TestCreateOnlyApply(t *testing.T) { required: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"), gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Errorf("Expect 2 actions, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") obj := actions[1].(clienttesting.CreateActionImpl).Object.(*unstructured.Unstructured) owners := obj.GetOwnerReferences() @@ -55,11 +51,7 @@ func TestCreateOnlyApply(t *testing.T) { required: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"), gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "get") + testingcommon.AssertActions(t, actions, "get") action := actions[0].(clienttesting.GetActionImpl) if action.Namespace != "ns1" || action.Name != "test" { @@ -79,7 +71,7 @@ func TestCreateOnlyApply(t *testing.T) { dynamicClient := fakedynamic.NewSimpleDynamicClient(scheme, objects...) applier := NewCreateOnlyApply(dynamicClient) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") obj, err := applier.Apply( context.TODO(), c.gvr, c.required, c.owner, nil, syncContext.Recorder()) diff --git a/pkg/work/spoke/apply/server_side_apply_test.go b/pkg/work/spoke/apply/server_side_apply_test.go index d6cc202ea..109e98cf6 100644 --- a/pkg/work/spoke/apply/server_side_apply_test.go +++ b/pkg/work/spoke/apply/server_side_apply_test.go @@ -16,6 +16,7 @@ import ( fakedynamic "k8s.io/client-go/dynamic/fake" clienttesting "k8s.io/client-go/testing" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" ) @@ -45,11 +46,7 @@ func TestServerSideApply(t *testing.T) { gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, conflict: true, validateActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "patch") + testingcommon.AssertActions(t, actions, "patch") }, }, } @@ -71,7 +68,7 @@ func TestServerSideApply(t *testing.T) { applier := NewServerSideApply(dynamicClient) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") option := &workapiv1.ManifestConfigOption{ UpdateStrategy: &workapiv1.UpdateStrategy{ Type: workapiv1.UpdateStrategyTypeServerSideApply, diff --git a/pkg/work/spoke/apply/update_apply_test.go b/pkg/work/spoke/apply/update_apply_test.go index fc4228e77..02a7e210b 100644 --- a/pkg/work/spoke/apply/update_apply_test.go +++ b/pkg/work/spoke/apply/update_apply_test.go @@ -15,6 +15,7 @@ import ( fakedynamic "k8s.io/client-go/dynamic/fake" "k8s.io/client-go/kubernetes/fake" clienttesting "k8s.io/client-go/testing" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" ) @@ -83,13 +84,7 @@ func TestApplyUnstructred(t *testing.T) { required: spoketesting.NewUnstructured("v1", "Secret", "ns1", "test"), gvr: schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, validateActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 2 { - t.Errorf("Expect 2 actions, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "create") - + testingcommon.AssertActions(t, actions, "get", "create") obj := actions[1].(clienttesting.CreateActionImpl).Object.(*unstructured.Unstructured) owners := obj.GetOwnerReferences() if len(owners) != 1 { @@ -111,8 +106,7 @@ func TestApplyUnstructred(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") obj := actions[1].(clienttesting.CreateActionImpl).Object.(*unstructured.Unstructured) owners := obj.GetOwnerReferences() @@ -133,8 +127,7 @@ func TestApplyUnstructred(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object.(*unstructured.Unstructured) owners := obj.GetOwnerReferences() @@ -174,8 +167,7 @@ func TestApplyUnstructred(t *testing.T) { if len(actions) != 2 { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object.(*unstructured.Unstructured) owners := obj.GetOwnerReferences() @@ -204,8 +196,7 @@ func TestApplyUnstructred(t *testing.T) { if len(actions) != 2 { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object.(*unstructured.Unstructured) labels := obj.GetLabels() @@ -234,8 +225,7 @@ func TestApplyUnstructred(t *testing.T) { if len(actions) != 2 { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object.(*unstructured.Unstructured) annotations := obj.GetAnnotations() @@ -303,7 +293,7 @@ func TestApplyUnstructred(t *testing.T) { applier := NewUpdateApply(dynamicClient, nil, nil) c.required.SetOwnerReferences([]metav1.OwnerReference{c.owner}) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") _, _, err := applier.applyUnstructured( context.TODO(), c.required, c.gvr, syncContext.Recorder()) @@ -335,8 +325,7 @@ func TestUpdateApplyKube(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") }, }, { @@ -350,8 +339,7 @@ func TestUpdateApplyKube(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") obj := actions[1].(clienttesting.UpdateActionImpl).Object.(*corev1.Secret) data, ok := obj.Data["test"] @@ -372,7 +360,7 @@ func TestUpdateApplyKube(t *testing.T) { applier := NewUpdateApply(nil, kubeclient, nil) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") obj, err := applier.Apply( context.TODO(), c.gvr, c.required, c.owner, nil, syncContext.Recorder()) @@ -441,7 +429,7 @@ func TestUpdateApplyDynamic(t *testing.T) { applier := NewUpdateApply(dynamicclient, nil, nil) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") obj, err := applier.Apply( context.TODO(), c.gvr, c.required, c.owner, nil, syncContext.Recorder()) @@ -492,8 +480,7 @@ func TestUpdateApplyApiExtension(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "create") + testingcommon.AssertActions(t, actions, "get", "create") }, }, { @@ -507,8 +494,7 @@ func TestUpdateApplyApiExtension(t *testing.T) { t.Errorf("Expect 2 actions, but have %d", len(actions)) } - spoketesting.AssertAction(t, actions[0], "get") - spoketesting.AssertAction(t, actions[1], "update") + testingcommon.AssertActions(t, actions, "get", "update") }, }, } @@ -523,7 +509,7 @@ func TestUpdateApplyApiExtension(t *testing.T) { applier := NewUpdateApply(nil, nil, apiextensionClient) - syncContext := spoketesting.NewFakeSyncContext(t, "test") + syncContext := testingcommon.NewFakeSyncContext(t, "test") obj, err := applier.Apply( context.TODO(), c.gvr, c.required, c.owner, nil, syncContext.Recorder()) diff --git a/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller_test.go b/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller_test.go index e52c5c27d..0ac1b557e 100644 --- a/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller_test.go +++ b/pkg/work/spoke/controllers/appliedmanifestcontroller/appliedmanifestwork_controller_test.go @@ -17,6 +17,7 @@ import ( fakeworkclient "open-cluster-management.io/api/client/work/clientset/versioned/fake" workinformers "open-cluster-management.io/api/client/work/informers/externalversions" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" ) @@ -217,7 +218,7 @@ func TestSyncManifestWork(t *testing.T) { rateLimiter: workqueue.NewItemExponentialFailureRateLimiter(0, 1*time.Second), } - controllerContext := spoketesting.NewFakeSyncContext(t, testingWork.Name) + controllerContext := testingcommon.NewFakeSyncContext(t, testingWork.Name) err := controller.sync(context.TODO(), controllerContext) if err != nil { t.Fatal(err) diff --git a/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller_test.go b/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller_test.go index c3e045011..b4a3f34e5 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller_test.go +++ b/pkg/work/spoke/controllers/finalizercontroller/appliedmanifestwork_finalize_controller_test.go @@ -16,6 +16,7 @@ import ( "k8s.io/client-go/util/workqueue" fakeworkclient "open-cluster-management.io/api/client/work/clientset/versioned/fake" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" @@ -195,7 +196,7 @@ func TestFinalize(t *testing.T) { rateLimiter: workqueue.NewItemExponentialFailureRateLimiter(0, 1*time.Second), } - controllerContext := spoketesting.NewFakeSyncContext(t, testingWork.Name) + controllerContext := testingcommon.NewFakeSyncContext(t, testingWork.Name) err := controller.syncAppliedManifestWork(context.TODO(), controllerContext, testingWork) if err != nil { t.Fatal(err) diff --git a/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller_test.go b/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller_test.go index 916993496..e3a20e6e9 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller_test.go +++ b/pkg/work/spoke/controllers/finalizercontroller/manifestwork_finalize_controller_test.go @@ -12,8 +12,8 @@ import ( fakeworkclient "open-cluster-management.io/api/client/work/clientset/versioned/fake" workinformers "open-cluster-management.io/api/client/work/informers/externalversions" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/spoke/controllers" - "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" ) func TestSyncManifestWorkController(t *testing.T) { @@ -65,11 +65,7 @@ func TestSyncManifestWorkController(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "delete") + testingcommon.AssertActions(t, actions, "delete") }, validateManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { if len(actions) != 0 { @@ -95,11 +91,7 @@ func TestSyncManifestWorkController(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "delete") + testingcommon.AssertActions(t, actions, "delete") }, validateManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { if len(actions) != 0 { @@ -159,10 +151,7 @@ func TestSyncManifestWorkController(t *testing.T) { } }, validateManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Suppose 1 action for manifestwork, but got %d", len(actions)) - } - spoketesting.AssertAction(t, actions[0], "update") + testingcommon.AssertActions(t, actions, "update") updateAction := actions[0].(clienttesting.UpdateActionImpl) obj := updateAction.Object.(*workapiv1.ManifestWork) if len(obj.Finalizers) != 0 { @@ -214,7 +203,7 @@ func TestSyncManifestWorkController(t *testing.T) { rateLimiter: workqueue.NewItemExponentialFailureRateLimiter(0, 1*time.Second), } - controllerContext := spoketesting.NewFakeSyncContext(t, c.workName) + controllerContext := testingcommon.NewFakeSyncContext(t, c.workName) err := controller.sync(context.TODO(), controllerContext) if err != nil { t.Errorf("Expect no sync error, but got %v", err) diff --git a/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller_test.go b/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller_test.go index e3d0e1cb7..488007b5c 100644 --- a/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller_test.go +++ b/pkg/work/spoke/controllers/finalizercontroller/unmanaged_appliedmanifestwork_controller_test.go @@ -13,7 +13,7 @@ import ( fakeworkclient "open-cluster-management.io/api/client/work/clientset/versioned/fake" workinformers "open-cluster-management.io/api/client/work/informers/externalversions" workapiv1 "open-cluster-management.io/api/work/v1" - "open-cluster-management.io/ocm/pkg/work/spoke/spoketesting" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" ) func TestSyncUnamanagedAppliedWork(t *testing.T) { @@ -56,11 +56,7 @@ func TestSyncUnamanagedAppliedWork(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "patch") + testingcommon.AssertActions(t, actions, "patch") }, }, { @@ -89,11 +85,7 @@ func TestSyncUnamanagedAppliedWork(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "patch") + testingcommon.AssertActions(t, actions, "patch") }, }, { @@ -128,11 +120,7 @@ func TestSyncUnamanagedAppliedWork(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "delete") + testingcommon.AssertActions(t, actions, "delete") }, }, { @@ -166,11 +154,7 @@ func TestSyncUnamanagedAppliedWork(t *testing.T) { }, }, validateAppliedManifestWorkActions: func(t *testing.T, actions []clienttesting.Action) { - if len(actions) != 1 { - t.Errorf("Expect 1 actions on appliedmanifestwork, but have %d", len(actions)) - } - - spoketesting.AssertAction(t, actions[0], "patch") + testingcommon.AssertActions(t, actions, "patch") }, }, { @@ -227,7 +211,7 @@ func TestSyncUnamanagedAppliedWork(t *testing.T) { rateLimiter: workqueue.NewItemExponentialFailureRateLimiter(0, c.evictionGracePeriod), } - controllerContext := spoketesting.NewFakeSyncContext(t, c.appliedManifestWorkName) + controllerContext := testingcommon.NewFakeSyncContext(t, c.appliedManifestWorkName) if err := controller.sync(context.TODO(), controllerContext); err != nil { t.Errorf("Expect no sync error, but got %v", err) } diff --git a/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller_test.go b/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller_test.go index 15d943df0..625b4d655 100644 --- a/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller_test.go +++ b/pkg/work/spoke/controllers/manifestcontroller/manifestwork_controller_test.go @@ -22,6 +22,7 @@ import ( workinformers "open-cluster-management.io/api/client/work/informers/externalversions" workapiv1 "open-cluster-management.io/api/work/v1" + testingcommon "open-cluster-management.io/ocm/pkg/common/testing" "open-cluster-management.io/ocm/pkg/work/helper" "open-cluster-management.io/ocm/pkg/work/spoke/apply" "open-cluster-management.io/ocm/pkg/work/spoke/auth/basic" @@ -202,33 +203,13 @@ func (t *testCase) validate( actualAppliedWorkActions = append(actualAppliedWorkActions, workAction) } } - if len(actualWorkActions) != len(t.expectedWorkAction) { - ts.Errorf("Expected work client has %d action but got %#v", len(t.expectedWorkAction), actualWorkActions) - } - for index := range actualWorkActions { - spoketesting.AssertAction(ts, actualWorkActions[index], t.expectedWorkAction[index]) - } - if len(actualAppliedWorkActions) != len(t.expectedAppliedWorkAction) { - ts.Errorf("Expected applied work client has %d action but got %#v", len(t.expectedAppliedWorkAction), actualAppliedWorkActions) - } - for index := range actualAppliedWorkActions { - spoketesting.AssertAction(ts, actualAppliedWorkActions[index], t.expectedAppliedWorkAction[index]) - } + testingcommon.AssertActions(ts, actualWorkActions, t.expectedWorkAction...) + testingcommon.AssertActions(ts, actualAppliedWorkActions, t.expectedAppliedWorkAction...) spokeDynamicActions := dynamicClient.Actions() - if len(spokeDynamicActions) != len(t.expectedDynamicAction) { - ts.Errorf("Expected dynamic client has %d action but got %#v", len(t.expectedDynamicAction), spokeDynamicActions) - } - for index := range spokeDynamicActions { - spoketesting.AssertAction(ts, spokeDynamicActions[index], t.expectedDynamicAction[index]) - } + testingcommon.AssertActions(ts, spokeDynamicActions, t.expectedDynamicAction...) spokeKubeActions := kubeClient.Actions() - if len(spokeKubeActions) != len(t.expectedKubeAction) { - ts.Errorf("Expected kube client has %d action but got %#v", len(t.expectedKubeAction), spokeKubeActions) - } - for index := range spokeKubeActions { - spoketesting.AssertAction(ts, spokeKubeActions[index], t.expectedKubeAction[index]) - } + testingcommon.AssertActions(ts, spokeKubeActions, t.expectedKubeAction...) actual, ok := actualWorkActions[len(actualWorkActions)-1].(clienttesting.UpdateActionImpl) if !ok { @@ -335,7 +316,7 @@ func TestSync(t *testing.T) { controller := newController(t, work, nil, spoketesting.NewFakeRestMapper()). withKubeObject(c.spokeObject...). withUnstructuredObject(c.spokeDynamicObject...) - syncContext := spoketesting.NewFakeSyncContext(t, workKey) + syncContext := testingcommon.NewFakeSyncContext(t, workKey) err := controller.toController().sync(context.TODO(), syncContext) if err != nil { t.Errorf("Should be success with no err: %v", err) @@ -375,7 +356,7 @@ func TestFailedToApplyResource(t *testing.T) { return true, &corev1.Secret{}, fmt.Errorf("Fake error") }) - syncContext := spoketesting.NewFakeSyncContext(t, workKey) + syncContext := testingcommon.NewFakeSyncContext(t, workKey) err := controller.toController().sync(context.TODO(), syncContext) if err == nil { t.Errorf("Should return an err") @@ -454,7 +435,7 @@ func TestUpdateStrategy(t *testing.T) { controller.dynamicClient.PrependReactor("patch", "newobjects", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) { return true, spoketesting.NewUnstructuredWithContent("v1", "NewObject", "ns1", "n1", map[string]interface{}{"spec": map[string]interface{}{"key1": "val1"}}), nil // clusterroleaggregator drops returned objects so no point in constructing them }) - syncContext := spoketesting.NewFakeSyncContext(t, workKey) + syncContext := testingcommon.NewFakeSyncContext(t, workKey) err := controller.toController().sync(context.TODO(), syncContext) if err != nil { t.Errorf("Should be success with no err: %v", err) @@ -487,7 +468,7 @@ func TestServerSideApplyConflict(t *testing.T) { controller.dynamicClient.PrependReactor("patch", "newobjects", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) { return true, nil, errors.NewConflict(schema.GroupResource{Resource: "newobjects"}, "n1", fmt.Errorf("conflict error")) }) - syncContext := spoketesting.NewFakeSyncContext(t, workKey) + syncContext := testingcommon.NewFakeSyncContext(t, workKey) err := controller.toController().sync(context.TODO(), syncContext) if err != nil { t.Errorf("Should be success with no err: %v", err) diff --git a/pkg/work/spoke/spoketesting/manifestwork_helpers.go b/pkg/work/spoke/spoketesting/manifestwork_helpers.go index 63e3fe547..ad3323e3c 100644 --- a/pkg/work/spoke/spoketesting/manifestwork_helpers.go +++ b/pkg/work/spoke/spoketesting/manifestwork_helpers.go @@ -2,39 +2,15 @@ package spoketesting import ( "fmt" - "testing" - - "github.com/openshift/library-go/pkg/operator/events" - "github.com/openshift/library-go/pkg/operator/events/eventstesting" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/restmapper" - clienttesting "k8s.io/client-go/testing" - "k8s.io/client-go/util/workqueue" workapiv1 "open-cluster-management.io/api/work/v1" ) -type FakeSyncContext struct { - workKey string - queue workqueue.RateLimitingInterface - recorder events.Recorder -} - -func NewFakeSyncContext(t *testing.T, workKey string) *FakeSyncContext { - return &FakeSyncContext{ - workKey: workKey, - queue: workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter()), - recorder: eventstesting.NewTestingEventRecorder(t), - } -} - -func (f FakeSyncContext) Queue() workqueue.RateLimitingInterface { return f.queue } -func (f FakeSyncContext) QueueKey() string { return f.workKey } -func (f FakeSyncContext) Recorder() events.Recorder { return f.recorder } - func NewSecret(name, namespace string, content string) *corev1.Secret { return &corev1.Secret{ TypeMeta: metav1.TypeMeta{ @@ -57,25 +33,6 @@ func NewSecretWithType(name, namespace string, content string, t corev1.SecretTy return secret } -func NewUnstructuredSecretBySize(namespace, name string, size int32) *unstructured.Unstructured { - data := "" - for i := int32(0); i < size; i++ { - data += "a" - } - - return &unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "v1", - "kind": "Secret", - "metadata": map[string]interface{}{ - "namespace": namespace, - "name": name, - }, - "data": data, - }, - } -} - func NewUnstructuredSecret(namespace, name string, terminated bool, uid string, owners ...metav1.OwnerReference) *unstructured.Unstructured { u := NewUnstructured("v1", "Secret", namespace, name, owners...) if terminated { @@ -188,9 +145,3 @@ func NewFakeRestMapper() meta.RESTMapper { } return restmapper.NewDiscoveryRESTMapper(resources) } - -func AssertAction(t *testing.T, actual clienttesting.Action, expected string) { - if actual.GetVerb() != expected { - t.Errorf("expected %s action but got: %#v", expected, actual) - } -}