2014-07-21 19:39:55 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2016-06-12 17:30:45 +02:00
|
|
|
"strings"
|
2014-07-21 19:39:55 +02:00
|
|
|
"testing"
|
2015-01-12 21:09:30 +01:00
|
|
|
|
2016-01-31 08:38:37 +01:00
|
|
|
"github.com/hashicorp/hil"
|
2014-07-21 19:39:55 +02:00
|
|
|
)
|
|
|
|
|
2014-07-21 20:36:21 +02:00
|
|
|
func TestNewInterpolatedVariable(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Result InterpolatedVariable
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"var.foo",
|
|
|
|
&UserVariable{
|
|
|
|
Name: "foo",
|
|
|
|
key: "var.foo",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2014-09-15 20:40:25 +02:00
|
|
|
{
|
|
|
|
"module.foo.bar",
|
|
|
|
&ModuleVariable{
|
|
|
|
Name: "foo",
|
|
|
|
Field: "bar",
|
|
|
|
key: "module.foo.bar",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2014-10-03 03:20:55 +02:00
|
|
|
{
|
|
|
|
"count.index",
|
|
|
|
&CountVariable{
|
|
|
|
Type: CountValueIndex,
|
|
|
|
key: "count.index",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count.nope",
|
|
|
|
&CountVariable{
|
|
|
|
Type: CountValueInvalid,
|
|
|
|
key: "count.nope",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2014-10-08 03:03:11 +02:00
|
|
|
{
|
|
|
|
"path.module",
|
|
|
|
&PathVariable{
|
|
|
|
Type: PathValueModule,
|
|
|
|
key: "path.module",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-02-23 23:34:25 +01:00
|
|
|
{
|
|
|
|
"self.address",
|
|
|
|
&SelfVariable{
|
|
|
|
Field: "address",
|
|
|
|
key: "self.address",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2017-03-14 00:09:06 +01:00
|
|
|
{
|
|
|
|
"terraform.env",
|
|
|
|
&TerraformVariable{
|
|
|
|
Field: "env",
|
|
|
|
key: "terraform.env",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2014-07-21 20:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual, err := NewInterpolatedVariable(tc.Input)
|
2015-10-08 14:48:04 +02:00
|
|
|
if err != nil != tc.Error {
|
2014-07-21 20:36:21 +02:00
|
|
|
t.Fatalf("%d. Error: %s", i, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, tc.Result) {
|
|
|
|
t.Fatalf("%d bad: %#v", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:39:55 +02:00
|
|
|
func TestNewResourceVariable(t *testing.T) {
|
|
|
|
v, err := NewResourceVariable("foo.bar.baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:53:42 +02:00
|
|
|
if v.Mode != ManagedResourceMode {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
2014-07-21 19:39:55 +02:00
|
|
|
if v.Type != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Name != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Field != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Multi {
|
|
|
|
t.Fatal("should not be multi")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.FullKey() != "foo.bar.baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:53:42 +02:00
|
|
|
func TestNewResourceVariableData(t *testing.T) {
|
|
|
|
v, err := NewResourceVariable("data.foo.bar.baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Mode != DataResourceMode {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Type != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Name != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Field != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Multi {
|
|
|
|
t.Fatal("should not be multi")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.FullKey() != "data.foo.bar.baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:39:55 +02:00
|
|
|
func TestNewUserVariable(t *testing.T) {
|
|
|
|
v, err := NewUserVariable("var.bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Name != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v.Name)
|
|
|
|
}
|
|
|
|
if v.FullKey() != "var.bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 17:30:45 +02:00
|
|
|
func TestNewUserVariable_oldMapDotIndexErr(t *testing.T) {
|
|
|
|
_, err := NewUserVariable("var.bar.baz")
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "Invalid dot index") {
|
|
|
|
t.Fatalf("Expected dot index err, got: %#v", err)
|
2014-07-22 17:53:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:39:55 +02:00
|
|
|
func TestResourceVariable_impl(t *testing.T) {
|
|
|
|
var _ InterpolatedVariable = new(ResourceVariable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceVariable_Multi(t *testing.T) {
|
|
|
|
v, err := NewResourceVariable("foo.bar.*.baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Type != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Name != "bar" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if v.Field != "baz" {
|
|
|
|
t.Fatalf("bad: %#v", v)
|
|
|
|
}
|
|
|
|
if !v.Multi {
|
|
|
|
t.Fatal("should be multi")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceVariable_MultiIndex(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Index int
|
|
|
|
Field string
|
|
|
|
}{
|
|
|
|
{"foo.bar.*.baz", -1, "baz"},
|
|
|
|
{"foo.bar.0.baz", 0, "baz"},
|
|
|
|
{"foo.bar.5.baz", 5, "baz"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
v, err := NewResourceVariable(tc.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !v.Multi {
|
|
|
|
t.Fatalf("should be multi: %s", tc.Input)
|
|
|
|
}
|
|
|
|
if v.Index != tc.Index {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", v.Index, tc.Input)
|
|
|
|
}
|
|
|
|
if v.Field != tc.Field {
|
|
|
|
t.Fatalf("bad: %s\n\n%s", v.Field, tc.Input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserVariable_impl(t *testing.T) {
|
|
|
|
var _ InterpolatedVariable = new(UserVariable)
|
|
|
|
}
|
|
|
|
|
2015-01-12 21:09:30 +01:00
|
|
|
func TestDetectVariables(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Result []InterpolatedVariable
|
|
|
|
}{
|
2015-01-12 21:13:30 +01:00
|
|
|
{
|
|
|
|
"foo $${var.foo}",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
2015-01-12 21:09:30 +01:00
|
|
|
{
|
|
|
|
"foo ${var.foo}",
|
|
|
|
[]InterpolatedVariable{
|
|
|
|
&UserVariable{
|
|
|
|
Name: "foo",
|
|
|
|
key: "var.foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo ${var.foo} ${var.bar}",
|
|
|
|
[]InterpolatedVariable{
|
|
|
|
&UserVariable{
|
|
|
|
Name: "foo",
|
|
|
|
key: "var.foo",
|
|
|
|
},
|
|
|
|
&UserVariable{
|
|
|
|
Name: "bar",
|
|
|
|
key: "var.bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-10-27 05:24:23 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
`foo ${module.foo.output["key"]}`,
|
|
|
|
[]InterpolatedVariable{
|
|
|
|
&ModuleVariable{
|
|
|
|
Name: "foo",
|
|
|
|
Field: "output",
|
|
|
|
key: "module.foo.output",
|
|
|
|
},
|
|
|
|
&ModuleVariable{
|
|
|
|
Name: "foo",
|
|
|
|
Field: "output",
|
|
|
|
key: "module.foo.output",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-12 21:09:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2016-01-31 08:38:37 +01:00
|
|
|
ast, err := hil.Parse(tc.Input)
|
2015-01-12 21:09:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s\n\nInput: %s", err, tc.Input)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := DetectVariables(ast)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, tc.Result) {
|
|
|
|
t.Fatalf("bad: %#v\n\nInput: %s", actual, tc.Input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|