2014-07-01 05:59:23 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-19 18:05:19 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-07-01 05:59:23 +02:00
|
|
|
)
|
|
|
|
|
2014-08-19 18:05:19 +02:00
|
|
|
func TestResourceConfigGet(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Config map[string]interface{}
|
|
|
|
Vars map[string]string
|
|
|
|
Key string
|
|
|
|
Value interface{}
|
|
|
|
}{
|
2014-09-10 02:41:05 +02:00
|
|
|
{
|
|
|
|
Config: nil,
|
2014-09-10 06:44:40 +02:00
|
|
|
Key: "foo",
|
|
|
|
Value: nil,
|
2014-09-10 02:41:05 +02:00
|
|
|
},
|
|
|
|
|
2014-08-19 18:05:19 +02:00
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "${var.foo}",
|
|
|
|
},
|
|
|
|
Key: "foo",
|
|
|
|
Value: "${var.foo}",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "${var.foo}",
|
|
|
|
},
|
|
|
|
Vars: map[string]string{"foo": "bar"},
|
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
2014-08-21 20:37:14 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
Key: "foo.0",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": []interface{}{1, 2, 5},
|
|
|
|
},
|
|
|
|
Key: "foo.5",
|
|
|
|
Value: nil,
|
|
|
|
},
|
2014-08-19 18:05:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2014-09-10 02:41:05 +02:00
|
|
|
var rawC *config.RawConfig
|
|
|
|
if tc.Config != nil {
|
|
|
|
var err error
|
|
|
|
rawC, err = config.NewRawConfig(tc.Config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-08-19 18:05:19 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 02:41:05 +02:00
|
|
|
rc := NewResourceConfig(rawC)
|
2014-08-19 18:05:19 +02:00
|
|
|
if tc.Vars != nil {
|
|
|
|
ctx := NewContext(&ContextOpts{Variables: tc.Vars})
|
2014-09-24 00:46:20 +02:00
|
|
|
if err := rc.interpolate(ctx.walkContext(walkInvalid, rootModulePath)); err != nil {
|
2014-08-19 18:05:19 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := rc.Get(tc.Key)
|
|
|
|
if !reflect.DeepEqual(v, tc.Value) {
|
|
|
|
t.Fatalf("%d bad: %#v", i, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|