add provider tests for force-new with a map

Adding and removing a single map that requires a new resource can cause
empty diffs, relying on the core proposed state values for destruction.
This commit is contained in:
James Bardin 2018-10-31 13:42:28 -04:00
parent f959b560a2
commit 8212a6a9d0
4 changed files with 149 additions and 0 deletions

View File

@ -22,6 +22,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_with_custom_diff": testResourceCustomDiff(),
"test_resource_timeout": testResourceTimeout(),
"test_resource_diff_suppress": testResourceDiffSuppress(),
"test_resource_force_new": testResourceForceNew(),
},
DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(),

View File

@ -0,0 +1,39 @@
package test
import (
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceForceNew() *schema.Resource {
return &schema.Resource{
Create: testResourceForceNewCreate,
Read: testResourceForceNewRead,
Delete: testResourceForceNewDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"triggers": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
func testResourceForceNewCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
return testResourceForceNewRead(d, meta)
}
func testResourceForceNewRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceForceNewDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,79 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceForceNew_create(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
triggers = {
"a" = "foo"
}
}`),
},
},
})
}
func TestResourceForceNew_update(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
triggers = {
"a" = "foo"
}
}`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
triggers = {
"a" = "bar"
}
}`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
triggers = {
"b" = "bar"
}
}`),
},
},
})
}
func TestResourceForceNew_remove(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
triggers = {
"a" = "bar"
}
}`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_force_new" "foo" {
} `),
},
},
})
}

View File

@ -443,3 +443,33 @@ output "value_from_map_from_list" {
func testAccCheckResourceDestroy(s *terraform.State) error {
return nil
}
func TestResource_removeForceNew(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "yep"
required_map = {
key = "value"
}
optional_force_new = "here"
}
`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource" "foo" {
required = "yep"
required_map = {
key = "value"
}
}
`),
},
},
})
}