Conditions | 7 |
Total Lines | 89 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package providers |
||
222 | func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) { |
||
223 | originHttpDo := httpDo |
||
224 | defer func() { httpDo = originHttpDo }() |
||
225 | rollback := utils.Memory("ALIBABA_CLOUD_CONFIG_FILE") |
||
226 | defer func() { |
||
227 | getHomePath = utils.GetHomePath |
||
228 | rollback() |
||
229 | }() |
||
230 | |||
231 | getHomePath = func() string { |
||
232 | return "" |
||
233 | } |
||
234 | provider, err := NewCLIProfileCredentialsProviderBuilder().Build() |
||
235 | assert.Nil(t, err) |
||
236 | _, err = provider.GetCredentials() |
||
237 | assert.EqualError(t, err, "cannot found home dir") |
||
238 | |||
239 | getHomePath = func() string { |
||
240 | return "/path/invalid/home/dir" |
||
241 | } |
||
242 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() |
||
243 | assert.Nil(t, err) |
||
244 | _, err = provider.GetCredentials() |
||
245 | assert.EqualError(t, err, "reading aliyun cli config from '/path/invalid/home/dir/.aliyun/config.json' failed open /path/invalid/home/dir/.aliyun/config.json: no such file or directory") |
||
246 | |||
247 | // testcase: specify credentials file |
||
248 | provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileFile("/path/to/config.invalid").Build() |
||
249 | assert.Nil(t, err) |
||
250 | _, err = provider.GetCredentials() |
||
251 | assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config.invalid' failed open /path/to/config.invalid: no such file or directory") |
||
252 | |||
253 | // testcase: specify credentials file with env |
||
254 | os.Setenv("ALIBABA_CLOUD_CONFIG_FILE", "/path/to/config.invalid") |
||
255 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() |
||
256 | assert.Nil(t, err) |
||
257 | _, err = provider.GetCredentials() |
||
258 | assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config.invalid' failed open /path/to/config.invalid: no such file or directory") |
||
259 | |||
260 | provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileFile("/path/to/config1.invalid").Build() |
||
261 | assert.Nil(t, err) |
||
262 | _, err = provider.GetCredentials() |
||
263 | assert.EqualError(t, err, "reading aliyun cli config from '/path/to/config1.invalid' failed open /path/to/config1.invalid: no such file or directory") |
||
264 | os.Unsetenv("ALIBABA_CLOUD_CONFIG_FILE") |
||
265 | |||
266 | getHomePath = func() string { |
||
267 | wd, _ := os.Getwd() |
||
268 | return path.Join(wd, "fixtures") |
||
269 | } |
||
270 | |||
271 | // get credentials by current profile |
||
272 | provider, err = NewCLIProfileCredentialsProviderBuilder().Build() |
||
273 | assert.Nil(t, err) |
||
274 | cc, err := provider.GetCredentials() |
||
275 | assert.Nil(t, err) |
||
276 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "cli_profile/static_ak"}, cc) |
||
277 | |||
278 | provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("inexist").Build() |
||
279 | assert.Nil(t, err) |
||
280 | _, err = provider.GetCredentials() |
||
281 | assert.EqualError(t, err, "unable to get profile with 'inexist'") |
||
282 | |||
283 | // The get_credentials_error profile is invalid |
||
284 | provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("get_credentials_error").Build() |
||
285 | assert.Nil(t, err) |
||
286 | _, err = provider.GetCredentials() |
||
287 | assert.Contains(t, err.Error(), "InvalidAccessKeyId.NotFound") |
||
288 | |||
289 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
290 | res = &httputil.Response{ |
||
291 | StatusCode: 200, |
||
292 | Body: []byte(`{"Credentials": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}}`), |
||
293 | } |
||
294 | return |
||
295 | } |
||
296 | provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("ChainableRamRoleArn").Build() |
||
297 | assert.Nil(t, err) |
||
298 | cc, err = provider.GetCredentials() |
||
299 | assert.Nil(t, err) |
||
300 | assert.Equal(t, "akid", cc.AccessKeyId) |
||
301 | assert.Equal(t, "aksecret", cc.AccessKeySecret) |
||
302 | assert.Equal(t, "ststoken", cc.SecurityToken) |
||
303 | assert.Equal(t, "cli_profile/ram_role_arn/ram_role_arn/static_ak", cc.ProviderName) |
||
304 | |||
305 | provider.innerProvider = new(testProvider) |
||
306 | cc, err = provider.GetCredentials() |
||
307 | assert.Nil(t, err) |
||
308 | assert.Equal(t, "test", cc.AccessKeyId) |
||
309 | assert.Equal(t, "test", cc.AccessKeySecret) |
||
310 | assert.Equal(t, "cli_profile/test", cc.ProviderName) |
||
311 | } |
||
312 |