Files
atlantis/server/events/command_runner_internal_test.go
Sarvar Muminov af2a806870 Implement a new policy check workflow (#1317)
* Adding policy_check support into yaml config

* Added policy check model and runtime structs

* Adding BuildPolicyCheckCommand to ProjectCommandBuilder

* Return incorrectly deleted code

* Remove BuildAutoPolicyPlanCommand from ProjectCommandBuilder

* Split runAutoCommand into two functions

runAutoPlanCommand - does what originally RunAutoplancommand was doing
except now it returns CommandResult and []models.ProjectCommandContext
runAutoPolicyCheckCommand - accepts CommandContext, CommandResult, and
[]models.ProjectCommandContext as arguments and runs PolicyCheckStep runner

* Refactor RunCommentCommand
* Remove BuildPolicyCheckCommand and rename StepCmdExec back to TerraformExec
* Add policy step runner logic and conftest interfaces.
* Add show step runner to policy check stage.

* Adding models.PolicyCheckCommand to buildCtx

This also means buildPlanAllCommands call buildCtx twice once with
models.PlanCommand and once with models.PolicyCheckCommand

* Adding new project_command_builder that supports policy_check

* Refactoring PolicyCheck specific logic into a PolicyCheckProjectCommandBuilder

* Moving events.CommandContext to models.CommandContext this will allow me
to remove buildCtx method and move ProjectCommandContext creation into
models package

* Policy Owners might be different types, for that reason we are
refactoring Owners into its own struct with specific keys defining
different owner types

Co-authored-by: Nish Krishnan <nishk@lyft.com>
Co-authored-by: Nish Krishnan <nishkrishnan@users.noreply.github.com>
2021-02-10 18:13:44 -08:00

174 lines
4.2 KiB
Go

package events
import (
"testing"
"github.com/runatlantis/atlantis/server/events/models"
. "github.com/runatlantis/atlantis/testing"
)
func TestApplyUpdateCommitStatus(t *testing.T) {
cases := map[string]struct {
cmd models.CommandName
pullStatus models.PullStatus
expStatus models.CommitStatus
expNumSuccess int
expNumTotal int
}{
"apply, one pending": {
cmd: models.ApplyCommand,
pullStatus: models.PullStatus{
Projects: []models.ProjectStatus{
{
Status: models.PlannedPlanStatus,
},
{
Status: models.AppliedPlanStatus,
},
},
},
expStatus: models.PendingCommitStatus,
expNumSuccess: 1,
expNumTotal: 2,
},
"apply, all successful": {
cmd: models.ApplyCommand,
pullStatus: models.PullStatus{
Projects: []models.ProjectStatus{
{
Status: models.AppliedPlanStatus,
},
{
Status: models.AppliedPlanStatus,
},
},
},
expStatus: models.SuccessCommitStatus,
expNumSuccess: 2,
expNumTotal: 2,
},
"apply, one errored, one pending": {
cmd: models.ApplyCommand,
pullStatus: models.PullStatus{
Projects: []models.ProjectStatus{
{
Status: models.AppliedPlanStatus,
},
{
Status: models.ErroredApplyStatus,
},
{
Status: models.PlannedPlanStatus,
},
},
},
expStatus: models.FailedCommitStatus,
expNumSuccess: 1,
expNumTotal: 3,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
csu := &MockCSU{}
cr := &ApplyCommandRunner{
commitStatusUpdater: csu,
}
cr.updateCommitStatus(&CommandContext{}, c.pullStatus)
Equals(t, models.Repo{}, csu.CalledRepo)
Equals(t, models.PullRequest{}, csu.CalledPull)
Equals(t, c.expStatus, csu.CalledStatus)
Equals(t, c.cmd, csu.CalledCommand)
Equals(t, c.expNumSuccess, csu.CalledNumSuccess)
Equals(t, c.expNumTotal, csu.CalledNumTotal)
})
}
}
func TestPlanUpdateCommitStatus(t *testing.T) {
cases := map[string]struct {
cmd models.CommandName
pullStatus models.PullStatus
expStatus models.CommitStatus
expNumSuccess int
expNumTotal int
}{
"single plan success": {
cmd: models.PlanCommand,
pullStatus: models.PullStatus{
Projects: []models.ProjectStatus{
{
Status: models.PlannedPlanStatus,
},
},
},
expStatus: models.SuccessCommitStatus,
expNumSuccess: 1,
expNumTotal: 1,
},
"one plan error, other errors": {
cmd: models.PlanCommand,
pullStatus: models.PullStatus{
Projects: []models.ProjectStatus{
{
Status: models.ErroredPlanStatus,
},
{
Status: models.PlannedPlanStatus,
},
{
Status: models.AppliedPlanStatus,
},
{
Status: models.ErroredApplyStatus,
},
},
},
expStatus: models.FailedCommitStatus,
expNumSuccess: 3,
expNumTotal: 4,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
csu := &MockCSU{}
cr := &PlanCommandRunner{
commitStatusUpdater: csu,
}
cr.updateCommitStatus(&CommandContext{}, c.pullStatus)
Equals(t, models.Repo{}, csu.CalledRepo)
Equals(t, models.PullRequest{}, csu.CalledPull)
Equals(t, c.expStatus, csu.CalledStatus)
Equals(t, c.cmd, csu.CalledCommand)
Equals(t, c.expNumSuccess, csu.CalledNumSuccess)
Equals(t, c.expNumTotal, csu.CalledNumTotal)
})
}
}
type MockCSU struct {
CalledRepo models.Repo
CalledPull models.PullRequest
CalledStatus models.CommitStatus
CalledCommand models.CommandName
CalledNumSuccess int
CalledNumTotal int
}
func (m *MockCSU) UpdateCombinedCount(repo models.Repo, pull models.PullRequest, status models.CommitStatus, command models.CommandName, numSuccess int, numTotal int) error {
m.CalledRepo = repo
m.CalledPull = pull
m.CalledStatus = status
m.CalledCommand = command
m.CalledNumSuccess = numSuccess
m.CalledNumTotal = numTotal
return nil
}
func (m *MockCSU) UpdateCombined(repo models.Repo, pull models.PullRequest, status models.CommitStatus, command models.CommandName) error {
return nil
}
func (m *MockCSU) UpdateProject(ctx models.ProjectCommandContext, cmdName models.CommandName, status models.CommitStatus, url string) error {
return nil
}