Skip to content

Commit d1d6907

Browse files
committed
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.
1 parent 5b5e892 commit d1d6907

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

builtin/providers/test/resource_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test
22

33
import (
4+
"reflect"
45
"regexp"
56
"strings"
67
"testing"
@@ -427,6 +428,65 @@ variable "maplist" {
427428
})
428429
}
429430

431+
func TestResource_dataSourceIndexMapList(t *testing.T) {
432+
resource.UnitTest(t, resource.TestCase{
433+
Providers: testAccProviders,
434+
CheckDestroy: testAccCheckResourceDestroy,
435+
Steps: []resource.TestStep{
436+
resource.TestStep{
437+
Config: strings.TrimSpace(`
438+
resource "test_resource" "foo" {
439+
required = "val"
440+
441+
required_map = {
442+
x = "y"
443+
}
444+
445+
list_of_map = [
446+
{
447+
a = "1"
448+
b = "2"
449+
},
450+
{
451+
c = "3"
452+
d = "4"
453+
},
454+
]
455+
}
456+
457+
output "map_from_list" {
458+
value = "${test_resource.foo.list_of_map[0]}"
459+
}
460+
461+
output "value_from_map_from_list" {
462+
value = "${lookup(test_resource.foo.list_of_map[1], "d")}"
463+
}
464+
`),
465+
ExpectError: nil,
466+
Check: func(s *terraform.State) error {
467+
root := s.ModuleByPath(terraform.RootModulePath)
468+
mapOut := root.Outputs["map_from_list"].Value
469+
expectedMapOut := map[string]interface{}{
470+
"a": "1",
471+
"b": "2",
472+
}
473+
474+
valueOut := root.Outputs["value_from_map_from_list"].Value
475+
expectedValueOut := "4"
476+
477+
if !reflect.DeepEqual(mapOut, expectedMapOut) {
478+
t.Fatalf("Expected: %#v\nGot: %#v", expectedMapOut, mapOut)
479+
}
480+
if !reflect.DeepEqual(valueOut, expectedValueOut) {
481+
t.Fatalf("Expected: %#v\nGot: %#v", valueOut, expectedValueOut)
482+
}
483+
return nil
484+
},
485+
},
486+
},
487+
})
488+
}
489+
430490
func testAccCheckResourceDestroy(s *terraform.State) error {
431491
return nil
432492
}

0 commit comments

Comments
 (0)