add failing test for lost elements in list blocks

Modifying an element loses the modification, and other elements in a
TypeList.
This commit is contained in:
James Bardin 2019-01-18 13:05:59 -05:00
parent 8b094f48f7
commit f78b5045d0
5 changed files with 210 additions and 27 deletions

View File

@ -28,6 +28,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_state_func": testResourceStateFunc(),
"test_resource_deprecated": testResourceDeprecated(),
"test_resource_defaults": testResourceDefaults(),
"test_resource_list": testResourceList(),
},
DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(),

View File

@ -66,9 +66,6 @@ resource "test_resource_defaults" "foo" {
}
func TestResourceDefaults_import(t *testing.T) {
// FIXME: this test fails
return
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,

View File

@ -0,0 +1,69 @@
package test
import (
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceList() *schema.Resource {
return &schema.Resource{
Create: testResourceListCreate,
Read: testResourceListRead,
Update: testResourceListUpdate,
Delete: testResourceListDelete,
Schema: map[string]*schema.Schema{
"list_block": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"string": {
Type: schema.TypeString,
Optional: true,
},
"int": {
Type: schema.TypeInt,
Optional: true,
},
"sublist_block": {
Type: schema.TypeList,
Optional: true,
Computed: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"string": {
Type: schema.TypeString,
Required: true,
},
"int": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
},
}
}
func testResourceListCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
return nil
}
func testResourceListRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceListUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceListDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,114 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
// an empty config should be ok, because no deprecated/removed fields are set.
func TestResourceList_changed(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list" "foo" {
list_block {
string = "a"
int = 1
}
list_block {
string = "b"
int = 2
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.#", "2",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.string", "a",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.int", "1",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.1.string", "b",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.1.int", "2",
),
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list" "foo" {
list_block {
string = "a"
int = 1
}
list_block {
string = "c"
int = 2
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.#", "2",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.string", "a",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.int", "1",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.1.string", "c",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.1.int", "2",
),
),
},
},
})
}
func TestResourceList_sublist(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list" "foo" {
list_block {
sublist_block {
string = "a"
int = 1
}
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.sublist_block.#", "1",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.sublist_block.0.string", "a",
),
resource.TestCheckResourceAttr(
"test_resource_list.foo", "list_block.0.sublist_block.0.int", "1",
),
),
},
},
})
}

View File

@ -91,21 +91,11 @@ func TestResourceNestedSet_emptyNestedListBlock(t *testing.T) {
root := s.ModuleByPath(addrs.RootModuleInstance)
res := root.Resources["test_resource_nested_set.foo"]
found := false
for k, v := range res.Primary.Attributes {
for k := range res.Primary.Attributes {
if !regexp.MustCompile(`^with_list\.\d+\.list_block\.`).MatchString(k) {
continue
}
found = true
if strings.HasSuffix(k, ".#") {
if v != "1" {
return fmt.Errorf("expected block with no objects: got %s:%s", k, v)
}
continue
}
// there should be no other attribute values for an empty block
return fmt.Errorf("unexpected attribute: %s:%s", k, v)
}
if !found {
return fmt.Errorf("with_list.X.list_block not found")
@ -199,14 +189,27 @@ resource "test_resource_nested_set" "foo" {
}
}
`),
Check: checkFunc,
Check: resource.ComposeTestCheckFunc(
checkFunc,
resource.TestCheckResourceAttr(
"test_resource_nested_set.foo", "single.#", "1",
),
// the hash of single seems to change here, so we're not
// going to test for "value" directly
// FIXME: figure out why the set hash changes
),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested_set" "foo" {
}
`),
Check: checkFunc,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_nested_set.foo", "single.#", "0",
),
checkFunc,
),
},
resource.TestStep{
Config: strings.TrimSpace(`
@ -456,9 +459,6 @@ resource "test_resource_nested_set" "foo" {
// This is the same as forceNewEmptyString, but we start with the empty value,
// instead of changing it.
func TestResourceNestedSet_nestedSetEmptyString(t *testing.T) {
checkFunc := func(s *terraform.State) error {
return nil
}
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
@ -473,19 +473,17 @@ resource "test_resource_nested_set" "foo" {
}
}
`),
Check: checkFunc,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_nested_set.foo", "multi.529860700.set.4196279896.required", "",
),
),
},
},
})
}
func TestResourceNestedSet_emptySet(t *testing.T) {
// FIXME: this test fails
return
checkFunc := func(s *terraform.State) error {
return nil
}
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
@ -497,7 +495,11 @@ resource "test_resource_nested_set" "foo" {
}
}
`),
Check: checkFunc,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_nested_set.foo", "multi.#", "1",
),
),
},
},
})