diff --git a/pkg/azurefile/azurefile_test.go b/pkg/azurefile/azurefile_test.go index 45433cc173..32125f5019 100644 --- a/pkg/azurefile/azurefile_test.go +++ b/pkg/azurefile/azurefile_test.go @@ -34,6 +34,7 @@ import ( "github.com/stretchr/testify/assert" "k8s.io/client-go/kubernetes/fake" + "sigs.k8s.io/cloud-provider-azure/pkg/auth" "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/fileclient/mockfileclient" "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/storageaccountclient/mockstorageaccountclient" azure "sigs.k8s.io/cloud-provider-azure/pkg/provider" @@ -56,6 +57,13 @@ func NewFakeDriver() *Driver { driver := NewDriver(&driverOptions) driver.Name = fakeDriverName driver.Version = vendorVersion + driver.cloud = &azure.Cloud{ + Config: azure.Config{ + AzureAuthConfig: auth.AzureAuthConfig{ + SubscriptionID: "subscriptionID", + }, + }, + } return driver } @@ -64,6 +72,13 @@ func NewFakeDriverCustomOptions(opts DriverOptions) *Driver { driver := NewDriver(&driverOptions) driver.Name = fakeDriverName driver.Version = vendorVersion + driver.cloud = &azure.Cloud{ + Config: azure.Config{ + AzureAuthConfig: auth.AzureAuthConfig{ + SubscriptionID: "subscriptionID", + }, + }, + } return driver } @@ -844,6 +859,7 @@ func TestGetFileShareQuota(t *testing.T) { mockFileClient := mockfileclient.NewMockInterface(ctrl) d.cloud.FileClient = mockFileClient mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(test.mockedFileShareResp, test.mockedFileShareErr).AnyTimes() + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() quota, err := d.getFileShareQuota("", resourceGroupName, accountName, fileShareName, test.secrets) if !reflect.DeepEqual(err, test.expectedError) { t.Errorf("test name: %s, Unexpected error: %v, expected error: %v", test.desc, err, test.expectedError) diff --git a/pkg/azurefile/controllerserver_test.go b/pkg/azurefile/controllerserver_test.go index f07228f3b7..9c5414516c 100644 --- a/pkg/azurefile/controllerserver_test.go +++ b/pkg/azurefile/controllerserver_test.go @@ -593,7 +593,7 @@ func TestCreateVolume(t *testing.T) { csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME, }) - expectedErr := status.Errorf(codes.InvalidArgument, "storeAccountKey must set as true in cross subscription(abc)") + expectedErr := status.Errorf(codes.InvalidArgument, "resourceGroup must be provided in cross subscription(abc)") _, err := d.CreateVolume(context.Background(), req) if !reflect.DeepEqual(err, expectedErr) { t.Errorf("Unexpected error: %v", err) @@ -762,6 +762,7 @@ func TestCreateVolume(t *testing.T) { FileServicePropertiesProperties: &storage.FileServicePropertiesProperties{}, } + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -831,6 +832,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -886,6 +888,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -962,6 +965,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() mockStorageAccountsClient.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{FileShareProperties: &storage.FileShareProperties{ShareQuota: nil}}, nil).AnyTimes() d.AddControllerServiceCapabilities( @@ -1035,6 +1039,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() mockStorageAccountsClient.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{FileShareProperties: &storage.FileShareProperties{ShareQuota: to.Int32Ptr(1)}}, nil).AnyTimes() d.AddControllerServiceCapabilities( @@ -1124,6 +1129,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -1195,6 +1201,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -1255,6 +1262,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -1316,6 +1324,7 @@ func TestCreateVolume(t *testing.T) { mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl) d.cloud.StorageAccountClient = mockStorageAccountsClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(keys, nil).AnyTimes() mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(accounts, nil).AnyTimes() @@ -1385,6 +1394,7 @@ func TestCreateVolume(t *testing.T) { d.cloud.StorageAccountClient = mockStorageAccountsClient tagValue := "TestTagValue" + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() first := mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf(accountLimitExceedManagementAPI)) second := mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) gomock.InOrder(first, second) @@ -1529,6 +1539,7 @@ func TestDeleteVolume(t *testing.T) { mockFileClient := mockfileclient.NewMockInterface(ctrl) d.cloud = &azure.Cloud{} d.cloud.FileClient = mockFileClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("test error")).Times(1) expectedErr := status.Errorf(codes.Internal, "DeleteFileShare fileshare under account(f5713de20cde511e8ba4900) rg() failed with error: test error") @@ -1560,6 +1571,7 @@ func TestDeleteVolume(t *testing.T) { mockFileClient := mockfileclient.NewMockInterface(ctrl) d.cloud = &azure.Cloud{} d.cloud.FileClient = mockFileClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1) expectedResp := &csi.DeleteSnapshotResponse{} @@ -1731,6 +1743,7 @@ func TestValidateVolumeCapabilities(t *testing.T) { mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(key, nil).AnyTimes() mockFileClient := mockfileclient.NewMockInterface(ctrl) d.cloud.FileClient = mockFileClient + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{FileShareProperties: &storage.FileShareProperties{ShareQuota: &fakeShareQuota}}, test.mockedFileShareErr).AnyTimes() _, err := d.ValidateVolumeCapabilities(context.Background(), &test.req) @@ -2211,6 +2224,7 @@ func TestControllerExpandVolume(t *testing.T) { d.cloud.Environment = azure2.Environment{StorageEndpointSuffix: "abc"} mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), "vol_1", gomock.Any()).Return(key, nil).AnyTimes() mockFileClient := mockfileclient.NewMockInterface(ctrl) + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().ResizeFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("test error")).AnyTimes() d.cloud.FileClient = mockFileClient @@ -2296,6 +2310,7 @@ func TestControllerExpandVolume(t *testing.T) { d.cloud.Environment = azure2.Environment{StorageEndpointSuffix: "abc"} mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), "capz-d18sqm", gomock.Any()).Return(key, nil).AnyTimes() mockFileClient := mockfileclient.NewMockInterface(ctrl) + mockFileClient.EXPECT().WithSubscriptionID(gomock.Any()).Return(mockFileClient).AnyTimes() mockFileClient.EXPECT().ResizeFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() shareQuota := int32(0) mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{FileShareProperties: &storage.FileShareProperties{ShareQuota: &shareQuota}}, nil).AnyTimes()