diff --git a/.github/workflows/linux_test.yml b/.github/workflows/linux_test.yml index b97d8db..6b79af0 100644 --- a/.github/workflows/linux_test.yml +++ b/.github/workflows/linux_test.yml @@ -39,7 +39,21 @@ jobs: - name: Download dependencies run: go mod download + - name: Set up AWS credentials + run: | + echo "[default]" > $HOME/.aws/credentials + echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> $HOME/.aws/credentials + echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> $HOME/.aws/credentials + + - name: Set up AWS config + run: | + echo "[default]" > $HOME/.aws/config + echo "region=us-east-1" >> $HOME/.aws/config + echo "output=json" >> $HOME/.aws/config + echo "endpoint_url=http://localhost:4566" >> $HOME/.aws/config + - uses: nao1215/actions-hottest@v1 with: args: '-cover -coverpkg=./... -coverprofile=coverage.out ./...' + - uses: k1LoW/octocov-action@v0 diff --git a/app/external/s3_helper_test.go b/app/external/s3_helper_test.go new file mode 100644 index 0000000..08d77ae --- /dev/null +++ b/app/external/s3_helper_test.go @@ -0,0 +1,53 @@ +package external + +import ( + "context" + "testing" + + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/nao1215/rainbow/app/domain/model" +) + +func s3client(t *testing.T) *s3.Client { + t.Helper() + config, err := model.NewAWSConfig(context.Background(), model.NewAWSProfile(""), model.RegionAPNortheast1) + if err != nil { + t.Fatal(err) + } + client, err := NewS3Client(config) + if err != nil { + t.Fatal(err) + } + return client +} + +func deleteAllS3BucketDelete(t *testing.T, client *s3.Client) { + t.Helper() + + buckets, err := client.ListBuckets(context.Background(), &s3.ListBucketsInput{}) + if err != nil { + t.Fatal(err) + } + + for _, bucket := range buckets.Buckets { + if _, err := client.DeleteBucket(context.Background(), &s3.DeleteBucketInput{Bucket: bucket.Name}); err != nil { + t.Fatal(err) + } + } +} + +func existS3Bucket(t *testing.T, client *s3.Client, bucket model.Bucket) bool { + t.Helper() + + buckets, err := client.ListBuckets(context.Background(), &s3.ListBucketsInput{}) + if err != nil { + t.Fatal(err) + } + + for _, b := range buckets.Buckets { + if *b.Name == bucket.String() { + return true + } + } + return false +} diff --git a/app/external/s3_test.go b/app/external/s3_test.go new file mode 100644 index 0000000..8749df6 --- /dev/null +++ b/app/external/s3_test.go @@ -0,0 +1,38 @@ +// Package external implements the external service. +package external + +import ( + "context" + "testing" + + "github.com/nao1215/rainbow/app/domain/model" + "github.com/nao1215/rainbow/app/domain/service" +) + +func TestS3BucketCreator_CreateS3Bucket(t *testing.T) { + deleteAllS3BucketDelete(t, s3client(t)) + + t.Run("success to create bucket", func(t *testing.T) { + config, err := model.NewAWSConfig(context.Background(), model.NewAWSProfile(""), model.RegionAPNortheast1) + if err != nil { + t.Fatal(err) + } + client, err := NewS3Client(config) + if err != nil { + t.Fatal(err) + } + + s3BucketCreator := NewS3BucketCreator(client) + input := &service.S3BucketCreatorInput{ + Bucket: model.Bucket("test-bucket"), + Region: model.RegionAPNortheast1, + } + if _, err = s3BucketCreator.CreateS3Bucket(context.Background(), input); err != nil { + t.Error(err) + } + + if !existS3Bucket(t, client, input.Bucket) { + t.Errorf("%s bucket does not exist", input.Bucket) + } + }) +} diff --git a/compose.yml b/compose.yml index 40286d1..0ac6dd4 100644 --- a/compose.yml +++ b/compose.yml @@ -11,6 +11,8 @@ services: - DOCKER_HOST=unix:///var/run/docker.sock - DNS_NAME_PATTERNS_TO_RESOLVE_UPSTREAM='.*cloudfront\.net' - CLOUDFRONT_STATIC_PORTS= 1 + - AWS_ACCESS_KEY_ID=test + - AWS_SECRET_ACCESS_KEY=test volumes: - "${LOCALSTACK_VOLUME_DIR:-./localstack}:/var/lib/localstack" - "/var/run/docker.sock:/var/run/docker.sock"