Merge pull request #228 from runatlantis/init-error

Return error if `terraform init` fails.
This commit is contained in:
Luke Kysow
2018-08-17 14:03:17 -10:00
committed by GitHub
2 changed files with 34 additions and 7 deletions

View File

@@ -16,14 +16,19 @@ func (i *InitStepRunner) Run(ctx models.ProjectCommandContext, extraArgs []strin
if ctx.ProjectConfig != nil && ctx.ProjectConfig.TerraformVersion != nil {
tfVersion = ctx.ProjectConfig.TerraformVersion
}
terraformInitCmd := append([]string{"init", "-no-color"}, extraArgs...)
// If we're running < 0.9 we have to use `terraform get` instead of `init`.
if MustConstraint("< 0.9.0").Check(tfVersion) {
ctx.Log.Info("running terraform version %s so will use `get` instead of `init`", tfVersion)
terraformGetCmd := append([]string{"get", "-no-color"}, extraArgs...)
_, err := i.TerraformExecutor.RunCommandWithVersion(ctx.Log, path, terraformGetCmd, tfVersion, ctx.Workspace)
return "", err
} else {
_, err := i.TerraformExecutor.RunCommandWithVersion(ctx.Log, path, append([]string{"init", "-no-color"}, extraArgs...), tfVersion, ctx.Workspace)
return "", err
terraformInitCmd = append([]string{"get", "-no-color"}, extraArgs...)
}
out, err := i.TerraformExecutor.RunCommandWithVersion(ctx.Log, path, terraformInitCmd, tfVersion, ctx.Workspace)
// Only include the init output if there was an error. Otherwise it's
// unnecessary and lengthens the comment.
if err != nil {
return out, err
}
return "", nil
}

View File

@@ -5,6 +5,7 @@ import (
version "github.com/hashicorp/go-version"
. "github.com/petergtz/pegomock"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/runtime"
@@ -57,10 +58,31 @@ func TestRun_UsesGetOrInitForRightVersion(t *testing.T) {
RepoRelDir: ".",
}, []string{"extra", "args"}, "/path")
Ok(t, err)
// Shouldn't return output since we don't print init output to PR.
// When there is no error, should not return init output to PR.
Equals(t, "", output)
terraform.VerifyWasCalledOnce().RunCommandWithVersion(logger, "/path", []string{c.expCmd, "-no-color", "extra", "args"}, tfVersion, "workspace")
})
}
}
func TestRun_ShowInitOutputOnError(t *testing.T) {
// If there was an error during init then we want the output to be returned.
RegisterMockTestingT(t)
tfClient := mocks.NewMockClient()
When(tfClient.RunCommandWithVersion(matchers.AnyPtrToLoggingSimpleLogger(), AnyString(), AnyStringSlice(), matchers2.AnyPtrToGoVersionVersion(), AnyString())).
ThenReturn("output", errors.New("error"))
tfVersion, _ := version.NewVersion("0.11.0")
iso := runtime.InitStepRunner{
TerraformExecutor: tfClient,
DefaultTFVersion: tfVersion,
}
output, err := iso.Run(models.ProjectCommandContext{
Workspace: "workspace",
RepoRelDir: ".",
}, nil, "/path")
ErrEquals(t, "error", err)
Equals(t, "output", output)
}