From 76ca1ba5477a789726970bc2a3b551587cd4b278 Mon Sep 17 00:00:00 2001 From: carmal891 Date: Thu, 7 Nov 2024 20:57:02 +0530 Subject: [PATCH] ReconcileCOSInstnce UT PR improvements for readability --- cloud/scope/powervs_cluster_test.go | 108 +++++++++++++++++++--------- 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/cloud/scope/powervs_cluster_test.go b/cloud/scope/powervs_cluster_test.go index 63c159288..a51781a2f 100644 --- a/cloud/scope/powervs_cluster_test.go +++ b/cloud/scope/powervs_cluster_test.go @@ -3093,6 +3093,46 @@ func TestReconcileCOSInstance(t *testing.T) { g.Expect(clusterScope.IBMPowerVSCluster.Status.COSInstance).To(BeNil()) }) + t.Run("When COS service instance is found in IBM Cloud and cluster status is updated", func(t *testing.T) { + g := NewWithT(t) + setup(t) + t.Cleanup(teardown) + err := os.Setenv("IBMCLOUD_APIKEY", "test-api-key") + g.Expect(err).To(BeNil()) + defer os.Unsetenv("IBMCLOUD_APIKEY") + + clusterScope := PowerVSClusterScope{ + ResourceClient: mockResourceController, + IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{ + Spec: infrav1beta2.IBMPowerVSClusterSpec{ + CosInstance: &infrav1beta2.CosInstance{ + BucketRegion: "test-region", + }, + ResourceGroup: &infrav1beta2.IBMPowerVSResourceReference{ + ID: ptr.To("test-resource-group-id"), + }, + Zone: ptr.To("test-zone"), + }, + Status: infrav1beta2.IBMPowerVSClusterStatus{ + ServiceInstance: &infrav1beta2.ResourceReference{ + ID: ptr.To("test-serviceinstance-id"), + }, + }, + }, + } + + mockResourceController.EXPECT().GetInstanceByName(gomock.Any(), gomock.Any(), gomock.Any()).Return(&resourcecontrollerv2.ResourceInstance{ + Name: ptr.To("test-cos-resource-name"), + State: ptr.To(string(infrav1beta2.ServiceInstanceStateActive)), + GUID: ptr.To("test-cos-instance-guid"), + }, nil) + + err = clusterScope.ReconcileCOSInstance() + g.Expect(err).ToNot(BeNil()) + g.Expect(clusterScope.IBMPowerVSCluster.Status.COSInstance.ID).To(Equal(ptr.To("test-cos-instance-guid"))) + g.Expect(clusterScope.IBMPowerVSCluster.Status.COSInstance.ControllerCreated).To(Equal(ptr.To(bool(false)))) + }) + t.Run("When COS service instance is not found in IBM Cloud and hence creates COS instance in cloud", func(t *testing.T) { g := NewWithT(t) setup(t) @@ -3241,7 +3281,7 @@ func TestReconcileCOSInstance(t *testing.T) { g.Expect(clusterScope.IBMPowerVSCluster.Status.COSInstance.ControllerCreated).To(Equal(ptr.To(bool(true)))) }) - t.Run("When check on whether bucket exist in service instance fails", func(t *testing.T) { + t.Run("When checkCOSBucket fails to determine whether bucket exist in cloud due to an unexpected error", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3322,7 +3362,7 @@ func TestCheckCOSServiceInstance(t *testing.T) { g.Expect(err).ToNot(BeNil()) }) - t.Run("When cos service instance is not found in IBM Cloud", func(t *testing.T) { + t.Run("When COS service instance is not found in IBM Cloud", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3345,7 +3385,7 @@ func TestCheckCOSServiceInstance(t *testing.T) { g.Expect(err).To(BeNil()) }) - t.Run("When cos service instance exists but state is not active", func(t *testing.T) { + t.Run("When COS service instance exists but state is not active", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3371,7 +3411,7 @@ func TestCheckCOSServiceInstance(t *testing.T) { g.Expect(err).ToNot(BeNil()) g.Expect(err).To(MatchError(fmt.Errorf("COS service instance is not in active state, current state: %s", "failed"))) }) - t.Run("When cos service instance exists and state is active", func(t *testing.T) { + t.Run("When COS service instance exists and state is active", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3415,7 +3455,7 @@ func TestCreateCOSBucket(t *testing.T) { mockCtrl.Finish() } - t.Run("When cos bucket creation fails in cloud", func(t *testing.T) { + t.Run("When COS bucket creation fails in cloud", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3543,7 +3583,7 @@ func TestCheckCOSBucket(t *testing.T) { teardown := func() { mockCtrl.Finish() } - t.Run("When checking COS bucket existence", func(t *testing.T) { + t.Run("When checking if COS bucket exists in cloud", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3561,40 +3601,40 @@ func TestCheckCOSBucket(t *testing.T) { } testScenarios := []struct { - name string - mockError error - expectedExists bool - expectErr bool + name string + mockError error + bucketExists bool + expectErr bool }{ { - name: "NoSuchBucket error", - mockError: awserr.New(s3.ErrCodeNoSuchBucket, "NoSuchBucket", nil), - expectedExists: false, - expectErr: false, + name: "NoSuchBucket error", + mockError: awserr.New(s3.ErrCodeNoSuchBucket, "NoSuchBucket", nil), + bucketExists: false, + expectErr: false, }, { - name: "Forbidden error", - mockError: awserr.New("Forbidden", "Forbidden", nil), - expectedExists: false, - expectErr: false, + name: "Forbidden error", + mockError: awserr.New("Forbidden", "Forbidden", nil), + bucketExists: false, + expectErr: false, }, { - name: "NotFound error", - mockError: awserr.New("NotFound", "NotFound", nil), - expectedExists: false, - expectErr: false, + name: "NotFound error", + mockError: awserr.New("NotFound", "NotFound", nil), + bucketExists: false, + expectErr: false, }, { - name: "Other aws error", - mockError: awserr.New("OtherAWSError", "OtherAWSError", nil), - expectedExists: false, - expectErr: true, + name: "Other aws error", + mockError: awserr.New("OtherAWSError", "OtherAWSError", nil), + bucketExists: false, + expectErr: true, }, { - name: "Bucket exists", - mockError: nil, - expectedExists: true, - expectErr: false, + name: "Bucket exists", + mockError: nil, + bucketExists: true, + expectErr: false, }, } @@ -3602,7 +3642,7 @@ func TestCheckCOSBucket(t *testing.T) { t.Run(scenario.name, func(_ *testing.T) { mockCOSController.EXPECT().GetBucketByName(gomock.Any()).Return(nil, scenario.mockError) exists, err := clusterScope.checkCOSBucket() - g.Expect(exists).To(Equal(scenario.expectedExists)) + g.Expect(exists).To(Equal(scenario.bucketExists)) if scenario.expectErr { g.Expect(err).ToNot(BeNil()) } else { @@ -3627,7 +3667,7 @@ func TestCreateCOSServiceInstance(t *testing.T) { mockCtrl.Finish() } - t.Run("When creating cos resource instance fails due to no resource group id", func(t *testing.T) { + t.Run("When creating COS resource instance fails due to missing resource group id", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3648,7 +3688,7 @@ func TestCreateCOSServiceInstance(t *testing.T) { g.Expect(err).ToNot(BeNil()) }) - t.Run("When creating cos resource instance fails", func(t *testing.T) { + t.Run("When creating COS resource instance fails", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown) @@ -3676,7 +3716,7 @@ func TestCreateCOSServiceInstance(t *testing.T) { g.Expect(err).ToNot(BeNil()) }) - t.Run("When create resource instance is successful", func(t *testing.T) { + t.Run("When COS resource instance creation is successful", func(t *testing.T) { g := NewWithT(t) setup(t) t.Cleanup(teardown)