@@ -19,13 +19,15 @@ package azurefile
1919import (
2020 "context"
2121 "encoding/base64"
22+ "errors"
2223 "fmt"
2324 "net/http"
2425 "net/url"
2526 "os"
2627 "path/filepath"
2728 "reflect"
2829 "sort"
30+ "strings"
2931 "testing"
3032 "time"
3133
@@ -35,6 +37,8 @@ import (
3537 "github.com/container-storage-interface/spec/lib/go/csi"
3638 "github.com/stretchr/testify/assert"
3739 "go.uber.org/mock/gomock"
40+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
41+ v1api "k8s.io/api/core/v1"
3842 "k8s.io/client-go/kubernetes/fake"
3943
4044 "sigs.k8s.io/cloud-provider-azure/pkg/azclient"
5458)
5559
5660func NewFakeDriver () * Driver {
57- var err error
5861 driverOptions := DriverOptions {
5962 NodeID : fakeNodeID ,
6063 DriverName : DefaultDriverName ,
@@ -66,9 +69,6 @@ func NewFakeDriver() *Driver {
6669 driver := NewDriver (& driverOptions )
6770 driver .Name = fakeDriverName
6871 driver .Version = vendorVersion
69- if err != nil {
70- panic (err )
71- }
7272 driver .AddControllerServiceCapabilities (
7373 []csi.ControllerServiceCapability_RPC_Type {
7474 csi .ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME ,
@@ -1473,3 +1473,127 @@ func TestGetStorageEndPointSuffix(t *testing.T) {
14731473 assert .Equal (t , test .expectedSuffix , suffix , test .name )
14741474 }
14751475}
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