2014-06-30 22:59:23 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2016-09-27 18:09:32 -05:00
|
|
|
"fmt"
|
2014-06-30 22:59:23 -05:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-08-19 11:05:19 -05:00
|
|
|
|
2016-01-31 01:43:41 -06:00
|
|
|
"github.com/hashicorp/hil/ast"
|
2014-08-19 11:05:19 -05:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-06-30 22:59:23 -05:00
|
|
|
)
|
|
|
|
|
2014-09-25 12:40:44 -05:00
|
|
|
func TestInstanceInfo(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Info *InstanceInfo
|
|
|
|
Result string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&InstanceInfo{
|
|
|
|
Id: "foo",
|
|
|
|
},
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceInfo{
|
|
|
|
Id: "foo",
|
|
|
|
ModulePath: rootModulePath,
|
|
|
|
},
|
|
|
|
"foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&InstanceInfo{
|
|
|
|
Id: "foo",
|
|
|
|
ModulePath: []string{"root", "consul"},
|
|
|
|
},
|
|
|
|
"module.consul.foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual := tc.Info.HumanId()
|
|
|
|
if actual != tc.Result {
|
|
|
|
t.Fatalf("%d: %s", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 11:05:19 -05:00
|
|
|
func TestResourceConfigGet(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Config map[string]interface{}
|
|
|
|
Vars map[string]string
|
|
|
|
Key string
|
|
|
|
Value interface{}
|
|
|
|
}{
|
2014-09-09 19:41:05 -05:00
|
|
|
{
|
|
|
|
Config: nil,
|
2014-09-09 23:44:40 -05:00
|
|
|
Key: "foo",
|
|
|
|
Value: nil,
|
2014-09-09 19:41:05 -05:00
|
|
|
},
|
|
|
|
|
2014-08-19 11:05:19 -05: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 13:37:14 -05: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,
|
|
|
|
},
|
2016-07-20 12:45:04 -05:00
|
|
|
|
|
|
|
// get from map
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{"key": 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// get from map with dot in key
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{"key.name": 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key.name",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
// get from map with overlapping key names
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key.name": 1,
|
|
|
|
"key.name.2": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key.name.2",
|
|
|
|
Value: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key.name": 1,
|
|
|
|
"key.name.foo": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key.name",
|
|
|
|
Value: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"listkey": []map[string]interface{}{
|
|
|
|
{"key": 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.listkey.0.key",
|
|
|
|
Value: 3,
|
|
|
|
},
|
|
|
|
// FIXME: this is ambiguous, and matches the nested map
|
|
|
|
// leaving here to catch this behaviour if it changes.
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key.name": 1,
|
|
|
|
"key.name.0": 2,
|
|
|
|
"key": map[string]interface{}{"name": 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key.name",
|
|
|
|
Value: 3,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
// TODO: can't access this nested list at all.
|
|
|
|
// FIXME: key with name matching substring of nested list can panic
|
|
|
|
{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"mapname": []map[string]interface{}{
|
|
|
|
map[string]interface{}{
|
|
|
|
"key.name": []map[string]interface{}{
|
|
|
|
{"subkey": 1},
|
|
|
|
},
|
|
|
|
"key": 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Key: "mapname.0.key.name.0.subkey",
|
|
|
|
Value: 3,
|
|
|
|
},
|
|
|
|
*/
|
2014-08-19 11:05:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
2014-09-09 19:41:05 -05: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 11:05:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if tc.Vars != nil {
|
2015-02-13 20:09:45 -06:00
|
|
|
vs := make(map[string]ast.Variable)
|
|
|
|
for k, v := range tc.Vars {
|
|
|
|
vs["var."+k] = ast.Variable{Value: v, Type: ast.TypeString}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rawC.Interpolate(vs); err != nil {
|
2014-08-19 11:05:19 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-13 20:09:45 -06:00
|
|
|
rc := NewResourceConfig(rawC)
|
|
|
|
rc.interpolateForce()
|
|
|
|
|
2014-08-19 11:05:19 -05:00
|
|
|
v, _ := rc.Get(tc.Key)
|
|
|
|
if !reflect.DeepEqual(v, tc.Value) {
|
|
|
|
t.Fatalf("%d bad: %#v", i, v)
|
|
|
|
}
|
2016-09-27 18:09:32 -05:00
|
|
|
|
|
|
|
// If we have vars, we don't test copying
|
|
|
|
if len(tc.Vars) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test copying
|
|
|
|
t.Run(fmt.Sprintf("copy-%d", i), func(t *testing.T) {
|
|
|
|
copy := rc.DeepCopy()
|
|
|
|
if !reflect.DeepEqual(copy, rc) {
|
|
|
|
t.Fatalf("bad:\n\n%#v\n\n%#v", copy, rc)
|
|
|
|
}
|
|
|
|
})
|
2014-08-19 11:05:19 -05:00
|
|
|
}
|
|
|
|
}
|
2015-02-03 18:19:18 -06:00
|
|
|
|
|
|
|
func testResourceConfig(
|
|
|
|
t *testing.T, c map[string]interface{}) *ResourceConfig {
|
|
|
|
raw, err := config.NewRawConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewResourceConfig(raw)
|
|
|
|
}
|