Skip to content

Commit c4d0ab0

Browse files
committed
Add tests for mocks and to show lack of retries
1 parent fb34edb commit c4d0ab0

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

aws_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func GetAwsConfig(ctx context.Context, c *Config) (context.Context, aws.Config,
9696

9797
if !c.SkipCredsValidation {
9898
if _, _, err := getAccountIDAndPartitionFromSTSGetCallerIdentity(baseCtx, stsClient(baseCtx, awsConfig, c)); err != nil {
99-
return ctx, awsConfig, fmt.Errorf("error validating provider credentials: %w", err)
99+
return ctx, awsConfig, fmt.Errorf("validating provider credentials: %w", err)
100100
}
101101
}
102102

aws_config_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/aws/aws-sdk-go-v2/config"
2323
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
2424
"github.com/aws/aws-sdk-go-v2/service/sts"
25+
"github.com/aws/smithy-go"
2526
"github.com/aws/smithy-go/middleware"
2627
smithyhttp "github.com/aws/smithy-go/transport/http"
2728
"github.com/google/go-cmp/cmp"
@@ -94,6 +95,52 @@ func TestGetAwsConfig(t *testing.T) {
9495
servicemocks.MockStsGetCallerIdentityValidEndpoint,
9596
},
9697
},
98+
{
99+
Config: &Config{
100+
AccessKey: servicemocks.MockStaticAccessKey,
101+
Region: "us-east-1",
102+
SecretKey: servicemocks.MockStaticSecretKey,
103+
MaxRetries: 100,
104+
},
105+
Description: "ExpiredToken",
106+
ExpectedRegion: "us-east-1",
107+
ExpectedError: func(err error) bool {
108+
return strings.Contains(err.Error(), "ExpiredToken")
109+
},
110+
MockStsEndpoints: []*servicemocks.MockEndpoint{
111+
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredToken,
112+
},
113+
},
114+
{
115+
Config: &Config{
116+
AccessKey: servicemocks.MockStaticAccessKey,
117+
Region: "us-east-1",
118+
SecretKey: servicemocks.MockStaticSecretKey,
119+
},
120+
Description: "ExpiredTokenException",
121+
ExpectedRegion: "us-east-1",
122+
ExpectedError: func(err error) bool {
123+
return strings.Contains(err.Error(), "ExpiredTokenException")
124+
},
125+
MockStsEndpoints: []*servicemocks.MockEndpoint{
126+
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredTokenException,
127+
},
128+
},
129+
{
130+
Config: &Config{
131+
AccessKey: servicemocks.MockStaticAccessKey,
132+
Region: "us-east-1",
133+
SecretKey: servicemocks.MockStaticSecretKey,
134+
},
135+
Description: "RequestExpired",
136+
ExpectedRegion: "us-east-1",
137+
ExpectedError: func(err error) bool {
138+
return strings.Contains(err.Error(), "RequestExpired")
139+
},
140+
MockStsEndpoints: []*servicemocks.MockEndpoint{
141+
servicemocks.MockStsGetCallerIdentityInvalidBodyRequestExpired,
142+
},
143+
},
97144
{
98145
Config: &Config{
99146
AccessKey: servicemocks.MockStaticAccessKey,
@@ -3043,6 +3090,76 @@ func TestRetryHandlers(t *testing.T) {
30433090
return results
30443091
}(),
30453092
},
3093+
"no retries for ExpiredToken": {
3094+
NextHandler: func() middleware.FinalizeHandler {
3095+
num := 0
3096+
reqsErrs := make([]error, 2)
3097+
for i := 0; i < 2; i++ {
3098+
reqsErrs[i] = &smithy.OperationError{
3099+
ServiceID: "STS",
3100+
OperationName: "GetCallerIdentity",
3101+
Err: &smithyhttp.ResponseError{
3102+
Response: &smithyhttp.Response{
3103+
Response: &http.Response{
3104+
StatusCode: 403,
3105+
},
3106+
},
3107+
Err: &smithy.GenericAPIError{
3108+
Code: "ExpiredToken",
3109+
Message: "The security token included in the request is expired",
3110+
},
3111+
},
3112+
}
3113+
}
3114+
return middleware.FinalizeHandlerFunc(func(ctx context.Context, in middleware.FinalizeInput) (out middleware.FinalizeOutput, metadata middleware.Metadata, err error) {
3115+
if num >= len(reqsErrs) {
3116+
err = fmt.Errorf("more requests than expected")
3117+
} else {
3118+
err = reqsErrs[num]
3119+
num++
3120+
}
3121+
return out, metadata, err
3122+
})
3123+
},
3124+
Err: &smithy.OperationError{
3125+
ServiceID: "STS",
3126+
OperationName: "GetCallerIdentity",
3127+
Err: &smithyhttp.ResponseError{
3128+
Response: &smithyhttp.Response{
3129+
Response: &http.Response{
3130+
StatusCode: 403,
3131+
},
3132+
},
3133+
Err: &smithy.GenericAPIError{
3134+
Code: "ExpiredToken",
3135+
Message: "The security token included in the request is expired",
3136+
},
3137+
},
3138+
},
3139+
ExpectResults: func() retry.AttemptResults {
3140+
results := retry.AttemptResults{
3141+
Results: make([]retry.AttemptResult, 1),
3142+
}
3143+
results.Results[0] = retry.AttemptResult{
3144+
Err: &smithy.OperationError{
3145+
ServiceID: "STS",
3146+
OperationName: "GetCallerIdentity",
3147+
Err: &smithyhttp.ResponseError{
3148+
Response: &smithyhttp.Response{
3149+
Response: &http.Response{
3150+
StatusCode: 403,
3151+
},
3152+
},
3153+
Err: &smithy.GenericAPIError{
3154+
Code: "ExpiredToken",
3155+
Message: "The security token included in the request is expired",
3156+
},
3157+
},
3158+
},
3159+
}
3160+
return results
3161+
}(),
3162+
},
30463163
"stops at maxRetries for other network errors": {
30473164
NextHandler: func() middleware.FinalizeHandler {
30483165
num := 0

awsauth_test.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,47 @@ func TestGetAccountIDAndPartitionFromSTSGetCallerIdentity(t *testing.T) {
321321
ErrCount: 1,
322322
},
323323
{
324-
Description: "sts:GetCallerIdentity expired token with invalid response",
324+
Description: "sts:GetCallerIdentity ExpiredToken with invalid JSON response",
325325
MockEndpoints: []*servicemocks.MockEndpoint{
326326
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredToken,
327327
},
328328
ErrCount: 1,
329329
},
330330
{
331-
Description: "sts:GetCallerIdentity expired token with valid response",
331+
Description: "sts:GetCallerIdentity ExpiredToken with valid JSON response",
332332
MockEndpoints: []*servicemocks.MockEndpoint{
333333
servicemocks.MockStsGetCallerIdentityValidBodyExpiredToken,
334334
},
335335
ErrCount: 1,
336336
},
337+
{
338+
Description: "sts:GetCallerIdentity ExpiredTokenException with invalid JSON response",
339+
MockEndpoints: []*servicemocks.MockEndpoint{
340+
servicemocks.MockStsGetCallerIdentityInvalidBodyExpiredTokenException,
341+
},
342+
ErrCount: 1,
343+
},
344+
{
345+
Description: "sts:GetCallerIdentity ExpiredTokenException with valid JSON response",
346+
MockEndpoints: []*servicemocks.MockEndpoint{
347+
servicemocks.MockStsGetCallerIdentityValidBodyExpiredTokenException,
348+
},
349+
ErrCount: 1,
350+
},
351+
{
352+
Description: "sts:GetCallerIdentity RequestExpired with invalid JSON response",
353+
MockEndpoints: []*servicemocks.MockEndpoint{
354+
servicemocks.MockStsGetCallerIdentityInvalidBodyRequestExpired,
355+
},
356+
ErrCount: 1,
357+
},
358+
{
359+
Description: "sts:GetCallerIdentity RequestExpired with valid JSON response",
360+
MockEndpoints: []*servicemocks.MockEndpoint{
361+
servicemocks.MockStsGetCallerIdentityValidBodyRequestExpired,
362+
},
363+
ErrCount: 1,
364+
},
337365
{
338366
Description: "sts:GetCallerIdentity success",
339367
MockEndpoints: []*servicemocks.MockEndpoint{

0 commit comments

Comments
 (0)