@@ -24,10 +24,16 @@ import (
24
24
"reflect"
25
25
"testing"
26
26
27
+ "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-08-01/network"
28
+ "github.com/golang/mock/gomock"
29
+
27
30
"github.com/Azure/go-autorest/autorest/azure"
28
31
"github.com/stretchr/testify/assert"
29
32
30
- azure2 "sigs.k8s.io/cloud-provider-azure/pkg/provider"
33
+ "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/subnetclient/mocksubnetclient"
34
+ azureprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
35
+
36
+ "sigs.k8s.io/cloud-provider-azure/pkg/retry"
31
37
)
32
38
33
39
// TestGetCloudProvider tests the func getCloudProvider().
@@ -160,7 +166,7 @@ func TestGetServicePrincipalToken(t *testing.T) {
160
166
}
161
167
resource := "unit-test"
162
168
d := NewFakeDriver ()
163
- d .cloud = & azure2 .Cloud {}
169
+ d .cloud = & azureprovider .Cloud {}
164
170
_ , err := d .getServicePrincipalToken (env , resource )
165
171
expectedErr := fmt .Errorf ("parameter 'clientID' cannot be empty" )
166
172
if ! reflect .DeepEqual (expectedErr , err ) {
@@ -178,7 +184,7 @@ func TestGetKeyvaultToken(t *testing.T) {
178
184
KeyVaultEndpoint : "unit-test" ,
179
185
}
180
186
d := NewFakeDriver ()
181
- d .cloud = & azure2 .Cloud {}
187
+ d .cloud = & azureprovider .Cloud {}
182
188
d .cloud .Environment = env
183
189
_ , err := d .getKeyvaultToken ()
184
190
expectedErr := fmt .Errorf ("parameter 'clientID' cannot be empty" )
@@ -198,7 +204,7 @@ func TestInitializeKvClient(t *testing.T) {
198
204
KeyVaultEndpoint : "unit-test" ,
199
205
}
200
206
d := NewFakeDriver ()
201
- d .cloud = & azure2 .Cloud {}
207
+ d .cloud = & azureprovider .Cloud {}
202
208
d .cloud .Environment = env
203
209
_ , err := d .initializeKvClient ()
204
210
expectedErr := fmt .Errorf ("parameter 'clientID' cannot be empty" )
@@ -217,7 +223,7 @@ func TestGetKeyVaultSecretContent(t *testing.T) {
217
223
KeyVaultEndpoint : "unit-test" ,
218
224
}
219
225
d := NewFakeDriver ()
220
- d .cloud = & azure2 .Cloud {}
226
+ d .cloud = & azureprovider .Cloud {}
221
227
d .cloud .Environment = env
222
228
valueURL := "unit-test"
223
229
secretName := "unit-test"
@@ -245,3 +251,123 @@ func createTestFile(path string) error {
245
251
246
252
return nil
247
253
}
254
+
255
+ func TestUpdateSubnetServiceEndpoints (t * testing.T ) {
256
+ d := NewFakeDriver ()
257
+ ctrl := gomock .NewController (t )
258
+ defer ctrl .Finish ()
259
+ mockSubnetClient := mocksubnetclient .NewMockInterface (ctrl )
260
+
261
+ config := azureprovider.Config {
262
+ ResourceGroup : "rg" ,
263
+ Location : "loc" ,
264
+ VnetName : "fake-vnet" ,
265
+ SubnetName : "fake-subnet" ,
266
+ }
267
+
268
+ d .cloud = & azureprovider.Cloud {
269
+ SubnetsClient : mockSubnetClient ,
270
+ Config : config ,
271
+ }
272
+ ctx := context .TODO ()
273
+
274
+ testCases := []struct {
275
+ name string
276
+ testFunc func (t * testing.T )
277
+ }{
278
+ {
279
+ name : "[fail] no subnet" ,
280
+ testFunc : func (t * testing.T ) {
281
+ retErr := retry .NewError (false , fmt .Errorf ("the subnet does not exist" ))
282
+ mockSubnetClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (network.Subnet {}, retErr ).Times (1 )
283
+ expectedErr := fmt .Errorf ("failed to get the subnet %s under vnet %s: %v" , config .SubnetName , config .VnetName , retErr )
284
+ err := d .updateSubnetServiceEndpoints (ctx )
285
+ if ! reflect .DeepEqual (err , expectedErr ) {
286
+ t .Errorf ("Unexpected error: %v" , err )
287
+ }
288
+ },
289
+ },
290
+ {
291
+ name : "[success] subnetPropertiesFormat is nil" ,
292
+ testFunc : func (t * testing.T ) {
293
+ mockSubnetClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (network.Subnet {}, nil ).Times (1 )
294
+ mockSubnetClient .EXPECT ().CreateOrUpdate (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).Times (1 )
295
+
296
+ err := d .updateSubnetServiceEndpoints (ctx )
297
+ if ! reflect .DeepEqual (err , nil ) {
298
+ t .Errorf ("Unexpected error: %v" , err )
299
+ }
300
+ },
301
+ },
302
+ {
303
+ name : "[success] ServiceEndpoints is nil" ,
304
+ testFunc : func (t * testing.T ) {
305
+ fakeSubnet := network.Subnet {
306
+ SubnetPropertiesFormat : & network.SubnetPropertiesFormat {},
307
+ }
308
+
309
+ mockSubnetClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (fakeSubnet , nil ).Times (1 )
310
+ mockSubnetClient .EXPECT ().CreateOrUpdate (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).Times (1 )
311
+
312
+ err := d .updateSubnetServiceEndpoints (ctx )
313
+ if ! reflect .DeepEqual (err , nil ) {
314
+ t .Errorf ("Unexpected error: %v" , err )
315
+ }
316
+ },
317
+ },
318
+ {
319
+ name : "[success] storageService does not exists" ,
320
+ testFunc : func (t * testing.T ) {
321
+ fakeSubnet := network.Subnet {
322
+ SubnetPropertiesFormat : & network.SubnetPropertiesFormat {
323
+ ServiceEndpoints : & []network.ServiceEndpointPropertiesFormat {},
324
+ },
325
+ }
326
+
327
+ mockSubnetClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (fakeSubnet , nil ).Times (1 )
328
+ mockSubnetClient .EXPECT ().CreateOrUpdate (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (nil ).Times (1 )
329
+
330
+ err := d .updateSubnetServiceEndpoints (ctx )
331
+ if ! reflect .DeepEqual (err , nil ) {
332
+ t .Errorf ("Unexpected error: %v" , err )
333
+ }
334
+ },
335
+ },
336
+ {
337
+ name : "[success] storageService already exists" ,
338
+ testFunc : func (t * testing.T ) {
339
+ fakeSubnet := network.Subnet {
340
+ SubnetPropertiesFormat : & network.SubnetPropertiesFormat {
341
+ ServiceEndpoints : & []network.ServiceEndpointPropertiesFormat {
342
+ {
343
+ Service : & storageService ,
344
+ },
345
+ },
346
+ },
347
+ }
348
+
349
+ mockSubnetClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).Return (fakeSubnet , nil ).Times (1 )
350
+
351
+ err := d .updateSubnetServiceEndpoints (ctx )
352
+ if ! reflect .DeepEqual (err , nil ) {
353
+ t .Errorf ("Unexpected error: %v" , err )
354
+ }
355
+ },
356
+ },
357
+ {
358
+ name : "[fail] SubnetsClient is nil" ,
359
+ testFunc : func (t * testing.T ) {
360
+ d .cloud .SubnetsClient = nil
361
+ expectedErr := fmt .Errorf ("SubnetsClient is nil" )
362
+ err := d .updateSubnetServiceEndpoints (ctx )
363
+ if ! reflect .DeepEqual (err , expectedErr ) {
364
+ t .Errorf ("Unexpected error: %v" , err )
365
+ }
366
+ },
367
+ },
368
+ }
369
+
370
+ for _ , tc := range testCases {
371
+ t .Run (tc .name , tc .testFunc )
372
+ }
373
+ }
0 commit comments