2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-12-11 20:23:08 +00:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerate(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
template string
|
|
|
|
additionalOpts []Opt
|
|
|
|
data interface{}
|
|
|
|
|
|
|
|
expected string
|
|
|
|
expectErr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"template without arguments": {
|
|
|
|
template: "this is a template",
|
|
|
|
data: nil,
|
|
|
|
expected: "this is a template",
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"template with arguments but no data": {
|
|
|
|
template: "this is a {{.String}}",
|
|
|
|
data: nil,
|
|
|
|
expected: "this is a <no value>",
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"template with arguments": {
|
|
|
|
template: "this is a {{.String}}",
|
|
|
|
data: struct {
|
|
|
|
String string
|
|
|
|
}{
|
|
|
|
String: "foobar",
|
|
|
|
},
|
|
|
|
expected: "this is a foobar",
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"template with builtin functions": {
|
|
|
|
template: `{{.String | truncate 10}}
|
|
|
|
{{.String | uppercase}}
|
|
|
|
{{.String | lowercase}}
|
|
|
|
{{.String | replace " " "."}}
|
|
|
|
{{.String | sha256}}
|
2021-01-27 18:59:06 +00:00
|
|
|
{{.String | base64}}
|
2021-01-13 17:49:17 +00:00
|
|
|
{{.String | truncate_sha256 20}}`,
|
2020-12-11 20:23:08 +00:00
|
|
|
data: struct {
|
|
|
|
String string
|
|
|
|
}{
|
|
|
|
String: "Some string with Multiple Capitals LETTERS",
|
|
|
|
},
|
|
|
|
expected: `Some strin
|
|
|
|
SOME STRING WITH MULTIPLE CAPITALS LETTERS
|
|
|
|
some string with multiple capitals letters
|
|
|
|
Some.string.with.Multiple.Capitals.LETTERS
|
|
|
|
da9872dd96609c72897defa11fe81017a62c3f44339d9d3b43fe37540ede3601
|
2021-01-27 18:59:06 +00:00
|
|
|
U29tZSBzdHJpbmcgd2l0aCBNdWx0aXBsZSBDYXBpdGFscyBMRVRURVJT
|
2021-01-13 17:49:17 +00:00
|
|
|
Some string 6841cf80`,
|
2020-12-11 20:23:08 +00:00
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
"custom function": {
|
|
|
|
template: "{{foo}}",
|
|
|
|
additionalOpts: []Opt{
|
|
|
|
Function("foo", func() string {
|
|
|
|
return "custom-foo"
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
expected: "custom-foo",
|
|
|
|
expectErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
opts := append(test.additionalOpts, Template(test.template))
|
|
|
|
st, err := NewTemplate(opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual, err := st.Generate(test.data)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("random", func(t *testing.T) {
|
|
|
|
for i := 1; i < 100; i++ {
|
|
|
|
st, err := NewTemplate(
|
|
|
|
Template(fmt.Sprintf("{{random %d}}", i)),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual, err := st.Generate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Regexp(t, fmt.Sprintf("^[a-zA-Z0-9]{%d}$", i), actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
t.Run("unix_time", func(t *testing.T) {
|
2020-12-11 20:23:08 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
st, err := NewTemplate(
|
2021-01-13 17:49:17 +00:00
|
|
|
Template("{{unix_time}}"),
|
2020-12-11 20:23:08 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual, err := st.Generate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Regexp(t, "^[0-9]+$", actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
t.Run("unix_time_millis", func(t *testing.T) {
|
2020-12-11 20:23:08 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
st, err := NewTemplate(
|
2021-01-13 17:49:17 +00:00
|
|
|
Template("{{unix_time_millis}}"),
|
2020-12-11 20:23:08 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual, err := st.Generate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Regexp(t, "^[0-9]+$", actual)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
t.Run("timestamp", func(t *testing.T) {
|
2020-12-11 20:23:08 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
st, err := NewTemplate(
|
2021-01-13 17:49:17 +00:00
|
|
|
Template(`{{timestamp "2006-01-02T15:04:05.000Z"}}`),
|
2020-12-11 20:23:08 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual, err := st.Generate(nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-01-13 17:49:17 +00:00
|
|
|
require.Regexp(t, `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$`, actual)
|
2020-12-11 20:23:08 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadConstructorArguments(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
opts []Opt
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"missing template": {
|
|
|
|
opts: nil,
|
|
|
|
},
|
|
|
|
"missing custom function name": {
|
|
|
|
opts: []Opt{
|
|
|
|
Template("foo bar"),
|
|
|
|
Function("", func() string {
|
|
|
|
return "foo"
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"missing custom function": {
|
|
|
|
opts: []Opt{
|
|
|
|
Template("foo bar"),
|
|
|
|
Function("foo", nil),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bad template": {
|
|
|
|
opts: []Opt{
|
|
|
|
Template("{{.String"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
st, err := NewTemplate(test.opts...)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
str, err := st.Generate(nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "", str)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("erroring custom function", func(t *testing.T) {
|
|
|
|
st, err := NewTemplate(
|
|
|
|
Template("{{foo}}"),
|
|
|
|
Function("foo", func() (string, error) {
|
|
|
|
return "", fmt.Errorf("an error!")
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
str, err := st.Generate(nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "", str)
|
|
|
|
})
|
|
|
|
}
|