Conditions | 6 |
Total Lines | 66 |
Code Lines | 52 |
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 |
||
104 | func TestDefaultCredentialsProvider_GetCredentials(t *testing.T) { |
||
105 | rollback := utils.Memory("ALIBABA_CLOUD_ACCESS_KEY_ID", |
||
106 | "ALIBABA_CLOUD_ACCESS_KEY_SECRET", |
||
107 | "ALIBABA_CLOUD_SECURITY_TOKEN", |
||
108 | "ALIBABA_CLOUD_ECS_METADATA_DISABLED", |
||
109 | "ALIBABA_CLOUD_PROFILE") |
||
110 | |||
111 | defer func() { |
||
112 | getHomePath = utils.GetHomePath |
||
113 | rollback() |
||
114 | }() |
||
115 | originHttpDo := httpDo |
||
116 | defer func() { httpDo = originHttpDo }() |
||
117 | |||
118 | // testcase: empty home |
||
119 | getHomePath = func() string { |
||
120 | return "" |
||
121 | } |
||
122 | |||
123 | os.Setenv("ALIBABA_CLOUD_ECS_METADATA_DISABLED", "true") |
||
124 | provider := NewDefaultCredentialsProvider() |
||
125 | assert.Len(t, provider.providerChain, 3) |
||
126 | _, err := provider.GetCredentials() |
||
127 | 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") |
||
128 | |||
129 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "akid") |
||
130 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "aksecret") |
||
131 | provider = NewDefaultCredentialsProvider() |
||
132 | assert.Len(t, provider.providerChain, 3) |
||
133 | cc, err := provider.GetCredentials() |
||
134 | assert.Nil(t, err) |
||
135 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", ProviderName: "default/env"}, cc) |
||
136 | // get again |
||
137 | cc, err = provider.GetCredentials() |
||
138 | assert.Nil(t, err) |
||
139 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "", ProviderName: "default/env"}, cc) |
||
140 | |||
141 | getHomePath = func() string { |
||
142 | wd, _ := os.Getwd() |
||
143 | return path.Join(wd, "fixtures") |
||
144 | } |
||
145 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_ID", "") |
||
146 | os.Setenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "") |
||
147 | os.Setenv("ALIBABA_CLOUD_PROFILE", "ChainableRamRoleArn") |
||
148 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
149 | res = &httputil.Response{ |
||
150 | StatusCode: 200, |
||
151 | Body: []byte(`{"Credentials": {"AccessKeyId":"akid","AccessKeySecret":"aksecret","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"ststoken"}}`), |
||
152 | } |
||
153 | return |
||
154 | } |
||
155 | provider = NewDefaultCredentialsProvider() |
||
156 | cc, err = provider.GetCredentials() |
||
157 | assert.Nil(t, err) |
||
158 | assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "ststoken", ProviderName: "default/cli_profile/ram_role_arn/ram_role_arn/static_ak"}, cc) |
||
159 | |||
160 | provider.lastUsedProvider = new(testProvider) |
||
161 | cc, err = provider.GetCredentials() |
||
162 | assert.Nil(t, err) |
||
163 | assert.Equal(t, "test", cc.AccessKeyId) |
||
164 | assert.Equal(t, "test", cc.AccessKeySecret) |
||
165 | assert.Equal(t, "default/test", cc.ProviderName) |
||
166 | |||
167 | provider.lastUsedProvider = new(testErrorProvider) |
||
168 | _, err = provider.GetCredentials() |
||
169 | assert.Equal(t, "error", err.Error()) |
||
170 | } |
||
199 |