-
Notifications
You must be signed in to change notification settings - Fork 38
test: add integration test for clusterschedulingpolicysnapshot controller #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
123 changes: 123 additions & 0 deletions
123
pkg/controllers/clustershedulingpolicysnapshot/controller_integration_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
|
|
||
| package clustershedulingpolicysnapshot | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
|
|
||
| fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1" | ||
| ) | ||
|
|
||
| const ( | ||
| testCRPName = "my-crp" | ||
| testSnapshotName = "my-snapshot" | ||
| ) | ||
|
|
||
| func policySnapshot() *fleetv1beta1.ClusterSchedulingPolicySnapshot { | ||
| return &fleetv1beta1.ClusterSchedulingPolicySnapshot{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: testSnapshotName, | ||
| Labels: map[string]string{ | ||
| fleetv1beta1.PolicyIndexLabel: "1", | ||
| fleetv1beta1.IsLatestSnapshotLabel: "true", | ||
| fleetv1beta1.CRPTrackingLabel: testCRPName, | ||
| }, | ||
| }, | ||
| Spec: fleetv1beta1.SchedulingPolicySnapshotSpec{ | ||
| PolicyHash: []byte("hash"), | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| var _ = Describe("Test clusterSchedulingPolicySnapshot Controller", func() { | ||
| const ( | ||
| timeout = time.Second * 10 | ||
| duration = time.Second * 10 | ||
| interval = time.Millisecond * 250 | ||
| ) | ||
|
|
||
| var ( | ||
| createdSnapshot = &fleetv1beta1.ClusterSchedulingPolicySnapshot{} | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| fakePlacementController.ResetQueue() | ||
| By("By creating a new clusterSchedulingPolicySnapshot") | ||
| snapshot := policySnapshot() | ||
| Expect(k8sClient.Create(ctx, snapshot)).Should(Succeed()) | ||
| }) | ||
|
|
||
| Context("When creating new clusterSchedulingPolicySnapshot", func() { | ||
| AfterEach(func() { | ||
| By("By deleting snapshot") | ||
| createdSnapshot := policySnapshot() | ||
| Expect(k8sClient.Delete(ctx, createdSnapshot)).Should(Succeed()) | ||
|
|
||
| By("By checking snapshot") | ||
| Eventually(func() bool { | ||
| return errors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: testSnapshotName}, createdSnapshot)) | ||
| }, duration, interval).Should(BeTrue(), "snapshot should be deleted") | ||
| }) | ||
|
|
||
| It("Should ignore the event", func() { | ||
| By("By checking placement controller queue") | ||
| Consistently(func() bool { | ||
| return fakePlacementController.Key() == "" | ||
| }, duration, interval).Should(BeTrue(), "controller should ignore the create event and not enqueue the request into the placementController queue") | ||
|
|
||
| }) | ||
| }) | ||
|
|
||
| Context("When updating clusterSchedulingPolicySnapshot", func() { | ||
| AfterEach(func() { | ||
| By("By deleting snapshot") | ||
| createdSnapshot := policySnapshot() | ||
| Expect(k8sClient.Delete(ctx, createdSnapshot)).Should(Succeed()) | ||
|
|
||
| By("By checking snapshot") | ||
| Eventually(func() bool { | ||
| return errors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: testSnapshotName}, createdSnapshot)) | ||
| }, duration, interval).Should(BeTrue(), "snapshot should be deleted") | ||
| }) | ||
|
|
||
| It("Should enqueue the event", func() { | ||
| Expect(k8sClient.Get(ctx, types.NamespacedName{Name: testSnapshotName}, createdSnapshot)).Should(Succeed()) | ||
|
|
||
| By("By updating the clusterSchedulingPolicySnapshot") | ||
| createdSnapshot.Spec.PolicyHash = []byte("modified-hash") | ||
| Expect(k8sClient.Update(ctx, createdSnapshot)).Should(Succeed()) | ||
|
|
||
| By("By checking placement controller queue") | ||
| Eventually(func() bool { | ||
| return fakePlacementController.Key() == testCRPName | ||
| }, timeout, interval).Should(BeTrue(), "placementController should receive the CRP name") | ||
| }) | ||
| }) | ||
|
|
||
| Context("When deleting clusterSchedulingPolicySnapshot", func() { | ||
| It("Should ignore the event", func() { | ||
| By("By deleting snapshot") | ||
| createdSnapshot := policySnapshot() | ||
| Expect(k8sClient.Delete(ctx, createdSnapshot)).Should(Succeed()) | ||
|
|
||
| By("By checking snapshot") | ||
| Eventually(func() bool { | ||
| return errors.IsNotFound(k8sClient.Get(ctx, types.NamespacedName{Name: testSnapshotName}, createdSnapshot)) | ||
| }, duration, interval).Should(BeTrue(), "snapshot should be deleted") | ||
|
|
||
| By("By checking placement controller queue") | ||
| Consistently(func() bool { | ||
| return fakePlacementController.Key() == "" | ||
| }, duration, interval).Should(BeTrue(), "controller should ignore the delete event and not enqueue the request into the placementController queue") | ||
| }) | ||
| }) | ||
| }) | ||
104 changes: 104 additions & 0 deletions
104
pkg/controllers/clustershedulingpolicysnapshot/suite_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
|
|
||
| package clustershedulingpolicysnapshot | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/klog/v2" | ||
| "k8s.io/klog/v2/klogr" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/envtest" | ||
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
|
||
| fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1" | ||
| "go.goms.io/fleet/test/utils/controller" | ||
| ) | ||
|
|
||
| var ( | ||
| cfg *rest.Config | ||
| mgr manager.Manager | ||
| k8sClient client.Client | ||
| testEnv *envtest.Environment | ||
| ctx context.Context | ||
| cancel context.CancelFunc | ||
| fakePlacementController *controller.FakeController | ||
| ) | ||
|
|
||
| func TestAPIs(t *testing.T) { | ||
| RegisterFailHandler(Fail) | ||
|
|
||
| RunSpecs(t, "ClusterSchedulingPolicySnapshot Controller Suite") | ||
| } | ||
|
|
||
| var _ = BeforeSuite(func() { | ||
| klog.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) | ||
|
|
||
| ctx, cancel = context.WithCancel(context.TODO()) | ||
|
|
||
| By("bootstrapping test environment") | ||
| testEnv = &envtest.Environment{ | ||
| CRDDirectoryPaths: []string{filepath.Join("../../../", "config", "crd", "bases")}, | ||
| ErrorIfCRDPathMissing: true, | ||
| } | ||
|
|
||
| var err error | ||
| cfg, err = testEnv.Start() | ||
| Expect(err).Should(Succeed()) | ||
| Expect(cfg).NotTo(BeNil()) | ||
|
|
||
| err = fleetv1beta1.AddToScheme(scheme.Scheme) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
|
|
||
| //+kubebuilder:scaffold:scheme | ||
| By("construct the k8s client") | ||
| k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) | ||
| Expect(err).Should(Succeed()) | ||
| Expect(k8sClient).NotTo(BeNil()) | ||
|
|
||
| By("starting the controller manager") | ||
| klog.InitFlags(flag.CommandLine) | ||
| flag.Parse() | ||
|
|
||
| mgr, err = ctrl.NewManager(cfg, ctrl.Options{ | ||
| Scheme: scheme.Scheme, | ||
| MetricsBindAddress: "0", | ||
| Logger: klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)), | ||
| }) | ||
| Expect(err).Should(Succeed()) | ||
|
|
||
| fakePlacementController = &controller.FakeController{} | ||
|
|
||
| err = (&Reconciler{ | ||
| Client: mgr.GetClient(), | ||
| PlacementController: fakePlacementController, | ||
| }).SetupWithManager(mgr) | ||
| Expect(err).Should(Succeed()) | ||
|
|
||
| go func() { | ||
| defer GinkgoRecover() | ||
| err = mgr.Start(ctx) | ||
| Expect(err).Should(Succeed(), "failed to run manager") | ||
| }() | ||
| }) | ||
|
|
||
| var _ = AfterSuite(func() { | ||
| defer klog.Flush() | ||
|
|
||
| cancel() | ||
| By("tearing down the test environment") | ||
| err := testEnv.Stop() | ||
| Expect(err).Should(Succeed()) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
zhiying-lin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Copyright (c) Microsoft Corporation. | ||
| Licensed under the MIT license. | ||
| */ | ||
|
|
||
| // Package controller provides a fake controller for testing. | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| ) | ||
|
|
||
| // FakeController is a fake controller which only stores one key. | ||
| type FakeController struct { | ||
| key string | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| // ResetQueue resets the value in the queue. | ||
| func (f *FakeController) ResetQueue() { | ||
| f.mu.Lock() | ||
| defer f.mu.Unlock() | ||
| f.key = "" | ||
| } | ||
|
|
||
| // Enqueue enqueues a string type key. | ||
| func (f *FakeController) Enqueue(obj interface{}) { | ||
| key, ok := obj.(string) | ||
| if !ok { | ||
| return | ||
| } | ||
| f.mu.Lock() | ||
| f.key = key | ||
| f.mu.Unlock() | ||
| } | ||
|
|
||
| // Run does nothing. | ||
| func (f *FakeController) Run(_ context.Context, _ int) error { | ||
| return nil | ||
| } | ||
|
|
||
| // Key returns the key stored in the queue. | ||
| func (f *FakeController) Key() string { | ||
| f.mu.RLock() | ||
| defer f.mu.RUnlock() | ||
| return f.key | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.