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.
This commit is contained in:
James Bardin 2019-04-23 12:31:19 -04:00
parent 3d46c04e28
commit 0f883b118a
2 changed files with 94 additions and 2 deletions

View File

@ -23,6 +23,16 @@ func testResourceMap() *schema.Resource {
Optional: true, Optional: true,
Elem: &schema.Schema{Type: schema.TypeString}, 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") d.SetId("testId")
return nil return testResourceMapRead(d, meta)
} }
func testResourceMapRead(d *schema.ResourceData, meta interface{}) error { 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 return nil
} }
func testResourceMapUpdate(d *schema.ResourceData, meta interface{}) error { func testResourceMapUpdate(d *schema.ResourceData, meta interface{}) error {
return nil return testResourceMapRead(d, meta)
} }
func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error { func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error {

View File

@ -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",
),
),
},
},
})
}