open-vault/helper/awsutil/error_test.go
Clint 5882156f53
Translate AWS Rate limiting errors to 502 errors (#5270)
* Initial implemntation of returning 529 for rate limits

- bump aws iam and sts packages to v1.14.31 to get mocking interface
- promote the iam and sts clients to the aws backend struct, for mocking in tests
- this also promotes some functions to methods on the Backend struct, so
  that we can use the injected client

Generating creds requires reading config/root for credentials to contact
IAM. Here we make pathConfigRoot a method on aws/backend so we can clear
the clients on successful update of config/root path. Adds a mutex to
safely clear the clients

* refactor locking and unlocking into methods on *backend

* refactor/simply the locking

* check client after grabbing lock
2018-09-18 15:26:06 -05:00

90 lines
2 KiB
Go

package awsutil
import (
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws/awserr"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/logical"
)
func Test_CheckAWSError(t *testing.T) {
testCases := []struct {
Name string
Err error
Expected error
}{
{
Name: "Something not checked",
Err: fmt.Errorf("something"),
},
{
Name: "Upstream throttle error",
Err: awserr.New("Throttling", "", nil),
Expected: logical.ErrUpstreamRateLimited,
},
{
Name: "Upstream RequestLimitExceeded",
Err: awserr.New("RequestLimitExceeded", "Request rate limited", nil),
Expected: logical.ErrUpstreamRateLimited,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := CheckAWSError(tc.Err)
if err == nil && tc.Expected != nil {
t.Fatalf("expected non-nil error (%#v), got nil", tc.Expected)
}
if err != nil && tc.Expected == nil {
t.Fatalf("expected nil error, got (%#v)", err)
}
if err != tc.Expected {
t.Fatalf("expected error (%#v), got (%#v)", tc.Expected, err)
}
})
}
}
func Test_AppendLogicalError(t *testing.T) {
awsErr := awserr.New("Throttling", "", nil)
testCases := []struct {
Name string
Err error
Expected error
}{
{
Name: "Something not checked",
Err: fmt.Errorf("something"),
Expected: fmt.Errorf("something"),
},
{
Name: "Upstream throttle error",
Err: awsErr,
Expected: multierror.Append(awsErr, logical.ErrUpstreamRateLimited),
},
{
Name: "Nil",
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
err := AppendLogicalError(tc.Err)
if err == nil && tc.Expected != nil {
t.Fatalf("expected non-nil error (%#v), got nil", tc.Expected)
}
if err != nil && tc.Expected == nil {
t.Fatalf("expected nil error, got (%#v)", err)
}
if err == nil && tc.Expected == nil {
return
}
if err.Error() != tc.Expected.Error() {
t.Fatalf("expected error (%#v), got (%#v)", tc.Expected.Error(), err.Error())
}
})
}
}