-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters