2015-05-16 00:05:47 -04:00
|
|
|
package interpolate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-05-16 00:14:41 -04:00
|
|
|
"strconv"
|
2015-05-16 00:05:47 -04:00
|
|
|
"testing"
|
2015-05-16 00:12:54 -04:00
|
|
|
"time"
|
2015-05-16 00:05:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFuncEnv(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`{{env "PACKER_TEST_ENV"}}`,
|
|
|
|
`foo`,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`{{env "PACKER_TEST_ENV_NOPE"}}`,
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv("PACKER_TEST_ENV", "foo")
|
|
|
|
defer os.Setenv("PACKER_TEST_ENV", "")
|
|
|
|
|
2015-05-23 19:06:11 -04:00
|
|
|
ctx := &Context{EnableEnv: true}
|
2015-05-16 00:05:47 -04:00
|
|
|
for _, tc := range cases {
|
|
|
|
i := &I{Value: tc.Input}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != tc.Output {
|
|
|
|
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuncEnv_disable(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
Error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`{{env "PACKER_TEST_ENV"}}`,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-05-23 19:06:11 -04:00
|
|
|
ctx := &Context{EnableEnv: false}
|
2015-05-16 00:05:47 -04:00
|
|
|
for _, tc := range cases {
|
|
|
|
i := &I{Value: tc.Input}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if (err != nil) != tc.Error {
|
|
|
|
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != tc.Output {
|
|
|
|
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 00:10:12 -04:00
|
|
|
|
2015-05-16 00:12:54 -04:00
|
|
|
func TestFuncIsotime(t *testing.T) {
|
|
|
|
ctx := &Context{}
|
|
|
|
i := &I{Value: "{{isotime}}"}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := time.Parse(time.RFC3339, result)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTime := time.Now().UTC()
|
|
|
|
if currentTime.Sub(val) > 2*time.Second {
|
|
|
|
t.Fatalf("val: %d (current: %d)", val, currentTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:10:12 -04:00
|
|
|
func TestFuncPwd(t *testing.T) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`{{pwd}}`,
|
|
|
|
wd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &Context{}
|
|
|
|
for _, tc := range cases {
|
|
|
|
i := &I{Value: tc.Input}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != tc.Output {
|
|
|
|
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 00:14:41 -04:00
|
|
|
|
|
|
|
func TestFuncTimestamp(t *testing.T) {
|
|
|
|
expected := strconv.FormatInt(InitTime.Unix(), 10)
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`{{timestamp}}`,
|
|
|
|
expected,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &Context{}
|
|
|
|
for _, tc := range cases {
|
|
|
|
i := &I{Value: tc.Input}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != tc.Output {
|
|
|
|
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 00:18:27 -04:00
|
|
|
|
|
|
|
func TestFuncUser(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
`{{user "foo"}}`,
|
|
|
|
`foo`,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`{{user "what"}}`,
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := &Context{
|
|
|
|
UserVariables: map[string]string{
|
|
|
|
"foo": "foo",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
i := &I{Value: tc.Input}
|
|
|
|
result, err := i.Render(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result != tc.Output {
|
|
|
|
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|