Skip to content

Commit

Permalink
Add unit test for cloudformation usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Jan 24, 2024
1 parent 718fbd2 commit c86d92f
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 64 deletions.
5 changes: 5 additions & 0 deletions app/domain/model/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const (
// StackStatus is the status of a CloudFormation stack.
type StackStatus string

// String returns the string representation of StackStatus.
func (s StackStatus) String() string {
return string(s)
}

// CloudFormation stack status constants
// Ref. https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-describing-stacks.html
const (
Expand Down
15 changes: 15 additions & 0 deletions app/external/mock/cloudformation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mock

import (
"context"

"github.com/nao1215/rainbow/app/domain/service"
)

// CFnStackLister is a mock of the CFnStackLister interface.
type CFnStackLister func(ctx context.Context, input *service.CFnStackListerInput) (*service.CFnStackListerOutput, error)

// CFnStackLister calls the CFnStackListerFunc.
func (m CFnStackLister) CFnStackLister(ctx context.Context, input *service.CFnStackListerInput) (*service.CFnStackListerOutput, error) {
return m(ctx, input)
}
84 changes: 84 additions & 0 deletions app/interactor/cloudformation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 TestCFnStackLister_ListCFnStack(t *testing.T) {
t.Parallel()

t.Run("success to get cloudformation stack list", func(t *testing.T) {
t.Parallel()

stackLister := mock.CFnStackLister(func(ctx context.Context, input *service.CFnStackListerInput) (*service.CFnStackListerOutput, error) {
want := &service.CFnStackListerInput{
Region: model.RegionAPEast1,
}
if diff := cmp.Diff(want, input); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}

return &service.CFnStackListerOutput{
Stacks: []*model.Stack{
{
StackName: aws.String("stackName1"),
StackID: aws.String(model.StackStatusCreateComplete.String()),
},
{
StackName: aws.String("stackName2"),
StackID: aws.String(model.StackStatusCreateComplete.String()),
},
},
}, nil
})

lister := NewCFnStackLister(stackLister)
output, err := lister.ListCFnStack(context.Background(), &usecase.CFnStackListerInput{
Region: model.RegionAPEast1,
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}

want := &usecase.CFnStackListerOutput{
Stacks: []*model.Stack{
{
StackName: aws.String("stackName1"),
StackID: aws.String(model.StackStatusCreateComplete.String()),
},
{
StackName: aws.String("stackName2"),
StackID: aws.String(model.StackStatusCreateComplete.String()),
},
},
}
if diff := cmp.Diff(want, output); diff != "" {
t.Errorf("differs: (-want +got)\n%s", diff)
}
})

t.Run("fail to get cloudformation stack list", func(t *testing.T) {
t.Parallel()

stackLister := mock.CFnStackLister(func(ctx context.Context, input *service.CFnStackListerInput) (*service.CFnStackListerOutput, error) {
return nil, errors.New("some error")
})

lister := NewCFnStackLister(stackLister)
_, err := lister.ListCFnStack(context.Background(), &usecase.CFnStackListerInput{
Region: model.RegionAPEast1,
})
if err == nil {
t.Error("expected error, but nil")
}
})
}
Loading

0 comments on commit c86d92f

Please sign in to comment.