| Conditions | 20 |
| Total Lines | 125 |
| Code Lines | 93 |
| 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 credentials.Test_EcsRAmRoleCredentialEnableIMDSv2 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 credentials |
||
| 140 | func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) { |
||
| 141 | auth := newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", false, 0, 0.5, nil) |
||
| 142 | origTestHookDo := hookDo |
||
| 143 | defer func() { hookDo = origTestHookDo }() |
||
| 144 | |||
| 145 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 146 | return func(req *http.Request) (*http.Response, error) { |
||
| 147 | return mockResponse(300, ``, errors.New("sdk test")) |
||
| 148 | } |
||
| 149 | } |
||
| 150 | accesskeyId, err := auth.GetAccessKeyId() |
||
| 151 | assert.NotNil(t, err) |
||
| 152 | assert.Equal(t, "refresh Ecs sts token err: sdk test", err.Error()) |
||
| 153 | assert.Equal(t, "", *accesskeyId) |
||
| 154 | |||
| 155 | auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 0, 0.5, nil) |
||
| 156 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 157 | assert.NotNil(t, err) |
||
| 158 | assert.Equal(t, "Failed to get token from ECS Metadata Service: sdk test", err.Error()) |
||
| 159 | assert.Equal(t, "", *accesskeyId) |
||
| 160 | |||
| 161 | auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 180, 0.5, nil) |
||
| 162 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 163 | assert.NotNil(t, err) |
||
| 164 | assert.Equal(t, "Failed to get token from ECS Metadata Service: sdk test", err.Error()) |
||
| 165 | assert.Equal(t, "", *accesskeyId) |
||
| 166 | |||
| 167 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 168 | return func(req *http.Request) (*http.Response, error) { |
||
| 169 | return mockResponse(300, ``, nil) |
||
| 170 | } |
||
| 171 | } |
||
| 172 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 173 | assert.NotNil(t, err) |
||
| 174 | assert.Equal(t, "Failed to get token from ECS Metadata Service: httpStatus: 300, message = ", err.Error()) |
||
| 175 | assert.Equal(t, "", *accesskeyId) |
||
| 176 | |||
| 177 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 178 | return func(req *http.Request) (*http.Response, error) { |
||
| 179 | return mockResponse(400, `role`, nil) |
||
| 180 | } |
||
| 181 | } |
||
| 182 | auth.RoleName = "" |
||
| 183 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 184 | assert.NotNil(t, err) |
||
| 185 | assert.Equal(t, "refresh Ecs sts token err: httpStatus: 400, message = role", err.Error()) |
||
| 186 | |||
| 187 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 188 | return func(req *http.Request) (*http.Response, error) { |
||
| 189 | return mockResponse(200, `role`, nil) |
||
| 190 | } |
||
| 191 | } |
||
| 192 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 193 | assert.NotNil(t, err) |
||
| 194 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character 'r' looking for beginning of value", err.Error()) |
||
| 195 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 196 | return func(req *http.Request) (*http.Response, error) { |
||
| 197 | return mockResponse(200, `"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration"`, nil) |
||
| 198 | } |
||
| 199 | } |
||
| 200 | auth.RoleName = "role" |
||
| 201 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 202 | assert.NotNil(t, err) |
||
| 203 | assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character ':' after top-level value", err.Error()) |
||
| 204 | assert.Equal(t, "", *accesskeyId) |
||
| 205 | |||
| 206 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 207 | return func(req *http.Request) (*http.Response, error) { |
||
| 208 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"fail"}`, nil) |
||
| 209 | } |
||
| 210 | } |
||
| 211 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 212 | assert.NotNil(t, err) |
||
| 213 | assert.Equal(t, "refresh Ecs sts token err: Code is not Success", err.Error()) |
||
| 214 | assert.Equal(t, "", *accesskeyId) |
||
| 215 | |||
| 216 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 217 | return func(req *http.Request) (*http.Response, error) { |
||
| 218 | return mockResponse(200, `{"AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"expiration","Code":"Success"}`, nil) |
||
| 219 | } |
||
| 220 | } |
||
| 221 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 222 | assert.NotNil(t, err) |
||
| 223 | assert.Equal(t, "refresh Ecs sts token err: AccessKeyId: , AccessKeySecret: accessKeySecret, SecurityToken: securitytoken, Expiration: expiration", err.Error()) |
||
| 224 | assert.Equal(t, "", *accesskeyId) |
||
| 225 | |||
| 226 | hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) { |
||
| 227 | return func(req *http.Request) (*http.Response, error) { |
||
| 228 | return mockResponse(200, `{"AccessKeyId":"accessKeyId","AccessKeySecret":"accessKeySecret","SecurityToken":"securitytoken","Expiration":"2018-01-02T15:04:05Z","Code":"Success"}`, nil) |
||
| 229 | } |
||
| 230 | } |
||
| 231 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 232 | assert.Nil(t, err) |
||
| 233 | assert.Equal(t, "accessKeyId", *accesskeyId) |
||
| 234 | |||
| 235 | accesskeySecret, err := auth.GetAccessKeySecret() |
||
| 236 | assert.Nil(t, err) |
||
| 237 | assert.Equal(t, "accessKeySecret", *accesskeySecret) |
||
| 238 | |||
| 239 | ststoken, err := auth.GetSecurityToken() |
||
| 240 | assert.Nil(t, err) |
||
| 241 | assert.Equal(t, "securitytoken", *ststoken) |
||
| 242 | |||
| 243 | err = errors.New("credentials") |
||
| 244 | err = hookParse(err) |
||
| 245 | assert.Equal(t, "credentials", err.Error()) |
||
| 246 | |||
| 247 | cred, err := auth.GetCredential() |
||
| 248 | assert.Nil(t, err) |
||
| 249 | assert.Equal(t, "accessKeyId", *cred.AccessKeyId) |
||
| 250 | assert.Equal(t, "accessKeySecret", *cred.AccessKeySecret) |
||
| 251 | assert.Equal(t, "securitytoken", *cred.SecurityToken) |
||
| 252 | assert.Nil(t, cred.BearerToken) |
||
| 253 | assert.Equal(t, "ecs_ram_role", *cred.Type) |
||
| 254 | |||
| 255 | originHookParse := hookParse |
||
| 256 | hookParse = func(err error) error { |
||
| 257 | return errors.New("error parse") |
||
| 258 | } |
||
| 259 | defer func() { |
||
| 260 | hookParse = originHookParse |
||
| 261 | }() |
||
| 262 | accesskeyId, err = auth.GetAccessKeyId() |
||
| 263 | assert.Equal(t, "refresh Ecs sts token err: error parse", err.Error()) |
||
| 264 | assert.Equal(t, "", *accesskeyId) |
||
| 265 | } |
||
| 266 |