Skip to content

Commit

Permalink
Extract code from plan/apply into ProjectPreExecute. Test
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Oct 20, 2017
1 parent 031a1eb commit 1586b62
Show file tree
Hide file tree
Showing 12 changed files with 696 additions and 48 deletions.
10 changes: 5 additions & 5 deletions server/events/apply_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
)

type ApplyExecutor struct {
Github github.Client
Terraform *terraform.Client
RequireApproval bool
Run *run.Run
Workspace Workspace
Github github.Client
Terraform *terraform.Client
RequireApproval bool
Run *run.Run
Workspace Workspace
ProjectPreExecute *ProjectPreExecute
}

Expand Down
118 changes: 118 additions & 0 deletions server/events/mocks/mock_project_config_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Automatically generated by pegomock. DO NOT EDIT!
// Source: github.com/hootsuite/atlantis/server/events (interfaces: ProjectConfigReader)

package mocks

import (
events "github.com/hootsuite/atlantis/server/events"
pegomock "github.com/petergtz/pegomock"
"reflect"
)

type MockProjectConfigReader struct {
fail func(message string, callerSkip ...int)
}

func NewMockProjectConfigReader() *MockProjectConfigReader {
return &MockProjectConfigReader{fail: pegomock.GlobalFailHandler}
}

func (mock *MockProjectConfigReader) Exists(execPath string) bool {
params := []pegomock.Param{execPath}
result := pegomock.GetGenericMockFrom(mock).Invoke("Exists", params, []reflect.Type{reflect.TypeOf((*bool)(nil)).Elem()})
var ret0 bool
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(bool)
}
}
return ret0
}

func (mock *MockProjectConfigReader) Read(execPath string) (events.ProjectConfig, error) {
params := []pegomock.Param{execPath}
result := pegomock.GetGenericMockFrom(mock).Invoke("Read", params, []reflect.Type{reflect.TypeOf((*events.ProjectConfig)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()})
var ret0 events.ProjectConfig
var ret1 error
if len(result) != 0 {
if result[0] != nil {
ret0 = result[0].(events.ProjectConfig)
}
if result[1] != nil {
ret1 = result[1].(error)
}
}
return ret0, ret1
}

func (mock *MockProjectConfigReader) VerifyWasCalledOnce() *VerifierProjectConfigReader {
return &VerifierProjectConfigReader{mock, pegomock.Times(1), nil}
}

func (mock *MockProjectConfigReader) VerifyWasCalled(invocationCountMatcher pegomock.Matcher) *VerifierProjectConfigReader {
return &VerifierProjectConfigReader{mock, invocationCountMatcher, nil}
}

func (mock *MockProjectConfigReader) VerifyWasCalledInOrder(invocationCountMatcher pegomock.Matcher, inOrderContext *pegomock.InOrderContext) *VerifierProjectConfigReader {
return &VerifierProjectConfigReader{mock, invocationCountMatcher, inOrderContext}
}

type VerifierProjectConfigReader struct {
mock *MockProjectConfigReader
invocationCountMatcher pegomock.Matcher
inOrderContext *pegomock.InOrderContext
}

func (verifier *VerifierProjectConfigReader) Exists(execPath string) *ProjectConfigReader_Exists_OngoingVerification {
params := []pegomock.Param{execPath}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "Exists", params)
return &ProjectConfigReader_Exists_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type ProjectConfigReader_Exists_OngoingVerification struct {
mock *MockProjectConfigReader
methodInvocations []pegomock.MethodInvocation
}

func (c *ProjectConfigReader_Exists_OngoingVerification) GetCapturedArguments() string {
execPath := c.GetAllCapturedArguments()
return execPath[len(execPath)-1]
}

func (c *ProjectConfigReader_Exists_OngoingVerification) GetAllCapturedArguments() (_param0 []string) {
params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
if len(params) > 0 {
_param0 = make([]string, len(params[0]))
for u, param := range params[0] {
_param0[u] = param.(string)
}
}
return
}

func (verifier *VerifierProjectConfigReader) Read(execPath string) *ProjectConfigReader_Read_OngoingVerification {
params := []pegomock.Param{execPath}
methodInvocations := pegomock.GetGenericMockFrom(verifier.mock).Verify(verifier.inOrderContext, verifier.invocationCountMatcher, "Read", params)
return &ProjectConfigReader_Read_OngoingVerification{mock: verifier.mock, methodInvocations: methodInvocations}
}

type ProjectConfigReader_Read_OngoingVerification struct {
mock *MockProjectConfigReader
methodInvocations []pegomock.MethodInvocation
}

func (c *ProjectConfigReader_Read_OngoingVerification) GetCapturedArguments() string {
execPath := c.GetAllCapturedArguments()
return execPath[len(execPath)-1]
}

func (c *ProjectConfigReader_Read_OngoingVerification) GetAllCapturedArguments() (_param0 []string) {
params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)
if len(params) > 0 {
_param0 = make([]string, len(params[0]))
for u, param := range params[0] {
_param0[u] = param.(string)
}
}
return
}
14 changes: 7 additions & 7 deletions server/events/plan_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"strings"

"github.com/hootsuite/atlantis/server/events/github"
"github.com/hootsuite/atlantis/server/events/locking"
"github.com/hootsuite/atlantis/server/events/models"
"github.com/hootsuite/atlantis/server/events/run"
"github.com/hootsuite/atlantis/server/events/terraform"
"github.com/pkg/errors"
"github.com/hootsuite/atlantis/server/events/locking"
)

