Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Support for Customising the Shell for Pre and Post Workflow Hooks #3451

Merged
merged 13 commits into from
Jul 7, 2023
Prev Previous commit
Next Next commit
Fix tests
X-Guardian committed Jul 7, 2023
commit a0ae9f8e6ed0a6ed690d257cb76b97e89adf911e
6 changes: 3 additions & 3 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
@@ -611,10 +611,10 @@ func TestGitHubWorkflow(t *testing.T) {
expNumHooks := len(c.Comments) + 1 - c.ExpParseFailedCount
// Let's verify the pre-workflow hook was called for each comment including the pull request opened event
mockPreWorkflowHookRunner.VerifyWasCalled(Times(expNumHooks)).Run(Any[models.WorkflowHookCommandContext](),
Eq("some dummy command"), Any[String](), Any[String](), Any[String]())
Eq("some dummy command"), Any[string](), Any[string](), Any[string]())
// Let's verify the post-workflow hook was called for each comment including the pull request opened event
mockPostWorkflowHookRunner.VerifyWasCalled(Times(expNumHooks)).Run(Any[models.WorkflowHookCommandContext](),
Eq("some post dummy command"), Any[String](), Any[String](), Any[String]())
Eq("some post dummy command"), Any[string](), Any[string](), Any[string]())

// Now we're ready to verify Atlantis made all the comments back (or
// replies) that we expect. We expect each plan to have 1 comment,
@@ -797,7 +797,7 @@ func TestSimpleWorkflow_terraformLockFile(t *testing.T) {

// Let's verify the pre-workflow hook was called for each comment including the pull request opened event
mockPreWorkflowHookRunner.VerifyWasCalled(Times(2)).Run(Any[models.WorkflowHookCommandContext](),
Eq("some dummy command"), Any[String](), Any[String](), Any[String]())
Eq("some dummy command"), Any[string](), Any[string](), Any[string]())

// Now we're ready to verify Atlantis made all the comments back (or
// replies) that we expect. We expect each plan to have 1 comment,
46 changes: 22 additions & 24 deletions server/events/post_workflow_hooks_command_runner_test.go
Original file line number Diff line number Diff line change
@@ -126,10 +126,8 @@ func TestRunPostHooks_Clone(t *testing.T) {

When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)

whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(repoDir))
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Any[String](),
Any[String](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Any[string](),
Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := postWh.RunPostHooks(ctx, nil)

@@ -163,8 +161,8 @@ func TestRunPostHooks_Clone(t *testing.T) {

Ok(t, err)

whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHook.RunCommand), EqString(defaultShell), EqString(defaultShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(Any[models.WorkflowHookCommandContext](),
Eq(testHook.RunCommand), Eq(defaultShell), Eq(defaultShellArgs), Eq(repoDir))
postWhWorkingDirLocker.VerifyWasCalled(Never()).TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, "path")
postWhWorkingDir.VerifyWasCalled(Never()).Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)
})
@@ -190,8 +188,8 @@ func TestRunPostHooks_Clone(t *testing.T) {

Assert(t, err != nil, "error not nil")
postWhWorkingDir.VerifyWasCalled(Never()).Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)
whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHook.RunCommand), EqString(defaultShell), EqString(defaultShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(Any[models.WorkflowHookCommandContext](),
Eq(testHook.RunCommand), Eq(defaultShell), Eq(defaultShellArgs), Eq(repoDir))
})

