Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
cvvz committed Sep 1, 2022
1 parent 000f436 commit 7bcaeb7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pkg/azurefile/azurefile_test.go
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 16 additions & 1 deletion pkg/azurefile/controllerserver_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7bcaeb7

Please sign in to comment.