Skip to content

Commit

Permalink
test: mock out apigateway operations for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spatocode committed Sep 30, 2023
1 parent 98a830f commit 4204614
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type CloudStorage interface {

type CloudMonitor interface {
Watch()
Clear() error
Clear(string) error
}

type CloudPlatform interface {
Expand Down
12 changes: 8 additions & 4 deletions cloud/aws/apigateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cf "github.com/awslabs/goformation/v7/cloudformation"
cfApigateway "github.com/awslabs/goformation/v7/cloudformation/apigateway"

"github.com/spatocode/jerm"
"github.com/spatocode/jerm/config"
"github.com/spatocode/jerm/internal/log"
"github.com/spatocode/jerm/internal/utils"
Expand All @@ -25,15 +26,14 @@ type ApiGateway struct {
cfTemplate *cf.Template
cfClient *cloudformation.Client
client *apigateway.Client
monitor *CloudWatch
monitor jerm.CloudMonitor
config *config.Config
awsConfig aws.Config
}

func NewApiGateway(config *config.Config, awsConfig aws.Config) *ApiGateway {
s3 := NewS3(config, awsConfig)
return &ApiGateway{
s3: s3,
s3: NewS3(config, awsConfig),
awsConfig: awsConfig,
monitor: NewCloudWatch(config, awsConfig),
cfClient: cloudformation.NewFromConfig(awsConfig),
Expand All @@ -42,6 +42,10 @@ func NewApiGateway(config *config.Config, awsConfig aws.Config) *ApiGateway {
}
}

func (a *ApiGateway) WithMonitor(monitor jerm.CloudMonitor) {
a.monitor = monitor
}

func (a *ApiGateway) setup(functionArn *string) error {
template := cf.NewTemplate()
template.Description = "Auto generated by Jerm"
Expand Down Expand Up @@ -320,7 +324,7 @@ func (a *ApiGateway) deleteLogs() error {
}
for _, item := range resp.Item {
groupName := fmt.Sprintf("API-Gateway-Execution-Logs_%s/%s", *id, *item.StageName)
a.monitor.deleteLogGroup(groupName)
a.monitor.Clear(groupName)
}
}
return nil
Expand Down
105 changes: 105 additions & 0 deletions cloud/aws/apigateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package aws

import (
"context"
"fmt"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/apigateway"
"github.com/aws/smithy-go/middleware"
"github.com/spatocode/jerm/config"
"github.com/stretchr/testify/assert"
)

func TestNewApiGateway(t *testing.T) {
assert := assert.New(t)
cfg := &config.Config{}
awsC := aws.Config{}
a := NewApiGateway(cfg, awsC)
assert.Equal(cfg, a.config)
assert.NotNil(a.client)
}

func TestNewApiGatewayWithMonitor(t *testing.T) {
assert := assert.New(t)
cfg := &config.Config{}
awsC := aws.Config{}
a := NewApiGateway(cfg, awsC)
monitor := NewCloudWatch(cfg, awsC)
a.WithMonitor(monitor)
assert.Equal(monitor, a.monitor)
}

func TestApiGatewayGetRestApis(t *testing.T) {
assert := assert.New(t)

cases := []tcase{
{
name: "get rest apis failure",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"GetRestApisErrorMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: nil,
}, middleware.Metadata{}, fmt.Errorf("GetRestApisError")
},
),
middleware.Before,
)
},
},
want: fmt.Errorf("operation error API Gateway: GetRestApis, GetRestApisError"),
wantErr: true,
},
{
name: "get rest apis successfull",
args: args{
withAPIOptionsFunc: func(s *middleware.Stack) error {
return s.Finalize.Add(
middleware.FinalizeMiddlewareFunc(
"GetRestApisMock",
func(ctx context.Context, fi middleware.FinalizeInput, fh middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) {
return middleware.FinalizeOutput{
Result: &apigateway.GetRestApisOutput{},
}, middleware.Metadata{}, nil
},
),
middleware.Before,
)
},
},
want: nil,
wantErr: false,
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
awsCfg, err := awsConfig.LoadDefaultConfig(
context.TODO(),
awsConfig.WithRegion("us-west-1"),
awsConfig.WithAPIOptions([]func(*middleware.Stack) error{tt.args.withAPIOptionsFunc}),
)
if err != nil {
t.Fatal(err)
}

cfg := &config.Config{}
client := NewApiGateway(cfg, awsCfg)
apis, err := client.getRestApis()
if (err != nil) != tt.wantErr {
assert.Errorf(err, "error = %#v, wantErr %#v", err, tt.wantErr)
return
}
if tt.wantErr && err.Error() != tt.want.Error() {
assert.EqualError(err, tt.want.Error())
}
assert.IsType(apis, []*string{})
})
}
}
3 changes: 2 additions & 1 deletion cloud/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ func (l *Lambda) Undeploy() error {
}

l.deleteLambdaFunction()
l.monitor.Clear()
groupName := fmt.Sprintf("/aws/lambda/%s", l.config.GetFunctionName())
l.monitor.Clear(groupName)

return nil
}
Expand Down
5 changes: 2 additions & 3 deletions cloud/aws/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ func (c *CloudWatch) deleteLogGroup(groupName string) error {
}

// Clear deletes AWS CloudWatch logs
func (c *CloudWatch) Clear() error {
groupName := fmt.Sprintf("/aws/lambda/%s", c.config.GetFunctionName())
err := c.deleteLogGroup(groupName)
func (c *CloudWatch) Clear(name string) error {
err := c.deleteLogGroup(name)
return err
}
2 changes: 1 addition & 1 deletion cloud/aws/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestCloudWatchClear(t *testing.T) {

cfg := &config.Config{}
client := NewCloudWatch(cfg, awsCfg)
err = client.Clear()
err = client.Clear("/test/group/name")
if (err != nil) != tt.wantErr {
assert.Errorf(err, "error = %#v, wantErr %#v", err, tt.wantErr)
return
Expand Down

0 comments on commit 4204614

Please sign in to comment.