Skip to content

Commit

Permalink
command/meta: validate config immediately
Browse files Browse the repository at this point in the history
* config: test for validating multi-vars (passes)

* command/plan: test invalid run

* command/meta: validate module on load
  • Loading branch information
mitchellh authored and jen20 committed Sep 3, 2016
1 parent d31656a commit 609219f
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 4 deletions.
7 changes: 6 additions & 1 deletion command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"log"

"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
Expand All @@ -27,7 +28,11 @@ const DefaultBackupExtension = ".backup"
const DefaultParallelism = 10

func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
log.Println("[INFO] Validating the context...")
ws, es := ctx.Validate()
log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))

if len(ws) > 0 || len(es) > 0 {
ui.Output(
"There are warnings and/or errors related to your configuration. Please\n" +
"fix these before continuing.\n")
Expand Down
5 changes: 5 additions & 0 deletions command/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
return nil, false, fmt.Errorf("Error downloading modules: %s", err)
}

// Validate the module right away
if err := mod.Validate(); err != nil {
return nil, false, err
}

opts.Module = mod
opts.Parallelism = copts.Parallelism
opts.State = state.State()
Expand Down
34 changes: 34 additions & 0 deletions command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,40 @@ func TestPlan_statePast(t *testing.T) {
}
}

func TestPlan_validate(t *testing.T) {
// This is triggered by not asking for input so we have to set this to false
test = false
defer func() { test = true }()

cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(testFixturePath("plan-invalid")); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)

p := testProvider()
ui := new(cli.MockUi)
c := &PlanCommand{
Meta: Meta{
ContextOpts: testCtxConfig(p),
Ui: ui,
},
}

args := []string{}
if code := c.Run(args); code != 1 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}

actual := ui.ErrorWriter.String()
if !strings.Contains(actual, "can't reference") {
t.Fatalf("bad: %s", actual)
}
}

func TestPlan_vars(t *testing.T) {
p := testProvider()
ui := new(cli.MockUi)
Expand Down
3 changes: 1 addition & 2 deletions command/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,7 @@ func pushTFVars() []atlas.TFVar {
return []atlas.TFVar{
{"bar", "foo", false},
{"baz", `{
A = "a"
interp = "${file("t.txt")}"
A = "a"
}`, true},
{"fob", `["a", "quotes \"in\" quotes"]`, true},
{"foo", "bar", false},
Expand Down
7 changes: 7 additions & 0 deletions command/test-fixtures/plan-invalid/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "test_instance" "foo" {
count = 5
}

resource "test_instance" "bar" {
count = "${length(test_instance.foo.*.id)}"
}
1 change: 0 additions & 1 deletion command/test-fixtures/push-tfvars/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ variable "baz" {

default = {
"A" = "a"
interp = "${file("t.txt")}"
}
}

Expand Down
7 changes: 7 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ func TestConfigValidate_countResourceVar(t *testing.T) {
}
}

func TestConfigValidate_countResourceVarMulti(t *testing.T) {
c := testConfig(t, "validate-count-resource-var-multi")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}

func TestConfigValidate_countUserVar(t *testing.T) {
c := testConfig(t, "validate-count-user-var")
if err := c.Validate(); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "aws_instance" "foo" {}

resource "aws_instance" "web" {
count = "${length(aws_instance.foo.*.bar)}"
}

0 comments on commit 609219f

Please sign in to comment.