Files
atlantis/server/events/project_command_context_builder_test.go
Ken Kaizu 3954955e13 fix(deps): update module github.com/petergtz/pegomock/v3 to v4 (#3534)
* fix(deps): update module github.com/petergtz/pegomock/v3 to v4 in go.mod

* remove pegomock generate m option, which is not support after v4

* make regen-mocks

* replace pegomock v4 primitive eq/matchers

* convert pegomock v4 Eq/Any matchers

* remove custom models.Repo matcher

* pegomock v4 cannot use result method args

ref https://github.com/petergtz/pegomock/issues/123

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2023-06-20 15:05:23 -04:00

130 lines
4.4 KiB
Go

package events_test
import (
"testing"
. "github.com/petergtz/pegomock/v4"
"github.com/runatlantis/atlantis/server/core/config/valid"
terraform_mocks "github.com/runatlantis/atlantis/server/core/terraform/mocks"
"github.com/runatlantis/atlantis/server/events"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/mocks"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
"github.com/stretchr/testify/assert"
)
func TestProjectCommandContextBuilder_PullStatus(t *testing.T) {
mockCommentBuilder := mocks.NewMockCommentBuilder()
subject := events.DefaultProjectCommandContextBuilder{
CommentBuilder: mockCommentBuilder,
}
projRepoRelDir := "dir1"
projWorkspace := "default"
projName := "project1"
projCfg := valid.MergedProjectCfg{
RepoRelDir: projRepoRelDir,
Workspace: projWorkspace,
Name: projName,
Workflow: valid.Workflow{
Name: valid.DefaultWorkflowName,
Apply: valid.DefaultApplyStage,
},
}
pullStatus := &models.PullStatus{
Projects: []models.ProjectStatus{},
}
commandCtx := &command.Context{
Log: logging.NewNoopLogger(t),
PullStatus: pullStatus,
}
expectedApplyCmt := "Apply Comment"
expectedPlanCmt := "Plan Comment"
terraformClient := terraform_mocks.NewMockClient()
When(terraformClient.ListAvailableVersions(commandCtx.Log))
t.Run("with project name defined", func(t *testing.T) {
When(mockCommentBuilder.BuildPlanComment(projRepoRelDir, projWorkspace, projName, []string{})).ThenReturn(expectedPlanCmt)
When(mockCommentBuilder.BuildApplyComment(projRepoRelDir, projWorkspace, projName, false)).ThenReturn(expectedApplyCmt)
pullStatus.Projects = []models.ProjectStatus{
{
Status: models.ErroredPolicyCheckStatus,
ProjectName: "project1",
RepoRelDir: "dir1",
},
}
result := subject.BuildProjectContext(commandCtx, command.Plan, "", projCfg, []string{}, "some/dir", false, false, false, false, false, terraformClient)
assert.Equal(t, models.ErroredPolicyCheckStatus, result[0].ProjectPlanStatus)
})
t.Run("with no project name defined", func(t *testing.T) {
projCfg.Name = ""
When(mockCommentBuilder.BuildPlanComment(projRepoRelDir, projWorkspace, "", []string{})).ThenReturn(expectedPlanCmt)
When(mockCommentBuilder.BuildApplyComment(projRepoRelDir, projWorkspace, "", false)).ThenReturn(expectedApplyCmt)
pullStatus.Projects = []models.ProjectStatus{
{
Status: models.ErroredPlanStatus,
RepoRelDir: "dir2",
},
{
Status: models.ErroredPolicyCheckStatus,
RepoRelDir: "dir1",
},
}
result := subject.BuildProjectContext(commandCtx, command.Plan, "", projCfg, []string{}, "some/dir", false, false, false, false, false, terraformClient)
assert.Equal(t, models.ErroredPolicyCheckStatus, result[0].ProjectPlanStatus)
})
t.Run("when ParallelApply is set to true", func(t *testing.T) {
projCfg.Name = "Apply Comment"
When(mockCommentBuilder.BuildPlanComment(projRepoRelDir, projWorkspace, "", []string{})).ThenReturn(expectedPlanCmt)
When(mockCommentBuilder.BuildApplyComment(projRepoRelDir, projWorkspace, "", false)).ThenReturn(expectedApplyCmt)
pullStatus.Projects = []models.ProjectStatus{
{
Status: models.ErroredPlanStatus,
RepoRelDir: "dir2",
},
{
Status: models.ErroredPolicyCheckStatus,
RepoRelDir: "dir1",
},
}
result := subject.BuildProjectContext(commandCtx, command.Plan, "", projCfg, []string{}, "some/dir", false, true, false, false, false, terraformClient)
assert.True(t, result[0].ParallelApplyEnabled)
assert.False(t, result[0].ParallelPlanEnabled)
})
t.Run("when AbortOnExcecutionOrderFail is set to true", func(t *testing.T) {
projCfg.Name = "Apply Comment"
When(mockCommentBuilder.BuildPlanComment(projRepoRelDir, projWorkspace, "", []string{})).ThenReturn(expectedPlanCmt)
When(mockCommentBuilder.BuildApplyComment(projRepoRelDir, projWorkspace, "", false)).ThenReturn(expectedApplyCmt)
pullStatus.Projects = []models.ProjectStatus{
{
Status: models.ErroredPlanStatus,
RepoRelDir: "dir2",
},
{
Status: models.ErroredPolicyCheckStatus,
RepoRelDir: "dir1",
},
}
result := subject.BuildProjectContext(commandCtx, command.Plan, "", projCfg, []string{}, "some/dir", false, false, false, false, true, terraformClient)
assert.True(t, result[0].AbortOnExcecutionOrderFail)
})
}