From d1d69076400a9c5799cfd0335984c1a71735a21a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 16 Dec 2016 09:39:13 -0500 Subject: [PATCH] Add a provider test for a list of maps Interpolation of a map from a list of maps was not working. Add a provider example test to cover this. --- builtin/providers/test/resource_test.go | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/builtin/providers/test/resource_test.go b/builtin/providers/test/resource_test.go index ee46f0b51..a8d9250ac 100644 --- a/builtin/providers/test/resource_test.go +++ b/builtin/providers/test/resource_test.go @@ -1,6 +1,7 @@ package test import ( + "reflect" "regexp" "strings" "testing" @@ -427,6 +428,65 @@ variable "maplist" { }) } +func TestResource_dataSourceIndexMapList(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "val" + + required_map = { + x = "y" + } + + list_of_map = [ + { + a = "1" + b = "2" + }, + { + c = "3" + d = "4" + }, + ] +} + +output "map_from_list" { + value = "${test_resource.foo.list_of_map[0]}" +} + +output "value_from_map_from_list" { + value = "${lookup(test_resource.foo.list_of_map[1], "d")}" +} + `), + ExpectError: nil, + Check: func(s *terraform.State) error { + root := s.ModuleByPath(terraform.RootModulePath) + mapOut := root.Outputs["map_from_list"].Value + expectedMapOut := map[string]interface{}{ + "a": "1", + "b": "2", + } + + valueOut := root.Outputs["value_from_map_from_list"].Value + expectedValueOut := "4" + + if !reflect.DeepEqual(mapOut, expectedMapOut) { + t.Fatalf("Expected: %#v\nGot: %#v", expectedMapOut, mapOut) + } + if !reflect.DeepEqual(valueOut, expectedValueOut) { + t.Fatalf("Expected: %#v\nGot: %#v", valueOut, expectedValueOut) + } + return nil + }, + }, + }, + }) +} + func testAccCheckResourceDestroy(s *terraform.State) error { return nil }