diff --git a/builtin/providers/test/provider.go b/builtin/providers/test/provider.go index 9bd6a6b54..948e82f87 100644 --- a/builtin/providers/test/provider.go +++ b/builtin/providers/test/provider.go @@ -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(), diff --git a/builtin/providers/test/resource_defaults_test.go b/builtin/providers/test/resource_defaults_test.go index 2e738e0cd..0e5876866 100644 --- a/builtin/providers/test/resource_defaults_test.go +++ b/builtin/providers/test/resource_defaults_test.go @@ -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, diff --git a/builtin/providers/test/resource_list.go b/builtin/providers/test/resource_list.go new file mode 100644 index 000000000..4a0d4c92e --- /dev/null +++ b/builtin/providers/test/resource_list.go @@ -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 +} diff --git a/builtin/providers/test/resource_list_test.go b/builtin/providers/test/resource_list_test.go new file mode 100644 index 000000000..4d78f3547 --- /dev/null +++ b/builtin/providers/test/resource_list_test.go @@ -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", + ), + ), + }, + }, + }) +} diff --git a/builtin/providers/test/resource_nested_set_test.go b/builtin/providers/test/resource_nested_set_test.go index 878bd4bbf..691f74916 100644 --- a/builtin/providers/test/resource_nested_set_test.go +++ b/builtin/providers/test/resource_nested_set_test.go @@ -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", + ), + ), }, }, })