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
|
|
|
|
2016-01-31 08:38:37 +01:00
|
|
|
"github.com/hashicorp/hil"
|
|
|
|
"github.com/hashicorp/hil/ast"
|
2015-01-13 20:50:44 +01:00
|
|
|
)
|
2014-08-19 22:14:45 +02:00
|
|
|
|
2015-09-15 15:50:31 +02:00
|
|
|
func TestInterpolateFuncCompact(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// empty string within array
|
|
|
|
{
|
2015-09-16 11:09:34 +02:00
|
|
|
`${compact(split(",", "a,,b"))}`,
|
|
|
|
NewStringList([]string{"a", "b"}).String(),
|
2015-09-15 15:50:31 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// empty string at the end of array
|
|
|
|
{
|
2015-09-16 11:09:34 +02:00
|
|
|
`${compact(split(",", "a,b,"))}`,
|
|
|
|
NewStringList([]string{"a", "b"}).String(),
|
2015-09-15 15:50:31 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// single empty string
|
|
|
|
{
|
|
|
|
`${compact(split(",", ""))}`,
|
|
|
|
NewStringList([]string{}).String(),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-22 17:10:42 +02:00
|
|
|
func TestInterpolateFuncCidrHost(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${cidrhost("192.168.1.0/24", 5)}`,
|
|
|
|
"192.168.1.5",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrhost("192.168.1.0/30", 255)}`,
|
|
|
|
nil,
|
|
|
|
true, // 255 doesn't fit in two bits
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrhost("not-a-cidr", 6)}`,
|
|
|
|
nil,
|
|
|
|
true, // not a valid CIDR mask
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrhost("10.256.0.0/8", 6)}`,
|
|
|
|
nil,
|
|
|
|
true, // can't have an octet >255
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolateFuncCidrNetmask(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${cidrnetmask("192.168.1.0/24")}`,
|
|
|
|
"255.255.255.0",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrnetmask("192.168.1.0/32")}`,
|
|
|
|
"255.255.255.255",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrnetmask("0.0.0.0/0")}`,
|
|
|
|
"0.0.0.0",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// This doesn't really make sense for IPv6 networks
|
|
|
|
// but it ought to do something sensible anyway.
|
|
|
|
`${cidrnetmask("1::/64")}`,
|
|
|
|
"ffff:ffff:ffff:ffff::",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrnetmask("not-a-cidr")}`,
|
|
|
|
nil,
|
|
|
|
true, // not a valid CIDR mask
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrnetmask("10.256.0.0/8")}`,
|
|
|
|
nil,
|
|
|
|
true, // can't have an octet >255
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolateFuncCidrSubnet(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${cidrsubnet("192.168.2.0/20", 4, 6)}`,
|
|
|
|
"192.168.6.0/24",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrsubnet("fe80::/48", 16, 6)}`,
|
|
|
|
"fe80:0:0:6::/64",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// IPv4 address encoded in IPv6 syntax gets normalized
|
|
|
|
`${cidrsubnet("::ffff:192.168.0.0/112", 8, 6)}`,
|
|
|
|
"192.168.6.0/24",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrsubnet("192.168.0.0/30", 4, 6)}`,
|
|
|
|
nil,
|
|
|
|
true, // not enough bits left
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrsubnet("192.168.0.0/16", 2, 16)}`,
|
|
|
|
nil,
|
|
|
|
true, // can't encode 16 in 2 bits
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrsubnet("not-a-cidr", 4, 6)}`,
|
|
|
|
nil,
|
|
|
|
true, // not a valid CIDR mask
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${cidrsubnet("10.256.0.0/8", 4, 6)}`,
|
|
|
|
nil,
|
|
|
|
true, // can't have an octet >255
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-08 07:34:56 +01:00
|
|
|
func TestInterpolateFuncCoalesce(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${coalesce("first", "second", "third")}`,
|
|
|
|
"first",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${coalesce("", "second", "third")}`,
|
|
|
|
"second",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${coalesce("", "", "")}`,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${coalesce("foo")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:53:33 +02:00
|
|
|
func TestInterpolateFuncDeprecatedConcat(t *testing.T) {
|
2015-01-13 21:47:54 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${concat("foo", "bar")}`,
|
|
|
|
"foobar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${concat("foo")}`,
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${concat()}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-03 16:53:33 +02:00
|
|
|
func TestInterpolateFuncConcat(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// String + list
|
|
|
|
{
|
|
|
|
`${concat("a", split(",", "b,c"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// List + string
|
|
|
|
{
|
|
|
|
`${concat(split(",", "a,b"), "c")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Single list
|
|
|
|
{
|
|
|
|
`${concat(split(",", ",foo,"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"", "foo", ""}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${concat(split(",", "a,b,c"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Two lists
|
|
|
|
{
|
|
|
|
`${concat(split(",", "a,b,c"), split(",", "d,e"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c", "d", "e"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
// Two lists with different separators
|
|
|
|
{
|
|
|
|
`${concat(split(",", "a,b,c"), split(" ", "d e"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c", "d", "e"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// More lists
|
|
|
|
{
|
|
|
|
`${concat(split(",", "a,b"), split(",", "c,d"), split(",", "e,f"), split(",", "0,1"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"a", "b", "c", "d", "e", "f", "0", "1"}).String(),
|
2015-05-03 16:53:33 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 21:06:04 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${file("%s")}`, path),
|
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
2014-07-23 03:35:36 +02:00
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Invalid path
|
|
|
|
{
|
|
|
|
`${file("/i/dont/exist")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2014-07-23 03:35:36 +02:00
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
`${file("foo", "bar")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2014-07-23 03:35:36 +02:00
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
2014-07-23 03:35:36 +02:00
|
|
|
}
|
2014-07-23 03:35:53 +02:00
|
|
|
|
2015-03-02 19:26:06 +01:00
|
|
|
func TestInterpolateFuncFormat(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${format("hello")}`,
|
|
|
|
"hello",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${format("hello %s", "world")}`,
|
|
|
|
"hello world",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${format("hello %d", 42)}`,
|
|
|
|
"hello 42",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${format("hello %05d", 42)}`,
|
|
|
|
"hello 00042",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${format("hello %05d", 12345)}`,
|
|
|
|
"hello 12345",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:34:40 +02:00
|
|
|
func TestInterpolateFuncFormatList(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// formatlist requires at least one list
|
|
|
|
{
|
|
|
|
`${formatlist("hello")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${formatlist("hello %s", "world")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// formatlist applies to each list element in turn
|
|
|
|
{
|
|
|
|
`${formatlist("<%s>", split(",", "A,B"))}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"<A>", "<B>"}).String(),
|
2015-05-06 05:34:40 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
// formatlist repeats scalar elements
|
|
|
|
{
|
|
|
|
`${join(", ", formatlist("%s=%s", "x", split(",", "A,B,C")))}`,
|
|
|
|
"x=A, x=B, x=C",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// Multiple lists are walked in parallel
|
|
|
|
{
|
|
|
|
`${join(", ", formatlist("%s=%s", split(",", "A,B,C"), split(",", "1,2,3")))}`,
|
|
|
|
"A=1, B=2, C=3",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// Mismatched list lengths generate an error
|
|
|
|
{
|
|
|
|
`${formatlist("%s=%2s", split(",", "A,B,C,D"), split(",", "1,2,3"))}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-06-26 01:45:17 +02:00
|
|
|
// Works with lists of length 1 [GH-2240]
|
|
|
|
{
|
|
|
|
`${formatlist("%s.id", split(",", "demo-rest-elb"))}`,
|
|
|
|
NewStringList([]string{"demo-rest-elb.id"}).String(),
|
|
|
|
false,
|
|
|
|
},
|
2015-05-06 05:34:40 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-07-12 21:55:19 +02:00
|
|
|
func TestInterpolateFuncIndex(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${index("test", "")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${index("%s", "foo")}`,
|
|
|
|
NewStringList([]string{"notfoo", "stillnotfoo", "bar"}).String()),
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${index("%s", "foo")}`,
|
|
|
|
NewStringList([]string{"foo"}).String()),
|
|
|
|
"0",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${index("%s", "bar")}`,
|
|
|
|
NewStringList([]string{"foo", "spam", "bar", "eggs"}).String()),
|
|
|
|
"2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-10 06:22:35 +02:00
|
|
|
func TestInterpolateFuncJoin(t *testing.T) {
|
2015-01-13 21:06:04 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${join(",")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2014-10-10 06:22:35 +02:00
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
{
|
2015-06-26 00:55:51 +02:00
|
|
|
fmt.Sprintf(`${join(",", "%s")}`,
|
|
|
|
NewStringList([]string{"foo"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO
|
|
|
|
{
|
|
|
|
`${join(",", "foo", "bar")}`,
|
|
|
|
"foo,bar",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
*/
|
2014-10-10 06:22:35 +02:00
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
{
|
2015-01-13 21:06:04 +01:00
|
|
|
fmt.Sprintf(`${join(".", "%s")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", "bar", "baz"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
"foo.bar.baz",
|
2015-01-13 20:50:44 +01:00
|
|
|
false,
|
|
|
|
},
|
2014-10-10 06:22:35 +02:00
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
2014-10-10 06:22:35 +02:00
|
|
|
}
|
|
|
|
|
2016-03-29 03:02:06 +02:00
|
|
|
func TestInterpolateFuncJSONEncode(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Vars: map[string]ast.Variable{
|
|
|
|
"easy": ast.Variable{
|
|
|
|
Value: "test",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
"hard": ast.Variable{
|
|
|
|
Value: " foo \\ \n \t \" bar ",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${jsonencode("test")}`,
|
|
|
|
`"test"`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${jsonencode(easy)}`,
|
|
|
|
`"test"`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${jsonencode(hard)}`,
|
|
|
|
`" foo \\ \n \t \" bar "`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${jsonencode("")}`,
|
|
|
|
`""`,
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${jsonencode()}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:37:40 +01:00
|
|
|
func TestInterpolateFuncReplace(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// Regular search and replace
|
|
|
|
{
|
|
|
|
`${replace("hello", "hel", "bel")}`,
|
|
|
|
"bello",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Search string doesn't match
|
|
|
|
{
|
|
|
|
`${replace("hello", "nope", "bel")}`,
|
|
|
|
"hello",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Regular expression
|
|
|
|
{
|
|
|
|
`${replace("hello", "/l/", "L")}`,
|
|
|
|
"heLLo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${replace("helo", "/(l)/", "$1$1")}`,
|
|
|
|
"hello",
|
|
|
|
false,
|
|
|
|
},
|
2015-03-02 18:44:45 +01:00
|
|
|
|
|
|
|
// Bad regexp
|
|
|
|
{
|
|
|
|
`${replace("helo", "/(l/", "$1$1")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-03-02 18:37:40 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-12 16:17:26 +02:00
|
|
|
func TestInterpolateFuncLength(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// Raw strings
|
|
|
|
{
|
|
|
|
`${length("")}`,
|
|
|
|
"0",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length("a")}`,
|
|
|
|
"1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(" ")}`,
|
|
|
|
"1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(" a ,")}`,
|
|
|
|
"4",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length("aaa")}`,
|
|
|
|
"3",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Lists
|
|
|
|
{
|
|
|
|
`${length(split(",", "a"))}`,
|
|
|
|
"1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(split(",", "foo,"))}`,
|
|
|
|
"2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(split(",", ",foo,"))}`,
|
|
|
|
"3",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(split(",", "foo,bar"))}`,
|
|
|
|
"2",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`${length(split(".", "one.two.three.four.five"))}`,
|
|
|
|
"5",
|
|
|
|
false,
|
|
|
|
},
|
2015-10-25 19:55:21 +01:00
|
|
|
// Want length 0 if we split an empty string then compact
|
|
|
|
{
|
|
|
|
`${length(compact(split(",", "")))}`,
|
|
|
|
"0",
|
|
|
|
false,
|
|
|
|
},
|
2015-04-12 16:17:26 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:36:31 +01:00
|
|
|
func TestInterpolateFuncSignum(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${signum()}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${signum("")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${signum(0)}`,
|
|
|
|
"0",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${signum(15)}`,
|
|
|
|
"1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${signum(-29)}`,
|
|
|
|
"-1",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-28 22:59:16 +01:00
|
|
|
func TestInterpolateFuncSplit(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${split(",")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2015-06-26 00:55:51 +02:00
|
|
|
{
|
|
|
|
`${split(",", "")}`,
|
|
|
|
NewStringList([]string{""}).String(),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-01-28 22:59:16 +01:00
|
|
|
{
|
|
|
|
`${split(",", "foo")}`,
|
2015-06-26 00:55:51 +02:00
|
|
|
NewStringList([]string{"foo"}).String(),
|
2015-01-28 22:59:16 +01:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-04-12 16:17:26 +02:00
|
|
|
{
|
|
|
|
`${split(",", ",,,")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"", "", "", ""}).String(),
|
2015-04-12 16:17:26 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${split(",", "foo,")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", ""}).String(),
|
2015-04-12 16:17:26 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${split(",", ",foo,")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"", "foo", ""}).String(),
|
2015-04-12 16:17:26 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-01-28 22:59:16 +01:00
|
|
|
{
|
|
|
|
`${split(".", "foo.bar.baz")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", "bar", "baz"}).String(),
|
2015-01-28 22:59:16 +01:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-21 22:12:43 +02:00
|
|
|
func TestInterpolateFuncLookup(t *testing.T) {
|
2015-01-13 21:06:04 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
2015-01-15 07:01:42 +01:00
|
|
|
Vars: map[string]ast.Variable{
|
|
|
|
"var.foo.bar": ast.Variable{
|
2015-01-14 19:40:43 +01:00
|
|
|
Value: "baz",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
2015-01-13 21:06:04 +01:00
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${lookup("foo", "bar")}`,
|
|
|
|
"baz",
|
|
|
|
false,
|
2014-07-21 22:12:43 +02:00
|
|
|
},
|
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Invalid key
|
|
|
|
{
|
|
|
|
`${lookup("foo", "baz")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
2014-07-21 22:12:43 +02:00
|
|
|
},
|
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
`${lookup("foo", "bar", "baz")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
2014-07-21 22:12:43 +02:00
|
|
|
},
|
|
|
|
},
|
2015-01-13 21:06:04 +01:00
|
|
|
})
|
2014-07-21 22:12:43 +02:00
|
|
|
}
|
2014-11-07 19:23:02 +01:00
|
|
|
|
2015-06-02 23:48:38 +02:00
|
|
|
func TestInterpolateFuncKeys(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Vars: map[string]ast.Variable{
|
|
|
|
"var.foo.bar": ast.Variable{
|
|
|
|
Value: "baz",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
"var.foo.qux": ast.Variable{
|
|
|
|
Value: "quack",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
"var.str": ast.Variable{
|
|
|
|
Value: "astring",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${keys("foo")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"bar", "qux"}).String(),
|
2015-06-02 23:48:38 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid key
|
|
|
|
{
|
|
|
|
`${keys("not")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
`${keys("foo", "bar")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Not a map
|
|
|
|
{
|
|
|
|
`${keys("str")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolateFuncValues(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Vars: map[string]ast.Variable{
|
|
|
|
"var.foo.bar": ast.Variable{
|
|
|
|
Value: "quack",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
"var.foo.qux": ast.Variable{
|
|
|
|
Value: "baz",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
"var.str": ast.Variable{
|
|
|
|
Value: "astring",
|
|
|
|
Type: ast.TypeString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${values("foo")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"quack", "baz"}).String(),
|
2015-06-02 23:48:38 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid key
|
|
|
|
{
|
|
|
|
`${values("not")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
`${values("foo", "bar")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Not a map
|
|
|
|
{
|
|
|
|
`${values("str")}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-11-07 19:23:02 +01:00
|
|
|
func TestInterpolateFuncElement(t *testing.T) {
|
2015-01-13 21:06:04 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${element("%s", "1")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", "baz"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
"baz",
|
|
|
|
false,
|
|
|
|
},
|
2014-11-07 19:23:02 +01:00
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
{
|
2015-06-26 00:55:51 +02:00
|
|
|
fmt.Sprintf(`${element("%s", "0")}`,
|
|
|
|
NewStringList([]string{"foo"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
2014-11-07 19:23:02 +01:00
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Invalid index should wrap vs. out-of-bounds
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${element("%s", "2")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", "baz"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
"foo",
|
|
|
|
false,
|
|
|
|
},
|
2014-11-07 19:23:02 +01:00
|
|
|
|
2016-02-22 23:47:17 +01:00
|
|
|
// Negative number should fail
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${element("%s", "-1")}`,
|
|
|
|
NewStringList([]string{"foo"}).String()),
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
// Too many args
|
|
|
|
{
|
|
|
|
fmt.Sprintf(`${element("%s", "0", "2")}`,
|
2015-06-25 16:48:37 +02:00
|
|
|
NewStringList([]string{"foo", "baz"}).String()),
|
2015-01-13 21:06:04 +01:00
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2014-11-07 19:23:02 +01:00
|
|
|
},
|
2015-01-13 20:50:44 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-25 09:23:36 +02:00
|
|
|
func TestInterpolateFuncBase64Encode(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// Regular base64 encoding
|
|
|
|
{
|
2015-10-04 00:12:51 +02:00
|
|
|
`${base64encode("abc123!?$*&()'-=@~")}`,
|
2015-09-25 09:23:36 +02:00
|
|
|
"YWJjMTIzIT8kKiYoKSctPUB+",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolateFuncBase64Decode(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
// Regular base64 decoding
|
|
|
|
{
|
2015-10-04 00:12:51 +02:00
|
|
|
`${base64decode("YWJjMTIzIT8kKiYoKSctPUB+")}`,
|
2015-09-25 09:23:36 +02:00
|
|
|
"abc123!?$*&()'-=@~",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Invalid base64 data decoding
|
|
|
|
{
|
2015-10-04 00:12:51 +02:00
|
|
|
`${base64decode("this-is-an-invalid-base64-data")}`,
|
2015-09-25 09:23:36 +02:00
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-20 03:49:51 +02:00
|
|
|
func TestInterpolateFuncLower(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${lower("HELLO")}`,
|
|
|
|
"hello",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${lower("")}`,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${lower()}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolateFuncUpper(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${upper("hello")}`,
|
|
|
|
"HELLO",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${upper("")}`,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`${upper()}`,
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-27 23:28:56 +01:00
|
|
|
func TestInterpolateFuncSha1(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${sha1("test")}`,
|
|
|
|
"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:54:04 +01:00
|
|
|
func TestInterpolateFuncSha256(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
2016-01-29 13:09:57 +01:00
|
|
|
{ // hexadecimal representation of sha256 sum
|
2016-01-17 00:54:04 +01:00
|
|
|
`${sha256("test")}`,
|
|
|
|
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-30 10:52:45 +01:00
|
|
|
func TestInterpolateFuncTrimSpace(t *testing.T) {
|
2016-01-30 00:28:04 +01:00
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
2016-01-30 10:52:45 +01:00
|
|
|
`${trimspace(" test ")}`,
|
2016-01-30 00:28:04 +01:00
|
|
|
"test",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-29 13:09:57 +01:00
|
|
|
func TestInterpolateFuncBase64Sha256(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${base64sha256("test")}`,
|
|
|
|
"n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg=",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // This will differ because we're base64-encoding hex represantiation, not raw bytes
|
|
|
|
`${base64encode(sha256("test"))}`,
|
|
|
|
"OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZjMTViMGYwMGEwOA==",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-23 12:29:11 +01:00
|
|
|
func TestInterpolateFuncMd5(t *testing.T) {
|
|
|
|
testFunction(t, testFunctionConfig{
|
|
|
|
Cases: []testFunctionCase{
|
|
|
|
{
|
|
|
|
`${md5("tada")}`,
|
|
|
|
"ce47d07243bb6eaf5e1322c81baf9bbf",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // Confirm that we're not trimming any whitespaces
|
|
|
|
`${md5(" tada ")}`,
|
|
|
|
"aadf191a583e53062de2d02c008141c4",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // We accept empty string too
|
|
|
|
`${md5("")}`,
|
|
|
|
"d41d8cd98f00b204e9800998ecf8427e",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-21 21:14:30 +01:00
|
|
|
func TestInterpolateFuncUUID(t *testing.T) {
|
|
|
|
results := make(map[string]bool)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
ast, err := hil.Parse("${uuid()}")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _, err := hil.Eval(ast, langEvalConfig(nil))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if results[out.(string)] {
|
|
|
|
t.Fatalf("Got unexpected duplicate uuid: %s", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
results[out.(string)] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:06:04 +01:00
|
|
|
type testFunctionConfig struct {
|
|
|
|
Cases []testFunctionCase
|
2015-01-15 07:01:42 +01:00
|
|
|
Vars map[string]ast.Variable
|
2015-01-13 21:06:04 +01:00
|
|
|
}
|
|
|
|
|
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 21:06:04 +01:00
|
|
|
func testFunction(t *testing.T, config testFunctionConfig) {
|
|
|
|
for i, tc := range config.Cases {
|
2016-01-31 08:38:37 +01:00
|
|
|
ast, err := hil.Parse(tc.Input)
|
2015-01-13 20:50:44 +01:00
|
|
|
if err != nil {
|
2015-05-03 16:53:58 +02:00
|
|
|
t.Fatalf("Case #%d: input: %#v\nerr: %s", i, tc.Input, err)
|
2015-01-13 20:50:44 +01:00
|
|
|
}
|
|
|
|
|
2016-01-31 08:38:37 +01:00
|
|
|
out, _, err := hil.Eval(ast, langEvalConfig(config.Vars))
|
2015-10-08 14:48:04 +02:00
|
|
|
if err != nil != tc.Error {
|
2015-05-03 16:53:58 +02:00
|
|
|
t.Fatalf("Case #%d:\ninput: %#v\nerr: %s", i, tc.Input, err)
|
2014-11-07 19:23:02 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:50:44 +01:00
|
|
|
if !reflect.DeepEqual(out, tc.Result) {
|
2015-03-02 18:37:40 +01:00
|
|
|
t.Fatalf(
|
2015-04-13 15:12:43 +02:00
|
|
|
"%d: bad output for input: %s\n\nOutput: %#v\nExpected: %#v",
|
|
|
|
i, tc.Input, out, tc.Result)
|
2014-11-07 19:23:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|