Skip to content

Commit

Permalink
import step runner requires workspace delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
krrrr38 committed Jan 5, 2023
1 parent 824da7a commit 7d12f31
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
5 changes: 1 addition & 4 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1151,10 +1151,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
ApplyStepRunner: &runtime.ApplyStepRunner{
TerraformExecutor: terraformClient,
},
ImportStepRunner: &runtime.ImportStepRunner{
TerraformExecutor: terraformClient,
DefaultTFVersion: defaultTFVersion,
},
ImportStepRunner: runtime.NewImportStepRunner(terraformClient, defaultTFVersion),
RunStepRunner: &runtime.RunStepRunner{
TerraformExecutor: terraformClient,
DefaultTFVersion: defaultTFVersion,
Expand Down
20 changes: 14 additions & 6 deletions server/core/runtime/import_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,29 @@ import (
"github.com/runatlantis/atlantis/server/events/command"
)

type ImportStepRunner struct {
TerraformExecutor TerraformExec
DefaultTFVersion *version.Version
type importStepRunner struct {
terraformExecutor TerraformExec
defaultTFVersion *version.Version
}

func (p *ImportStepRunner) Run(ctx command.ProjectContext, extraArgs []string, path string, envs map[string]string) (string, error) {
tfVersion := p.DefaultTFVersion
func NewImportStepRunner(terraformExecutor TerraformExec, defaultTfVersion *version.Version) Runner {
runner := &importStepRunner{
terraformExecutor: terraformExecutor,
defaultTFVersion: defaultTfVersion,
}
return NewWorkspaceStepRunnerDelegate(terraformExecutor, defaultTfVersion, runner)
}

func (p *importStepRunner) Run(ctx command.ProjectContext, extraArgs []string, path string, envs map[string]string) (string, error) {
tfVersion := p.defaultTFVersion
if ctx.TerraformVersion != nil {
tfVersion = ctx.TerraformVersion
}

importCmd := []string{"import"}
importCmd = append(importCmd, extraArgs...)
importCmd = append(importCmd, ctx.EscapedCommentArgs...)
out, err := p.TerraformExecutor.RunCommandWithVersion(ctx, filepath.Clean(path), importCmd, envs, tfVersion, ctx.Workspace)
out, err := p.terraformExecutor.RunCommandWithVersion(ctx, filepath.Clean(path), importCmd, envs, tfVersion, ctx.Workspace)

// If the import was successful and a plan file exists, delete the plan.
planPath := filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName))
Expand Down
53 changes: 38 additions & 15 deletions server/core/runtime/import_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/runatlantis/atlantis/server/core/terraform/mocks"
matchers2 "github.com/runatlantis/atlantis/server/core/terraform/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
. "github.com/runatlantis/atlantis/testing"
)
Expand All @@ -29,33 +28,57 @@ func TestImportStepRunner_Run_Success(t *testing.T) {
Log: logger,
EscapedCommentArgs: []string{"-var", "foo=bar", "addr", "id"},
Workspace: workspace,
RepoRelDir: ".",
User: models.User{Username: "username"},
Pull: models.PullRequest{
Num: 2,
},
BaseRepo: models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
},
}

RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
tfVersion, _ := version.NewVersion("0.15.0")
s := &ImportStepRunner{
TerraformExecutor: terraform,
DefaultTFVersion: tfVersion,
s := NewImportStepRunner(terraform, tfVersion)

When(terraform.RunCommandWithVersion(matchers.AnyCommandProjectContext(), AnyString(), AnyStringSlice(), matchers2.AnyMapOfStringToString(), matchers2.AnyPtrToGoVersionVersion(), AnyString())).
ThenReturn("output", nil)
output, err := s.Run(context, []string{}, tmpDir, map[string]string(nil))
Ok(t, err)
Equals(t, "output", output)
commands := []string{"import", "-var", "foo=bar", "addr", "id"}
terraform.VerifyWasCalledOnce().RunCommandWithVersion(context, tmpDir, commands, map[string]string(nil), tfVersion, workspace)
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}

func TestImportStepRunner_Run_Workspace(t *testing.T) {
logger := logging.NewNoopLogger(t)
workspace := "something"
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, fmt.Sprintf("%s.tfplan", workspace))
err := os.WriteFile(planPath, nil, 0600)
Ok(t, err)

context := command.ProjectContext{
Log: logger,
EscapedCommentArgs: []string{"-var", "foo=bar", "addr", "id"},
Workspace: workspace,
}

RegisterMockTestingT(t)
terraform := mocks.NewMockClient()
tfVersion, _ := version.NewVersion("0.15.0")
s := NewImportStepRunner(terraform, tfVersion)

When(terraform.RunCommandWithVersion(matchers.AnyCommandProjectContext(), AnyString(), AnyStringSlice(), matchers2.AnyMapOfStringToString(), matchers2.AnyPtrToGoVersionVersion(), AnyString())).
ThenReturn("output", nil)
output, err := s.Run(context, []string{}, tmpDir, map[string]string(nil))
Ok(t, err)
Equals(t, "output", output)

// switch workspace
terraform.VerifyWasCalledOnce().RunCommandWithVersion(context, tmpDir, []string{"workspace", "show"}, map[string]string(nil), tfVersion, workspace)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(context, tmpDir, []string{"workspace", "select", workspace}, map[string]string(nil), tfVersion, workspace)

// exec import
commands := []string{"import", "-var", "foo=bar", "addr", "id"}
terraform.VerifyWasCalledOnce().RunCommandWithVersion(context, tmpDir, commands, map[string]string(nil), tfVersion, "default")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(context, tmpDir, commands, map[string]string(nil), tfVersion, workspace)

_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
5 changes: 1 addition & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
TerraformExecutor: terraformClient,
DefaultTFVersion: defaultTfVersion,
},
ImportStepRunner: &runtime.ImportStepRunner{
TerraformExecutor: terraformClient,
DefaultTFVersion: defaultTfVersion,
},
ImportStepRunner: runtime.NewImportStepRunner(terraformClient, defaultTfVersion),
WorkingDir: workingDir,
Webhooks: webhooksManager,
WorkingDirLocker: workingDirLocker,
Expand Down

0 comments on commit 7d12f31

Please sign in to comment.