test for missing map entries

This commit is contained in:
James Bardin 2019-01-23 16:33:19 -05:00
parent 9b30da500d
commit 675d700a5f
3 changed files with 85 additions and 0 deletions

View File

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

View File

@ -0,0 +1,52 @@
package test
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceMap() *schema.Resource {
return &schema.Resource{
Create: testResourceMapCreate,
Read: testResourceMapRead,
Update: testResourceMapUpdate,
Delete: testResourceMapDelete,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"map_of_three": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func testResourceMapCreate(d *schema.ResourceData, meta interface{}) error {
// make sure all elements are passed to the map
m := d.Get("map_of_three").(map[string]interface{})
if len(m) != 3 {
return fmt.Errorf("expected 3 map values, got %#v\n", m)
}
d.SetId("testId")
return nil
}
func testResourceMapRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceMapUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceMapDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,32 @@
package test
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceMap_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
{
Config: `
resource "test_resource_map" "foobar" {
name = "test"
map_of_three = {
one = "one"
two = "two"
empty = ""
}
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"test_resource_map.foobar", "map_of_three.empty", "",
),
),
},
},
})
}