add tests to preserve existing Set nil behavior

While it may not be intuitive, providers expect that setting a `nil`
value will appear as an empty string in state.
This commit is contained in:
James Bardin 2019-06-28 12:09:50 -04:00
parent 75602df5ef
commit 0d2363a058
2 changed files with 44 additions and 0 deletions

View File

@ -202,6 +202,14 @@ func testResourceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("set", []interface{}{}) d.Set("set", []interface{}{})
} }
// This mimics many providers always setting a *string value.
// The existing behavior is that this will appear in the state as an empty
// string, which we have to maintain.
o := d.Get("optional")
if o == "" {
d.Set("optional", nil)
}
// This should not show as set unless it's set in the config // This should not show as set unless it's set in the config
_, ok := d.GetOkExists("get_ok_exists_false") _, ok := d.GetOkExists("get_ok_exists_false")
if ok { if ok {

View File

@ -1050,3 +1050,39 @@ resource "test_resource" "foo" {
}, },
}) })
} }
func TestResource_unsetNil(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 = "yep"
required_map = {
key = "value"
}
optional = "a"
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource.foo", "optional", "a"),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "yep"
required_map = {
key = "value"
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource.foo", "optional", ""),
),
},
},
})
}