Files
atlantis/server/events/runtime/policy_check_step_runner_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

77 lines
2.3 KiB
Go

package runtime
import (
"errors"
"testing"
"github.com/hashicorp/go-version"
. "github.com/petergtz/pegomock"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/runtime/mocks"
"github.com/runatlantis/atlantis/server/events/yaml/valid"
"github.com/runatlantis/atlantis/server/logging"
. "github.com/runatlantis/atlantis/testing"
)
func TestRun(t *testing.T) {
RegisterMockTestingT(t)
logger := logging.NewNoopLogger()
workspace := "default"
v, _ := version.NewVersion("1.0")
workdir := "/path"
executablePath := "some/path/conftest"
context := models.ProjectCommandContext{
Log: logger,
EscapedCommentArgs: []string{"comment", "args"},
Workspace: workspace,
RepoRelDir: ".",
User: models.User{Username: "username"},
Pull: models.PullRequest{
Num: 2,
},
BaseRepo: models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
},
PolicySets: valid.PolicySets{
Version: v,
PolicySets: []valid.PolicySet{},
},
}
executorWorkflow := mocks.NewMockVersionedExecutorWorkflow()
s := &PolicyCheckStepRunner{
versionEnsurer: executorWorkflow,
executor: executorWorkflow,
}
t.Run("success", func(t *testing.T) {
When(executorWorkflow.EnsureExecutorVersion(logger, v)).ThenReturn(executablePath, nil)
When(executorWorkflow.Run(context, executablePath, map[string]string(nil), workdir)).ThenReturn("Success!", nil)
output, err := s.Run(context, []string{"extra", "args"}, workdir, map[string]string(nil))
Ok(t, err)
Equals(t, "Success!", output)
})
t.Run("ensure version failure", func(t *testing.T) {
expectedErr := errors.New("error ensuring version")
When(executorWorkflow.EnsureExecutorVersion(logger, v)).ThenReturn("", expectedErr)
_, err := s.Run(context, []string{"extra", "args"}, workdir, map[string]string(nil))
Assert(t, err != nil, "error is not nil")
})
t.Run("executor failure", func(t *testing.T) {
When(executorWorkflow.EnsureExecutorVersion(logger, v)).ThenReturn(executablePath, nil)
When(executorWorkflow.Run(context, executablePath, map[string]string(nil), workdir)).ThenReturn("", errors.New("error running executor"))
_, err := s.Run(context, []string{"extra", "args"}, workdir, map[string]string(nil))
Assert(t, err != nil, "error is not nil")
})
}