-
Notifications
You must be signed in to change notification settings - Fork 6
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
Orca 679 global atlantis lock new release branch #49
Changes from 10 commits
892b52a
3e2dff3
20b53c4
f4b6268
8206184
888a2b7
02f375c
e225a63
6ab3f62
9e6473d
edb49da
fac637e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package events_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/google/go-github/v31/github" | ||
stats "github.com/lyft/gostats" | ||
. "github.com/petergtz/pegomock" | ||
"github.com/runatlantis/atlantis/server/events" | ||
"github.com/runatlantis/atlantis/server/events/locking" | ||
"github.com/runatlantis/atlantis/server/events/models" | ||
"github.com/runatlantis/atlantis/server/events/models/fixtures" | ||
) | ||
|
||
// func setupApplyCmd(t *testing.T) *events.ApplyCommandRunner { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented? |
||
// tmp, cleanup := TempDir(t) | ||
// defer cleanup() | ||
|
||
// githubGetter = mocks.NewMockGithubPullGetter() | ||
// eventParsing = mocks.NewMockEventParsing() | ||
// vcsClient = vcsmocks.NewMockClient() | ||
// commitUpdater = mocks.NewMockCommitStatusUpdater() | ||
// applyLockChecker = lockingmocks.NewMockApplyLockChecker() | ||
|
||
// projectCommandBuilder = mocks.NewMockProjectCommandBuilder() | ||
// projectCommandRunner = mocks.NewMockProjectCommandRunner() | ||
|
||
// pullUpdater := &events.PullUpdater{ | ||
// HidePrevPlanComments: false, | ||
// VCSClient: vcsClient, | ||
// MarkdownRenderer: &events.MarkdownRenderer{}, | ||
// } | ||
|
||
// defaultBoltDB, err := db.New(tmp) | ||
// Ok(t, err) | ||
|
||
// dbUpdater := &events.DBUpdater{ | ||
// DB: defaultBoltDB, | ||
// } | ||
|
||
// autoMerger := &events.AutoMerger{ | ||
// VCSClient: vcsClient, | ||
// GlobalAutomerge: false, | ||
// } | ||
|
||
// scopeNull := stats.NewStore(stats.NewNullSink(), false) | ||
// parallelPoolSize := 1 | ||
|
||
// return events.NewApplyCommandRunner( | ||
// vcsClient, | ||
// false, | ||
// applyLockChecker, | ||
// commitUpdater, | ||
// projectCommandBuilder, | ||
// projectCommandRunner, | ||
// autoMerger, | ||
// pullUpdater, | ||
// dbUpdater, | ||
// defaultBoltDB, | ||
// parallelPoolSize, | ||
// ) | ||
|
||
//} | ||
|
||
func TestApplyCommandRunner_IsLocked(t *testing.T) { | ||
RegisterMockTestingT(t) | ||
|
||
cases := []struct { | ||
Description string | ||
ApplyLocked bool | ||
ApplyLockError error | ||
ExpComment string | ||
}{ | ||
{ | ||
Description: "When global apply lock is present IsDisabled returns true", | ||
ApplyLocked: true, | ||
ApplyLockError: nil, | ||
ExpComment: "**Error:** Running `atlantis apply` is disabled.", | ||
}, | ||
{ | ||
Description: "When no global apply lock is present and DisableApply flag is false IsDisabled returns false", | ||
ApplyLocked: false, | ||
ApplyLockError: nil, | ||
ExpComment: "Ran Apply for 0 projects:\n\n\n\n", | ||
}, | ||
{ | ||
Description: "If ApplyLockChecker returns an error IsDisabled return value of DisableApply flag", | ||
ApplyLockError: errors.New("error"), | ||
ApplyLocked: false, | ||
ExpComment: "Ran Apply for 0 projects:\n\n\n\n", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.Description, func(t *testing.T) { | ||
vcsClient := setup(t) | ||
|
||
scopeNull := stats.NewStore(stats.NewNullSink(), false) | ||
|
||
pull := &github.PullRequest{ | ||
State: github.String("open"), | ||
} | ||
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num} | ||
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil) | ||
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil) | ||
|
||
ctx := &events.CommandContext{ | ||
User: fixtures.User, | ||
Log: noopLogger, | ||
Pull: modelPull, | ||
HeadRepo: fixtures.GithubRepo, | ||
Trigger: events.Comment, | ||
Scope: scopeNull, | ||
} | ||
|
||
When(applyLockChecker.CheckApplyLock()).ThenReturn(locking.ApplyCommandLock{Locked: c.ApplyLocked}, c.ApplyLockError) | ||
applyCommandRunner.Run(ctx, &events.CommentCommand{Name: models.ApplyCommand}) | ||
|
||
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, c.ExpComment, "apply") | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
"github.com/google/go-github/v31/github" | ||
. "github.com/petergtz/pegomock" | ||
"github.com/runatlantis/atlantis/server/events" | ||
lockingmocks "github.com/runatlantis/atlantis/server/events/locking/mocks" | ||
"github.com/runatlantis/atlantis/server/events/mocks" | ||
eventmocks "github.com/runatlantis/atlantis/server/events/mocks" | ||
"github.com/runatlantis/atlantis/server/events/mocks/matchers" | ||
|
@@ -60,6 +61,7 @@ var autoMerger *events.AutoMerger | |
var policyCheckCommandRunner *events.PolicyCheckCommandRunner | ||
var approvePoliciesCommandRunner *events.ApprovePoliciesCommandRunner | ||
var planCommandRunner *events.PlanCommandRunner | ||
var applyLockChecker *lockingmocks.MockApplyLockChecker | ||
var applyCommandRunner *events.ApplyCommandRunner | ||
var unlockCommandRunner *events.UnlockCommandRunner | ||
var preWorkflowHooksCommandRunner events.PreWorkflowHooksCommandRunner | ||
|
@@ -86,10 +88,12 @@ func setup(t *testing.T) *vcsmocks.MockClient { | |
|
||
drainer = &events.Drainer{} | ||
deleteLockCommand = eventmocks.NewMockDeleteLockCommand() | ||
applyLockChecker = lockingmocks.NewMockApplyLockChecker() | ||
When(logger.GetLevel()).ThenReturn(logging.Info) | ||
When(logger.NewLogger("runatlantis/atlantis#1", true, logging.Info)). | ||
ThenReturn(pullLogger) | ||
|
||
scope := stats.NewStore(stats.NewNullSink(), false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this added for fixing the tests you thought were broken? lol do we still need it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this is to remove the logging warnings about statsd failing to reach the localhost. Just to reduce the noise :) |
||
dbUpdater = &events.DBUpdater{ | ||
DB: defaultBoltDB, | ||
} | ||
|
@@ -132,7 +136,7 @@ func setup(t *testing.T) *vcsmocks.MockClient { | |
applyCommandRunner = events.NewApplyCommandRunner( | ||
vcsClient, | ||
false, | ||
false, | ||
applyLockChecker, | ||
commitUpdater, | ||
projectCommandBuilder, | ||
projectCommandRunner, | ||
|
@@ -167,8 +171,6 @@ func setup(t *testing.T) *vcsmocks.MockClient { | |
|
||
When(preWorkflowHooksCommandRunner.RunPreHooks(matchers.AnyPtrToEventsCommandContext())).ThenReturn(nil) | ||
|
||
scope := stats.NewDefaultStore() | ||
|
||
ch = events.DefaultCommandRunner{ | ||
VCSClient: vcsClient, | ||
CommentCommandRunnerByCmd: commentCommandRunnerByCmd, | ||
|
@@ -296,23 +298,6 @@ func TestRunCommentCommand_DisableApplyAllDisabled(t *testing.T) { | |
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, "**Error:** Running `atlantis apply` without flags is disabled. You must specify which project to apply via the `-d <dir>`, `-w <workspace>` or `-p <project name>` flags.", "apply") | ||
} | ||
|
||
func TestRunCommentCommand_ApplyDisabled(t *testing.T) { | ||
t.Log("if \"atlantis apply\" is run and this is disabled globally atlantis should" + | ||
" comment saying that this is not allowed") | ||
vcsClient := setup(t) | ||
applyCommandRunner.DisableApply = true | ||
defer func() { applyCommandRunner.DisableApply = false }() | ||
pull := &github.PullRequest{ | ||
State: github.String("open"), | ||
} | ||
modelPull := models.PullRequest{BaseRepo: fixtures.GithubRepo, State: models.OpenPullState, Num: fixtures.Pull.Num} | ||
When(githubGetter.GetPullRequest(fixtures.GithubRepo, fixtures.Pull.Num)).ThenReturn(pull, nil) | ||
When(eventParsing.ParseGithubPull(pull)).ThenReturn(modelPull, modelPull.BaseRepo, fixtures.GithubRepo, nil) | ||
|
||
ch.RunCommentCommand(fixtures.GithubRepo, nil, nil, fixtures.User, modelPull.Num, &events.CommentCommand{Name: models.ApplyCommand}) | ||
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, modelPull.Num, "**Error:** Running `atlantis apply` is disabled.", "apply") | ||
} | ||
|
||
func TestRunCommentCommand_DisableDisableAutoplan(t *testing.T) { | ||
t.Log("if \"DisableAutoplan is true\" are disabled and we are silencing return and do not comment with error") | ||
setup(t) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The applyDisabledComment is very generic, would be good to think about how to make that more useful depending on how the apply command lock is configured. Not in scope for this PR though.