Skip to content

Commit

Permalink
ReconcileCOSInstnce UT PR improvements for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
carmal891 committed Nov 7, 2024
1 parent 438d3b2 commit 76ca1ba
Showing 1 changed file with 74 additions and 34 deletions.
108 changes: 74 additions & 34 deletions cloud/scope/powervs_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -3561,48 +3601,48 @@ 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,
},
}

for _, scenario := range testScenarios {
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 {
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 76ca1ba

Please sign in to comment.