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

Add working directory locks in gateway delete clone logic #220

Merged
merged 2 commits into from
Mar 25, 2022
Merged
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
15 changes: 15 additions & 0 deletions server/lyft/gateway/autoplan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ type AutoplanValidator struct {
PrjCmdBuilder events.ProjectPlanCommandBuilder
PullUpdater *events.PullUpdater
WorkingDir events.WorkingDir
WorkingDirLocker events.WorkingDirLocker
}

const DefaultWorkspace = "default"

func (r *AutoplanValidator) isValid(baseRepo models.Repo, headRepo models.Repo, pull models.PullRequest, user models.User) (bool, error) {
if opStarted := r.Drainer.StartOp(); !opStarted {
return false, errors.New("atlantis is shutting down, cannot process current event")
Expand Down Expand Up @@ -80,12 +83,24 @@ func (r *AutoplanValidator) isValid(baseRepo models.Repo, headRepo models.Repo,
ctx.Log.Warn("unable to update commit status: %s", statusErr)
}
// If error happened after clone was made, we should clean it up here too
unlockFn, lockErr := r.WorkingDirLocker.TryLock(baseRepo.FullName, pull.Num, DefaultWorkspace)
if lockErr != nil {
ctx.Log.Warn("workspace was locked")
return false, errors.Wrap(err, lockErr.Error())
}
defer unlockFn()
if cloneErr := r.WorkingDir.Delete(baseRepo, pull); cloneErr != nil {
ctx.Log.With("err", cloneErr).Warn("unable to delete clone after autoplan failed")
}
r.PullUpdater.UpdatePull(ctx, events.AutoplanCommand{}, command.Result{Error: err})
return false, errors.Wrap(err, "Failed building autoplan commands")
}
unlockFn, err := r.WorkingDirLocker.TryLock(baseRepo.FullName, pull.Num, DefaultWorkspace)
if err != nil {
ctx.Log.Warn("workspace was locked")
return false, err
}
defer unlockFn()
// Delete repo clone generated to validate plan
if err := r.WorkingDir.Delete(baseRepo, pull); err != nil {
return false, errors.Wrap(err, "Failed deleting cloned repo")
Expand Down
38 changes: 37 additions & 1 deletion server/lyft/gateway/autoplan_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ var projectCommandBuilder *mocks.MockProjectCommandBuilder
var drainer *events.Drainer
var commitStatusUpdater *mocks.MockCommitStatusUpdater
var workingDir *mocks.MockWorkingDir
var workingDirLocker *mocks.MockWorkingDirLocker

func setupAutoplan(t *testing.T) *vcsmocks.MockClient {
RegisterMockTestingT(t)
projectCommandBuilder = mocks.NewMockProjectCommandBuilder()
commitStatusUpdater = mocks.NewMockCommitStatusUpdater()
workingDir = mocks.NewMockWorkingDir()
workingDirLocker = mocks.NewMockWorkingDirLocker()
vcsClient := vcsmocks.NewMockClient()
drainer = &events.Drainer{}
pullUpdater := &events.PullUpdater{
Expand All @@ -53,6 +55,7 @@ func setupAutoplan(t *testing.T) *vcsmocks.MockClient {
PrjCmdBuilder: projectCommandBuilder,
CommitStatusUpdater: commitStatusUpdater,
WorkingDir: workingDir,
WorkingDirLocker: workingDirLocker,
}
return vcsClient
}
Expand All @@ -64,32 +67,62 @@ func TestIsValid_DrainOngoing(t *testing.T) {
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, containsTerraformChanges == false, "should be false when an error occurs")
workingDir.VerifyWasCalled(Never()).Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalled(Never()).TryLock(AnyString(), AnyInt(), AnyString())
}

func TestIsValid_DeleteCloneError(t *testing.T) {
t.Log("delete clone error")
_ = setupAutoplan(t)
When(workingDir.Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(errors.New("failed to delete clone"))
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(func() {}, nil)
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, containsTerraformChanges == false, "should be false when an error occurs")
workingDir.VerifyWasCalledOnce().Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}

func TestIsValid_LockError(t *testing.T) {
t.Log("lock error")
_ = setupAutoplan(t)
When(workingDir.Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(errors.New("failed to delete clone"))
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(nil, errors.New("can't lock"))
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, containsTerraformChanges == false, "should be false when an error occurs")
workingDir.VerifyWasCalled(Never()).Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}

func TestIsValid_ProjectBuilderError(t *testing.T) {
t.Log("project builder error")
vcsClient := setupAutoplan(t)
When(projectCommandBuilder.BuildAutoplanCommands(matchers.AnyPtrToEventsCommandContext())).
ThenReturn([]command.ProjectContext{}, errors.New("err"))
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(func() {}, nil)
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
vcsClient.VerifyWasCalledOnce().CreateComment(fixtures.GithubRepo, fixtures.Pull.Num, "**Plan Error**\n```\nerr\n```\n", "plan")
Assert(t, containsTerraformChanges == false, "should be false when an error occurs")
workingDir.VerifyWasCalledOnce().Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}

func TestIsValid_ProjectBuilderLockError(t *testing.T) {
t.Log("project builder lock error")
_ = setupAutoplan(t)
When(projectCommandBuilder.BuildAutoplanCommands(matchers.AnyPtrToEventsCommandContext())).
ThenReturn([]command.ProjectContext{}, errors.New("err"))
When(workingDir.Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(errors.New("failed to delete clone"))
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(nil, errors.New("can't lock"))
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, containsTerraformChanges == false, "should be false when an error occurs")
workingDir.VerifyWasCalled(Never()).Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}

func TestIsValid_TerraformChanges(t *testing.T) {
t.Log("verify returns true if terraform changes exist")
_ = setupAutoplan(t)
When(workingDir.Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(nil)
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(func() {}, nil)
When(projectCommandBuilder.BuildAutoplanCommands(matchers.AnyPtrToEventsCommandContext())).
ThenReturn([]command.ProjectContext{
{
Expand All @@ -110,13 +143,15 @@ func TestIsValid_TerraformChanges(t *testing.T) {
AnyInt(),
AnyInt())
workingDir.VerifyWasCalledOnce().Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}

func TestPullRequestHasTerraformChanges_NoTerraformChanges(t *testing.T) {
t.Log("verify returns false if terraform changes don't exist")
vcsClient := setupAutoplan(t)
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
When(workingDir.Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn(nil)
When(workingDirLocker.TryLock(AnyString(), AnyInt(), AnyString())).ThenReturn(func() {}, nil)
containsTerraformChanges := autoplanValidator.InstrumentedIsValid(fixtures.GithubRepo, fixtures.GithubRepo, fixtures.Pull, fixtures.User)
Assert(t, containsTerraformChanges == false, "should have no terraform changes")
vcsClient.VerifyWasCalled(Never()).CreateComment(matchers.AnyModelsRepo(), AnyInt(), AnyString(), AnyString())
commitStatusUpdater.VerifyWasCalled(Times(3)).UpdateCombinedCount(
Expand All @@ -127,4 +162,5 @@ func TestPullRequestHasTerraformChanges_NoTerraformChanges(t *testing.T) {
AnyInt(),
AnyInt())
workingDir.VerifyWasCalledOnce().Delete(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())
workingDirLocker.VerifyWasCalledOnce().TryLock(AnyString(), AnyInt(), AnyString())
}
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
PrjCmdBuilder: projectCommandBuilder,
PullUpdater: pullUpdater,
WorkingDir: workingDir,
WorkingDirLocker: workingDirLocker,
}
gatewayEventsController := &gateway.VCSEventsController{
Logger: logger,
Expand Down