| Conditions | 17 |
| Total Lines | 166 |
| Code Lines | 108 |
| 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:
Complex classes like providers.TestECSRAMRoleCredentialsProvider_getCredentials often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package providers |
||
| 133 | func TestECSRAMRoleCredentialsProvider_getCredentials(t *testing.T) { |
||
| 134 | originHttpDo := httpDo |
||
| 135 | defer func() { httpDo = originHttpDo }() |
||
| 136 | |||
| 137 | p, err := NewECSRAMRoleCredentialsProviderBuilder().Build() |
||
| 138 | assert.Nil(t, err) |
||
| 139 | |||
| 140 | // case 1: server error |
||
| 141 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 142 | err = errors.New("mock server error") |
||
| 143 | return |
||
| 144 | } |
||
| 145 | _, err = p.getCredentials() |
||
| 146 | assert.NotNil(t, err) |
||
| 147 | assert.Equal(t, "get role name failed: mock server error", err.Error()) |
||
| 148 | |||
| 149 | // case 2: get role name ok, get credentials failed with server error |
||
| 150 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 151 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 152 | res = &httputil.Response{ |
||
| 153 | StatusCode: 200, |
||
| 154 | Body: []byte("rolename"), |
||
| 155 | } |
||
| 156 | return |
||
| 157 | } |
||
| 158 | err = errors.New("mock server error") |
||
| 159 | return |
||
| 160 | } |
||
| 161 | |||
| 162 | _, err = p.getCredentials() |
||
| 163 | assert.NotNil(t, err) |
||
| 164 | assert.Equal(t, "refresh Ecs sts token err: mock server error", err.Error()) |
||
| 165 | |||
| 166 | // case 3: 4xx error |
||
| 167 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 168 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 169 | res = &httputil.Response{ |
||
| 170 | StatusCode: 200, |
||
| 171 | Body: []byte("rolename"), |
||
| 172 | } |
||
| 173 | return |
||
| 174 | } |
||
| 175 | |||
| 176 | res = &httputil.Response{ |
||
| 177 | StatusCode: 400, |
||
| 178 | Body: []byte("4xx error"), |
||
| 179 | } |
||
| 180 | return |
||
| 181 | } |
||
| 182 | |||
| 183 | _, err = p.getCredentials() |
||
| 184 | assert.NotNil(t, err) |
||
| 185 | assert.Equal(t, "refresh Ecs sts token err, httpStatus: 400, message = 4xx error", err.Error()) |
||
| 186 | |||
| 187 | // case 4: invalid json |
||
| 188 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 189 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 190 | res = &httputil.Response{ |
||
| 191 | StatusCode: 200, |
||
| 192 | Body: []byte("rolename"), |
||
| 193 | } |
||
| 194 | return |
||
| 195 | } |
||
| 196 | |||
| 197 | res = &httputil.Response{ |
||
| 198 | StatusCode: 200, |
||
| 199 | Body: []byte("invalid json"), |
||
| 200 | } |
||
| 201 | return |
||
| 202 | } |
||
| 203 | |||
| 204 | _, err = p.getCredentials() |
||
| 205 | assert.NotNil(t, err) |
||
| 206 | assert.Equal(t, "refresh Ecs sts token err, json.Unmarshal fail: invalid character 'i' looking for beginning of value", err.Error()) |
||
| 207 | |||
| 208 | // case 5: empty response json |
||
| 209 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 210 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 211 | res = &httputil.Response{ |
||
| 212 | StatusCode: 200, |
||
| 213 | Body: []byte("rolename"), |
||
| 214 | } |
||
| 215 | return |
||
| 216 | } |
||
| 217 | |||
| 218 | res = &httputil.Response{ |
||
| 219 | StatusCode: 200, |
||
| 220 | Body: []byte("null"), |
||
| 221 | } |
||
| 222 | return |
||
| 223 | } |
||
| 224 | |||
| 225 | _, err = p.getCredentials() |
||
| 226 | assert.NotNil(t, err) |
||
| 227 | assert.Equal(t, "refresh Ecs sts token err, fail to get credentials", err.Error()) |
||
| 228 | |||
| 229 | // case 6: empty session ak response json |
||
| 230 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 231 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 232 | res = &httputil.Response{ |
||
| 233 | StatusCode: 200, |
||
| 234 | Body: []byte("rolename"), |
||
| 235 | } |
||
| 236 | return |
||
| 237 | } |
||
| 238 | |||
| 239 | res = &httputil.Response{ |
||
| 240 | StatusCode: 200, |
||
| 241 | Body: []byte("{}"), |
||
| 242 | } |
||
| 243 | return |
||
| 244 | } |
||
| 245 | _, err = p.getCredentials() |
||
| 246 | assert.NotNil(t, err) |
||
| 247 | assert.Equal(t, "refresh Ecs sts token err, fail to get credentials", err.Error()) |
||
| 248 | |||
| 249 | // case 7: non-success response |
||
| 250 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 251 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 252 | res = &httputil.Response{ |
||
| 253 | StatusCode: 200, |
||
| 254 | Body: []byte("rolename"), |
||
| 255 | } |
||
| 256 | return |
||
| 257 | } |
||
| 258 | |||
| 259 | res = &httputil.Response{ |
||
| 260 | StatusCode: 200, |
||
| 261 | Body: []byte(`{"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token","Code":"Failed"}`), |
||
| 262 | } |
||
| 263 | return |
||
| 264 | } |
||
| 265 | _, err = p.getCredentials() |
||
| 266 | assert.NotNil(t, err) |
||
| 267 | assert.Equal(t, "refresh Ecs sts token err, Code is not Success", err.Error()) |
||
| 268 | |||
| 269 | // case 8: mock ok value |
||
| 270 | httpDo = func(req *httputil.Request) (res *httputil.Response, err error) { |
||
| 271 | if req.Path == "/latest/meta-data/ram/security-credentials/" { |
||
| 272 | res = &httputil.Response{ |
||
| 273 | StatusCode: 200, |
||
| 274 | Body: []byte("rolename"), |
||
| 275 | } |
||
| 276 | return |
||
| 277 | } |
||
| 278 | |||
| 279 | res = &httputil.Response{ |
||
| 280 | StatusCode: 200, |
||
| 281 | Body: []byte(`{"AccessKeyId":"saki","AccessKeySecret":"saks","Expiration":"2021-10-20T04:27:09Z","SecurityToken":"token","Code":"Success"}`), |
||
| 282 | } |
||
| 283 | return |
||
| 284 | } |
||
| 285 | creds, err := p.getCredentials() |
||
| 286 | assert.Nil(t, err) |
||
| 287 | assert.Equal(t, "saki", creds.AccessKeyId) |
||
| 288 | assert.Equal(t, "saks", creds.AccessKeySecret) |
||
| 289 | assert.Equal(t, "token", creds.SecurityToken) |
||
| 290 | assert.Equal(t, "2021-10-20T04:27:09Z", creds.Expiration) |
||
| 291 | |||
| 292 | // needUpdateCredential |
||
| 293 | assert.True(t, p.needUpdateCredential()) |
||
| 294 | p.expirationTimestamp = time.Now().Unix() |
||
| 295 | assert.True(t, p.needUpdateCredential()) |
||
| 296 | |||
| 297 | p.expirationTimestamp = time.Now().Unix() + 300 |
||
| 298 | assert.False(t, p.needUpdateCredential()) |
||
| 299 | } |
||
| 501 |