-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'awsbase.ErrCodeEquals', AWS SDK for Go v2 variants of helper in …
…v2/awsv1shim/tfawserr.
- Loading branch information
Showing
3 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package awsbase | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sts/types" | ||
smithy "github.com/aws/smithy-go" | ||
) | ||
|
||
func TestIsCannotAssumeRoleError(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Err error | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "nil error", | ||
}, | ||
{ | ||
Name: "Top-level NoValidCredentialSourcesError", | ||
Err: NoValidCredentialSourcesError{}, | ||
}, | ||
{ | ||
Name: "Top-level CannotAssumeRoleError", | ||
Err: CannotAssumeRoleError{}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Nested CannotAssumeRoleError", | ||
Err: fmt.Errorf("test: %w", CannotAssumeRoleError{}), | ||
Expected: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(testCase.Name, func(t *testing.T) { | ||
got := IsCannotAssumeRoleError(testCase.Err) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %t, expected %t", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsNoValidCredentialSourcesError(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Err error | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "nil error", | ||
}, | ||
{ | ||
Name: "Top-level CannotAssumeRoleError", | ||
Err: CannotAssumeRoleError{}, | ||
}, | ||
{ | ||
Name: "Top-level NoValidCredentialSourcesError", | ||
Err: NoValidCredentialSourcesError{}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Nested NoValidCredentialSourcesError", | ||
Err: fmt.Errorf("test: %w", NoValidCredentialSourcesError{}), | ||
Expected: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(testCase.Name, func(t *testing.T) { | ||
got := IsNoValidCredentialSourcesError(testCase.Err) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %t, expected %t", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestErrCodeEquals(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Err error | ||
Codes []string | ||
Expected bool | ||
}{ | ||
{ | ||
Name: "nil error", | ||
}, | ||
{ | ||
Name: "Top-level CannotAssumeRoleError", | ||
Err: CannotAssumeRoleError{}, | ||
}, | ||
{ | ||
Name: "Top-level smithy.GenericAPIError matching first code", | ||
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}, | ||
Codes: []string{"TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Top-level smithy.GenericAPIError matching last code", | ||
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}, | ||
Codes: []string{"NotMatching", "TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Top-level smithy.GenericAPIError no code", | ||
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}, | ||
}, | ||
{ | ||
Name: "Top-level smithy.GenericAPIError non-matching codes", | ||
Err: &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}, | ||
Codes: []string{"NotMatching", "AlsoNotMatching"}, | ||
}, | ||
{ | ||
Name: "Wrapped smithy.GenericAPIError matching first code", | ||
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}), | ||
Codes: []string{"TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Wrapped smithy.GenericAPIError matching last code", | ||
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}), | ||
Codes: []string{"NotMatching", "TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Wrapped smithy.GenericAPIError non-matching codes", | ||
Err: fmt.Errorf("test: %w", &smithy.GenericAPIError{Code: "TestCode", Message: "TestMessage"}), | ||
Codes: []string{"NotMatching", "AlsoNotMatching"}, | ||
}, | ||
{ | ||
Name: "Top-level sts ExpiredTokenException matching first code", | ||
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")}, | ||
Codes: []string{"TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Top-level sts ExpiredTokenException matching last code", | ||
Err: &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")}, | ||
Codes: []string{"NotMatching", "TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Wrapped sts ExpiredTokenException matching first code", | ||
Err: fmt.Errorf("test: %w", &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")}), | ||
Codes: []string{"TestCode"}, | ||
Expected: true, | ||
}, | ||
{ | ||
Name: "Wrapped sts ExpiredTokenException matching last code", | ||
Err: fmt.Errorf("test: %w", &types.ExpiredTokenException{ErrorCodeOverride: aws.String("TestCode"), Message: aws.String("TestMessage")}), | ||
Codes: []string{"NotMatching", "TestCode"}, | ||
Expected: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
|
||
t.Run(testCase.Name, func(t *testing.T) { | ||
got := ErrCodeEquals(testCase.Err, testCase.Codes...) | ||
|
||
if got != testCase.Expected { | ||
t.Errorf("got %t, expected %t", got, testCase.Expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package errs | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// IsA indicates whether an error matches an error type. | ||
func IsA[T error](err error) bool { | ||
_, ok := As[T](err) | ||
return ok | ||
} | ||
|
||
// As is equivalent to errors.As(), but returns the value in-line. | ||
func As[T error](err error) (T, bool) { | ||
var as T | ||
ok := errors.As(err, &as) | ||
return as, ok | ||
} |