Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andrii Nasinnyk <[email protected]>
  • Loading branch information
anasinnyk committed Jun 14, 2019
1 parent b8d744f commit 755a0d2
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 20 deletions.
133 changes: 133 additions & 0 deletions server/events/mocks/mock_env_step_runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/events/project_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type CustomStepRunner interface {
Run(ctx models.ProjectCommandContext, cmd string, path string, envs map[string]string) (string, error)
}

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_env_step_runner.go EnvStepRunner

// EnvStepRunner runs env steps.
type EnvStepRunner interface {
Run(ctx models.ProjectCommandContext, name string, cmd string, value string, path string, envs map[string]string) (string, string, error)
}
Expand Down
51 changes: 31 additions & 20 deletions server/events/project_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) {
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
}

envs := make(map[string]string)
repoDir, cleanup := TempDir(t)
defer cleanup()
When(mockWorkingDir.Clone(
Expand Down Expand Up @@ -88,32 +89,38 @@ func TestDefaultProjectCommandRunner_Plan(t *testing.T) {
{
StepName: "init",
},
{
StepName: "env",
},
},
Workspace: "default",
RepoRelDir: ".",
}
// 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(mockInit.Run(ctx, nil, repoDir, envs)).ThenReturn("init", nil)
When(mockPlan.Run(ctx, nil, repoDir, envs)).ThenReturn("plan", nil)
When(mockApply.Run(ctx, nil, repoDir, envs)).ThenReturn("apply", nil)
When(mockRun.Run(ctx, "", repoDir, envs)).ThenReturn("run", nil)
When(mockEnv.Run(ctx, "", "", "", repoDir, envs)).ThenReturn("name", "value", 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, "run\napply\nplan\ninit\nenv", res.PlanSuccess.TerraformOutput)

expSteps := []string{"run", "apply", "plan", "init", "var"}
expSteps := []string{"run", "apply", "plan", "init", "var", "env"}
for _, step := range expSteps {
switch step {
case "init":
mockInit.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockInit.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "plan":
mockPlan.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockPlan.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "apply":
mockApply.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockApply.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "run":
mockRun.VerifyWasCalledOnce().Run(ctx, "", repoDir)
mockRun.VerifyWasCalledOnce().Run(ctx, "", repoDir, envs)
case "env":
mockEnv.VerifyWasCalledOnce().Run(ctx, "", "", "", repoDir, envs)
}
}
}
Expand Down Expand Up @@ -239,9 +246,12 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) {
{
StepName: "init",
},
{
StepName: "env",
},
},
expSteps: []string{"run", "apply", "plan", "init"},
expOut: "run\napply\nplan\ninit",
expSteps: []string{"run", "apply", "plan", "init", "env"},
expOut: "run\napply\nplan\ninit\nenv",
},
}

Expand Down Expand Up @@ -271,6 +281,7 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) {
Webhooks: mockSender,
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
}
envs := make(map[string]string)
repoDir, cleanup := TempDir(t)
defer cleanup()
When(mockWorkingDir.GetWorkingDir(
Expand All @@ -287,10 +298,10 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) {
RepoRelDir: ".",
PullMergeable: c.pullMergeable,
}
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(mockInit.Run(ctx, nil, repoDir, envs)).ThenReturn("init", nil)
When(mockPlan.Run(ctx, nil, repoDir, envs)).ThenReturn("plan", nil)
When(mockApply.Run(ctx, nil, repoDir, envs)).ThenReturn("apply", nil)
When(mockRun.Run(ctx, "", repoDir, envs)).ThenReturn("run", nil)
When(mockApproved.PullIsApproved(ctx.BaseRepo, ctx.Pull)).ThenReturn(true, nil)

res := runner.Apply(ctx)
Expand All @@ -302,13 +313,13 @@ func TestDefaultProjectCommandRunner_Apply(t *testing.T) {
case "approved":
mockApproved.VerifyWasCalledOnce().PullIsApproved(ctx.BaseRepo, ctx.Pull)
case "init":
mockInit.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockInit.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "plan":
mockPlan.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockPlan.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "apply":
mockApply.VerifyWasCalledOnce().Run(ctx, nil, repoDir)
mockApply.VerifyWasCalledOnce().Run(ctx, nil, repoDir, envs)
case "run":
mockRun.VerifyWasCalledOnce().Run(ctx, "", repoDir)
mockRun.VerifyWasCalledOnce().Run(ctx, "", repoDir, envs)
}
}
})
Expand Down

0 comments on commit 755a0d2

Please sign in to comment.