@@ -17,14 +17,21 @@ limitations under the License.
17
17
package blob
18
18
19
19
import (
20
+ "context"
20
21
"errors"
21
22
"fmt"
22
23
"io/ioutil"
24
+ "k8s.io/legacy-cloud-providers/azure/retry"
23
25
"os"
24
26
"reflect"
25
27
"testing"
26
28
29
+ "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
30
+ "github.com/golang/mock/gomock"
27
31
"github.com/stretchr/testify/assert"
32
+
33
+ "k8s.io/legacy-cloud-providers/azure"
34
+ "k8s.io/legacy-cloud-providers/azure/clients/storageaccountclient/mockstorageaccountclient"
28
35
)
29
36
30
37
const (
@@ -45,6 +52,88 @@ func TestNewFakeDriver(t *testing.T) {
45
52
assert .NotNil (t , d )
46
53
}
47
54
55
+ func TestNewDriver (t * testing.T ) {
56
+ driver := NewDriver (fakeNodeID )
57
+ fakedriver := NewFakeDriver ()
58
+ fakedriver .Name = DriverName
59
+ fakedriver .Version = driverVersion
60
+ assert .Equal (t , driver , fakedriver )
61
+ }
62
+
63
+ func TestRun (t * testing.T ) {
64
+ fakeCredFile := "fake-cred-file.json"
65
+ fakeCredContent := `{
66
+ "tenantId": "1234",
67
+ "subscriptionId": "12345",
68
+ "aadClientId": "123456",
69
+ "aadClientSecret": "1234567",
70
+ "resourceGroup": "rg1",
71
+ "location": "loc"
72
+ }`
73
+
74
+ testCases := []struct {
75
+ name string
76
+ testFunc func (t * testing.T )
77
+ }{
78
+ {
79
+ name : "Successful run" ,
80
+ testFunc : func (t * testing.T ) {
81
+ if err := ioutil .WriteFile (fakeCredFile , []byte (fakeCredContent ), 0666 ); err != nil {
82
+ t .Error (err )
83
+ }
84
+
85
+ defer func () {
86
+ if err := os .Remove (fakeCredFile ); err != nil {
87
+ t .Error (err )
88
+ }
89
+ }()
90
+
91
+ originalCredFile , ok := os .LookupEnv (DefaultAzureCredentialFileEnv )
92
+ if ok {
93
+ defer os .Setenv (DefaultAzureCredentialFileEnv , originalCredFile )
94
+ } else {
95
+ defer os .Unsetenv (DefaultAzureCredentialFileEnv )
96
+ }
97
+ os .Setenv (DefaultAzureCredentialFileEnv , fakeCredFile )
98
+
99
+ d := NewFakeDriver ()
100
+ d .Run ("tcp://127.0.0.1:0" , "" , true )
101
+ },
102
+ },
103
+ {
104
+ name : "Successful run with node ID missing" ,
105
+ testFunc : func (t * testing.T ) {
106
+ if err := ioutil .WriteFile (fakeCredFile , []byte (fakeCredContent ), 0666 ); err != nil {
107
+ t .Error (err )
108
+ }
109
+
110
+ defer func () {
111
+ if err := os .Remove (fakeCredFile ); err != nil {
112
+ t .Error (err )
113
+ }
114
+ }()
115
+
116
+ originalCredFile , ok := os .LookupEnv (DefaultAzureCredentialFileEnv )
117
+ if ok {
118
+ defer os .Setenv (DefaultAzureCredentialFileEnv , originalCredFile )
119
+ } else {
120
+ defer os .Unsetenv (DefaultAzureCredentialFileEnv )
121
+ }
122
+ os .Setenv (DefaultAzureCredentialFileEnv , fakeCredFile )
123
+
124
+ d := NewFakeDriver ()
125
+ d .cloud = & azure.Cloud {}
126
+ d .NodeID = ""
127
+ d .Run ("tcp://127.0.0.1:0" , "" , true )
128
+ },
129
+ },
130
+ }
131
+
132
+ for _ , tc := range testCases {
133
+ t .Run (tc .name , tc .testFunc )
134
+ }
135
+ }
136
+
48
137
func TestAppendDefaultMountOptions (t * testing.T ) {
49
138
tests := []struct {
50
139
options []string
@@ -346,3 +435,168 @@ func TestIsSupportedProtocol(t *testing.T) {
346
435
}
347
436
}
348
437
}
438
+
439
+ func TestGetAuthEnv (t * testing.T ) {
440
+
441
+ testCases := []struct {
442
+ name string
443
+ testFunc func (t * testing.T )
444
+ }{
445
+ {
446
+ name : "Get storage access key error" ,
447
+ testFunc : func (t * testing.T ) {
448
+ d := NewFakeDriver ()
449
+ attrib := make (map [string ]string )
450
+ secret := make (map [string ]string )
451
+ attrib ["containername" ] = "unit-test"
452
+ attrib ["keyvaultsecretname" ] = "unit-test"
453
+ attrib ["keyvaultsecretversion" ] = "unit-test"
454
+ attrib ["storageaccountname" ] = "unit-test"
455
+ attrib ["azurestorageidentityclientid" ] = "unit-test"
456
+ attrib ["azurestorageidentityobjectid" ] = "unit-test"
457
+ attrib ["azurestorageidentityresourceid" ] = "unit-test"
458
+ attrib ["msiendpoint" ] = "unit-test"
459
+ attrib ["azurestoragespnclientid" ] = "unit-test"
460
+ attrib ["azurestoragespntenantid" ] = "unit-test"
461
+ attrib ["azurestorageaadendpoint" ] = "unit-test"
462
+ volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
463
+ d .cloud = & azure.Cloud {}
464
+ ctrl := gomock .NewController (t )
465
+ defer ctrl .Finish ()
466
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
467
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
468
+ accountListKeysResult := storage.AccountListKeysResult {}
469
+ rerr := & retry.Error {
470
+ RawError : fmt .Errorf ("test" ),
471
+ }
472
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (accountListKeysResult , rerr ).AnyTimes ()
473
+ _ , _ , _ , err := d .GetAuthEnv (context .TODO (), volumeID , attrib , secret )
474
+ expectedErr := fmt .Errorf ("no key for storage account(f5713de20cde511e8ba4900) under resource group(rg), err Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test" )
475
+ if ! reflect .DeepEqual (err , expectedErr ) {
476
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
477
+ }
478
+ },
479
+ },
480
+ {
481
+ name : "valid request" ,
482
+ testFunc : func (t * testing.T ) {
483
+ d := NewFakeDriver ()
484
+ attrib := make (map [string ]string )
485
+ secret := make (map [string ]string )
486
+ volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
487
+ d .cloud = & azure.Cloud {}
488
+ ctrl := gomock .NewController (t )
489
+ defer ctrl .Finish ()
490
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
491
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
492
+ s := "unit-test"
493
+ accountkey := storage.AccountKey {
494
+ Value : & s ,
495
+ }
496
+ accountkeylist := []storage.AccountKey {}
497
+ accountkeylist = append (accountkeylist , accountkey )
498
+ list := storage.AccountListKeysResult {
499
+ Keys : & accountkeylist ,
500
+ }
501
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (list , nil ).AnyTimes ()
502
+ _ , _ , _ , err := d .GetAuthEnv (context .TODO (), volumeID , attrib , secret )
503
+ expectedErr := error (nil )
504
+ if ! reflect .DeepEqual (err , expectedErr ) {
505
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
506
+ }
507
+ },
508
+ },
509
+ {
510
+ name : "secret not empty " ,
511
+ testFunc : func (t * testing.T ) {
512
+ d := NewFakeDriver ()
513
+ attrib := make (map [string ]string )
514
+ secret := make (map [string ]string )
515
+ volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
516
+ secret ["accountname" ] = "unit-test"
517
+ secret ["azurestorageaccountname" ] = "unit-test"
518
+ secret ["accountkey" ] = "unit-test"
519
+ secret ["azurestorageaccountkey" ] = "unit-test"
520
+ secret ["azurestorageaccountsastoken" ] = "unit-test"
521
+ secret ["msisecret" ] = "unit-test"
522
+ secret ["azurestoragespnclientsecret" ] = "unit-test"
523
+ _ , _ , _ , err := d .GetAuthEnv (context .TODO (), volumeID , attrib , secret )
524
+ expectedErr := fmt .Errorf ("could not find containerName from attributes(map[]) or volumeID(rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41)" )
525
+ if ! reflect .DeepEqual (err , expectedErr ) {
526
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
527
+ }
528
+ },
529
+ },
530
+ }
531
+ for _ , tc := range testCases {
532
+ t .Run (tc .name , tc .testFunc )
533
+ }
534
+ }
535
+
536
+ func TestGetStorageAccountAndContainer (t * testing.T ) {
537
+ testCases := []struct {
538
+ name string
539
+ testFunc func (t * testing.T )
540
+ }{
541
+ {
542
+ name : "Get storage access key error" ,
543
+ testFunc : func (t * testing.T ) {
544
+ d := NewFakeDriver ()
545
+ attrib := make (map [string ]string )
546
+ secret := make (map [string ]string )
547
+ attrib ["containername" ] = "unit-test"
548
+ attrib ["keyvaultsecretname" ] = "unit-test"
549
+ attrib ["keyvaultsecretversion" ] = "unit-test"
550
+ attrib ["storageaccountname" ] = "unit-test"
551
+ volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
552
+ d .cloud = & azure.Cloud {}
553
+ ctrl := gomock .NewController (t )
554
+ defer ctrl .Finish ()
555
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
556
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
557
+ accountListKeysResult := storage.AccountListKeysResult {}
558
+ rerr := & retry.Error {
559
+ RawError : fmt .Errorf ("test" ),
560
+ }
561
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (accountListKeysResult , rerr ).AnyTimes ()
562
+ _ , _ , _ , _ , err := d .GetStorageAccountAndContainer (context .TODO (), volumeID , attrib , secret )
563
+ expectedErr := fmt .Errorf ("no key for storage account(f5713de20cde511e8ba4900) under resource group(rg), err Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test" )
564
+ if ! reflect .DeepEqual (err , expectedErr ) {
565
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
566
+ }
567
+ },
568
+ },
569
+ {
570
+ name : "valid request" ,
571
+ testFunc : func (t * testing.T ) {
572
+ d := NewFakeDriver ()
573
+ attrib := make (map [string ]string )
574
+ secret := make (map [string ]string )
575
+ volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
576
+ d .cloud = & azure.Cloud {}
577
+ ctrl := gomock .NewController (t )
578
+ defer ctrl .Finish ()
579
+ mockStorageAccountsClient := mockstorageaccountclient .NewMockInterface (ctrl )
580
+ d .cloud .StorageAccountClient = mockStorageAccountsClient
581
+ s := "unit-test"
582
+ accountkey := storage.AccountKey {
583
+ Value : & s ,
584
+ }
585
+ accountkeylist := []storage.AccountKey {}
586
+ accountkeylist = append (accountkeylist , accountkey )
587
+ list := storage.AccountListKeysResult {
588
+ Keys : & accountkeylist ,
589
+ }
590
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (list , nil ).AnyTimes ()
591
+ _ , _ , _ , _ , err := d .GetStorageAccountAndContainer (context .TODO (), volumeID , attrib , secret )
592
+ expectedErr := error (nil )
593
+ if ! reflect .DeepEqual (err , expectedErr ) {
594
+ t .Errorf ("actualErr: (%v), expectedErr: (%v)" , err , expectedErr )
595
+ }
596
+ },
597
+ },
598
+ }
599
+ for _ , tc := range testCases {
600
+ t .Run (tc .name , tc .testFunc )
601
+ }
602
+ }
0 commit comments