add test for set value drift

The changed value for the set attribute should be correctly read from
the provider.
This commit is contained in:
James Bardin 2019-03-13 19:17:38 -04:00
parent e080706e2e
commit 4006c0fc84
1 changed files with 58 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
@ -732,3 +733,60 @@ resource "test_resource" "foo" {
},
})
}
func TestResource_setDrift(t *testing.T) {
testProvider := testAccProviders["test"]
res := testProvider.(*schema.Provider).ResourcesMap["test_resource"]
// reset the Read function after the test
defer func() {
res.Read = testResourceRead
}()
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "first"
required_map = {
a = "a"
}
set = ["a", "b"]
}
`),
Check: func(s *terraform.State) error {
return nil
},
},
resource.TestStep{
PreConfig: func() {
// update the Read function to return the wrong "set" attribute values.
res.Read = func(d *schema.ResourceData, meta interface{}) error {
// update as expected first
if err := testResourceRead(d, meta); err != nil {
return err
}
d.Set("set", []interface{}{"a", "x"})
return nil
}
},
// Leave the config, so we can detect the mismatched set values.
// Updating the config would force the test to pass even if the Read
// function values were ignored.
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "second"
required_map = {
a = "a"
}
set = ["a", "b"]
}
`),
ExpectNonEmptyPlan: true,
},
},
})
}