add tests for nested default values

Don't lose default values set within a nested block.
This commit is contained in:
James Bardin 2019-01-16 16:54:02 -05:00
parent 176a5abfd3
commit 4439a7dcf4
3 changed files with 160 additions and 0 deletions

View File

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

View File

@ -0,0 +1,70 @@
package test
import (
"fmt"
"math/rand"
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceDefaults() *schema.Resource {
return &schema.Resource{
Create: testResourceDefaultsCreate,
Read: testResourceDefaultsRead,
Delete: testResourceDefaultsDelete,
Update: testResourceDefaultsUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"default_string": {
Type: schema.TypeString,
Optional: true,
Default: "default string",
},
"default_bool": {
Type: schema.TypeString,
Optional: true,
Default: true,
},
"nested": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"string": {
Type: schema.TypeString,
Optional: true,
Default: "default nested",
},
"optional": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}
}
func testResourceDefaultsCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId(fmt.Sprintf("%x", rand.Int63()))
return testResourceDefaultsRead(d, meta)
}
func testResourceDefaultsUpdate(d *schema.ResourceData, meta interface{}) error {
return testResourceDefaultsRead(d, meta)
}
func testResourceDefaultsRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceDefaultsDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,89 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceDefaults_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_defaults" "foo" {
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "default_string", "default string",
),
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "default_bool", "1",
),
resource.TestCheckNoResourceAttr(
"test_resource_defaults.foo", "nested.#",
),
),
},
},
})
}
func TestResourceDefaults_inSet(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_defaults" "foo" {
nested {
optional = "val"
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "default_string", "default string",
),
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "default_bool", "1",
),
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "nested.2826070548.optional", "val",
),
resource.TestCheckResourceAttr(
"test_resource_defaults.foo", "nested.2826070548.string", "default nested",
),
),
},
},
})
}
func TestResourceDefaults_import(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_defaults" "foo" {
nested {
optional = "val"
}
}
`),
},
{
ImportState: true,
ImportStateVerify: true,
ResourceName: "test_resource_defaults.foo",
},
},
})
}