1
1
package azure
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/base64"
7
+ "encoding/json"
8
+ "fmt"
9
+ "io"
6
10
"net/http"
7
11
"reflect"
8
12
"regexp"
@@ -12,6 +16,7 @@ import (
12
16
"github.com/Azure/azure-pipeline-go/pipeline"
13
17
"github.com/Azure/go-autorest/autorest"
14
18
"github.com/Azure/go-autorest/autorest/mocks"
19
+ "github.com/google/go-cmp/cmp"
15
20
16
21
corev1 "k8s.io/api/core/v1"
17
22
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -329,6 +334,138 @@ func TestConfigEnvWithUserKey(t *testing.T) {
329
334
}
330
335
}
331
336
337
+ // custom sender for mocking
338
+ type sender struct {
339
+ response []* http.Response
340
+ body string
341
+ }
342
+
343
+ // Do implements the Sender interface for mocking
344
+ // Do accepts the passed request and body, then appends the response and emits it.
345
+ func (s * sender ) Do (r * http.Request ) (* http.Response , error ) {
346
+ resp := & http.Response {
347
+ StatusCode : http .StatusOK ,
348
+ Request : r ,
349
+ Body : io .NopCloser (bytes .NewBufferString (s .body )),
350
+ }
351
+ s .response = append (s .response , resp )
352
+ return resp , nil
353
+ }
354
+
355
+ func TestUserProvidedTags (t * testing.T ) {
356
+ for _ , tt := range []struct {
357
+ name string
358
+ userTags []configv1.AzureResourceTag
359
+ expectedTags map [string ]string
360
+ infraName string
361
+ responseBody string
362
+ }{
363
+ {
364
+ name : "no-user-tags" ,
365
+ infraName : "some-infra" ,
366
+ // only default tags
367
+ expectedTags : map [string ]string {
368
+ "kubernetes.io_cluster.some-infra" : "owned" ,
369
+ },
370
+ responseBody : `{"nameAvailable":true}` ,
371
+ },
372
+ {
373
+ name : "with-user-tags" ,
374
+ infraName : "test-infra" ,
375
+ userTags : []configv1.AzureResourceTag {
376
+ {
377
+ Key : "tag1" ,
378
+ Value : "value1" ,
379
+ },
380
+ {
381
+ Key : "tag2" ,
382
+ Value : "value2" ,
383
+ },
384
+ },
385
+ // default tags and user tags
386
+ expectedTags : map [string ]string {
387
+ "kubernetes.io_cluster.test-infra" : "owned" ,
388
+ "tag1" : "value1" ,
389
+ "tag2" : "value2" ,
390
+ },
391
+ responseBody : `{"nameAvailable":true}` ,
392
+ },
393
+ } {
394
+ t .Run (tt .name , func (t * testing.T ) {
395
+ sender := & sender {
396
+ body : tt .responseBody ,
397
+ }
398
+
399
+ storageConfig := & imageregistryv1.ImageRegistryConfigStorageAzure {}
400
+
401
+ drv := NewDriver (context .Background (), storageConfig , nil )
402
+ drv .authorizer = autorest.NullAuthorizer {}
403
+ drv .sender = sender
404
+
405
+ _ , _ , err := drv .assureStorageAccount (
406
+ & Azure {
407
+ SubscriptionID : "subscription-id" ,
408
+ ResourceGroup : "resource-group" ,
409
+ },
410
+ & configv1.Infrastructure {
411
+ Status : configv1.InfrastructureStatus {
412
+ InfrastructureName : tt .infraName ,
413
+ Platform : configv1 .AzurePlatformType ,
414
+ PlatformStatus : & configv1.PlatformStatus {
415
+ Type : configv1 .AzurePlatformType ,
416
+ Azure : & configv1.AzurePlatformStatus {
417
+ ResourceTags : tt .userTags ,
418
+ },
419
+ },
420
+ },
421
+ },
422
+ )
423
+ if err != nil {
424
+ t .Errorf ("unexpected error %q" , err )
425
+ }
426
+
427
+ // flag to confirm presence of tags
428
+ foundTags := false
429
+
430
+ for _ , resp := range sender .response {
431
+ if resp != nil && resp .Request != nil && resp .Request .Body != nil {
432
+
433
+ reqBody := make (map [string ]interface {})
434
+ if err := json .NewDecoder (resp .Request .Body ).Decode (& reqBody ); err != nil {
435
+ t .Fatalf ("error decoding request: %q" , err )
436
+ }
437
+
438
+ // ignore request without tags
439
+ if _ , ok := reqBody ["tags" ]; ok {
440
+ foundTags = true
441
+
442
+ tags , ok := reqBody ["tags" ].(map [string ]interface {})
443
+ if ! ok {
444
+ t .Fatal ("unable to type assert tags field" )
445
+ }
446
+ // convert into correct type
447
+ receivedTags := make (map [string ]string )
448
+ for k , v := range tags {
449
+ receivedTags [k ] = fmt .Sprintf ("%+v" , v )
450
+ }
451
+
452
+ // compare the tags
453
+ if ! reflect .DeepEqual (tt .expectedTags , receivedTags ) {
454
+ t .Fatalf (
455
+ "unexpected tags: %s" ,
456
+ cmp .Diff (tt .expectedTags , receivedTags ),
457
+ )
458
+ }
459
+ }
460
+ }
461
+ }
462
+ if ! foundTags {
463
+ t .Fatal ("no tags present in the request" )
464
+ }
465
+ })
466
+ }
467
+ }
468
+
332
469
func Test_assureStorageAccount (t * testing.T ) {
333
470
for _ , tt := range []struct {
334
471
name string
0 commit comments