Skip to content

Commit

Permalink
fix(dockertestx): Use temporary environment (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sawadashota authored May 14, 2021
1 parent d49a919 commit baf72bf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
36 changes: 32 additions & 4 deletions dockertestx/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dockertestx
import (
"context"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
Expand All @@ -27,10 +28,21 @@ func NewDynamoDB() (string, PurgeFunc, error) {
return "", nil, err
}

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedCredentialsFiles([]string{"stub/aws_credentials.txt"}),
)
{
clean, err := temporaryEnv("AWS_ACCESS_KEY_ID", "dummy")
if err != nil {
return "", nil, err
}
defer clean()
}
{
clean, err := temporaryEnv("AWS_SECRET_ACCESS_KEY", "dummy")
if err != nil {
return "", nil, err
}
defer clean()
}
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
return "", nil, err
}
Expand Down Expand Up @@ -58,3 +70,19 @@ func NewDynamoDB() (string, PurgeFunc, error) {

return endpoint, purgeFunc, nil
}

func temporaryEnv(key, value string) (func(), error) {
v, ok := os.LookupEnv(key)
if err := os.Setenv(key, value); err != nil {
return nil, err
}

if ok {
return func() {
_ = os.Setenv(key, v)
}, nil
}
return func() {
_ = os.Unsetenv(key)
}, nil
}
2 changes: 1 addition & 1 deletion dockertestx/dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestNewDynamoDB(t *testing.T) {

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedCredentialsFiles([]string{"stub/aws_credentials.txt"}),
config.WithSharedCredentialsFiles([]string{"testdata/aws_credentials.txt"}),
)
require.NoError(t, err)

Expand Down
File renamed without changes.

0 comments on commit baf72bf

Please sign in to comment.