From 7880607290f5070a15ca09aa37d9e69be7926bfe Mon Sep 17 00:00:00 2001 From: Taylor Sutton Date: Mon, 19 Jul 2021 18:23:43 -0400 Subject: [PATCH] Fix mocks. --- .gitignore | 2 +- Makefile | 9 ++++---- cmd/sfncli/cloudwatchreporter_test.go | 14 ++++++------ cmd/sfncli/runner_test.go | 32 +++++++++++++-------------- mocks/mocks.go | 2 ++ tools/tools.go | 2 +- 6 files changed, 31 insertions(+), 30 deletions(-) create mode 100644 mocks/mocks.go diff --git a/.gitignore b/.gitignore index 262c8c2..820ac34 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,5 @@ vendor/ bin/ release/ -gen-go/ +mocks/mock_*.go cmd/sfncli/sfncli diff --git a/Makefile b/Makefile index acd3a37..bde8efa 100644 --- a/Makefile +++ b/Makefile @@ -37,11 +37,10 @@ clean: mocks: mkdir -p bin - go build -o ./bin/mockgen ./vendor/github.com/golang/mock/mockgen - rm -rf gen-go/mocksfn && mkdir -p gen-go/mocksfn - ./bin/mockgen -source vendor/github.com/aws/aws-sdk-go/service/sfn/sfniface/interface.go -destination gen-go/mocksfn/mocksfn.go -package mocksfn - rm -rf gen-go/mockcloudwatch && mkdir -p gen-go/mockcloudwatch - ./bin/mockgen -source vendor/github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface/interface.go -destination gen-go/mockcloudwatch/mockcloudwatch.go -package mockcloudwatch + go build -o ./bin/mockgen github.com/golang/mock/mockgen + rm -rf mocks/mock_*.go + ./bin/mockgen -source ./vendor/github.com/aws/aws-sdk-go/service/sfn/sfniface/interface.go -destination mocks/mock_sfn.go -package mocks + ./bin/mockgen -source ./vendor/github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface/interface.go -destination mocks/mock_cloudwatch.go -package mocks install_deps: go mod vendor diff --git a/cmd/sfncli/cloudwatchreporter_test.go b/cmd/sfncli/cloudwatchreporter_test.go index 202c770..3851c33 100644 --- a/cmd/sfncli/cloudwatchreporter_test.go +++ b/cmd/sfncli/cloudwatchreporter_test.go @@ -8,7 +8,7 @@ import ( "testing" "time" - "github.com/Clever/sfncli/gen-go/mockcloudwatch" + "github.com/Clever/sfncli/mocks" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/golang/mock/gomock" @@ -21,7 +21,7 @@ func TestCloudWatchReporterReportsActiveZero(t *testing.T) { defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) cwr := NewCloudWatchReporter(mockCW, mockActivityArn) go cwr.ReportActivePercent(testCtx, 100*time.Millisecond) mockCW.EXPECT().PutMetricData(&cloudwatch.PutMetricDataInput{ @@ -44,7 +44,7 @@ func TestCloudWatchReporterReportsActiveFiftyPercent(t *testing.T) { defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) mockCW.EXPECT().PutMetricData(fuzzy(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{{ Dimensions: []*cloudwatch.Dimension{{ @@ -75,7 +75,7 @@ func TestCloudWatchReporterReportsActiveHundredPercent(t *testing.T) { defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) mockCW.EXPECT().PutMetricData(fuzzy(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{{ Dimensions: []*cloudwatch.Dimension{{ @@ -99,7 +99,7 @@ func TestCloudWatchReporterReportsActiveOneHundredPercentWhenPausedForever(t *te defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) mockCW.EXPECT().PutMetricData(fuzzy(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{{ Dimensions: []*cloudwatch.Dimension{{ @@ -137,7 +137,7 @@ func TestCloudWatchReporterReportsActiveOnehundredPercentWhenPaused(t *testing.T defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) mockCW.EXPECT().PutMetricData(fuzzy(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{{ Dimensions: []*cloudwatch.Dimension{{ @@ -173,7 +173,7 @@ func TestCloudWatchReporterReportsActiveFiftyPercentWhenPaused(t *testing.T) { defer testCtxCancel() controller := gomock.NewController(t) defer controller.Finish() - mockCW := mockcloudwatch.NewMockCloudWatchAPI(controller) + mockCW := mocks.NewMockCloudWatchAPI(controller) mockCW.EXPECT().PutMetricData(fuzzy(&cloudwatch.PutMetricDataInput{ MetricData: []*cloudwatch.MetricDatum{{ Dimensions: []*cloudwatch.Dimension{{ diff --git a/cmd/sfncli/runner_test.go b/cmd/sfncli/runner_test.go index 763c11f..eec2791 100644 --- a/cmd/sfncli/runner_test.go +++ b/cmd/sfncli/runner_test.go @@ -10,7 +10,7 @@ import ( "testing" "time" - "github.com/Clever/sfncli/gen-go/mocksfn" + "github.com/Clever/sfncli/mocks" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/sfn" "github.com/golang/mock/gomock" @@ -66,7 +66,7 @@ func TestTaskFailureTaskInputNotJSON(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -88,7 +88,7 @@ func TestTaskOutputEmptyStringAsJSON(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskSuccessWithContext(gomock.Any(), &sfn.SendTaskSuccessInput{ TaskToken: aws.String(mockTaskToken), Output: aws.String(`{"_EXECUTION_NAME":"fake-WFM-uuid"}`), @@ -109,7 +109,7 @@ func TestTaskFailureCommandNotFound(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -130,7 +130,7 @@ func TestTaskFailureCommandKilled(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -155,7 +155,7 @@ func TestTaskFailureCommandExitedNonzero(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -176,7 +176,7 @@ func TestTaskFailureCustomErrorName(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -197,7 +197,7 @@ func TestTaskFailureTaskOutputNotJSON(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -218,7 +218,7 @@ func TestTaskFailureCommandTerminated(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -243,7 +243,7 @@ func TestTaskFailureCommandTerminated(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -268,7 +268,7 @@ func TestTaskFailureCommandTerminated(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskFailure(&sfn.SendTaskFailureInput{ Cause: aws.String(expectedError.ErrorCause()), Error: aws.String(expectedError.ErrorName()), @@ -294,7 +294,7 @@ func TestTaskSuccessSignalForwarded(t *testing.T) { cmdArgs := []string{} controller := gomock.NewController(t) - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskSuccessWithContext(gomock.Any(), &sfn.SendTaskSuccessInput{ Output: aws.String(`{"_EXECUTION_NAME":"fake-WFM-uuid","signal":"1"}`), TaskToken: aws.String(mockTaskToken), @@ -316,7 +316,7 @@ func TestTaskSuccessOutputIsLastLineOfStdout(t *testing.T) { cmdArgs := []string{} controller := gomock.NewController(t) - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskSuccessWithContext(gomock.Any(), &sfn.SendTaskSuccessInput{ Output: aws.String(`{"_EXECUTION_NAME":"fake-WFM-uuid","task":"output"}`), TaskToken: aws.String(mockTaskToken), @@ -336,7 +336,7 @@ func TestTaskWorkDirectorySetup(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskSuccessWithContext(gomock.Any(), &workdirMatcher{ taskToken: mockTaskToken, expectedPrefix: "/tmp", @@ -356,7 +356,7 @@ func TestTaskWorkDirectoryUnsetByDefault(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) mockSFN.EXPECT().SendTaskSuccessWithContext(gomock.Any(), &sfn.SendTaskSuccessInput{ TaskToken: aws.String(mockTaskToken), Output: aws.String(`{"_EXECUTION_NAME":"fake-WFM-uuid","work_dir":""}`), // returns the result of WORK_DIR @@ -376,7 +376,7 @@ func TestTaskWorkDirectoryCleaned(t *testing.T) { controller := gomock.NewController(t) defer controller.Finish() - mockSFN := mocksfn.NewMockSFNAPI(controller) + mockSFN := mocks.NewMockSFNAPI(controller) dirMatcher := workdirMatcher{ taskToken: mockTaskToken, expectedPrefix: "/tmp/test", diff --git a/mocks/mocks.go b/mocks/mocks.go new file mode 100644 index 0000000..272ff5f --- /dev/null +++ b/mocks/mocks.go @@ -0,0 +1,2 @@ +// Package mocks holds the generated mocks for testing. +package mocks diff --git a/tools/tools.go b/tools/tools.go index 4f3313b..8b7a2e0 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -4,5 +4,5 @@ package main import ( - _ "github.com/golang/mock/mockgen" + _ "github.com/golang/mock/gomock" )