From d714c6f2f1d520e13b170333e040f0582415fd38 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 7 Oct 2014 20:09:30 -0700 Subject: [PATCH] terraform: test path variables --- config/interpolate.go | 6 +++ terraform/context.go | 21 +++++++++- terraform/context_test.go | 38 +++++++++++++++++++ terraform/terraform_test.go | 15 ++++++++ terraform/test-fixtures/plan-path-var/main.tf | 5 +++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/plan-path-var/main.tf diff --git a/config/interpolate.go b/config/interpolate.go index fc8ddea72..e96b6b76c 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -86,7 +86,9 @@ type PathValueType byte const ( PathValueInvalid PathValueType = iota + PathValueCwd PathValueModule + PathValueRoot ) // A ResourceVariable is a variable that is referencing the field @@ -226,8 +228,12 @@ func NewPathVariable(key string) (*PathVariable, error) { var fieldType PathValueType parts := strings.SplitN(key, ".", 2) switch parts[1] { + case "cwd": + fieldType = PathValueCwd case "module": fieldType = PathValueModule + case "root": + fieldType = PathValueRoot } return &PathVariable{ diff --git a/terraform/context.go b/terraform/context.go index fa3ff1065..c70634a9a 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -3,6 +3,7 @@ package terraform import ( "fmt" "log" + "os" "sort" "strconv" "strings" @@ -54,7 +55,7 @@ type ContextOpts struct { Provisioners map[string]ResourceProvisionerFactory Variables map[string]string - UIInput UIInput + UIInput UIInput } // NewContext creates a new context. @@ -1437,6 +1438,24 @@ func (c *walkContext) computeVars( } vs[n] = value + case *config.PathVariable: + switch v.Type { + case config.PathValueCwd: + wd, err := os.Getwd() + if err != nil { + return fmt.Errorf( + "Couldn't get cwd for var %s: %s", + v.FullKey(), err) + } + + vs[n] = wd + case config.PathValueModule: + if t := c.Context.module.Child(c.Path[1:]); t != nil { + vs[n] = t.Config().Dir + } + case config.PathValueRoot: + vs[n] = c.Context.module.Config().Dir + } case *config.ResourceVariable: var attr string var err error diff --git a/terraform/context_test.go b/terraform/context_test.go index 3d2c1cf3a..8134dc674 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -2,6 +2,7 @@ package terraform import ( "fmt" + "os" "reflect" "sort" "strings" @@ -2822,6 +2823,43 @@ func TestContextPlan_moduleDestroy(t *testing.T) { } } +func TestContextPlan_pathVar(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + + m := testModule(t, "plan-path-var") + p := testProvider("aws") + p.DiffFn = testDiffFn + ctx := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + plan, err := ctx.Plan(nil) + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(plan.String()) + expected := strings.TrimSpace(testTerraformPlanPathVarStr) + + // Warning: this ordering REALLY matters for this test. The + // order is: cwd, module, root. + expected = fmt.Sprintf( + expected, + cwd, + m.Config().Dir, + m.Config().Dir) + + if actual != expected { + t.Fatalf("bad:\n%s\n\nexpected:\n\n%s", actual, expected) + } +} + func TestContextPlan_diffVar(t *testing.T) { m := testModule(t, "plan-diffvar") p := testProvider("aws") diff --git a/terraform/terraform_test.go b/terraform/terraform_test.go index c0ac52537..6aedf6447 100644 --- a/terraform/terraform_test.go +++ b/terraform/terraform_test.go @@ -875,3 +875,18 @@ STATE: ` + +const testTerraformPlanPathVarStr = ` +DIFF: + +CREATE: aws_instance.foo + cwd: "" => "%s/barpath" + module: "" => "%s/foopath" + root: "" => "%s/barpath" + type: "" => "aws_instance" + +STATE: + + +` + diff --git a/terraform/test-fixtures/plan-path-var/main.tf b/terraform/test-fixtures/plan-path-var/main.tf new file mode 100644 index 000000000..130125698 --- /dev/null +++ b/terraform/test-fixtures/plan-path-var/main.tf @@ -0,0 +1,5 @@ +resource "aws_instance" "foo" { + cwd = "${path.cwd}/barpath" + module = "${path.module}/foopath" + root = "${path.root}/barpath" +}