From 0e277a6714eb33f828850f0660aa6988477baf12 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Tue, 24 Nov 2015 15:57:38 -0600 Subject: [PATCH] core: test coverage around map key regression tests to cover the HCL-level issue fixed in https://github.com/hashicorp/hcl/pull/65 --- command/flag_kv_test.go | 14 ++++++- terraform/context_apply_test.go | 40 +++++++++++++++++++ .../apply-map-var-override/main.tf | 14 +++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/apply-map-var-override/main.tf diff --git a/command/flag_kv_test.go b/command/flag_kv_test.go index a134a1692..07b70d022 100644 --- a/command/flag_kv_test.go +++ b/command/flag_kv_test.go @@ -35,6 +35,12 @@ func TestFlagKV(t *testing.T) { false, }, + { + "map.key=foo", + map[string]string{"map.key": "foo"}, + false, + }, + { "key", nil, @@ -84,6 +90,12 @@ foo = "bar" map[string]string{"foo": "bar"}, false, }, + + { + `map.key = "foo"`, + map[string]string{"map.key": "foo"}, + false, + }, } path := testTempFile(t) @@ -96,7 +108,7 @@ foo = "bar" f := new(FlagKVFile) err := f.Set(path) if err != nil != tc.Error { - t.Fatalf("bad error. Input: %#v", tc.Input) + t.Fatalf("bad error. Input: %#v, err: %s", tc.Input, err) } actual := map[string]string(*f) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 4060dd3e3..b6fea26fc 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -807,6 +807,46 @@ func TestContext2Apply_countVariable(t *testing.T) { } } +func TestContext2Apply_mapVariableOverride(t *testing.T) { + m := testModule(t, "apply-map-var-override") + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + ctx := testContext2(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + Variables: map[string]string{ + "images.us-west-2": "overridden", + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + state, err := ctx.Apply() + if err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(state.String()) + expected := strings.TrimSpace(` +aws_instance.bar: + ID = foo + ami = overridden + type = aws_instance +aws_instance.foo: + ID = foo + ami = image-1234 + type = aws_instance + `) + if actual != expected { + t.Fatalf("got: \n%s\nexpected: \n%s", actual, expected) + } +} + func TestContext2Apply_module(t *testing.T) { m := testModule(t, "apply-module") p := testProvider("aws") diff --git a/terraform/test-fixtures/apply-map-var-override/main.tf b/terraform/test-fixtures/apply-map-var-override/main.tf new file mode 100644 index 000000000..bd0fadcaa --- /dev/null +++ b/terraform/test-fixtures/apply-map-var-override/main.tf @@ -0,0 +1,14 @@ +variable "images" { + default = { + us-east-1 = "image-1234" + us-west-2 = "image-4567" + } +} + +resource "aws_instance" "foo" { + ami = "${lookup(var.images, "us-east-1")}" +} + +resource "aws_instance" "bar" { + ami = "${lookup(var.images, "us-west-2")}" +}