From c10eed752f85b86b07caffccc6036443147a6eda Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 12 Jun 2014 23:16:28 -0700 Subject: [PATCH] terraform: tests for ResourceConfig.Get --- terraform/resource_provider.go | 9 ++++ terraform/resource_provider_test.go | 66 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/terraform/resource_provider.go b/terraform/resource_provider.go index 6ef9d3bd3..99eedb6f5 100644 --- a/terraform/resource_provider.go +++ b/terraform/resource_provider.go @@ -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())) } } diff --git a/terraform/resource_provider_test.go b/terraform/resource_provider_test.go index c62ff46f1..61478d49e 100644 --- a/terraform/resource_provider_test.go +++ b/terraform/resource_provider_test.go @@ -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{}