Skip to content

Commit 08e75c8

Browse files
authored
Merge pull request kubernetes#91719 from v-xuxin/enrich-unit-test
Azure: Enrich the unit tests for azure_storage
2 parents 1f40158 + f88ac4e commit 08e75c8

File tree

1 file changed

+80
-12
lines changed

1 file changed

+80
-12
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_storage_test.go

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
package azure
2020

2121
import (
22+
"fmt"
2223
"testing"
2324

2425
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
@@ -32,11 +33,7 @@ func TestCreateFileShare(t *testing.T) {
3233
ctrl := gomock.NewController(t)
3334
defer ctrl.Finish()
3435

35-
cloud := &Cloud{}
36-
mockFileClient := mockfileclient.NewMockInterface(ctrl)
37-
mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
38-
cloud.FileClient = mockFileClient
39-
36+
cloud := &Cloud{controllerCommon: &controllerCommon{resourceGroup: "rg"}}
4037
name := "baz"
4138
sku := "sku"
4239
kind := "StorageV2"
@@ -45,6 +42,7 @@ func TestCreateFileShare(t *testing.T) {
4542
bogus := "bogus"
4643

4744
tests := []struct {
45+
rg string
4846
name string
4947
acct string
5048
acctType string
@@ -95,6 +93,25 @@ func TestCreateFileShare(t *testing.T) {
9593
expectAcct: "baz",
9694
expectKey: "key",
9795
},
96+
{
97+
rg: "rg",
98+
name: "foo",
99+
acct: "",
100+
acctType: sku,
101+
acctKind: kind,
102+
loc: location,
103+
gb: 10,
104+
accounts: []storage.Account{
105+
{Name: &name, Sku: &storage.Sku{Name: storage.SkuName(sku)}, Kind: storage.Kind(kind), Location: &location},
106+
},
107+
keys: storage.AccountListKeysResult{
108+
Keys: &[]storage.AccountKey{
109+
{Value: &value},
110+
},
111+
},
112+
err: fmt.Errorf("create fileshare error"),
113+
expectErr: true,
114+
},
98115
{
99116
name: "foo",
100117
acct: "",
@@ -122,13 +139,17 @@ func TestCreateFileShare(t *testing.T) {
122139
}
123140

124141
for _, test := range tests {
142+
mockFileClient := mockfileclient.NewMockInterface(ctrl)
143+
cloud.FileClient = mockFileClient
144+
mockFileClient.EXPECT().CreateFileShare(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(test.err).AnyTimes()
145+
125146
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
126147
cloud.StorageAccountClient = mockStorageAccountsClient
127148
mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), "rg", gomock.Any()).Return(test.keys, nil).AnyTimes()
128149
mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), "rg").Return(test.accounts, nil).AnyTimes()
129150
mockStorageAccountsClient.EXPECT().Create(gomock.Any(), "rg", gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
130151

131-
account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, test.acctKind, "rg", test.loc, test.gb)
152+
account, key, err := cloud.CreateFileShare(test.name, test.acct, test.acctType, test.acctKind, test.rg, test.loc, test.gb)
132153
if test.expectErr && err == nil {
133154
t.Errorf("unexpected non-error")
134155
continue
@@ -151,15 +172,12 @@ func TestDeleteFileShare(t *testing.T) {
151172
defer ctrl.Finish()
152173

153174
cloud := &Cloud{}
154-
mockFileClient := mockfileclient.NewMockInterface(ctrl)
155-
mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
156-
cloud.FileClient = mockFileClient
157-
158175
tests := []struct {
159176
rg string
160177
acct string
161178
name string
162179

180+
err error
163181
expectErr bool
164182
}{
165183
{
@@ -169,11 +187,20 @@ func TestDeleteFileShare(t *testing.T) {
169187

170188
expectErr: false,
171189
},
190+
{
191+
rg: "rg",
192+
acct: "bar",
193+
name: "",
194+
195+
err: fmt.Errorf("delete fileshare error"),
196+
expectErr: true,
197+
},
172198
}
173199

174200
for _, test := range tests {
175-
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
176-
cloud.StorageAccountClient = mockStorageAccountsClient
201+
mockFileClient := mockfileclient.NewMockInterface(ctrl)
202+
cloud.FileClient = mockFileClient
203+
mockFileClient.EXPECT().DeleteFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(test.err).Times(1)
177204

178205
err := cloud.DeleteFileShare(test.rg, test.acct, test.name)
179206
if test.expectErr && err == nil {
@@ -229,3 +256,44 @@ func TestResizeFileShare(t *testing.T) {
229256
}
230257
}
231258
}
259+
260+
func TestGetFileShare(t *testing.T) {
261+
ctrl := gomock.NewController(t)
262+
defer ctrl.Finish()
263+
264+
cloud := &Cloud{}
265+
mockFileClient := mockfileclient.NewMockInterface(ctrl)
266+
mockFileClient.EXPECT().GetFileShare(gomock.Any(), gomock.Any(), gomock.Any()).Return(storage.FileShare{}, nil).AnyTimes()
267+
cloud.FileClient = mockFileClient
268+
269+
tests := []struct {
270+
rg string
271+
acct string
272+
name string
273+
274+
expectErr bool
275+
}{
276+
{
277+
rg: "rg",
278+
acct: "bar",
279+
name: "foo",
280+
281+
expectErr: false,
282+
},
283+
}
284+
285+
for _, test := range tests {
286+
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
287+
cloud.StorageAccountClient = mockStorageAccountsClient
288+
289+
_, err := cloud.GetFileShare(test.rg, test.acct, test.name)
290+
if test.expectErr && err == nil {
291+
t.Errorf("unexpected non-error")
292+
continue
293+
}
294+
if !test.expectErr && err != nil {
295+
t.Errorf("unexpected error: %v", err)
296+
continue
297+
}
298+
}
299+
}

0 commit comments

Comments
 (0)