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

Fixes for pre-workflow-hook #1409

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions server/events/pre_workflow_hooks_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks(

log.Info("running pre hooks")

preWorkflowHooks := make([]*valid.PreWorkflowHook, 0)
for _, repo := range w.GlobalCfg.Repos {
if repo.IDMatches(baseRepo.ID()) && len(repo.PreWorkflowHooks) > 0 {
preWorkflowHooks = append(preWorkflowHooks, repo.PreWorkflowHooks...)
}
}

if len(preWorkflowHooks) == 0 {
return
}

unlockFn, err := w.WorkingDirLocker.TryLock(baseRepo.FullName, pull.Num, DefaultWorkspace)
if err != nil {
log.Warn("workspace is locked")
Expand All @@ -67,13 +78,6 @@ func (w *DefaultPreWorkflowHooksCommandRunner) RunPreHooks(
return
}

preWorkflowHooks := make([]*valid.PreWorkflowHook, 0)
for _, repo := range w.GlobalCfg.Repos {
if repo.IDMatches(baseRepo.ID()) && len(repo.PreWorkflowHooks) > 0 {
preWorkflowHooks = append(preWorkflowHooks, repo.PreWorkflowHooks...)
}
}

ctx := models.PreWorkflowHookCommandContext{
BaseRepo: baseRepo,
HeadRepo: headRepo,
Expand All @@ -100,7 +104,7 @@ func (w *DefaultPreWorkflowHooksCommandRunner) runHooks(
_, err := w.PreWorkflowHookRunner.Run(ctx, hook.RunCommand, repoDir)

if err != nil {
return nil
return err
}
}

Expand Down
55 changes: 47 additions & 8 deletions server/events/pre_workflow_hooks_command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/runatlantis/atlantis/server/events/models/fixtures"
"github.com/runatlantis/atlantis/server/events/runtime"
vcsmocks "github.com/runatlantis/atlantis/server/events/vcs/mocks"
"github.com/runatlantis/atlantis/server/events/yaml/valid"
"github.com/runatlantis/atlantis/server/logging"
logmocks "github.com/runatlantis/atlantis/server/logging/mocks"
. "github.com/runatlantis/atlantis/testing"
Expand All @@ -21,33 +22,73 @@ var wh events.DefaultPreWorkflowHooksCommandRunner
var whWorkingDir *mocks.MockWorkingDir
var whWorkingDirLocker *mocks.MockWorkingDirLocker
var whDrainer *events.Drainer
var whLogger *logmocks.MockSimpleLogging

func preWorkflowHooksSetup(t *testing.T) *vcsmocks.MockClient {
RegisterMockTestingT(t)
vcsClient := vcsmocks.NewMockClient()
logger := logmocks.NewMockSimpleLogging()
whLogger = logmocks.NewMockSimpleLogging()
whWorkingDir = mocks.NewMockWorkingDir()
whWorkingDirLocker = mocks.NewMockWorkingDirLocker()
whDrainer = &events.Drainer{}
pullLogger = logging.NewSimpleLogger("runatlantis/atlantis#1", true, logging.Info)
When(whLogger.GetLevel()).ThenReturn(logging.Info)
When(whLogger.NewLogger("runatlantis/atlantis#1", true, logging.Info)).
ThenReturn(pullLogger)

wh = events.DefaultPreWorkflowHooksCommandRunner{
VCSClient: vcsClient,
Logger: logger,
Logger: whLogger,
WorkingDirLocker: whWorkingDirLocker,
WorkingDir: whWorkingDir,
Drainer: whDrainer,
PreWorkflowHookRunner: &runtime.PreWorkflowHookRunner{},
GlobalCfg: valid.GlobalCfg{
Repos: []valid.Repo{
valid.Repo{
ID: "github.com/runatlantis/atlantis",
PreWorkflowHooks: []*valid.PreWorkflowHook{
&valid.PreWorkflowHook{
StepName: "run",
RunCommand: `echo "exit with error"; exit 1`,
},
},
},
},
},
}
return vcsClient
}

func TestPreWorkflowHooksCommandLogErrors(t *testing.T) {
preWorkflowHooksSetup(t)

wh.RunPreHooks(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, strings.Contains(pullLogger.History.String(), "[EROR] Pre workflow hook run error results: exit status 1:"), "Failing pre workflow should log error")
}

func TestPreWorkflowHooksDoesntRun(t *testing.T) {
preWorkflowHooksSetup(t)
wh.GlobalCfg = valid.GlobalCfg{
Repos: []valid.Repo{
valid.Repo{
ID: "github.com/runatlantis/atlantis",
PreWorkflowHooks: []*valid.PreWorkflowHook{},
},
},
}

wh.RunPreHooks(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
whWorkingDirLocker.VerifyWasCalled(Times(0)).TryLock(AnyString(), AnyInt(), AnyString())
whWorkingDir.VerifyWasCalled(Times(0)).Clone(matchers.AnyPtrToLoggingSimpleLogger(), matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest(), AnyString())
}

func TestPreWorkflowHooksCommand_LogPanics(t *testing.T) {
t.Log("if there is a panic it is commented back on the pull request")
vcsClient := preWorkflowHooksSetup(t)
logger := wh.Logger.NewLogger("log", false, logging.LogLevel(1))

When(whWorkingDir.Clone(
logger,
pullLogger,
fixtures.GithubRepo,
fixtures.Pull,
events.DefaultWorkspace,
Expand All @@ -62,11 +103,9 @@ func TestPreWorkflowHooksCommand_LogPanics(t *testing.T) {
// we delete the plans.
func TestRunPreHooks_Clone(t *testing.T) {
preWorkflowHooksSetup(t)
logger := wh.Logger.NewLogger("log", false, logging.LogLevel(1))

When(whWorkingDir.Clone(logger, fixtures.GithubRepo, fixtures.Pull, events.DefaultWorkspace)).
ThenReturn("path/to/repo", false, nil)
wh.RunPreHooks(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
whWorkingDir.VerifyWasCalledOnce().Clone(pullLogger, fixtures.GithubRepo, fixtures.Pull, events.DefaultWorkspace)
}

func TestRunPreHooks_DrainOngoing(t *testing.T) {
Expand All @@ -82,6 +121,6 @@ func TestRunPreHooks_DrainNotOngoing(t *testing.T) {
preWorkflowHooksSetup(t)
When(whWorkingDir.Clone(logger, fixtures.GithubRepo, fixtures.Pull, events.DefaultWorkspace)).ThenPanic("panic test - if you're seeing this in a test failure this isn't the failing test")
wh.RunPreHooks(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
whWorkingDir.VerifyWasCalledOnce().Clone(logger, fixtures.GithubRepo, fixtures.Pull, events.DefaultWorkspace)
whWorkingDir.VerifyWasCalledOnce().Clone(pullLogger, fixtures.GithubRepo, fixtures.Pull, events.DefaultWorkspace)
Equals(t, 0, whDrainer.GetStatus().InProgressOps)
}