From 0f883b118ab97cc77367f92feca22226f2d39a5a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 23 Apr 2019 12:31:19 -0400 Subject: [PATCH] test that computed maps are applied correctly Verify that a computed map which is not correctly marked as unknown in the plan is still applied correctly. --- builtin/providers/test/resource_map.go | 19 ++++- builtin/providers/test/resource_map_test.go | 77 +++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/builtin/providers/test/resource_map.go b/builtin/providers/test/resource_map.go index fe524f69fa..38f6841067 100644 --- a/builtin/providers/test/resource_map.go +++ b/builtin/providers/test/resource_map.go @@ -23,6 +23,16 @@ func testResourceMap() *schema.Resource { Optional: true, Elem: &schema.Schema{Type: schema.TypeString}, }, + "map_values": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "computed_map": { + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } @@ -35,15 +45,20 @@ func testResourceMapCreate(d *schema.ResourceData, meta interface{}) error { } d.SetId("testId") - return nil + return testResourceMapRead(d, meta) } func testResourceMapRead(d *schema.ResourceData, meta interface{}) error { + var computedMap map[string]interface{} + if v, ok := d.GetOk("map_values"); ok { + computedMap = v.(map[string]interface{}) + } + d.Set("computed_map", computedMap) return nil } func testResourceMapUpdate(d *schema.ResourceData, meta interface{}) error { - return nil + return testResourceMapRead(d, meta) } func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error { diff --git a/builtin/providers/test/resource_map_test.go b/builtin/providers/test/resource_map_test.go index 800247edab..6e279b7f52 100644 --- a/builtin/providers/test/resource_map_test.go +++ b/builtin/providers/test/resource_map_test.go @@ -30,3 +30,80 @@ resource "test_resource_map" "foobar" { }, }) } + +func TestResourceMap_computedMap(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + { + Config: ` +resource "test_resource_map" "foobar" { + name = "test" + map_of_three = { + one = "one" + two = "two" + empty = "" + } + map_values = { + a = "1" + b = "2" + } +}`, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource_map.foobar", "computed_map.a", "1", + ), + resource.TestCheckResourceAttr( + "test_resource_map.foobar", "computed_map.b", "2", + ), + ), + }, + { + Config: ` +resource "test_resource_map" "foobar" { + name = "test" + map_of_three = { + one = "one" + two = "two" + empty = "" + } + map_values = { + a = "3" + b = "4" + } +}`, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource_map.foobar", "computed_map.a", "3", + ), + resource.TestCheckResourceAttr( + "test_resource_map.foobar", "computed_map.b", "4", + ), + ), + }, + { + Config: ` +resource "test_resource_map" "foobar" { + name = "test" + map_of_three = { + one = "one" + two = "two" + empty = "" + } + map_values = { + a = "3" + } +}`, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "test_resource_map.foobar", "computed_map.a", "3", + ), + resource.TestCheckNoResourceAttr( + "test_resource_map.foobar", "computed_map.b", + ), + ), + }, + }, + }) +}