Skip to content

Commit

Permalink
feat(dockertestx): S3 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxnew authored May 31, 2021
1 parent 7333ad4 commit 5741787
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
1 change: 0 additions & 1 deletion dockertestx/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/pkg/errors"
)
Expand Down
59 changes: 59 additions & 0 deletions dockertestx/s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dockertestx

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/ory/dockertest/v3"
"github.com/pkg/errors"
)

const (
S3AWSAccessKeyID = "AKIAIOSFODNN7DUMMY" // #nosec
S3AWSSecretAccessKey = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYDUMMYKEY" // #nosec
)

type S3Factory struct{}

func (f *S3Factory) repository() string {
return "minio/minio"
}

func (f *S3Factory) create(p *Pool, opt ContainerOption) (*state, error) {
rOpt := &dockertest.RunOptions{
Name: opt.Name,
Repository: f.repository(),
Tag: opt.Tag,
Env: []string{
fmt.Sprintf("MINIO_ACCESS_KEY=%s", S3AWSAccessKeyID),
fmt.Sprintf("MINIO_SECRET_KEY=%s", S3AWSSecretAccessKey),
},
Cmd: []string{"server", "/data"},
}
resource, err := p.Pool.RunWithOptions(rOpt)
if err != nil {
return nil, errors.WithMessage(err, "Could not start resource")
}
return &state{
ContainerName: opt.Name,
Repository: f.repository(),
Tag: opt.Tag,
Env: rOpt.Env,
DSN: fmt.Sprintf("http://localhost:%s", resource.GetPort("9000/tcp")),
r: resource,
}, nil
}

func (f *S3Factory) ready(p *Pool, s *state) error {
return p.Pool.Retry(func() error {
cl := s3.New(s3.Options{
Credentials: credentials.NewStaticCredentialsProvider(S3AWSAccessKeyID, S3AWSSecretAccessKey, ""),
EndpointResolver: s3.EndpointResolverFromURL(s.DSN),
})
_, err := cl.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
fmt.Printf("%v\n", err)
return err
})
}
37 changes: 37 additions & 0 deletions dockertestx/s3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dockertestx_test

import (
"context"
"testing"

"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tier4/x-go/dockertestx"
)

func TestPool_NewS3(t *testing.T) {
t.Parallel()

p, err := dockertestx.New(dockertestx.PoolOption{})
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, p.Purge())
})

endpoint, err := p.NewResource(new(dockertestx.S3Factory), dockertestx.ContainerOption{})
require.NoError(t, err)

