test for Required win MinItems > 1

This commit is contained in:
James Bardin 2019-07-26 14:36:19 -07:00
parent 799ebbf79b
commit 682286e184
3 changed files with 135 additions and 0 deletions

View File

@ -37,6 +37,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_config_mode": testResourceConfigMode(), "test_resource_config_mode": testResourceConfigMode(),
"test_resource_nested_id": testResourceNestedId(), "test_resource_nested_id": testResourceNestedId(),
"test_undeleteable": testResourceUndeleteable(), "test_undeleteable": testResourceUndeleteable(),
"test_resource_required_min": testResourceRequiredMin(),
}, },
DataSourcesMap: map[string]*schema.Resource{ DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(), "test_data_source": testDataSource(),

View File

@ -0,0 +1,68 @@
package test
import (
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceRequiredMin() *schema.Resource {
return &schema.Resource{
Create: testResourceRequiredMinCreate,
Read: testResourceRequiredMinRead,
Update: testResourceRequiredMinUpdate,
Delete: testResourceRequiredMinDelete,
CustomizeDiff: func(d *schema.ResourceDiff, _ interface{}) error {
if d.HasChange("dependent_list") {
d.SetNewComputed("computed_list")
}
return nil
},
Schema: map[string]*schema.Schema{
"min_items": {
Type: schema.TypeList,
Optional: true,
MinItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"val": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"required_min_items": {
Type: schema.TypeList,
Required: true,
MinItems: 2,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"val": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func testResourceRequiredMinCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
return testResourceRequiredMinRead(d, meta)
}
func testResourceRequiredMinRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceRequiredMinUpdate(d *schema.ResourceData, meta interface{}) error {
return testResourceRequiredMinRead(d, meta)
}
func testResourceRequiredMinDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,66 @@
package test
import (
"regexp"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResource_dynamicRequiredMinItems(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: `
resource "test_resource_required_min" "a" {
}
`,
ExpectError: regexp.MustCompile(`"required_min_items" blocks are required`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list" "a" {
dependent_list {
val = "a"
}
}
resource "test_resource_required_min" "b" {
dynamic "required_min_items" {
for_each = test_resource_list.a.computed_list
content {
val = required_min_items.value
}
}
}
`),
ExpectError: regexp.MustCompile(`required_min_items: attribute supports 2 item as a minimum`),
},
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_list" "c" {
dependent_list {
val = "a"
}
dependent_list {
val = "b"
}
}
resource "test_resource_required_min" "b" {
dynamic "required_min_items" {
for_each = test_resource_list.c.computed_list
content {
val = required_min_items.value
}
}
}
`),
},
},
})
}