@@ -20,6 +20,7 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"os"
23
+ "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/cos"
23
24
"testing"
24
25
25
26
"github.com/IBM-Cloud/power-go-client/power/models"
@@ -5722,11 +5723,13 @@ func TestDeleteTransitGatewayConnections(t *testing.T) {
5722
5723
func TestReconcileCOSInstance (t * testing.T ) {
5723
5724
var (
5724
5725
mockResourceController * mockRC.MockResourceController
5726
+ mockCOSController * mockcos.MockCos
5725
5727
mockCtrl * gomock.Controller
5726
5728
)
5727
5729
setup := func (t * testing.T ) {
5728
5730
t .Helper ()
5729
5731
mockCtrl = gomock .NewController (t )
5732
+ mockCOSController = mockcos .NewMockCos (mockCtrl )
5730
5733
mockResourceController = mockRC .NewMockResourceController (mockCtrl )
5731
5734
}
5732
5735
teardown := func () {
@@ -5986,14 +5989,110 @@ func TestReconcileCOSInstance(t *testing.T) {
5986
5989
Name : ptr .To ("test-resource-instance-name" ),
5987
5990
}, nil , nil )
5988
5991
5992
+ mockCOSController .EXPECT ().GetBucketByName (gomock .Any ()).Return (nil , fmt .Errorf ("failed to get bucket by name" ))
5993
+
5994
+ cos .NewServiceFunc = func (_ cos.ServiceOptions , _ , _ string ) (cos.Cos , error ) {
5995
+ return mockCOSController , nil
5996
+ }
5997
+
5989
5998
err = clusterScope .ReconcileCOSInstance (ctx )
5990
5999
g .Expect (err ).ToNot (BeNil ())
5991
6000
g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ID ).To (Equal (ptr .To ("test-resource-instance-guid" )))
5992
6001
g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ControllerCreated ).To (Equal (ptr .To (true )))
5993
6002
})
6003
+ t .Run ("When create COS bucket fails" , func (t * testing.T ) {
6004
+ g := NewWithT (t )
6005
+ setup (t )
6006
+ t .Cleanup (teardown )
6007
+ err := os .Setenv ("IBMCLOUD_APIKEY" , "test-api-key" )
6008
+ g .Expect (err ).To (BeNil ())
6009
+ defer os .Unsetenv ("IBMCLOUD_APIKEY" )
6010
+
6011
+ clusterScope := PowerVSClusterScope {
6012
+ ResourceClient : mockResourceController ,
6013
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {
6014
+ Spec : infrav1beta2.IBMPowerVSClusterSpec {
6015
+ CosInstance : & infrav1beta2.CosInstance {
6016
+ BucketRegion : "test-bucket-region" ,
6017
+ },
6018
+ ResourceGroup : & infrav1beta2.IBMPowerVSResourceReference {
6019
+ ID : ptr .To ("test-resource-group-id" ),
6020
+ },
6021
+ },
6022
+ Status : infrav1beta2.IBMPowerVSClusterStatus {
6023
+ ServiceInstance : & infrav1beta2.ResourceReference {
6024
+ ID : ptr .To ("test-serviceinstance-id" ),
6025
+ },
6026
+ },
6027
+ },
6028
+ }
6029
+ mockResourceController .EXPECT ().GetInstanceByName (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
6030
+
6031
+ mockResourceController .EXPECT ().CreateResourceInstance (gomock .Any ()).Return (& resourcecontrollerv2.ResourceInstance {
6032
+ ID : ptr .To ("test-resource-instance-id" ),
6033
+ GUID : ptr .To ("test-resource-instance-guid" ),
6034
+ Name : ptr .To ("test-resource-instance-name" ),
6035
+ }, nil , nil )
6036
+
6037
+ mockCOSController .EXPECT ().GetBucketByName (gomock .Any ()).Return (nil , awserr .New (s3 .ErrCodeNoSuchBucket , "bucket does not exist" , nil ))
6038
+ mockCOSController .EXPECT ().CreateBucket (gomock .Any ()).Return (nil , fmt .Errorf ("failed to create bucket" ))
5994
6039
5995
- //TODO: Complete cases to cover control flow on checkCOSBucket and createCOSBucket
5996
- // Github issue: https://github.com/kubernetes-sigs/cluster-api-provider-ibmcloud/issues/2034
6040
+ cos .NewServiceFunc = func (_ cos.ServiceOptions , _ , _ string ) (cos.Cos , error ) {
6041
+ return mockCOSController , nil
6042
+ }
6043
+
6044
+ err = clusterScope .ReconcileCOSInstance (ctx )
6045
+ g .Expect (err ).ToNot (BeNil ())
6046
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ID ).To (Equal (ptr .To ("test-resource-instance-guid" )))
6047
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ControllerCreated ).To (Equal (ptr .To (true )))
6048
+ })
6049
+
6050
+ t .Run ("When create COS bucket succeeds" , func (t * testing.T ) {
6051
+ g := NewWithT (t )
6052
+ setup (t )
6053
+ t .Cleanup (teardown )
6054
+ err := os .Setenv ("IBMCLOUD_APIKEY" , "test-api-key" )
6055
+ g .Expect (err ).To (BeNil ())
6056
+ defer os .Unsetenv ("IBMCLOUD_APIKEY" )
6057
+
6058
+ clusterScope := PowerVSClusterScope {
6059
+ ResourceClient : mockResourceController ,
6060
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {
6061
+ Spec : infrav1beta2.IBMPowerVSClusterSpec {
6062
+ CosInstance : & infrav1beta2.CosInstance {
6063
+ BucketRegion : "test-bucket-region" ,
6064
+ },
6065
+ ResourceGroup : & infrav1beta2.IBMPowerVSResourceReference {
6066
+ ID : ptr .To ("test-resource-group-id" ),
6067
+ },
6068
+ },
6069
+ Status : infrav1beta2.IBMPowerVSClusterStatus {
6070
+ ServiceInstance : & infrav1beta2.ResourceReference {
6071
+ ID : ptr .To ("test-serviceinstance-id" ),
6072
+ },
6073
+ },
6074
+ },
6075
+ }
6076
+ mockResourceController .EXPECT ().GetInstanceByName (gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil , nil )
6077
+
6078
+ mockResourceController .EXPECT ().CreateResourceInstance (gomock .Any ()).Return (& resourcecontrollerv2.ResourceInstance {
6079
+ ID : ptr .To ("test-resource-instance-id" ),
6080
+ GUID : ptr .To ("test-resource-instance-guid" ),
6081
+ Name : ptr .To ("test-resource-instance-name" ),
6082
+ }, nil , nil )
6083
+
6084
+ mockCOSController .EXPECT ().GetBucketByName (gomock .Any ()).Return (nil , awserr .New (s3 .ErrCodeNoSuchBucket , "bucket does not exist" , nil ))
6085
+ mockCOSController .EXPECT ().CreateBucket (gomock .Any ()).Return (nil , nil )
6086
+
6087
+ cos .NewServiceFunc = func (_ cos.ServiceOptions , _ , _ string ) (cos.Cos , error ) {
6088
+ return mockCOSController , nil
6089
+ }
6090
+
6091
+ err = clusterScope .ReconcileCOSInstance (ctx )
6092
+ g .Expect (err ).To (BeNil ())
6093
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ID ).To (Equal (ptr .To ("test-resource-instance-guid" )))
6094
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .COSInstance .ControllerCreated ).To (Equal (ptr .To (true )))
6095
+ })
5997
6096
}
5998
6097
5999
6098
func TestCheckCOSServiceInstance (t * testing.T ) {
0 commit comments