Skip to content

Commit

Permalink
add sample test
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Jan 8, 2024
1 parent 81047d1 commit d580eb7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/linux_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions app/external/s3_helper_test.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions app/external/s3_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
2 changes: 2 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d580eb7

Please sign in to comment.