@@ -19,13 +19,15 @@ package azurefile
19
19
import (
20
20
"context"
21
21
"encoding/base64"
22
+ "errors"
22
23
"fmt"
23
24
"net/http"
24
25
"net/url"
25
26
"os"
26
27
"path/filepath"
27
28
"reflect"
28
29
"sort"
30
+ "strings"
29
31
"testing"
30
32
"time"
31
33
@@ -35,6 +37,8 @@ import (
35
37
"github.com/container-storage-interface/spec/lib/go/csi"
36
38
"github.com/stretchr/testify/assert"
37
39
"go.uber.org/mock/gomock"
40
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41
+ v1api "k8s.io/api/core/v1"
38
42
"k8s.io/client-go/kubernetes/fake"
39
43
40
44
"sigs.k8s.io/cloud-provider-azure/pkg/azclient"
54
58
)
55
59
56
60
func NewFakeDriver () * Driver {
57
- var err error
58
61
driverOptions := DriverOptions {
59
62
NodeID : fakeNodeID ,
60
63
DriverName : DefaultDriverName ,
@@ -66,9 +69,6 @@ func NewFakeDriver() *Driver {
66
69
driver := NewDriver (& driverOptions )
67
70
driver .Name = fakeDriverName
68
71
driver .Version = vendorVersion
69
- if err != nil {
70
- panic (err )
71
- }
72
72
driver .AddControllerServiceCapabilities (
73
73
[]csi.ControllerServiceCapability_RPC_Type {
74
74
csi .ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME ,
@@ -1473,3 +1473,127 @@ func TestGetStorageEndPointSuffix(t *testing.T) {
1473
1473
assert .Equal (t , test .expectedSuffix , suffix , test .name )
1474
1474
}
1475
1475
}
1476
+
1477
+ func TestGetStorageAccesskey (t * testing.T ) {
1478
+ options := & storage.AccountOptions {
1479
+ Name : "test-sa" ,
1480
+ SubscriptionID : "test-subID" ,
1481
+ ResourceGroup : "test-rg" ,
1482
+ }
1483
+ fakeAccName := options .Name
1484
+ fakeAccKey := "test-key"
1485
+ secretNamespace := "test-ns"
1486
+ testCases := []struct {
1487
+ name string
1488
+ secrets map [string ]string
1489
+ secretName string
1490
+ expectedError error
1491
+ }{
1492
+ {
1493
+ name : "error is not nil" , // test case shoudl run first to avoid cache hit
1494
+ secrets : make (map [string ]string ),
1495
+ secretName : "foobar" ,
1496
+ expectedError : errors .New ("" ),
1497
+ },
1498
+ {
1499
+ name : "Secrets is larger than 0" ,
1500
+ secrets : map [string ]string {
1501
+ "accountName" : fakeAccName ,
1502
+ "accountNameField" : fakeAccName ,
1503
+ "defaultSecretAccountName" : fakeAccName ,
1504
+ "accountKey" : fakeAccKey ,
1505
+ "accountKeyField" : fakeAccKey ,
1506
+ "defaultSecretAccountKey" : fakeAccKey ,
1507
+ },
1508
+ expectedError : nil ,
1509
+ },
1510
+ {
1511
+ name : "secretName is Empty" ,
1512
+ secrets : make (map [string ]string ),
1513
+ secretName : "" ,
1514
+ expectedError : nil ,
1515
+ },
1516
+ {
1517
+ name : "successful input/error is nil" ,
1518
+ secrets : make (map [string ]string ),
1519
+ secretName : fmt .Sprintf (secretNameTemplate , options .Name ),
1520
+ expectedError : nil ,
1521
+ },
1522
+ }
1523
+ d := NewFakeDriver ()
1524
+ d .cloud = & storage.AccountRepo {}
1525
+ d .kubeClient = fake .NewSimpleClientset ()
1526
+ ctrl := gomock .NewController (t )
1527
+ defer ctrl .Finish ()
1528
+ mockStorageAccountsClient := mock_accountclient .NewMockInterface (ctrl )
1529
+ d .cloud .ComputeClientFactory = mock_azclient .NewMockClientFactory (gomock .NewController (t ))
1530
+ d .cloud .ComputeClientFactory .(* mock_azclient.MockClientFactory ).EXPECT ().GetAccountClient ().Return (mockStorageAccountsClient ).AnyTimes ()
1531
+ accountListKeysResult := []* armstorage.AccountKey {}
1532
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return (accountListKeysResult , nil ).AnyTimes ()
1533
+ d .cloud .ComputeClientFactory .(* mock_azclient.MockClientFactory ).EXPECT ().GetAccountClientForSub (gomock .Any ()).Return (mockStorageAccountsClient , nil ).AnyTimes ()
1534
+ secret := & v1api.Secret {
1535
+ ObjectMeta : metav1.ObjectMeta {
1536
+ Namespace : secretNamespace ,
1537
+ Name : fmt .Sprintf (secretNameTemplate , options .Name ),
1538
+ },
1539
+ Data : map [string ][]byte {
1540
+ defaultSecretAccountName : []byte (fakeAccName ),
1541
+ defaultSecretAccountKey : []byte (fakeAccKey ),
1542
+ },
1543
+ Type : "Opaque" ,
1544
+ }
1545
+ secret .Namespace = secretNamespace
1546
+ _ , secretCreateErr := d .kubeClient .CoreV1 ().Secrets (secretNamespace ).Create (context .TODO (), secret , metav1.CreateOptions {})
1547
+ if secretCreateErr != nil {
1548
+ t .Error ("failed to create secret" )
1549
+ }
1550
+ for _ , tc := range testCases {
1551
+ t .Run (tc .name , func (t * testing.T ) {
1552
+ accKey , err := d .GetStorageAccesskey (context .TODO (), options , tc .secrets , tc .secretName , secretNamespace )
1553
+ if tc .expectedError != nil {
1554
+ assert .Error (t , err , "there should be an error" )
1555
+ } else {
1556
+ assert .Equal (t , nil , err , "error should be nil" )
1557
+ assert .Equal (t , fakeAccKey , accKey , "account keys must match" )
1558
+ }
1559
+ })
1560
+ }
1561
+ }
1562
+
1563
+ func TestGetStorageAccesskeyWithSubsID (t * testing.T ) {
1564
+ testCases := []struct {
1565
+ name string
1566
+ expectedError error
1567
+ }{
1568
+ {
1569
+ name : "Get storage access key error with cloud is nil" ,
1570
+ expectedError : fmt .Errorf ("could not get account key: cloud or ComputeClientFactory is nil" ),
1571
+ },
1572
+ {
1573
+ name : "Get storage access key error with ComputeClientFactory is nil" ,
1574
+ expectedError : fmt .Errorf ("could not get account key: cloud or ComputeClientFactory is nil" ),
1575
+ },
1576
+ {
1577
+ name : "Get storage access key successfully" ,
1578
+ expectedError : nil ,
1579
+ },
1580
+ }
1581
+
1582
+ for _ , tc := range testCases {
1583
+ d := NewFakeDriver ()
1584
+ d .cloud = & storage.AccountRepo {}
1585
+ if ! strings .Contains (tc .name , "is nil" ) {
1586
+ ctrl := gomock .NewController (t )
1587
+ defer ctrl .Finish ()
1588
+ mockStorageAccountsClient := mock_accountclient .NewMockInterface (ctrl )
1589
+ d .cloud .ComputeClientFactory = mock_azclient .NewMockClientFactory (ctrl )
1590
+ d .cloud .ComputeClientFactory .(* mock_azclient.MockClientFactory ).EXPECT ().GetAccountClient ().Return (mockStorageAccountsClient ).AnyTimes ()
1591
+ s := "unit-test"
1592
+ accountkey := armstorage.AccountKey {Value : & s }
1593
+ mockStorageAccountsClient .EXPECT ().ListKeys (gomock .Any (), gomock .Any (), gomock .Any ()).Return ([]* armstorage.AccountKey {& accountkey }, tc .expectedError ).AnyTimes ()
1594
+ d .cloud .ComputeClientFactory .(* mock_azclient.MockClientFactory ).EXPECT ().GetAccountClientForSub (gomock .Any ()).Return (mockStorageAccountsClient , nil ).AnyTimes ()
1595
+ }
1596
+ _ , err := d .GetStorageAccesskeyWithSubsID (context .TODO (), "test-subID" , "test-rg" , "test-sa" , true )
1597
+ assert .Equal (t , tc .expectedError , err )
1598
+ }
1599
+ }
0 commit comments