cl := s3.New(s3.Options{
Credentials: credentials.NewStaticCredentialsProvider(dockertestx.S3AWSAccessKeyID, dockertestx.S3AWSSecretAccessKey, ""),
EndpointResolver: s3.EndpointResolverFromURL(endpoint),
UsePathStyle: true,
})
_, err = cl.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: aws.String("test"),
})
assert.NoError(t, err)
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ go 1.16
require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/aws/aws-sdk-go v1.38.40
github.com/aws/aws-sdk-go-v2 v1.5.0
github.com/aws/aws-sdk-go-v2 v1.6.0
github.com/aws/aws-sdk-go-v2/config v1.2.0
github.com/aws/aws-sdk-go-v2/credentials v1.2.0
github.com/aws/aws-sdk-go-v2/internal/ini v1.0.0 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.3.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0
github.com/containerd/continuity v0.1.0 // indirect
github.com/fatih/color v1.11.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,28 @@ github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
github.com/aws/aws-sdk-go v1.38.40 h1:VVqBFV24tGgXR11tFXPjmR+0ItbnUepbuQjdmhgu3U0=
github.com/aws/aws-sdk-go v1.38.40/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/aws/aws-sdk-go-v2 v1.5.0 h1:o0TprBiEkqtMGD2Ira1VCq3zwWej256zHLSRcd+cxUA=
github.com/aws/aws-sdk-go-v2 v1.5.0/go.mod h1:tI4KhsR5VkzlUa2DZAdwx7wCAYGwkZZ1H31PYrBFx1w=
github.com/aws/aws-sdk-go-v2 v1.6.0 h1:r20hdhm8wZmKkClREfacXrKfX0Y7/s0aOoeraFbf/sY=
github.com/aws/aws-sdk-go-v2 v1.6.0/go.mod h1:tI4KhsR5VkzlUa2DZAdwx7wCAYGwkZZ1H31PYrBFx1w=
github.com/aws/aws-sdk-go-v2/config v1.2.0 h1:3JVWs+ilru3/5Zq6KbuQj8aqp7DW+Uw/wv6gCYZ2UnI=
github.com/aws/aws-sdk-go-v2/config v1.2.0/go.mod h1:JgPbg7YzzczkGu1Zi0hHVKYXVzx4OTKnNSD+h+qlpLw=
github.com/aws/aws-sdk-go-v2/credentials v1.2.0 h1:NxD//04/Y4nid+Slj8JjouisY/DAYjjXW4lqWNkBaO8=
github.com/aws/aws-sdk-go-v2/credentials v1.2.0/go.mod h1:3Xxgc7WsldLnLnPSRcNOT5eVRgb55Kkgp8mE5kAmLrU=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.1.0 h1:EVNLR3OULDnvp92ISADQwZwVsdz7dasl1MCneUVJQnQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.1.0/go.mod h1:GOKx1449nzMoUdTKrP41RsPn1hogOaxb+5MaoOiZgqc=
github.com/aws/aws-sdk-go-v2/internal/ini v1.0.0 h1:k7I9E6tyVWBo7H9ffpnxDWudtjau6Qt9rnOYgV+ciEQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.0.0/go.mod h1:g3XMXuxvqSMUjnsXXp/960152w0wFS4CXVYgQaSVOHE=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.3.0 h1:h23KXu//Os2NZPEYGcT4NBg62QUXmnKew0Pt89PQdc4=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.3.0/go.mod h1:qekkKtOUKslcQfTy5eIdgehZxSCiKeEDvwN2yZIrshE=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.1.0 h1:XwqxIO9LtNXznBbEMNGumtLN60k4nVqDpVwVWx3XU/o=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.1.0/go.mod h1:zdjOOy0ojUn3iNELo6ycIHSMCp4xUbycSHfb8PnbbyM=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.0 h1:i+cyzgQbk02N3pbwBTwjOChoDuIxsGdE6ucD2ZLVnJQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.0/go.mod h1:mruB7K2oMCoU0WhUeTV1CxpqoP7Q0N1uo5TXH4r2dZA=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.1 h1:l7pDLsmOGrnR8LT+3gIv8NlHpUhs7220E457KEC2UM0=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.1/go.mod h1:2+ehJPkdIdl46VCj67Emz/EH2hpebHZtaLdzqg+sWOI=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.3.1 h1:VH1Y4k+IZ5kcRVqSNw7eAkXyfS7k2/ibKjrNtbhYhV4=
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.3.1/go.mod h1:IpjxfORBAFfkMM0VEx5gPPnEy6WV4Hk0F/+zb/SUWyw=
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0 h1:FZ5UL5aiybSJKiJglPT7YMMwc431IgOX5gvlFAzSjzs=
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0/go.mod h1:zHCjYoODbYRLz/iFicYswq1gRoxBnHvpY5h2Vg3/tJ4=
github.com/aws/aws-sdk-go-v2/service/sso v1.2.0 h1:6cTVa8anc914VgJzca8Jd0ewA7Y5fbEFheXqidum0tg=
github.com/aws/aws-sdk-go-v2/service/sso v1.2.0/go.mod h1:5qnaL4AtNElFr+a5mdkvD+89jGwpTVyWWX5W/eLzmes=
github.com/aws/aws-sdk-go-v2/service/sts v1.4.0 h1:pcnLXm4eMWUgqfbjsRyeSz90CKunJUmre+trKJx/FAk=
Expand Down

0 comments on commit 5741787

Please sign in to comment.