terraform: tests for ResourceConfig.Get

This commit is contained in:
Mitchell Hashimoto 2014-06-12 23:16:28 -07:00
parent 9751878513
commit c10eed752f
2 changed files with 75 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package terraform
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/hashicorp/terraform/config"
@ -107,6 +108,14 @@ func (c *ResourceConfig) Get(k string) (interface{}, bool) {
return nil, false
}
current = v.Interface()
case reflect.Slice:
i, err := strconv.ParseInt(part, 0, 0)
if err != nil {
return nil, false
}
current = cv.Index(int(i)).Interface()
default:
panic(fmt.Sprintf("Unknown kind: %s", cv.Kind()))
}
}

View File

@ -1,6 +1,7 @@
package terraform
import (
"reflect"
"testing"
)
@ -50,6 +51,71 @@ func TestResourceConfig_CheckSet(t *testing.T) {
}
}
func TestResourceConfig_Get(t *testing.T) {
cases := []struct {
Raw map[string]interface{}
Computed []string
Input string
Output interface{}
OutputOk bool
}{
{
map[string]interface{}{
"foo": "bar",
},
nil,
"foo",
"bar",
true,
},
{
map[string]interface{}{},
nil,
"foo",
nil,
false,
},
{
map[string]interface{}{
"foo": map[interface{}]interface{}{
"bar": "baz",
},
},
nil,
"foo.bar",
"baz",
true,
},
{
map[string]interface{}{
"foo": []string{
"one",
"two",
},
},
nil,
"foo.1",
"two",
true,
},
}
for i, tc := range cases {
rc := &ResourceConfig{
ComputedKeys: tc.Computed,
Raw: tc.Raw,
}
actual, ok := rc.Get(tc.Input)
if tc.OutputOk != ok {
t.Fatalf("bad ok: %d", i)
}
if !reflect.DeepEqual(tc.Output, actual) {
t.Fatalf("bad %d: %#v", i, actual)
}
}
}
func TestResourceConfig_IsSet(t *testing.T) {
cases := []struct {
Raw map[string]interface{}