2014-07-21 22:12:43 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2014-10-10 06:22:35 +02:00
|
|
|
"fmt"
|
2014-07-23 03:35:36 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-01-13 20:50:44 +01:00
|
|
|
"reflect"
|
2014-07-21 22:12:43 +02:00
|
|
|
"testing"
|
2014-08-19 22:14:45 +02:00
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
"github.com/hashicorp/terraform/config/lang"
|
|
|
|
)
|
2014-08-19 22:14:45 +02:00
|
|
|
|
2014-07-23 03:35:36 +02:00
|
|
|
func TestInterpolateFuncFile(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "tf")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
path := tf.Name()
|
|
|
|
tf.Write([]byte("foo"))
|
|
|
|
tf.Close()
|
|
|
|
defer os.Remove(path)
|
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
testFunction(t, []testFunctionCase{
|
2014-07-23 03:35:36 +02:00
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
fmt.Sprintf(`${file("%s")}`, path),
|
2014-07-23 03:35:36 +02:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid path
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
`${file("/i/dont/exist")}`,
|
|
|
|
nil,
|
2014-07-23 03:35:36 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Too many args
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
`${file("foo", "bar")}`,
|
|
|
|
nil,
|
2014-07-23 03:35:36 +02:00
|
|
|
true,
|
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
2014-07-23 03:35:36 +02:00
|
|
|
}
|
2014-07-23 03:35:53 +02:00
|
|
|
|
2014-10-10 06:22:35 +02:00
|
|
|
func TestInterpolateFuncJoin(t *testing.T) {
|
2015-01-13 20:50:44 +01:00
|
|
|
testFunction(t, []testFunctionCase{
|
2014-10-10 06:22:35 +02:00
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
`${join(",")}`,
|
|
|
|
nil,
|
2014-10-10 06:22:35 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
`${join(",", "foo")}`,
|
2014-10-10 06:22:35 +02:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
/*
|
|
|
|
TODO
|
|
|
|
{
|
|
|
|
`${join(",", "foo", "bar")}`,
|
|
|
|
"foo,bar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
*/
|
2014-10-10 06:22:35 +02:00
|
|
|
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
fmt.Sprintf(`${join(".", "%s")}`,
|
2014-10-10 06:22:35 +02:00
|
|
|
fmt.Sprintf(
|
|
|
|
"foo%sbar%sbaz",
|
|
|
|
InterpSplitDelim,
|
2015-01-13 20:50:44 +01:00
|
|
|
InterpSplitDelim)),
|
2014-10-10 06:22:35 +02:00
|
|
|
"foo.bar.baz",
|
|
|
|
false,
|
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
2014-10-10 06:22:35 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
/*
|
2014-07-21 22:12:43 +02:00
|
|
|
func TestInterpolateFuncLookup(t *testing.T) {
|
2015-01-13 20:50:44 +01:00
|
|
|
testFunction(t, []testFunctionCase{
|
2014-07-21 22:12:43 +02:00
|
|
|
cases := []struct {
|
|
|
|
M map[string]string
|
|
|
|
Args []string
|
|
|
|
Result string
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
map[string]string{
|
|
|
|
"var.foo.bar": "baz",
|
|
|
|
},
|
|
|
|
[]string{"foo", "bar"},
|
|
|
|
"baz",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid key
|
|
|
|
{
|
|
|
|
map[string]string{
|
|
|
|
"var.foo.bar": "baz",
|
|
|
|
},
|
|
|
|
[]string{"foo", "baz"},
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
map[string]string{
|
|
|
|
"var.foo.bar": "baz",
|
|
|
|
},
|
|
|
|
[]string{"foo", "bar", "baz"},
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tc := range cases {
|
|
|
|
actual, err := interpolationFuncLookup(tc.M, tc.Args...)
|
|
|
|
if (err != nil) != tc.Error {
|
|
|
|
t.Fatalf("%d: err: %s", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != tc.Result {
|
|
|
|
t.Fatalf("%d: bad: %#v", i, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-13 20:50:44 +01:00
|
|
|
*/
|
2014-11-07 19:23:02 +01:00
|
|
|
|
|
|
|
func TestInterpolateFuncElement(t *testing.T) {
|
2015-01-13 20:50:44 +01:00
|
|
|
testFunction(t, []testFunctionCase{
|
2014-11-07 19:23:02 +01:00
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
fmt.Sprintf(`${element("%s", "1")}`,
|
|
|
|
"foo"+InterpSplitDelim+"baz"),
|
2014-11-07 19:23:02 +01:00
|
|
|
"baz",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
`${element("foo", "0")}`,
|
2014-11-07 19:23:02 +01:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid index should wrap vs. out-of-bounds
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
fmt.Sprintf(`${element("%s", "2")}`,
|
|
|
|
"foo"+InterpSplitDelim+"baz"),
|
2014-11-07 19:23:02 +01:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Too many args
|
|
|
|
{
|
2015-01-13 20:50:44 +01:00
|
|
|
fmt.Sprintf(`${element("%s", "0", "2")}`,
|
|
|
|
"foo"+InterpSplitDelim+"baz"),
|
|
|
|
nil,
|
2014-11-07 19:23:02 +01:00
|
|
|
true,
|
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type testFunctionCase struct {
|
|
|
|
Input string
|
|
|
|
Result interface{}
|
|
|
|
Error bool
|
|
|
|
}
|
2014-11-07 19:23:02 +01:00
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
func testFunction(t *testing.T, cases []testFunctionCase) {
|
2014-11-07 19:23:02 +01:00
|
|
|
for i, tc := range cases {
|
2015-01-13 20:50:44 +01:00
|
|
|
ast, err := lang.Parse(tc.Input)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%d: err: %s", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine := langEngine(nil)
|
|
|
|
out, _, err := engine.Execute(ast)
|
2014-11-07 19:23:02 +01:00
|
|
|
if (err != nil) != tc.Error {
|
|
|
|
t.Fatalf("%d: err: %s", i, err)
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
if !reflect.DeepEqual(out, tc.Result) {
|
|
|
|
t.Fatalf("%d: bad: %#v", i, out)
|
2014-11-07 19:23:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|