provider/test: Added complex-ish list testing

Added a list SetNew test to try and reproduce issues testing diff
customization with the Nomad provider. We are running into "diffs didn't
match during apply", with the plan diff exhibiting a strange
off-by-one-type error in a list diff:

  datacenters.#:         "1" => "2"
  datacenters.0:         "dc1" => "dc2"
  datacenters.1:         "" => "dc3"
  datacenters.2:         "" => "dc3"

The test here does not reproduce that issue, unfortunately, but should
help pinpoint the root cause through elimination.
This commit is contained in:
Chris Marchesi 2017-06-01 08:04:11 -07:00 committed by Martin Atkins
parent 529d7e6dae
commit 5d5a670d69
2 changed files with 77 additions and 1 deletions

View File

@ -30,10 +30,57 @@ func testResourceCustomDiff() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"list": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
type listDiffCases struct {
Type string
Value string
}
func testListDiffCases(index int) []listDiffCases {
switch index {
case 0:
return []listDiffCases{
{
Type: "add",
Value: "dc1",
},
}
case 1:
return []listDiffCases{
{
Type: "remove",
Value: "dc1",
},
{
Type: "add",
Value: "dc2",
},
{
Type: "add",
Value: "dc3",
},
}
}
return nil
}
func testListDiffCasesReadResult(index int) []interface{} {
switch index {
case 1:
return []interface{}{"dc1"}
default:
return []interface{}{"dc2", "dc3"}
}
}
func testResourceCustomDiffCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
@ -54,6 +101,9 @@ func testResourceCustomDiffCreate(d *schema.ResourceData, meta interface{}) erro
}
func testResourceCustomDiffRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("list", testListDiffCasesReadResult(d.Get("index").(int))); err != nil {
return err
}
return nil
}
@ -64,6 +114,26 @@ func testResourceCustomDiffCustomizeDiff(d *schema.ResourceDiff, meta interface{
// Note that this gets put into state after the update, regardless of whether
// or not anything is acted upon in the diff.
d.SetNew("computed", d.Get("computed").(int)+1)
// This tests a diffed list, based off of the value of index
dcs := testListDiffCases(d.Get("index").(int))
s := d.Get("list").([]interface{})
for _, dc := range dcs {
switch dc.Type {
case "add":
s = append(s, dc.Value)
case "remove":
for i := range s {
if s[i].(string) == dc.Value {
copy(s[i:], s[i+1:])
s = s[:len(s)-1]
break
}
}
}
}
d.SetNew("list", s)
return nil
}
@ -75,7 +145,7 @@ func testResourceCustomDiffUpdate(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("expected computed to be 1 ahead of index, got computed: %d, index: %d", expected, actual)
}
d.Set("index", new)
return nil
return testResourceCustomDiffRead(d, meta)
}
func testResourceCustomDiffDelete(d *schema.ResourceData, meta interface{}) error {

View File

@ -18,6 +18,8 @@ func TestResourceWithCustomDiff(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "computed", "1"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "index", "1"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.#", "1"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.0", "dc1"),
),
ExpectNonEmptyPlan: true,
},
@ -26,6 +28,10 @@ func TestResourceWithCustomDiff(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "computed", "2"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "index", "2"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.#", "2"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.0", "dc2"),
resource.TestCheckResourceAttr("test_resource_with_custom_diff.foo", "list.1", "dc3"),
resource.TestCheckNoResourceAttr("test_resource_with_custom_diff.foo", "list.2"),
),
ExpectNonEmptyPlan: true,
},