@@ -33,13 +33,15 @@ import (
33
33
mockP "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs/mock"
34
34
capiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
35
35
36
+ "github.com/IBM/vpc-go-sdk/vpcv1"
36
37
"sigs.k8s.io/cluster-api-provider-ibmcloud/cmd/capibmadm/utils"
37
38
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/powervs"
38
39
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller"
39
40
mockRC "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcecontroller/mock"
40
41
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/resourcemanager"
41
42
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/transitgateway"
42
43
"sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc"
44
+ mockV "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/cloud/services/vpc/mock"
43
45
44
46
. "github.com/onsi/gomega"
45
47
)
@@ -1987,3 +1989,329 @@ func TestReconcileNetwork(t *testing.T) {
1987
1989
})
1988
1990
1989
1991
}
1992
+
1993
+ func TestReconcileVPC (t * testing.T ) {
1994
+ var (
1995
+ mockVPC * mockV.MockVpc
1996
+ mockCtrl * gomock.Controller
1997
+ )
1998
+
1999
+ setup := func (t * testing.T ) {
2000
+ t .Helper ()
2001
+ mockCtrl = gomock .NewController (t )
2002
+ mockVPC = mockV .NewMockVpc (mockCtrl )
2003
+ }
2004
+ teardown := func () {
2005
+ mockCtrl .Finish ()
2006
+ }
2007
+ t .Run ("When VPC already exists in cloud, " , func (t * testing.T ) {
2008
+ g := NewWithT (t )
2009
+ setup (t )
2010
+ t .Cleanup (teardown )
2011
+
2012
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("VPCID" )}
2013
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2014
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2015
+ mockVPC .EXPECT ().GetVPCByName (gomock .Any ()).Return (vpcOutput , nil )
2016
+ return mockVPC , nil
2017
+ }
2018
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2019
+
2020
+ output , err := clusterScope .ReconcileVPC ()
2021
+ g .Expect (err ).To (BeNil ())
2022
+ g .Expect (output ).To (BeFalse ())
2023
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .VPC .ID ).To (Equal (vpcOutput .ID ))
2024
+ })
2025
+ t .Run ("When GetVPCByName returns error" , func (t * testing.T ) {
2026
+ g := NewWithT (t )
2027
+ setup (t )
2028
+ t .Cleanup (teardown )
2029
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2030
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2031
+ mockVPC .EXPECT ().GetVPCByName (gomock .Any ()).Return (nil , fmt .Errorf ("GetVPCByName error" ))
2032
+ return mockVPC , nil
2033
+ }
2034
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2035
+ output , err := clusterScope .ReconcileVPC ()
2036
+ g .Expect (err ).ToNot (BeNil ())
2037
+ g .Expect (output ).To (BeFalse ())
2038
+ })
2039
+ t .Run ("Create new VPC when VPC doesnt exist" , func (t * testing.T ) {
2040
+ g := NewWithT (t )
2041
+ setup (t )
2042
+ t .Cleanup (teardown )
2043
+
2044
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("vpcID" ), DefaultSecurityGroup : & vpcv1.SecurityGroupReference {ID : ptr .To ("DefaultSecurityGroupID" )}}
2045
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2046
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2047
+ mockVPC .EXPECT ().GetVPCByName (gomock .Any ()).Return (nil , nil )
2048
+ mockVPC .EXPECT ().CreateVPC (gomock .Any ()).Return (vpcOutput , nil , nil )
2049
+ mockVPC .EXPECT ().CreateSecurityGroupRule (gomock .Any ()).Return (nil , nil , nil )
2050
+ return mockVPC , nil
2051
+ }
2052
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2053
+
2054
+ output , err := clusterScope .ReconcileVPC ()
2055
+ g .Expect (err ).To (BeNil ())
2056
+ g .Expect (output ).To (BeTrue ())
2057
+ g .Expect (clusterScope .IBMPowerVSCluster .Status .VPC .ID ).To (Equal (vpcOutput .ID ))
2058
+ })
2059
+ t .Run ("When CreateVPC returns error" , func (t * testing.T ) {
2060
+ g := NewWithT (t )
2061
+ setup (t )
2062
+ t .Cleanup (teardown )
2063
+
2064
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2065
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2066
+ mockVPC .EXPECT ().GetVPCByName (gomock .Any ()).Return (nil , nil )
2067
+ mockVPC .EXPECT ().CreateVPC (gomock .Any ()).Return (nil , nil , fmt .Errorf ("CreateVPC returns error" ))
2068
+ //mockVPC.EXPECT().CreateSecurityGroupRule(gomock.Any()).Return(nil, nil, nil)
2069
+ return mockVPC , nil
2070
+ }
2071
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2072
+
2073
+ output , err := clusterScope .ReconcileVPC ()
2074
+ g .Expect (err ).ToNot (BeNil ())
2075
+ g .Expect (output ).To (BeFalse ())
2076
+ })
2077
+
2078
+ t .Run ("When VPC ID is already set and is proper" , func (t * testing.T ) {
2079
+ g := NewWithT (t )
2080
+ setup (t )
2081
+ t .Cleanup (teardown )
2082
+
2083
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("VPCID" )}
2084
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2085
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2086
+ mockVPC .EXPECT ().GetVPC (gomock .Any ()).Return (vpcOutput , nil , nil )
2087
+ return mockVPC , nil
2088
+ }
2089
+ clusterScopeParams .IBMPowerVSCluster .Spec .VPC .ID = ptr .To ("VPCID" )
2090
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2091
+ output , err := clusterScope .ReconcileVPC ()
2092
+ g .Expect (err ).To (BeNil ())
2093
+ g .Expect (output ).To (BeFalse ())
2094
+ })
2095
+
2096
+ t .Run ("When VPC ID is already set and GetVPC returns error" , func (t * testing.T ) {
2097
+ g := NewWithT (t )
2098
+ setup (t )
2099
+ t .Cleanup (teardown )
2100
+
2101
+ //vpcOutput := &vpcv1.VPC{Name: ptr.To("VPCName"), ID: ptr.To("VPCID")}
2102
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2103
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2104
+ mockVPC .EXPECT ().GetVPC (gomock .Any ()).Return (nil , nil , fmt .Errorf ("GetVPC returns error" ))
2105
+ return mockVPC , nil
2106
+ }
2107
+ clusterScopeParams .IBMPowerVSCluster .Spec .VPC .ID = ptr .To ("VPCID" )
2108
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2109
+ output , err := clusterScope .ReconcileVPC ()
2110
+ g .Expect (err ).ToNot (BeNil ())
2111
+ g .Expect (output ).To (BeFalse ())
2112
+ })
2113
+
2114
+ t .Run ("When VPC ID is already set and GetVPC returns empty output" , func (t * testing.T ) {
2115
+ g := NewWithT (t )
2116
+ setup (t )
2117
+ t .Cleanup (teardown )
2118
+
2119
+ //vpcOutput := &vpcv1.VPC{Name: ptr.To("VPCName"), ID: ptr.To("VPCID")}
2120
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2121
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2122
+ mockVPC .EXPECT ().GetVPC (gomock .Any ()).Return (nil , nil , nil )
2123
+ return mockVPC , nil
2124
+ }
2125
+ clusterScopeParams .IBMPowerVSCluster .Spec .VPC .ID = ptr .To ("VPCID" )
2126
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2127
+ output , err := clusterScope .ReconcileVPC ()
2128
+ g .Expect (err ).ToNot (BeNil ())
2129
+ g .Expect (output ).To (BeFalse ())
2130
+ })
2131
+
2132
+ t .Run ("When VPC ID is already set and GetVPC returns vpc in pending state" , func (t * testing.T ) {
2133
+ g := NewWithT (t )
2134
+ setup (t )
2135
+ t .Cleanup (teardown )
2136
+
2137
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("VPCID" ), Status : ptr .To (string (infrav1beta2 .VPCStatePending ))}
2138
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2139
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2140
+ mockVPC .EXPECT ().GetVPC (gomock .Any ()).Return (vpcOutput , nil , nil )
2141
+ return mockVPC , nil
2142
+ }
2143
+ clusterScopeParams .IBMPowerVSCluster .Spec .VPC .ID = ptr .To ("VPCID" )
2144
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2145
+ output , err := clusterScope .ReconcileVPC ()
2146
+ g .Expect (err ).To (BeNil ())
2147
+ g .Expect (output ).To (BeTrue ())
2148
+ })
2149
+ }
2150
+
2151
+ func TestPowerVSScopeCreateVPC (t * testing.T ) {
2152
+ var (
2153
+ mockVPC * mockV.MockVpc
2154
+ mockCtrl * gomock.Controller
2155
+ )
2156
+
2157
+ setup := func (t * testing.T ) {
2158
+ t .Helper ()
2159
+ mockCtrl = gomock .NewController (t )
2160
+ mockVPC = mockV .NewMockVpc (mockCtrl )
2161
+ }
2162
+ teardown := func () {
2163
+ mockCtrl .Finish ()
2164
+ }
2165
+ t .Run ("When resourceGroupID is nil " , func (t * testing.T ) {
2166
+ g := NewWithT (t )
2167
+ setup (t )
2168
+ t .Cleanup (teardown )
2169
+
2170
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2171
+ clusterScopeParams .IBMPowerVSCluster .Spec .ResourceGroup .ID = nil
2172
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2173
+
2174
+ output , err := clusterScope .createVPC ()
2175
+ g .Expect (err ).ToNot (BeNil ())
2176
+ g .Expect (output ).To (BeNil ())
2177
+
2178
+ })
2179
+ t .Run ("When resourceGroupID is not nil and create VPC is successful" , func (t * testing.T ) {
2180
+ g := NewWithT (t )
2181
+ setup (t )
2182
+ t .Cleanup (teardown )
2183
+
2184
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("vpcID" ), DefaultSecurityGroup : & vpcv1.SecurityGroupReference {ID : ptr .To ("DefaultSecurityGroupID" )}}
2185
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2186
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2187
+ mockVPC .EXPECT ().CreateVPC (gomock .Any ()).Return (vpcOutput , nil , nil )
2188
+ mockVPC .EXPECT ().CreateSecurityGroupRule (gomock .Any ()).Return (nil , nil , nil )
2189
+ return mockVPC , nil
2190
+ }
2191
+
2192
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2193
+ output , err := clusterScope .createVPC ()
2194
+ g .Expect (err ).To (BeNil ())
2195
+ g .Expect (output ).To (Equal (vpcOutput .ID ))
2196
+
2197
+ })
2198
+
2199
+ t .Run ("When resourceGroupID is not nil and CreateSecurityGroupRule returns error" , func (t * testing.T ) {
2200
+ g := NewWithT (t )
2201
+ setup (t )
2202
+ t .Cleanup (teardown )
2203
+
2204
+ vpcOutput := & vpcv1.VPC {Name : ptr .To ("VPCName" ), ID : ptr .To ("vpcID" ), DefaultSecurityGroup : & vpcv1.SecurityGroupReference {ID : ptr .To ("DefaultSecurityGroupID" )}}
2205
+ clusterScopeParams := getPowerVSClusterScopeParams ()
2206
+ clusterScopeParams .VPCClientFactory = func () (vpc.Vpc , error ) {
2207
+ mockVPC .EXPECT ().CreateVPC (gomock .Any ()).Return (vpcOutput , nil , nil )
2208
+ mockVPC .EXPECT ().CreateSecurityGroupRule (gomock .Any ()).Return (nil , nil , fmt .Errorf ("CreateSecurityGroupRule returns error" ))
2209
+ return mockVPC , nil
2210
+ }
2211
+
2212
+ clusterScope , _ := NewPowerVSClusterScope (clusterScopeParams )
2213
+ output , err := clusterScope .createVPC ()
2214
+ g .Expect (err ).ToNot (BeNil ())
2215
+ g .Expect (output ).To (BeNil ())
2216
+
2217
+ })
2218
+ }
2219
+
2220
+ func TestGetServiceName (t * testing.T ) {
2221
+ testCases := []struct {
2222
+ name string
2223
+ resourceType infrav1beta2.ResourceType
2224
+ expectedName * string
2225
+ clusterScope PowerVSClusterScope
2226
+ }{
2227
+ // {
2228
+ // name: "Resource type is service instance and ServiceInstance is nil",
2229
+ // resourceType: infrav1beta2.ResourceTypeServiceInstance,
2230
+ // clusterScope: PowerVSClusterScope{
2231
+ // IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{},
2232
+ // },
2233
+ // expectedName: ptr.To(fmt.Sprintf("%s-serviceInstance", clusterscope.IBMPowerVSCluster.Name)),
2234
+ // },
2235
+ {
2236
+ name : "Resource type is service instance and ServiceInstance is not nil" ,
2237
+ resourceType : infrav1beta2 .ResourceTypeServiceInstance ,
2238
+ clusterScope : PowerVSClusterScope {
2239
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {ServiceInstance : & infrav1beta2.IBMPowerVSResourceReference {Name : ptr .To ("ServiceInstanceName" )}}},
2240
+ },
2241
+ expectedName : ptr .To ("ServiceInstanceName" ),
2242
+ },
2243
+ {
2244
+ name : "Resource type is network and Network is not nil" ,
2245
+ resourceType : infrav1beta2 .ResourceTypeNetwork ,
2246
+ clusterScope : PowerVSClusterScope {
2247
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {Network : infrav1beta2.IBMPowerVSResourceReference {Name : ptr .To ("NetworkName" )}}},
2248
+ },
2249
+ expectedName : ptr .To ("NetworkName" ),
2250
+ },
2251
+ {
2252
+ name : "Resource type is vpc and VPC is not nil" ,
2253
+ resourceType : infrav1beta2 .ResourceTypeVPC ,
2254
+ clusterScope : PowerVSClusterScope {
2255
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {VPC : & infrav1beta2.VPCResourceReference {Name : ptr .To ("VPCName" )}}},
2256
+ },
2257
+ expectedName : ptr .To ("VPCName" ),
2258
+ },
2259
+ {
2260
+ name : "Resource type is transit gateway and transitgateway is not nil" ,
2261
+ resourceType : infrav1beta2 .ResourceTypeTransitGateway ,
2262
+ clusterScope : PowerVSClusterScope {
2263
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {TransitGateway : & infrav1beta2.TransitGateway {Name : ptr .To ("TransitGatewayName" )}}},
2264
+ },
2265
+ expectedName : ptr .To ("TransitGatewayName" ),
2266
+ },
2267
+ {
2268
+ name : "Resource type is dhcp server and dhcpserver is not nil" ,
2269
+ resourceType : infrav1beta2 .ResourceTypeDHCPServer ,
2270
+ clusterScope : PowerVSClusterScope {
2271
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {DHCPServer : & infrav1beta2.DHCPServer {Name : ptr .To ("DHCPServerName" )}}},
2272
+ },
2273
+ expectedName : ptr .To ("DHCPServerName" ),
2274
+ },
2275
+ {
2276
+ name : "Resource type is cos instance and cos instance is not nil" ,
2277
+ resourceType : infrav1beta2 .ResourceTypeCOSInstance ,
2278
+ clusterScope : PowerVSClusterScope {
2279
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {CosInstance : & infrav1beta2.CosInstance {Name : "CosInstanceName" }}},
2280
+ },
2281
+ expectedName : ptr .To ("CosInstanceName" ),
2282
+ },
2283
+ {
2284
+ name : "Resource type is cos bucket and cos bucket is not nil" ,
2285
+ resourceType : infrav1beta2 .ResourceTypeCOSBucket ,
2286
+ clusterScope : PowerVSClusterScope {
2287
+ IBMPowerVSCluster : & infrav1beta2.IBMPowerVSCluster {Spec : infrav1beta2.IBMPowerVSClusterSpec {CosInstance : & infrav1beta2.CosInstance {BucketName : "CosBucketName" }}},
2288
+ },
2289
+ expectedName : ptr .To ("CosBucketName" ),
2290
+ },
2291
+ // TODO
2292
+ // {
2293
+ // name: "Resource type is subnet",
2294
+ // resourceType: infrav1beta2.ResourceTypeSubnet,
2295
+ // clusterScope: PowerVSClusterScope{
2296
+ // IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{},
2297
+ // },
2298
+ // expectedName: ptr.To("SubnetName"),
2299
+ // },
2300
+ // {
2301
+ // name: "Resource type is load balancer",
2302
+ // resourceType: infrav1beta2.ResourceTypeLoadBalancer,
2303
+ // clusterScope: PowerVSClusterScope{
2304
+ // IBMPowerVSCluster: &infrav1beta2.IBMPowerVSCluster{},
2305
+ // },
2306
+ // expectedName: ptr.To("LoadBalancerName"),
2307
+ // },
2308
+ }
2309
+ for _ , tc := range testCases {
2310
+ g := NewWithT (t )
2311
+ t .Run (tc .name , func (_ * testing.T ) {
2312
+ rgID := tc .clusterScope .GetServiceName (infrav1beta2 .ResourceType (tc .resourceType ))
2313
+ g .Expect (rgID ).To (Equal (tc .expectedName ))
2314
+ })
2315
+ }
2316
+
2317
+ }
0 commit comments