Merge pull request #304 from runatlantis/quote-planpath

Quote args and plan path that may contain spaces.
This commit is contained in:
Luke Kysow
2018-10-01 20:22:12 -05:00
committed by GitHub
4 changed files with 37 additions and 32 deletions

View File

@@ -21,7 +21,9 @@ func (a *ApplyStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []stri
return "", fmt.Errorf("no plan found at path %q and workspace %qdid you run plan?", ctx.RepoRelDir, ctx.Workspace)
}
tfApplyCmd := append(append(append([]string{"apply", "-input=false", "-no-color"}, extraArgs...), ctx.CommentArgs...), planPath)
// NOTE: we need to quote the plan path because Bitbucket Server can
// have spaces in its repo owner names which is part of the path.
tfApplyCmd := append(append(append([]string{"apply", "-input=false", "-no-color"}, extraArgs...), ctx.CommentArgs...), fmt.Sprintf("%q", planPath))
var tfVersion *version.Version
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion

View File

@@ -1,6 +1,7 @@
package runtime_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -63,7 +64,7 @@ func TestRun_Success(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, nil, "workspace")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, nil, "workspace")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
@@ -95,7 +96,7 @@ func TestRun_AppliesCorrectProjectPlan(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, nil, "default")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, nil, "default")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}
@@ -126,7 +127,7 @@ func TestRun_UsesConfiguredTFVersion(t *testing.T) {
}, []string{"extra", "args"}, tmpDir)
Ok(t, err)
Equals(t, "output", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", planPath}, tfVersion, "workspace")
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, tmpDir, []string{"apply", "-input=false", "-no-color", "extra", "args", "comment", "args", fmt.Sprintf("%q", planPath)}, tfVersion, "workspace")
_, err = os.Stat(planPath)
Assert(t, os.IsNotExist(err), "planfile should be deleted")
}

View File

@@ -117,15 +117,17 @@ func (p *PlanStepRunner) buildPlanCmd(ctx models.ProjectCommandContext, extraArg
func (p *PlanStepRunner) tfVars(ctx models.ProjectCommandContext) []string {
// NOTE: not using maps and looping here because we need to keep the
// ordering for testing purposes.
// NOTE: quoting the values because in Bitbucket the owner can have
// spaces, ex -var atlantis_repo_owner="bitbucket owner".
return []string{
"-var",
fmt.Sprintf("%s=%s", "atlantis_user", ctx.User.Username),
fmt.Sprintf("%s=%q", "atlantis_user", ctx.User.Username),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo", ctx.BaseRepo.FullName),
fmt.Sprintf("%s=%q", "atlantis_repo", ctx.BaseRepo.FullName),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo_name", ctx.BaseRepo.Name),
fmt.Sprintf("%s=%q", "atlantis_repo_name", ctx.BaseRepo.Name),
"-var",
fmt.Sprintf("%s=%s", "atlantis_repo_owner", ctx.BaseRepo.Owner),
fmt.Sprintf("%s=%q", "atlantis_repo_owner", ctx.BaseRepo.Owner),
"-var",
fmt.Sprintf("%s=%d", "atlantis_pull_num", ctx.Pull.Num),
}

View File

@@ -63,13 +63,13 @@ func TestRun_NoWorkspaceIn08(t *testing.T) {
"-out",
"\"/path/default.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
@@ -198,13 +198,13 @@ func TestRun_SwitchesWorkspace(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
@@ -267,13 +267,13 @@ func TestRun_CreatesWorkspace(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
@@ -327,13 +327,13 @@ func TestRun_NoWorkspaceSwitchIfNotNecessary(t *testing.T) {
"-out",
"\"/path/workspace.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
@@ -395,13 +395,13 @@ func TestRun_AddsEnvVarFile(t *testing.T) {
"-out",
fmt.Sprintf("%q", filepath.Join(tmpDir, "workspace.tfplan")),
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",
@@ -456,13 +456,13 @@ func TestRun_UsesDiffPathForProject(t *testing.T) {
"-out",
"\"/path/projectname-default.tfplan\"",
"-var",
"atlantis_user=username",
"atlantis_user=\"username\"",
"-var",
"atlantis_repo=owner/repo",
"atlantis_repo=\"owner/repo\"",
"-var",
"atlantis_repo_name=repo",
"atlantis_repo_name=\"repo\"",
"-var",
"atlantis_repo_owner=owner",
"atlantis_repo_owner=\"owner\"",
"-var",
"atlantis_pull_num=2",
"extra",