Issue 615: Add envProxy and tests to s3Reader
parent
973b67f6d5
commit
8fb10e0b3a
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
|
"github.com/cloudskiff/driftctl/pkg/envproxy"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
|
@ -33,9 +34,12 @@ func NewS3Reader(path string) (*S3Backend, error) {
|
||||||
Key: &key,
|
Key: &key,
|
||||||
Bucket: &bucket,
|
Bucket: &bucket,
|
||||||
}
|
}
|
||||||
|
envProxy := envproxy.NewEnvProxy("DCTL_S3_", "AWS_")
|
||||||
|
envProxy.Apply()
|
||||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||||
SharedConfigState: session.SharedConfigEnable,
|
SharedConfigState: session.SharedConfigEnable,
|
||||||
}))
|
}))
|
||||||
|
envProxy.Restore()
|
||||||
backend.S3Client = s3.New(sess)
|
backend.S3Client = s3.New(sess)
|
||||||
return &backend, nil
|
return &backend, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestS3Backend_ReadWithError(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
fakeS3 := &awstest.MockFakeS3{}
|
fakeS3 := &awstest.MockFakeS3{}
|
||||||
|
|
Loading…
Reference in New Issue