From b8d744fc68f30456981222cccade787815bf2a7a Mon Sep 17 00:00:00 2001 From: Andrii Nasinnyk Date: Fri, 14 Jun 2019 16:08:11 +0300 Subject: [PATCH] fix: tests Signed-off-by: Andrii Nasinnyk --- .../mocks/matchers/map_of_string_to_string.go | 21 +++++++++++++++++++ .../events/mocks/mock_custom_step_runner.go | 20 +++++++++++------- server/events/mocks/mock_step_runner.go | 20 +++++++++++------- server/events/project_command_runner_test.go | 14 ++++--------- server/events/yaml/raw/step.go | 2 +- 5 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 server/events/mocks/matchers/map_of_string_to_string.go diff --git a/server/events/mocks/matchers/map_of_string_to_string.go b/server/events/mocks/matchers/map_of_string_to_string.go new file mode 100644 index 0000000000..4d969915af --- /dev/null +++ b/server/events/mocks/matchers/map_of_string_to_string.go @@ -0,0 +1,21 @@ +// Code generated by pegomock. DO NOT EDIT. +package matchers + +import ( + "reflect" + "github.com/petergtz/pegomock" + + +) + +func AnyMapOfStringToString() map[string]string { + pegomock.RegisterMatcher(pegomock.NewAnyMatcher(reflect.TypeOf((*(map[string]string))(nil)).Elem())) + var nullValue map[string]string + return nullValue +} + +func EqMapOfStringToString(value map[string]string) map[string]string { + pegomock.RegisterMatcher(&pegomock.EqMatcher{Value: value}) + var nullValue map[string]string + return nullValue +} diff --git a/server/events/mocks/mock_custom_step_runner.go b/server/events/mocks/mock_custom_step_runner.go index bb47e51fc5..dfe10e8a06 100644 --- a/server/events/mocks/mock_custom_step_runner.go +++ b/server/events/mocks/mock_custom_step_runner.go @@ -25,11 +25,11 @@ func NewMockCustomStepRunner(options ...pegomock.Option) *MockCustomStepRunner { func (mock *MockCustomStepRunner) SetFailHandler(fh pegomock.FailHandler) { mock.fail = fh } func (mock *MockCustomStepRunner) FailHandler() pegomock.FailHandler { return mock.fail } -func (mock *MockCustomStepRunner) Run(ctx models.ProjectCommandContext, cmd string, path string) (string, error) { +func (mock *MockCustomStepRunner) Run(ctx models.ProjectCommandContext, cmd string, path string, envs map[string]string) (string, error) { if mock == nil { panic("mock must not be nil. Use myMock := NewMockCustomStepRunner().") } - params := []pegomock.Param{ctx, cmd, path} + params := []pegomock.Param{ctx, cmd, path, envs} result := pegomock.GetGenericMockFrom(mock).Invoke("Run", params, []reflect.Type{reflect.TypeOf((*string)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()}) var ret0 string var ret1 error @@ -81,8 +81,8 @@ type VerifierMockCustomStepRunner struct { timeout time.Duration } -func (verifier *VerifierMockCustomStepRunner) Run(ctx models.ProjectCommandContext, cmd string, path string) *MockCustomStepRunner_Run_OngoingVerification { - params := []pegomock.Param{ctx, cmd, path} +func (verifier *VerifierMockCustomStepRunner) Run(ctx models.ProjectCommandContext, cmd string, path string, envs map[string]string) *MockCustomStepRunner_Run_OngoingVerification { + params := []pegomock.Param{ctx, cmd, path, envs} methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "Run", params, verifier.timeout) return &MockCustomStepRunner_Run_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations} } @@ -92,12 +92,12 @@ type MockCustomStepRunner_Run_OngoingVerification struct { methodInvocations []pegomock.MethodInvocation } -func (c *MockCustomStepRunner_Run_OngoingVerification) GetCapturedArguments() (models.ProjectCommandContext, string, string) { - ctx, cmd, path := c.GetAllCapturedArguments() - return ctx[len(ctx)-1], cmd[len(cmd)-1], path[len(path)-1] +func (c *MockCustomStepRunner_Run_OngoingVerification) GetCapturedArguments() (models.ProjectCommandContext, string, string, map[string]string) { + ctx, cmd, path, envs := c.GetAllCapturedArguments() + return ctx[len(ctx)-1], cmd[len(cmd)-1], path[len(path)-1], envs[len(envs)-1] } -func (c *MockCustomStepRunner_Run_OngoingVerification) GetAllCapturedArguments() (_param0 []models.ProjectCommandContext, _param1 []string, _param2 []string) { +func (c *MockCustomStepRunner_Run_OngoingVerification) GetAllCapturedArguments() (_param0 []models.ProjectCommandContext, _param1 []string, _param2 []string, _param3 []map[string]string) { params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations) if len(params) > 0 { _param0 = make([]models.ProjectCommandContext, len(params[0])) @@ -112,6 +112,10 @@ func (c *MockCustomStepRunner_Run_OngoingVerification) GetAllCapturedArguments() for u, param := range params[2] { _param2[u] = param.(string) } + _param3 = make([]map[string]string, len(params[3])) + for u, param := range params[3] { + _param3[u] = param.(map[string]string) + } } return } diff --git a/server/events/mocks/mock_step_runner.go b/server/events/mocks/mock_step_runner.go index 4f5af225e3..14aff3aeba 100644 --- a/server/events/mocks/mock_step_runner.go +++ b/server/events/mocks/mock_step_runner.go @@ -25,11 +25,11 @@ func NewMockStepRunner(options ...pegomock.Option) *MockStepRunner { func (mock *MockStepRunner) SetFailHandler(fh pegomock.FailHandler) { mock.fail = fh } func (mock *MockStepRunner) FailHandler() pegomock.FailHandler { return mock.fail } -func (mock *MockStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string) (string, error) { +func (mock *MockStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string, envs map[string]string) (string, error) { if mock == nil { panic("mock must not be nil. Use myMock := NewMockStepRunner().") } - params := []pegomock.Param{ctx, extraArgs, path} + params := []pegomock.Param{ctx, extraArgs, path, envs} result := pegomock.GetGenericMockFrom(mock).Invoke("Run", params, []reflect.Type{reflect.TypeOf((*string)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()}) var ret0 string var ret1 error @@ -81,8 +81,8 @@ type VerifierMockStepRunner struct { timeout time.Duration } -func (verifier *VerifierMockStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string) *MockStepRunner_Run_OngoingVerification { - params := []pegomock.Param{ctx, extraArgs, path} +func (verifier *VerifierMockStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []string, path string, envs map[string]string) *MockStepRunner_Run_OngoingVerification { + params := []pegomock.Param{ctx, extraArgs, path, envs} methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "Run", params, verifier.timeout) return &MockStepRunner_Run_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations} } @@ -92,12 +92,12 @@ type MockStepRunner_Run_OngoingVerification struct { methodInvocations []pegomock.MethodInvocation } -func (c *MockStepRunner_Run_OngoingVerification) GetCapturedArguments() (models.ProjectCommandContext, []string, string) { - ctx, extraArgs, path := c.GetAllCapturedArguments() - return ctx[len(ctx)-1], extraArgs[len(extraArgs)-1], path[len(path)-1] +func (c *MockStepRunner_Run_OngoingVerification) GetCapturedArguments() (models.ProjectCommandContext, []string, string, map[string]string) { + ctx, extraArgs, path, envs := c.GetAllCapturedArguments() + return ctx[len(ctx)-1], extraArgs[len(extraArgs)-1], path[len(path)-1], envs[len(envs)-1] } -func (c *MockStepRunner_Run_OngoingVerification) GetAllCapturedArguments() (_param0 []models.ProjectCommandContext, _param1 [][]string, _param2 []string) { +func (c *MockStepRunner_Run_OngoingVerification) GetAllCapturedArguments() (_param0 []models.ProjectCommandContext, _param1 [][]string, _param2 []string, _param3 []map[string]string) { params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations) if len(params) > 0 { _param0 = make([]models.ProjectCommandContext, len(params[0])) @@ -112,6 +112,10 @@ func (c *MockStepRunner_Run_OngoingVerification) GetAllCapturedArguments() (_par for u, param := range params[2] { _param2[u] = param.(string) } + _param3 = make([]map[string]string, len(params[3])) + for u, param := range params[3] { + _param3[u] = param.(map[string]string) + } } return } diff --git a/server/events/project_command_runner_test.go b/server/events/project_command_runner_test.go index 6cc0ca9142..1dc20caf51 100644 --- a/server/events/project_command_runner_test.go +++ b/server/events/project_command_runner_test.go @@ -34,6 +34,7 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) { mockInit := mocks.NewMockStepRunner() mockPlan := mocks.NewMockStepRunner() mockApply := mocks.NewMockStepRunner() + mockEnv := mocks.NewMockEnvStepRunner() mockRun := mocks.NewMockCustomStepRunner() mockWorkingDir := mocks.NewMockWorkingDir() mockLocker := mocks.NewMockProjectLocker() @@ -45,6 +46,7 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) { PlanStepRunner: mockPlan, ApplyStepRunner: mockApply, RunStepRunner: mockRun, + EnvStepRunner: mockEnv, PullApprovedChecker: nil, WorkingDir: mockWorkingDir, Webhooks: nil, @@ -86,28 +88,20 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) { { StepName: "init", }, - { - StepName: "env", - Env: "test", - RunCommand: "echo 123", - }, }, Workspace: "default", RepoRelDir: ".", - Env: map[string]string{}, } // Each step will output its step name. When(mockInit.Run(ctx, nil, repoDir)).ThenReturn("init", nil) When(mockPlan.Run(ctx, nil, repoDir)).ThenReturn("plan", nil) When(mockApply.Run(ctx, nil, repoDir)).ThenReturn("apply", nil) When(mockRun.Run(ctx, "", repoDir)).ThenReturn("run", nil) - When(mockRun.Run(ctx, "echo 123", repoDir)).ThenReturn("123", nil) res := runner.Plan(ctx) Assert(t, res.PlanSuccess != nil, "exp plan success") Equals(t, "https://lock-key", res.PlanSuccess.LockURL) Equals(t, "run\napply\nplan\ninit", res.PlanSuccess.TerraformOutput) - Equals(t, "123", ctx.Env["test"]) expSteps := []string{"run", "apply", "plan", "init", "var"} for _, step := range expSteps { @@ -120,8 +114,6 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) { mockApply.VerifyWasCalledOnce().Run(ctx, nil, repoDir) case "run": mockRun.VerifyWasCalledOnce().Run(ctx, "", repoDir) - case "env": - mockRun.VerifyWasCalledOnce().Run(ctx, "echo 123", repoDir) } } } @@ -260,6 +252,7 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) { mockPlan := mocks.NewMockStepRunner() mockApply := mocks.NewMockStepRunner() mockRun := mocks.NewMockCustomStepRunner() + mockEnv := mocks.NewMockEnvStepRunner() mockApproved := mocks2.NewMockPullApprovedChecker() mockWorkingDir := mocks.NewMockWorkingDir() mockLocker := mocks.NewMockProjectLocker() @@ -272,6 +265,7 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) { PlanStepRunner: mockPlan, ApplyStepRunner: mockApply, RunStepRunner: mockRun, + EnvStepRunner: mockEnv, PullApprovedChecker: mockApproved, WorkingDir: mockWorkingDir, Webhooks: mockSender, diff --git a/server/events/yaml/raw/step.go b/server/events/yaml/raw/step.go index 53f5214c85..b4026ae7b9 100644 --- a/server/events/yaml/raw/step.go +++ b/server/events/yaml/raw/step.go @@ -262,7 +262,7 @@ func (s *Step) unmarshalGeneric(unmarshal func(interface{}) error) error { // name: k // value: hi //optional // command: exec - // We validate if the key var + // We validate if the key env var envStep map[string]map[string]string err = unmarshal(&envStep) if err == nil {