Skip to content

Commit 609219f

Browse files
mitchellhjen20
authored andcommitted
command/meta: validate config immediately
* config: test for validating multi-vars (passes) * command/plan: test invalid run * command/meta: validate module on load
1 parent d31656a commit 609219f

File tree

8 files changed

+65
-4
lines changed

8 files changed

+65
-4
lines changed

command/command.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"log"
56

67
"github.com/hashicorp/terraform/terraform"
78
"github.com/mitchellh/cli"
@@ -27,7 +28,11 @@ const DefaultBackupExtension = ".backup"
2728
const DefaultParallelism = 10
2829

2930
func validateContext(ctx *terraform.Context, ui cli.Ui) bool {
30-
if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
31+
log.Println("[INFO] Validating the context...")
32+
ws, es := ctx.Validate()
33+
log.Printf("[INFO] Validation result: %d warnings, %d errors", len(ws), len(es))
34+
35+
if len(ws) > 0 || len(es) > 0 {
3136
ui.Output(
3237
"There are warnings and/or errors related to your configuration. Please\n" +
3338
"fix these before continuing.\n")

command/meta.go

+5
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) {
165165
return nil, false, fmt.Errorf("Error downloading modules: %s", err)
166166
}
167167

168+
// Validate the module right away
169+
if err := mod.Validate(); err != nil {
170+
return nil, false, err
171+
}
172+
168173
opts.Module = mod
169174
opts.Parallelism = copts.Parallelism
170175
opts.State = state.State()

command/plan_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,40 @@ func TestPlan_statePast(t *testing.T) {
395395
}
396396
}
397397

398+
func TestPlan_validate(t *testing.T) {
399+
// This is triggered by not asking for input so we have to set this to false
400+
test = false
401+
defer func() { test = true }()
402+
403+
cwd, err := os.Getwd()
404+
if err != nil {
405+
t.Fatalf("err: %s", err)
406+
}
407+
if err := os.Chdir(testFixturePath("plan-invalid")); err != nil {
408+
t.Fatalf("err: %s", err)
409+
}
410+
defer os.Chdir(cwd)
411+
412+
p := testProvider()
413+
ui := new(cli.MockUi)
414+
c := &PlanCommand{
415+
Meta: Meta{
416+
ContextOpts: testCtxConfig(p),
417+
Ui: ui,
418+
},
419+
}
420+
421+
args := []string{}
422+
if code := c.Run(args); code != 1 {
423+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
424+
}
425+
426+
actual := ui.ErrorWriter.String()
427+
if !strings.Contains(actual, "can't reference") {
428+
t.Fatalf("bad: %s", actual)
429+
}
430+
}
431+
398432
func TestPlan_vars(t *testing.T) {
399433
p := testProvider()
400434
ui := new(cli.MockUi)

command/push_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,7 @@ func pushTFVars() []atlas.TFVar {
765765
return []atlas.TFVar{
766766
{"bar", "foo", false},
767767
{"baz", `{
768-
A = "a"
769-
interp = "${file("t.txt")}"
768+
A = "a"
770769
}`, true},
771770
{"fob", `["a", "quotes \"in\" quotes"]`, true},
772771
{"foo", "bar", false},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "test_instance" "foo" {
2+
count = 5
3+
}
4+
5+
resource "test_instance" "bar" {
6+
count = "${length(test_instance.foo.*.id)}"
7+
}

command/test-fixtures/push-tfvars/main.tf

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ variable "baz" {
77

88
default = {
99
"A" = "a"
10-
interp = "${file("t.txt")}"
1110
}
1211
}
1312

config/config_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ func TestConfigValidate_countResourceVar(t *testing.T) {
195195
}
196196
}
197197

198+
func TestConfigValidate_countResourceVarMulti(t *testing.T) {
199+
c := testConfig(t, "validate-count-resource-var-multi")
200+
if err := c.Validate(); err == nil {
201+
t.Fatal("should not be valid")
202+
}
203+
}
204+
198205
func TestConfigValidate_countUserVar(t *testing.T) {
199206
c := testConfig(t, "validate-count-user-var")
200207
if err := c.Validate(); err != nil {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_instance" "foo" {}
2+
3+
resource "aws_instance" "web" {
4+
count = "${length(aws_instance.foo.*.bar)}"
5+
}

0 commit comments

Comments
 (0)