Skip to content

Commit 998c477

Browse files
authored
Merge pull request kubernetes#95114 from antcs/issues-94240
add unit tests for getStorageAccounts in azure_storageaccount.go
2 parents 5254b75 + ea2d348 commit 998c477

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

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

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,219 @@ func TestGetStorageAccessKeys(t *testing.T) {
8484
}
8585
}
8686
}
87+
88+
func TestGetStorageAccount(t *testing.T) {
89+
ctrl := gomock.NewController(t)
90+
defer ctrl.Finish()
91+
92+
cloud := &Cloud{}
93+
94+
name := "testAccount"
95+
location := "testLocation"
96+
networkID := "networkID"
97+
accountProperties := storage.AccountProperties{
98+
NetworkRuleSet: &storage.NetworkRuleSet{
99+
VirtualNetworkRules: &[]storage.VirtualNetworkRule{
100+
{
101+
VirtualNetworkResourceID: &networkID,
102+
Action: storage.Allow,
103+
State: "state",
104+
},
105+
},
106+
}}
107+
108+
account := storage.Account{
109+
Sku: &storage.Sku{
110+
Name: "testSku",
111+
Tier: "testSkuTier",
112+
},
113+
Kind: "testKind",
114+
Location: &location,
115+
Name: &name,
116+
AccountProperties: &accountProperties,
117+
}
118+
119+
testResourceGroups := []storage.Account{account}
120+
121+
accountOptions := &AccountOptions{
122+
ResourceGroup: "rg",
123+
VirtualNetworkResourceIDs: []string{networkID},
124+
}
125+
126+
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
127+
cloud.StorageAccountClient = mockStorageAccountsClient
128+
129+
mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), "rg").Return(testResourceGroups, nil).Times(1)
130+
131+
accountsWithLocations, err := cloud.getStorageAccounts(accountOptions)
132+
if err != nil {
133+
t.Errorf("unexpected error: %v", err)
134+
}
135+
136+
if accountsWithLocations == nil {
137+
t.Error("unexpected error as returned accounts are nil")
138+
}
139+
140+
if len(accountsWithLocations) == 0 {
141+
t.Error("unexpected error as returned accounts slice is empty")
142+
}
143+
144+
expectedAccountWithLocation := accountWithLocation{
145+
Name: "testAccount",
146+
StorageType: "testSku",
147+
Location: "testLocation",
148+
}
149+
150+
accountWithLocation := accountsWithLocations[0]
151+
if accountWithLocation.Name != expectedAccountWithLocation.Name {
152+
t.Errorf("expected %s, but was %s", accountWithLocation.Name, expectedAccountWithLocation.Name)
153+
}
154+
155+
if accountWithLocation.StorageType != expectedAccountWithLocation.StorageType {
156+
t.Errorf("expected %s, but was %s", accountWithLocation.StorageType, expectedAccountWithLocation.StorageType)
157+
}
158+
159+
if accountWithLocation.Location != expectedAccountWithLocation.Location {
160+
t.Errorf("expected %s, but was %s", accountWithLocation.Location, expectedAccountWithLocation.Location)
161+
}
162+
}
163+
164+
func TestGetStorageAccountEdgeCases(t *testing.T) {
165+
ctrl := gomock.NewController(t)
166+
defer ctrl.Finish()
167+
168+
cloud := &Cloud{}
169+
170+
// default account with name, location, sku, kind
171+
name := "testAccount"
172+
location := "testLocation"
173+
sku := &storage.Sku{
174+
Name: "testSku",
175+
Tier: "testSkuTier",
176+
}
177+
account := storage.Account{
178+
Sku: sku,
179+
Kind: "testKind",
180+
Location: &location,
181+
Name: &name,
182+
}
183+
184+
accountPropertiesWithoutNetworkRuleSet := storage.AccountProperties{NetworkRuleSet: nil}
185+
accountPropertiesWithoutVirtualNetworkRules := storage.AccountProperties{
186+
NetworkRuleSet: &storage.NetworkRuleSet{
187+
VirtualNetworkRules: nil,
188+
}}
189+
190+
tests := []struct {
191+
testCase string
192+
testAccountOptions *AccountOptions
193+
testResourceGroups []storage.Account
194+
expectedResult []accountWithLocation
195+
expectedError error
196+
}{
197+
{
198+
testCase: "account name is nil",
199+
testAccountOptions: &AccountOptions{
200+
ResourceGroup: "rg",
201+
},
202+
testResourceGroups: []storage.Account{},
203+
expectedResult: []accountWithLocation{},
204+
expectedError: nil,
205+
},
206+
{
207+
testCase: "account location is nil",
208+
testAccountOptions: &AccountOptions{
209+
ResourceGroup: "rg",
210+
},
211+
testResourceGroups: []storage.Account{{Name: &name}},
212+
expectedResult: []accountWithLocation{},
213+
expectedError: nil,
214+
},
215+
{
216+
testCase: "account sku is nil",
217+
testAccountOptions: &AccountOptions{
218+
ResourceGroup: "rg",
219+
},
220+
testResourceGroups: []storage.Account{{Name: &name, Location: &location}},
221+
expectedResult: []accountWithLocation{},
222+
expectedError: nil,
223+
},
224+
{
225+
testCase: "account options type is not empty and not equal account storage type",
226+
testAccountOptions: &AccountOptions{
227+
ResourceGroup: "rg",
228+
Type: "testAccountOptionsType",
229+
},
230+
testResourceGroups: []storage.Account{account},
231+
expectedResult: []accountWithLocation{},
232+
expectedError: nil,
233+
},
234+
{
235+
testCase: "account options kind is not empty and not equal account type",
236+
testAccountOptions: &AccountOptions{
237+
ResourceGroup: "rg",
238+
Kind: "testAccountOptionsKind",
239+
},
240+
testResourceGroups: []storage.Account{account},
241+
expectedResult: []accountWithLocation{},
242+
expectedError: nil,
243+
},
244+
{
245+
testCase: "account options location is not empty and not equal account location",
246+
testAccountOptions: &AccountOptions{
247+
ResourceGroup: "rg",
248+
Location: "testAccountOptionsLocation",
249+
},
250+
testResourceGroups: []storage.Account{account},
251+
expectedResult: []accountWithLocation{},
252+
expectedError: nil,
253+
},
254+
{
255+
testCase: "account options account properties are nil",
256+
testAccountOptions: &AccountOptions{
257+
ResourceGroup: "rg",
258+
VirtualNetworkResourceIDs: []string{"id"},
259+
},
260+
testResourceGroups: []storage.Account{},
261+
expectedResult: []accountWithLocation{},
262+
expectedError: nil,
263+
},
264+
{
265+
testCase: "account options account properties network rule set is nil",
266+
testAccountOptions: &AccountOptions{
267+
ResourceGroup: "rg",
268+
VirtualNetworkResourceIDs: []string{"id"},
269+
},
270+
testResourceGroups: []storage.Account{{Name: &name, Kind: "kind", Location: &location, Sku: sku, AccountProperties: &accountPropertiesWithoutNetworkRuleSet}},
271+
expectedResult: []accountWithLocation{},
272+
expectedError: nil,
273+
},
274+
{
275+
testCase: "account options account properties virtual network rule is nil",
276+
testAccountOptions: &AccountOptions{
277+
ResourceGroup: "rg",
278+
VirtualNetworkResourceIDs: []string{"id"},
279+
},
280+
testResourceGroups: []storage.Account{{Name: &name, Kind: "kind", Location: &location, Sku: sku, AccountProperties: &accountPropertiesWithoutVirtualNetworkRules}},
281+
expectedResult: []accountWithLocation{},
282+
expectedError: nil,
283+
},
284+
}
285+
286+
for _, test := range tests {
287+
t.Logf("running test case: %s", test.testCase)
288+
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
289+
cloud.StorageAccountClient = mockStorageAccountsClient
290+
291+
mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), "rg").Return(test.testResourceGroups, nil).AnyTimes()
292+
293+
accountsWithLocations, err := cloud.getStorageAccounts(test.testAccountOptions)
294+
if err != test.expectedError {
295+
t.Errorf("unexpected error: %v", err)
296+
}
297+
298+
if len(accountsWithLocations) != len(test.expectedResult) {
299+
t.Error("unexpected error as returned accounts slice is not empty")
300+
}
301+
}
302+
}

0 commit comments

Comments
 (0)