Issue 615: Add envProxy and tests to s3Reader

main
Louis TOUSSAINT 2021-07-02 14:48:25 +02:00
parent 973b67f6d5
commit 8fb10e0b3a
2 changed files with 29 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"strings"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/cloudskiff/driftctl/pkg/envproxy"
"github.com/pkg/errors"
"github.com/aws/aws-sdk-go/service/s3"
@ -33,9 +34,12 @@ func NewS3Reader(path string) (*S3Backend, error) {
Key: &key,
Bucket: &bucket,
}
envProxy := envproxy.NewEnvProxy("DCTL_S3_", "AWS_")
envProxy.Apply()
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
envProxy.Restore()
backend.S3Client = s3.New(sess)
return &backend, nil
}

View File

@ -67,6 +67,31 @@ func TestNewS3Reader(t *testing.T) {
)
}
func TestNewS3ReaderWithEnvProxy(t *testing.T) {
assert := assert.New(t)
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
os.Setenv("DCTL_S3_DEFAULT_REGION", "eu-west-3")
reader, err := NewS3Reader("sample_bucket/path/to/state.tfstate")
got := reader.S3Client.(*s3.S3).Config.Region
if aws.StringValue(got) != "eu-west-3" {
t.Errorf("NewS3Reader().S3Client.Config.Region got = %v, want %v", aws.StringValue(got), "eu-west-3")
}
if err != nil {
t.Error(err)
}
assert.Equal(
"path/to/state.tfstate",
*reader.input.Key,
)
assert.Equal(
"sample_bucket",
*reader.input.Bucket,
)
}
func TestS3Backend_ReadWithError(t *testing.T) {
assert := assert.New(t)
fakeS3 := &awstest.MockFakeS3{}