Skip to content

Commit cc46d84

Browse files
Improve UT coverage in cloud/scope package for PowerVS (#741)
Signed-off-by: Prajyot-Parab <[email protected]>
1 parent 77cf0a3 commit cc46d84

9 files changed

+725
-65
lines changed

cloud/scope/cluster_test.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
. "github.com/onsi/gomega"
3737
)
3838

39-
func setupClusterScope(clusterName string) *ClusterScope {
39+
func setupClusterScope(clusterName string, mockvpc *mock.MockVpc) *ClusterScope {
4040
cluster := newCluster(clusterName)
4141
vpcCluster := newVPCCluster(clusterName)
4242

@@ -93,8 +93,8 @@ func TestNewClusterScope(t *testing.T) {
9393
}
9494

9595
func TestCreateVPC(t *testing.T) {
96-
mockctrl = gomock.NewController(GinkgoT())
97-
mockvpc = mock.NewMockVpc(mockctrl)
96+
mockctrl := gomock.NewController(GinkgoT())
97+
mockvpc := mock.NewMockVpc(mockctrl)
9898
g := NewWithT(t)
9999

100100
vpcCluster := infrav1beta1.IBMVPCCluster{
@@ -111,7 +111,7 @@ func TestCreateVPC(t *testing.T) {
111111
}}
112112

113113
t.Run("Create VPC", func(t *testing.T) {
114-
scope := setupClusterScope(clusterName)
114+
scope := setupClusterScope(clusterName, mockvpc)
115115
listVpcsOptions := &vpcv1.ListVpcsOptions{}
116116
createVPCOptions := &vpcv1.CreateVPCOptions{}
117117
vpcCollection := &vpcv1.VPCCollection{
@@ -194,8 +194,8 @@ func TestCreateVPC(t *testing.T) {
194194
}
195195

196196
func TestDeleteVPC(t *testing.T) {
197-
mockctrl = gomock.NewController(GinkgoT())
198-
mockvpc = mock.NewMockVpc(mockctrl)
197+
mockctrl := gomock.NewController(GinkgoT())
198+
mockvpc := mock.NewMockVpc(mockctrl)
199199
g := NewWithT(t)
200200

201201
vpcCluster := infrav1beta1.IBMVPCCluster{
@@ -209,7 +209,7 @@ func TestDeleteVPC(t *testing.T) {
209209
}}
210210

211211
t.Run("Delete VPC", func(t *testing.T) {
212-
scope := setupClusterScope(clusterName)
212+
scope := setupClusterScope(clusterName, mockvpc)
213213
scope.IBMVPCCluster.Spec = vpcCluster.Spec
214214
scope.IBMVPCCluster.Status = vpcCluster.Status
215215
deleteVpcOptions := &vpcv1.DeleteVPCOptions{}
@@ -230,8 +230,8 @@ func TestDeleteVPC(t *testing.T) {
230230
}
231231

232232
func TestReserveFIP(t *testing.T) {
233-
mockctrl = gomock.NewController(GinkgoT())
234-
mockvpc = mock.NewMockVpc(mockctrl)
233+
mockctrl := gomock.NewController(GinkgoT())
234+
mockvpc := mock.NewMockVpc(mockctrl)
235235
g := NewWithT(t)
236236

237237
vpcCluster := infrav1beta1.IBMVPCCluster{
@@ -254,7 +254,7 @@ func TestReserveFIP(t *testing.T) {
254254
floatingIPOptions := &vpcv1.CreateFloatingIPOptions{}
255255

256256
t.Run("Should reserve FloatingIP", func(t *testing.T) {
257-
scope := setupClusterScope(clusterName)
257+
scope := setupClusterScope(clusterName, mockvpc)
258258
expectedOutput := &vpcv1.FloatingIP{
259259
Name: core.StringPtr("foo-cluster-control-plane"),
260260
}
@@ -269,7 +269,7 @@ func TestReserveFIP(t *testing.T) {
269269
})
270270

271271
t.Run("Return exsisting FloatingIP", func(t *testing.T) {
272-
scope := setupClusterScope("foo-cluster-1")
272+
scope := setupClusterScope("foo-cluster-1", mockvpc)
273273
expectedOutput := &vpcv1.FloatingIP{
274274
Name: core.StringPtr("foo-cluster-1-control-plane"),
275275
}
@@ -281,14 +281,14 @@ func TestReserveFIP(t *testing.T) {
281281
})
282282

283283
t.Run("Error when listing FloatingIPs", func(t *testing.T) {
284-
scope := setupClusterScope(clusterName)
284+
scope := setupClusterScope(clusterName, mockvpc)
285285
mockvpc.EXPECT().ListFloatingIps(gomock.AssignableToTypeOf(listFloatingIpsOptions)).Return(floatingIPCollection, detailedResponse, errors.New("Error when listing FloatingIPs"))
286286
_, err := scope.ReserveFIP()
287287
g.Expect(err).To(Not(BeNil()))
288288
})
289289

290290
t.Run("Error when creating FloatingIP", func(t *testing.T) {
291-
scope := setupClusterScope(clusterName)
291+
scope := setupClusterScope(clusterName, mockvpc)
292292
scope.IBMVPCCluster.Spec = vpcCluster.Spec
293293
mockvpc.EXPECT().ListFloatingIps(gomock.AssignableToTypeOf(listFloatingIpsOptions)).Return(floatingIPCollection, detailedResponse, nil)
294294
mockvpc.EXPECT().CreateFloatingIP(gomock.AssignableToTypeOf(floatingIPOptions)).Return(nil, detailedResponse, errors.New("Error when creating FloatingIP"))
@@ -299,8 +299,8 @@ func TestReserveFIP(t *testing.T) {
299299
}
300300

301301
func TestDeleteFloatingIP(t *testing.T) {
302-
mockctrl = gomock.NewController(GinkgoT())
303-
mockvpc = mock.NewMockVpc(mockctrl)
302+
mockctrl := gomock.NewController(GinkgoT())
303+
mockvpc := mock.NewMockVpc(mockctrl)
304304
g := NewWithT(t)
305305

306306
vpcCluster := infrav1beta1.IBMVPCCluster{
@@ -311,7 +311,7 @@ func TestDeleteFloatingIP(t *testing.T) {
311311
}}
312312

313313
t.Run("Delete FloatingIP", func(t *testing.T) {
314-
scope := setupClusterScope(clusterName)
314+
scope := setupClusterScope(clusterName, mockvpc)
315315
deleteFIPOption := &vpcv1.DeleteFloatingIPOptions{}
316316
detailedResponse := &core.DetailedResponse{}
317317

@@ -344,8 +344,8 @@ func TestDeleteFloatingIP(t *testing.T) {
344344
}
345345

346346
func TestCreateSubnet(t *testing.T) {
347-
mockctrl = gomock.NewController(GinkgoT())
348-
mockvpc = mock.NewMockVpc(mockctrl)
347+
mockctrl := gomock.NewController(GinkgoT())
348+
mockvpc := mock.NewMockVpc(mockctrl)
349349
g := NewWithT(t)
350350
vpcCluster := infrav1beta1.IBMVPCCluster{
351351
Spec: infrav1beta1.IBMVPCClusterSpec{
@@ -389,7 +389,7 @@ func TestCreateSubnet(t *testing.T) {
389389
subnetPublicGatewayOptions := &vpcv1.SetSubnetPublicGatewayOptions{}
390390

391391
t.Run("Should create Subnet", func(t *testing.T) {
392-
scope := setupClusterScope(clusterName)
392+
scope := setupClusterScope(clusterName, mockvpc)
393393
expectedOutput := &vpcv1.Subnet{
394394
Name: core.StringPtr("foo-cluster-subnet"),
395395
ID: core.StringPtr("foo-cluster-subnet-id"),
@@ -412,7 +412,7 @@ func TestCreateSubnet(t *testing.T) {
412412
})
413413

414414
t.Run("Return exsisting Subnet", func(t *testing.T) {
415-
scope := setupClusterScope("foo-cluster-1")
415+
scope := setupClusterScope("foo-cluster-1", mockvpc)
416416
expectedOutput := &vpcv1.Subnet{
417417
Name: core.StringPtr("foo-cluster-1-subnet"),
418418
ID: core.StringPtr("foo-cluster-1-subnet-id"),
@@ -425,14 +425,14 @@ func TestCreateSubnet(t *testing.T) {
425425
})
426426

427427
t.Run("Error when listing Subnets", func(t *testing.T) {
428-
scope := setupClusterScope(clusterName)
428+
scope := setupClusterScope(clusterName, mockvpc)
429429
mockvpc.EXPECT().ListSubnets(gomock.AssignableToTypeOf(listSubnetsOptions)).Return(subnetCollection, detailedResponse, errors.New("Error when listing subnets"))
430430
_, err := scope.CreateSubnet()
431431
g.Expect(err).To(Not(BeNil()))
432432
})
433433

434434
t.Run("Error when listing VPC AddressPerfixes", func(t *testing.T) {
435-
scope := setupClusterScope(clusterName)
435+
scope := setupClusterScope(clusterName, mockvpc)
436436
scope.IBMVPCCluster.Spec = vpcCluster.Spec
437437
scope.IBMVPCCluster.Status = vpcCluster.Status
438438
mockvpc.EXPECT().ListSubnets(gomock.AssignableToTypeOf(listSubnetsOptions)).Return(subnetCollection, detailedResponse, nil)
@@ -442,7 +442,7 @@ func TestCreateSubnet(t *testing.T) {
442442
})
443443

444444
t.Run("Error not found a valid CIDR for VPC in zone", func(t *testing.T) {
445-
scope := setupClusterScope(clusterName)
445+
scope := setupClusterScope(clusterName, mockvpc)
446446
scope.IBMVPCCluster.Spec = vpcCluster.Spec
447447
scope.IBMVPCCluster.Status = vpcCluster.Status
448448
scope.IBMVPCCluster.Spec.Zone = "foo-zone-temp"
@@ -453,7 +453,7 @@ func TestCreateSubnet(t *testing.T) {
453453
})
454454

455455
t.Run("Error when creating PublicGateWay", func(t *testing.T) {
456-
scope := setupClusterScope(clusterName)
456+
scope := setupClusterScope(clusterName, mockvpc)
457457
scope.IBMVPCCluster.Spec = vpcCluster.Spec
458458
scope.IBMVPCCluster.Status = vpcCluster.Status
459459
subnet := &vpcv1.Subnet{}
@@ -466,7 +466,7 @@ func TestCreateSubnet(t *testing.T) {
466466
})
467467

468468
t.Run("Error when attaching PublicGateWay", func(t *testing.T) {
469-
scope := setupClusterScope(clusterName)
469+
scope := setupClusterScope(clusterName, mockvpc)
470470
scope.IBMVPCCluster.Spec = vpcCluster.Spec
471471
scope.IBMVPCCluster.Status = vpcCluster.Status
472472
subnet := &vpcv1.Subnet{
@@ -483,7 +483,7 @@ func TestCreateSubnet(t *testing.T) {
483483
})
484484

485485
t.Run("Error when creating Subnet", func(t *testing.T) {
486-
scope := setupClusterScope(clusterName)
486+
scope := setupClusterScope(clusterName, mockvpc)
487487
scope.IBMVPCCluster.Spec = vpcCluster.Spec
488488
scope.IBMVPCCluster.Status = vpcCluster.Status
489489
mockvpc.EXPECT().ListSubnets(gomock.AssignableToTypeOf(listSubnetsOptions)).Return(subnetCollection, detailedResponse, nil)
@@ -496,8 +496,8 @@ func TestCreateSubnet(t *testing.T) {
496496
}
497497

498498
func TestDeleteSubnet(t *testing.T) {
499-
mockctrl = gomock.NewController(GinkgoT())
500-
mockvpc = mock.NewMockVpc(mockctrl)
499+
mockctrl := gomock.NewController(GinkgoT())
500+
mockvpc := mock.NewMockVpc(mockctrl)
501501
g := NewWithT(t)
502502

503503
vpcCluster := infrav1beta1.IBMVPCCluster{Spec: infrav1beta1.IBMVPCClusterSpec{
@@ -509,7 +509,7 @@ func TestDeleteSubnet(t *testing.T) {
509509
}}
510510

511511
t.Run("Delete Subnet", func(t *testing.T) {
512-
scope := setupClusterScope(clusterName)
512+
scope := setupClusterScope(clusterName, mockvpc)
513513
scope.IBMVPCCluster.Spec = vpcCluster.Spec
514514
scope.IBMVPCCluster.Status = vpcCluster.Status
515515
getPGWOptions := &vpcv1.GetSubnetPublicGatewayOptions{}

cloud/scope/common_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ import (
2121
corev1 "k8s.io/api/core/v1"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
2425

2526
infrav1beta1 "sigs.k8s.io/cluster-api-provider-ibmcloud/api/v1beta1"
27+
28+
. "github.com/onsi/gomega"
2629
)
2730

2831
const (
2932
clusterName = "foo-cluster"
3033
machineName = "foo-machine"
34+
pvsImage = "foo-image"
35+
pvsNetwork = "foo-network"
3136
)
3237

3338
func newCluster(name string) *capiv1beta1.Cluster {
@@ -49,6 +54,15 @@ func newVPCCluster(name string) *infrav1beta1.IBMVPCCluster {
4954
}
5055
}
5156

57+
func newPowerVSCluster(name string) *infrav1beta1.IBMPowerVSCluster {
58+
return &infrav1beta1.IBMPowerVSCluster{
59+
ObjectMeta: metav1.ObjectMeta{
60+
Name: name,
61+
Namespace: "default",
62+
},
63+
}
64+
}
65+
5266
func newMachine(machineName string) *capiv1beta1.Machine {
5367
return &capiv1beta1.Machine{
5468
ObjectMeta: metav1.ObjectMeta{
@@ -77,3 +91,16 @@ func newBootstrapSecret(clusterName, machineName string) *corev1.Secret {
7791
},
7892
}
7993
}
94+
95+
func createObject(g *WithT, obj client.Object, namespace string) {
96+
if obj.DeepCopyObject() != nil {
97+
obj.SetNamespace(namespace)
98+
g.Expect(testEnv.Create(ctx, obj)).To(Succeed())
99+
}
100+
}
101+
102+
func cleanupObject(g *WithT, obj client.Object) {
103+
if obj.DeepCopyObject() != nil {
104+
g.Expect(testEnv.Cleanup(ctx, obj)).To(Succeed())
105+
}
106+
}

0 commit comments

Comments
 (0)