add failing test for set elements with custom diff

Adding a DiffSuppressFunc for set elements can cause them to be missed
in the set diff entirely.
This commit is contained in:
James Bardin 2019-02-01 21:34:12 -05:00
parent 4a603011c5
commit 2e2374cfcb
3 changed files with 129 additions and 0 deletions

View File

@ -30,6 +30,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_deprecated": testResourceDeprecated(),
"test_resource_defaults": testResourceDefaults(),
"test_resource_list": testResourceList(),
"test_resource_list_set": testResourceListSet(),
"test_resource_map": testResourceMap(),
"test_resource_computed_set": testResourceComputedSet(),
},

View File

@ -0,0 +1,73 @@
package test
import (
"fmt"
"math/rand"
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceListSet() *schema.Resource {
return &schema.Resource{
Create: testResourceListSetCreate,
Read: testResourceListSetRead,
Delete: testResourceListSetDelete,
Update: testResourceListSetUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"list": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"set": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"elem": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(_, o, n string, _ *schema.ResourceData) bool {
return o == n
},
},
},
},
Set: func(v interface{}) int {
raw := v.(map[string]interface{})
if el, ok := raw["elem"]; ok {
return schema.HashString(el)
}
return 42
},
},
},
},
},
},
}
}
func testResourceListSetCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(fmt.Sprintf("%x", rand.Int63()))
return testResourceListSetRead(d, meta)
}
func testResourceListSetUpdate(d *schema.ResourceData, meta interface{}) error {
return testResourceListSetRead(d, meta)
}
func testResourceListSetRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceListSetDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,55 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceListSet_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list_set" "foo" {
list {
set {
elem = "A"
}
set {
elem = "B"
}
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.1255198513.elem", "B"),
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.3554254475.elem", "A"),
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.#", "2"),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list_set" "foo" {
list {
set {
elem = "B"
}
set {
elem = "C"
}
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.1255198513.elem", "B"),
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.1037565863.elem", "C"),
resource.TestCheckResourceAttr("test_resource_list_set.foo", "list.0.set.#", "2"),
),
},
},
})
}