| Conditions | 6 |
| Total Lines | 55 |
| Code Lines | 43 |
| 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 |
||
| 103 | func TestDefaultCredentialsProvider_GetCredentials(t *testing.T) { |
||
| 104 | rollback := utils.Memory("ALIBABA_CLOUD_ACCESS_KEY_ID", |
||
| 105 | "ALIBABA_CLOUD_ACCESS_KEY_SECRET", |
||
| 106 | "ALIBABA_CLOUD_SECURITY_TOKEN", |
||
| 107 | "ALIBABA_CLOUD_ECS_METADATA_DISABLED", |
||
| 108 | "ALIBABA_CLOUD_PROFILE") |
||
| 109 | |||
| 110 | defer func() { |
||
| 111 | getHomePath = utils.GetHomePath |
||
| 112 | rollback() |
||
| 113 | }() |
||
| 114 | originHttpDo := httpDo |
||
| 115 | defer func() { httpDo = originHttpDo }() |
||
| 116 | |||
| 117 | // testcase: empty home |
||
| 118 | getHomePath = func() string { |
||
| 119 | return "" |
||
| 120 | } |
||
| 121 | |||
| 122 | os.Setenv("ALIBABA_CLOUD_ECS_METADATA_DISABLED", "true") |
||
| 123 | provider := NewDefaultCredentialsProvider() |
||
| 124 | assert.Len(t, provider.providerChain, 3) |
||
| 125 | _, err := provider.GetCredentials() |
||
| 126 | assert.EqualError(t, err, "unable to get credentials from any of the providers in the chain: unable to get credentials from enviroment variables, Access key ID must be specified via environment variable (ALIBABA_CLOUD_ACCESS_KEY_ID), cannot found home dir, cannot found home dir") |
||
| 127 | |||
| 128 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "akid") |
||
| 129 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "aksecret") |
||
| 130 | provider = NewDefaultCredentialsProvider() |
||
| 131 | assert.Len(t, provider.providerChain, 3) |
||
| 132 | cc, err := provider.GetCredentials() |
||
| 133 | assert.Nil(t, err) |
||
| 134 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", ProviderName: "default/env"}, cc) |
||
| 135 | // get again |
||
| 136 | cc, err = provider.GetCredentials() |
||
| 137 | assert.Nil(t, err) |
||
| 138 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", ProviderName: "default/env"}, cc) |
||
| 139 | |||
| 140 | getHomePath = func() string { |
||
| 141 | wd, _ := os.Getwd() |
||
| 142 | return path.Join(wd, "fixtures") |
||
| 143 | } |
||
| 144 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "") |
||
| 145 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "") |
||
| 146 | os.Setenv("ALIBABA_CLOUD_PROFILE", "ChainableRamRoleArn") |
||
| 147 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 148 | res = &httputil.Response{ |
||
| 149 | StatusCode: 200, |
||
| 150 | Body: []byte(`{"Credentials": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}}`), |
||
| 151 | } |
||
| 152 | return |
||
| 153 | } |
||
| 154 | provider = NewDefaultCredentialsProvider() |
||
| 155 | cc, err = provider.GetCredentials() |
||
| 156 | assert.Nil(t, err) |
||
| 157 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "ststoken", ProviderName: "default/cli_profile/ram_role_arn/ram_role_arn/static_ak"}, cc) |
||
| 158 | } |
||
| 159 |