From 1e8c91d9a40356b1a8858cdc36c767bbcc4baf4f Mon Sep 17 00:00:00 2001 From: Naohiro CHIKAMATSU Date: Sun, 7 Jan 2024 12:42:23 +0900 Subject: [PATCH 1/2] Add unit test for cloudfront interactor --- app/external/mock/cloudfront.go | 23 ++++++ app/interactor/cloudfront.go | 4 + app/interactor/cloudfront_test.go | 127 ++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 app/external/mock/cloudfront.go create mode 100644 app/interactor/cloudfront_test.go diff --git a/app/external/mock/cloudfront.go b/app/external/mock/cloudfront.go new file mode 100644 index 0000000..f09eed9 --- /dev/null +++ b/app/external/mock/cloudfront.go @@ -0,0 +1,23 @@ +package mock + +import ( + "context" + + "github.com/nao1215/rainbow/app/domain/service" +) + +// CloudFrontCreator is a mock of the CloudFrontCreator interface. +type CloudFrontCreator func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) + +// CreateCloudFront calls the CreateCloudFrontFunc. +func (m CloudFrontCreator) CreateCloudFront(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) { + return m(ctx, input) +} + +// OAICreator is a mock of the OAICreator interface. +type OAICreator func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) + +// CreateOAI calls the CreateOAIFunc. +func (m OAICreator) CreateOAI(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) { + return m(ctx, input) +} diff --git a/app/interactor/cloudfront.go b/app/interactor/cloudfront.go index c47cfbc..ab5f180 100644 --- a/app/interactor/cloudfront.go +++ b/app/interactor/cloudfront.go @@ -40,6 +40,10 @@ func NewCloudFrontCreator(opts *CloudFrontCreatorOptions) *CloudFrontCreator { // CreateCloudFront creates a CDN. func (c *CloudFrontCreator) CreateCloudFront(ctx context.Context, input *usecase.CreateCloudFrontInput) (*usecase.CreateCloudFrontOutput, error) { + if err := input.Bucket.Validate(); err != nil { + return nil, err + } + oaiOutput, err := c.opts.OAICreator.CreateOAI(ctx, &service.OAICreatorInput{}) if err != nil { return nil, err diff --git a/app/interactor/cloudfront_test.go b/app/interactor/cloudfront_test.go new file mode 100644 index 0000000..2807f8f --- /dev/null +++ b/app/interactor/cloudfront_test.go @@ -0,0 +1,127 @@ +package interactor + +import ( + "context" + "errors" + "testing" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/google/go-cmp/cmp" + "github.com/nao1215/rainbow/app/domain/model" + "github.com/nao1215/rainbow/app/domain/service" + "github.com/nao1215/rainbow/app/external/mock" + "github.com/nao1215/rainbow/app/usecase" +) + +func TestCloudFrontCreator_CreateCloudFront(t *testing.T) { + t.Parallel() + + t.Run("success to create CloudFront", func(t *testing.T) { + t.Parallel() + + oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) { + return &service.OAICreatorOutput{ + ID: aws.String("test-oai-id"), + }, nil + }) + + cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) { + want := &service.CloudFrontCreatorInput{ + Bucket: model.Bucket("test-bucket"), + OAIID: aws.String("test-oai-id"), + } + if diff := cmp.Diff(want, input); diff != "" { + t.Errorf("differs: (-want +got)\n%s", diff) + } + return &service.CloudFrontCreatorOutput{ + Domain: model.Domain("test.cloudfront.net"), + }, nil + }) + + creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{ + OAICreator: oaiCreator, + CloudFrontCreator: cloudFrontCreator, + }) + + got, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{ + Bucket: model.Bucket("test-bucket"), + }) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + want := &usecase.CreateCloudFrontOutput{ + Domain: model.Domain("test.cloudfront.net"), + } + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("differs: (-want +got)\n%s", diff) + } + }) + + t.Run("fail to create CloudFront", func(t *testing.T) { + t.Parallel() + + oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) { + return &service.OAICreatorOutput{ + ID: aws.String("test-oai-id"), + }, nil + }) + + cloudFrontCreator := mock.CloudFrontCreator(func(ctx context.Context, input *service.CloudFrontCreatorInput) (*service.CloudFrontCreatorOutput, error) { + want := &service.CloudFrontCreatorInput{ + Bucket: model.Bucket("test-bucket"), + OAIID: aws.String("test-oai-id"), + } + if diff := cmp.Diff(want, input); diff != "" { + t.Errorf("differs: (-want +got)\n%s", diff) + } + return nil, errors.New("some error") + }) + + creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{ + OAICreator: oaiCreator, + CloudFrontCreator: cloudFrontCreator, + }) + + _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{ + Bucket: model.Bucket("test-bucket"), + }) + if err == nil { + t.Error("expected error") + } + }) + + t.Run("fail to create OAI", func(t *testing.T) { + t.Parallel() + + oaiCreator := mock.OAICreator(func(ctx context.Context, input *service.OAICreatorInput) (*service.OAICreatorOutput, error) { + return nil, errors.New("some error") + }) + + creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{ + OAICreator: oaiCreator, + CloudFrontCreator: nil, + }) + + if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{ + Bucket: model.Bucket("test-bucket"), + }); err == nil { + t.Error("expected error") + } + }) + + t.Run("fail to validate s3 bucket name", func(t *testing.T) { + t.Parallel() + + creator := NewCloudFrontCreator(&CloudFrontCreatorOptions{ + OAICreator: nil, + CloudFrontCreator: nil, + }) + + if _, err := creator.CreateCloudFront(context.Background(), &usecase.CreateCloudFrontInput{ + Bucket: model.Bucket(""), + }); err == nil { + t.Error("expected error") + } + }) +} From b58235d134427c1c1486abb23b5c65760dc90994 Mon Sep 17 00:00:00 2001 From: Naohiro CHIKAMATSU Date: Sun, 7 Jan 2024 12:44:19 +0900 Subject: [PATCH 2/2] Update --- .github/pull_request_template.md | 9 -- doc/img/cover.svg | 158 ++++++++++++++++++------------- 2 files changed, 92 insertions(+), 75 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 1117942..5087d32 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,12 +1,3 @@ ---- -name: Pull Request -about: Create a pull request to help us improve -title: "[Pull Request] XXX" -labels: "" -assignees: '' - ---- - ## Summary [Provide a brief overview and purpose of the PR] diff --git a/doc/img/cover.svg b/doc/img/cover.svg index 50a6735..50fe3ad 100644 --- a/doc/img/cover.svg +++ b/doc/img/cover.svg @@ -7,7 +7,7 @@ > - + - + - + cmd/subcmd @@ -46,12 +46,12 @@ - + config/spare/config.go @@ -59,12 +59,12 @@ - + ui @@ -72,12 +72,12 @@ - + utils @@ -85,12 +85,12 @@ - + version/version.go @@ -98,12 +98,12 @@ - + di/wire_gen.go @@ -111,7 +111,7 @@ - + - + external @@ -137,25 +137,25 @@ - + interactor - + common.go @@ -163,12 +163,12 @@ - + s3hub @@ -176,12 +176,12 @@ - + common.go @@ -189,12 +189,12 @@ - + s3hub.go @@ -202,18 +202,18 @@ - + - + file/file.go @@ -221,18 +221,18 @@ - + - + aws.go @@ -240,12 +240,12 @@ - + domain.go @@ -253,7 +253,7 @@ - + - + - + cloudfront.go @@ -285,25 +285,25 @@ - + mock/s3.go + data-math="N">mock - + s3.go @@ -311,12 +311,12 @@ - + s3_policy.go @@ -324,12 +324,12 @@ - + s3_retryer.go @@ -337,18 +337,25 @@ - + +cloudfront.go + - + s3.go @@ -356,12 +363,12 @@ - + common.go @@ -369,12 +376,12 @@ - + cp.go @@ -382,18 +389,18 @@ - + - + ls.go @@ -401,12 +408,12 @@ - + mb.go @@ -414,12 +421,12 @@ - + rm.go @@ -427,12 +434,12 @@ - + root.go @@ -440,8 +447,27 @@ - + + + + + + + + + + + + + +s3.go + \ No newline at end of file