From 22536589b29e7e5ce4462bef21a0ef130bc21e32 Mon Sep 17 00:00:00 2001 From: Brian Mendoza Date: Thu, 21 Mar 2024 08:07:18 -0400 Subject: [PATCH] test adding finalizer --- cloud/scope/object_storage_bucket.go | 24 ++++++++-------- cloud/scope/object_storage_bucket_test.go | 27 ++++++++---------- ...nodeobjectstoragebucket_controller_test.go | 28 ++++++++++++++++++- 3 files changed, 51 insertions(+), 28 deletions(-) diff --git a/cloud/scope/object_storage_bucket.go b/cloud/scope/object_storage_bucket.go index d2227125a..c11c0677a 100644 --- a/cloud/scope/object_storage_bucket.go +++ b/cloud/scope/object_storage_bucket.go @@ -25,11 +25,11 @@ type ObjectStorageBucketScopeParams struct { } type ObjectStorageBucketScope struct { - Client k8sClient - Bucket *infrav1alpha1.LinodeObjectStorageBucket - Logger logr.Logger - LinodeClient LinodeObjectStorageClient - BucketPatchHelper *patch.Helper + Client k8sClient + Bucket *infrav1alpha1.LinodeObjectStorageBucket + Logger logr.Logger + LinodeClient LinodeObjectStorageClient + PatchHelper *patch.Helper } const AccessKeyNameTemplate = "%s-access-keys" @@ -67,23 +67,23 @@ func NewObjectStorageBucketScope(ctx context.Context, apiKey string, params Obje return nil, fmt.Errorf("failed to create linode client: %w", err) } - bucketPatchHelper, err := patch.NewHelper(params.Bucket, params.Client) + patchHelper, err := patch.NewHelper(params.Bucket, params.Client) if err != nil { return nil, fmt.Errorf("failed to init patch helper: %w", err) } return &ObjectStorageBucketScope{ - Client: params.Client, - Bucket: params.Bucket, - Logger: *params.Logger, - LinodeClient: linodeClient, - BucketPatchHelper: bucketPatchHelper, + Client: params.Client, + Bucket: params.Bucket, + Logger: *params.Logger, + LinodeClient: linodeClient, + PatchHelper: patchHelper, }, nil } // PatchObject persists the object storage bucket configuration and status. func (s *ObjectStorageBucketScope) PatchObject(ctx context.Context) error { - return s.BucketPatchHelper.Patch(ctx, s.Bucket) + return s.PatchHelper.Patch(ctx, s.Bucket) } // Close closes the current scope persisting the object storage bucket configuration and status. diff --git a/cloud/scope/object_storage_bucket_test.go b/cloud/scope/object_storage_bucket_test.go index 09fe37394..771b40441 100644 --- a/cloud/scope/object_storage_bucket_test.go +++ b/cloud/scope/object_storage_bucket_test.go @@ -459,11 +459,8 @@ func TestGenerateKeySecret(t *testing.T) { } objScope := &ObjectStorageBucketScope{ - Client: mockClient, - Bucket: testcase.Bucket, - Logger: logr.Logger{}, - LinodeClient: nil, - BucketPatchHelper: nil, + Client: mockClient, + Bucket: testcase.Bucket, } secret, err := objScope.GenerateKeySecret(context.Background(), testcase.keys) @@ -504,11 +501,11 @@ func TestShouldInitKeys(t *testing.T) { t.Parallel() objScope := &ObjectStorageBucketScope{ - Client: nil, - Bucket: testcase.Bucket, - Logger: logr.Logger{}, - LinodeClient: &linodego.Client{}, - BucketPatchHelper: &patch.Helper{}, + Client: nil, + Bucket: testcase.Bucket, + Logger: logr.Logger{}, + LinodeClient: &linodego.Client{}, + PatchHelper: &patch.Helper{}, } shouldInit := objScope.ShouldInitKeys() @@ -546,11 +543,11 @@ func TestShouldRotateKeys(t *testing.T) { t.Parallel() objScope := &ObjectStorageBucketScope{ - Client: nil, - Bucket: testcase.Bucket, - Logger: logr.Logger{}, - LinodeClient: &linodego.Client{}, - BucketPatchHelper: &patch.Helper{}, + Client: nil, + Bucket: testcase.Bucket, + Logger: logr.Logger{}, + LinodeClient: &linodego.Client{}, + PatchHelper: &patch.Helper{}, } rotate := objScope.ShouldRotateKeys() diff --git a/controller/linodeobjectstoragebucket_controller_test.go b/controller/linodeobjectstoragebucket_controller_test.go index 46d64ce77..40a4a6552 100644 --- a/controller/linodeobjectstoragebucket_controller_test.go +++ b/controller/linodeobjectstoragebucket_controller_test.go @@ -33,6 +33,7 @@ import ( "k8s.io/client-go/tools/record" "k8s.io/utils/ptr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" + "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log/zap" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -420,6 +421,32 @@ var _ = Describe("apply", Label("apply"), func() { } }) + It("fails when a finalizer cannot be added", Label("current"), func(ctx SpecContext) { + mockK8sClient := mock.NewMockk8sClient(mockCtrl) + prev := mockK8sClient.EXPECT(). + Scheme(). + Return(scheme.Scheme). + Times(1) + mockK8sClient.EXPECT(). + Scheme(). + After(prev). + Return(runtime.NewScheme()). + Times(2) + + patchHelper, err := patch.NewHelper(&obj, mockK8sClient) + Expect(err).NotTo(HaveOccurred()) + + // Create a scope directly since only a subset of fields are needed. + bScope := scope.ObjectStorageBucketScope{ + Client: mockK8sClient, + Bucket: &obj, + PatchHelper: patchHelper, + } + + _, err = reconciler.reconcile(ctx, &bScope) + Expect(err.Error()).To(ContainSubstring("no kind is registered")) + }) + It("fails when it can't ensure a bucket exists", func(ctx SpecContext) { mockLinodeClient := mock.NewMockLinodeObjectStorageClient(mockCtrl) mockLinodeClient.EXPECT(). @@ -427,7 +454,6 @@ var _ = Describe("apply", Label("apply"), func() { Return(nil, errors.New("non-404 error")). Times(1) - // Create a scope directly since only a subset of fields are needed. bScope := scope.ObjectStorageBucketScope{ LinodeClient: mockLinodeClient, Bucket: &obj,