add test fetching computed set value by address
This is not a recommended method, but it does serve to verify that the set values in the ResourceData internal state are correctly computed, which indicates that the expected configuration was passed in.
This commit is contained in:
parent
af8115dc9b
commit
8d32229f7d
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/hashcode"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
|
@ -35,6 +36,26 @@ func testResourceStateFunc() *schema.Resource {
|
|||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
|
||||
// set block with computed elements
|
||||
"set_block": {
|
||||
Type: schema.TypeSet,
|
||||
Optional: true,
|
||||
Set: setBlockHash,
|
||||
Elem: &schema.Resource{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"required": {
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
"optional": {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +65,13 @@ func stateFuncHash(v interface{}) string {
|
|||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func setBlockHash(v interface{}) int {
|
||||
m := v.(map[string]interface{})
|
||||
required, _ := m["required"].(string)
|
||||
optional, _ := m["optional"].(string)
|
||||
return hashcode.String(fmt.Sprintf("%s|%s", required, optional))
|
||||
}
|
||||
|
||||
func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
d.SetId(fmt.Sprintf("%x", rand.Int63()))
|
||||
|
||||
|
@ -57,6 +85,22 @@ func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error
|
|||
}
|
||||
}
|
||||
|
||||
// Check that we can lookup set elements by our computed hash.
|
||||
// This is not advised, but we can use this to make sure the final diff was
|
||||
// prepared with the correct values.
|
||||
setBlock, ok := d.GetOk("set_block")
|
||||
if ok {
|
||||
set := setBlock.(*schema.Set)
|
||||
for _, obj := range set.List() {
|
||||
idx := setBlockHash(obj)
|
||||
requiredAddr := fmt.Sprintf("%s.%d.%s", "set_block", idx, "required")
|
||||
_, ok := d.GetOkExists(requiredAddr)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to get attr %q from %#v", fmt.Sprintf(requiredAddr), d.State().Attributes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return testResourceStateFuncRead(d, meta)
|
||||
}
|
||||
|
||||
|
|
|
@ -58,3 +58,28 @@ resource "test_resource_state_func" "foo" {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestResourceStateFunc_getOkSetElem(t *testing.T) {
|
||||
resource.UnitTest(t, resource.TestCase{
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckResourceDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: strings.TrimSpace(`
|
||||
resource "test_resource_state_func" "foo" {
|
||||
}
|
||||
|
||||
resource "test_resource_state_func" "bar" {
|
||||
set_block {
|
||||
required = "foo"
|
||||
optional = test_resource_state_func.foo.id
|
||||
}
|
||||
set_block {
|
||||
required = test_resource_state_func.foo.id
|
||||
}
|
||||
}
|
||||
`),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue