Skip to content

Commit

Permalink
test adding finalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mendoza committed Mar 21, 2024
1 parent d359d21 commit 2253658
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
24 changes: 12 additions & 12 deletions cloud/scope/object_storage_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 12 additions & 15 deletions cloud/scope/object_storage_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
28 changes: 27 additions & 1 deletion controller/linodeobjectstoragebucket_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -420,14 +421,39 @@ 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().
GetObjectStorageBucket(gomock.Any(), gomock.Any(), gomock.Any()).
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,
Expand Down

0 comments on commit 2253658

Please sign in to comment.