t.Run("error cloning", func(t *testing.T) {
@@ -222,8 +220,8 @@ func TestRunPostHooks_Clone(t *testing.T) {

Assert(t, err != nil, "error not nil")

whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHook.RunCommand), EqString(defaultShell), EqString(defaultShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalled(Never()).Run(Any[models.WorkflowHookCommandContext](),
Eq(testHook.RunCommand), Eq(defaultShell), Eq(defaultShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -251,7 +249,7 @@ func TestRunPostHooks_Clone(t *testing.T) {
When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[String](), Any[String](), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error"))
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error"))

err := postWh.RunPostHooks(ctx, cmd)

@@ -291,7 +289,7 @@ func TestRunPostHooks_Clone(t *testing.T) {
When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[String](), Any[String](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := postWh.RunPostHooks(ctx, cmd)

@@ -324,14 +322,14 @@ func TestRunPostHooks_Clone(t *testing.T) {

When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPostWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHookWithShell.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHookWithShell.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := postWh.RunPostHooks(ctx, nil)

Ok(t, err)
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHookWithShell.RunCommand), EqString(testHookWithShell.Shell), EqString(defaultShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShell.RunCommand), Eq(testHookWithShell.Shell), Eq(defaultShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -358,14 +356,14 @@ func TestRunPostHooks_Clone(t *testing.T) {

When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPostWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHook.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := postWh.RunPostHooks(ctx, nil)

Ok(t, err)
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHookWithShellArgs.RunCommand), EqString(defaultShell), EqString(testHookWithShellArgs.ShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellArgs.RunCommand), Eq(defaultShell), Eq(testHookWithShellArgs.ShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -392,14 +390,14 @@ func TestRunPostHooks_Clone(t *testing.T) {

When(postWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(postWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPostWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHookWithShellandShellArgs.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPostWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellandShellArgs.RunCommand), Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := postWh.RunPostHooks(ctx, nil)

Ok(t, err)
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(),
EqString(testHookWithShellandShellArgs.RunCommand), EqString(testHookWithShellandShellArgs.Shell), EqString(testHookWithShellandShellArgs.ShellArgs), EqString(repoDir))
whPostWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellandShellArgs.RunCommand), Eq(testHookWithShellandShellArgs.Shell), Eq(testHookWithShellandShellArgs.ShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

28 changes: 19 additions & 9 deletions server/events/pre_workflow_hooks_command_runner_test.go
Original file line number Diff line number Diff line change
@@ -128,12 +128,14 @@ func TestRunPreHooks_Clone(t *testing.T) {

When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Any[String](), Any[String](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := preWh.RunPreHooks(ctx, cmd)

Ok(t, err)
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Eq(defaultShell), Eq(defaultShellArgs), Eq(repoDir))
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHook.RunCommand), Eq(defaultShell), Eq(defaultShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -247,7 +249,8 @@ func TestRunPreHooks_Clone(t *testing.T) {

When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand), Any[String](), Any[String](), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error"))
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, errors.New("some error"))

err := preWh.RunPreHooks(ctx, cmd)

@@ -318,12 +321,14 @@ func TestRunPreHooks_Clone(t *testing.T) {

When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPreWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHookWithShell.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHookWithShell.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := preWh.RunPreHooks(ctx, nil)

Ok(t, err)
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHookWithShell.RunCommand), EqString(testHookWithShell.Shell), EqString(defaultShellArgs), EqString(repoDir))
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShell.RunCommand), Eq(testHookWithShell.Shell), Eq(defaultShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -350,12 +355,14 @@ func TestRunPreHooks_Clone(t *testing.T) {

When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPreWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHook.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](), Eq(testHook.RunCommand),
Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := preWh.RunPreHooks(ctx, nil)

Ok(t, err)
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHookWithShellArgs.RunCommand), EqString(defaultShell), EqString(testHookWithShellArgs.ShellArgs), EqString(repoDir))
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellArgs.RunCommand), Eq(defaultShell), Eq(testHookWithShellArgs.ShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})

@@ -382,12 +389,15 @@ func TestRunPreHooks_Clone(t *testing.T) {

When(preWhWorkingDirLocker.TryLock(testdata.GithubRepo.FullName, newPull.Num, events.DefaultWorkspace, events.DefaultRepoRelDir)).ThenReturn(unlockFn, nil)
When(preWhWorkingDir.Clone(log, testdata.GithubRepo, newPull, events.DefaultWorkspace)).ThenReturn(repoDir, false, nil)
When(whPreWorkflowHookRunner.Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHookWithShellandShellArgs.RunCommand), AnyString(), AnyString(), EqString(repoDir))).ThenReturn(result, runtimeDesc, nil)
When(whPreWorkflowHookRunner.Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellandShellArgs.RunCommand), Any[string](), Any[string](), Eq(repoDir))).ThenReturn(result, runtimeDesc, nil)

err := preWh.RunPreHooks(ctx, nil)

Ok(t, err)
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(runtimematchers.AnyModelsWorkflowHookCommandContext(), EqString(testHookWithShellandShellArgs.RunCommand), EqString(testHookWithShellandShellArgs.Shell), EqString(testHookWithShellandShellArgs.ShellArgs), EqString(repoDir))
whPreWorkflowHookRunner.VerifyWasCalledOnce().Run(Any[models.WorkflowHookCommandContext](),
Eq(testHookWithShellandShellArgs.RunCommand), Eq(testHookWithShellandShellArgs.Shell),
Eq(testHookWithShellandShellArgs.ShellArgs), Eq(repoDir))
Assert(t, *unlockCalled == true, "unlock function called")
})
}