//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_lock_url_generator.go LockURLGenerator
Expand All @@ -30,12 +30,12 @@ const atlantisUserTFVar = "atlantis_user"
// PlanExecutor handles everything related to running terraform plan
// including integration with S3, Terraform, and GitHub
type PlanExecutor struct {
Github github.Client
Terraform *terraform.Client
Locker locking.Locker
LockURL func(id string) (url string)
Run *run.Run
Workspace Workspace
Github github.Client
Terraform *terraform.Client
Locker locking.Locker
LockURL func(id string) (url string)
Run *run.Run
Workspace Workspace
ProjectPreExecute *ProjectPreExecute
}

Expand Down
12 changes: 9 additions & 3 deletions server/events/project_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ type PostApply struct {
Commands []string `yaml:"commands"`
}

type ConfigReader struct{}
//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_project_config_reader.go ProjectConfigReader
type ProjectConfigReader interface {
Exists(execPath string) bool
Read(execPath string) (ProjectConfig, error)
}

type ProjectConfigManager struct{}

type ProjectConfigYaml struct {
PrePlan PrePlan `yaml:"pre_plan"`
Expand All @@ -54,13 +60,13 @@ type CommandExtraArguments struct {
Arguments []string `yaml:"arguments"`
}

func (c *ConfigReader) Exists(execPath string) bool {
func (c *ProjectConfigManager) Exists(execPath string) bool {
// Check if config file exists
_, err := os.Stat(filepath.Join(execPath, ProjectConfigFile))
return err == nil
}

func (c *ConfigReader) Read(execPath string) (ProjectConfig, error) {
func (c *ProjectConfigManager) Read(execPath string) (ProjectConfig, error) {
var pc ProjectConfig
filename := filepath.Join(execPath, ProjectConfigFile)
raw, err := ioutil.ReadFile(filename)
Expand Down
8 changes: 4 additions & 4 deletions server/events/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ extra_arguments:
`

func TestConfigFileExists_invalid_path(t *testing.T) {
var c events.ConfigReader
var c events.ProjectConfigManager
Equals(t, c.Exists("/invalid/path"), false)
}

func TestConfigFileExists_valid_path(t *testing.T) {
var c events.ConfigReader
var c events.ProjectConfigManager
writeAtlantisConfigFile([]byte(projectConfigFileStr))
defer os.Remove(tempConfigFile)
Equals(t, c.Exists("/tmp"), true)
}

func TestConfigFileRead_invalid_config(t *testing.T) {
var c events.ConfigReader
var c events.ProjectConfigManager
str := []byte(`---invalid`)
writeAtlantisConfigFile(str)
defer os.Remove(tempConfigFile)
Expand All @@ -52,7 +52,7 @@ func TestConfigFileRead_invalid_config(t *testing.T) {
}

func TestConfigFileRead_valid_config(t *testing.T) {
var c events.ConfigReader
var c events.ProjectConfigManager
writeAtlantisConfigFile([]byte(projectConfigFileStr))
defer os.Remove(tempConfigFile)
_, err := c.Read("/tmp")
Expand Down
27 changes: 14 additions & 13 deletions server/events/project_pre_execute.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package events

import (
"path/filepath"
"fmt"
"path/filepath"
"strings"

"github.com/hashicorp/go-version"
"github.com/hootsuite/atlantis/server/events/locking"
"github.com/pkg/errors"
"github.com/hootsuite/atlantis/server/events/models"
"github.com/hootsuite/atlantis/server/events/terraform"
"github.com/hashicorp/go-version"
"github.com/hootsuite/atlantis/server/events/run"
"strings"
"github.com/hootsuite/atlantis/server/events/terraform"
"github.com/pkg/errors"
)

type ProjectPreExecute struct {
Locker locking.Locker
ConfigReader *ConfigReader
Terraform *terraform.Client
Run *run.Run
Locker locking.Locker
ConfigReader ProjectConfigReader
Terraform terraform.Runner
Run run.Runner
}

type PreExecuteResult struct {
ProjectResult ProjectResult
ProjectConfig ProjectConfig
ProjectResult ProjectResult
ProjectConfig ProjectConfig
TerraformVersion *version.Version
LockResponse locking.TryLockResponse
LockResponse locking.TryLockResponse
}

func (p *ProjectPreExecute) Execute(ctx *CommandContext, repoDir string, project models.Project) PreExecuteResult {
Expand Down Expand Up @@ -81,7 +82,7 @@ func (p *ProjectPreExecute) Execute(ctx *CommandContext, repoDir string, project
if len(commands) > 0 {
_, err := p.Run.Execute(ctx.Log, commands, absolutePath, tfEnv, terraformVersion, stage)
if err != nil {
return PreExecuteResult{ProjectResult: ProjectResult{Error: errors.Wrapf(err, "running pre %s commands", stage)}}
return PreExecuteResult{ProjectResult: ProjectResult{Error: errors.Wrapf(err, "running %s commands", stage)}}
}
}
return PreExecuteResult{ProjectConfig: config, TerraformVersion: terraformVersion, LockResponse: lockAttempt}
Expand Down
Loading

0 comments on commit 1586b62

Please sign in to comment.