2017-04-17 14:25:59 +00:00
|
|
|
package functions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2021-03-22 15:22:23 +00:00
|
|
|
"strings"
|
2017-04-17 14:25:59 +00:00
|
|
|
"testing"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2024-02-28 22:54:18 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-17 14:25:59 +00:00
|
|
|
)
|
|
|
|
|
2024-02-28 22:54:18 +00:00
|
|
|
func renderHelper(t *testing.T, rawTpl string, ctx map[string]interface{}) string {
|
|
|
|
tpl, err := template.New("mytemplate").Funcs(GetFunctionMap()).Parse(rawTpl)
|
|
|
|
require.NoError(t, err)
|
2017-04-17 14:25:59 +00:00
|
|
|
|
2021-03-22 15:22:23 +00:00
|
|
|
SetSubTemplateVariables(ctx)
|
|
|
|
|
2017-04-17 14:25:59 +00:00
|
|
|
buf := bytes.NewBufferString("")
|
2024-02-28 22:54:18 +00:00
|
|
|
require.NoError(t, tpl.Execute(buf, ctx))
|
2017-04-17 14:25:59 +00:00
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomString() string {
|
|
|
|
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
|
2024-02-28 22:54:18 +00:00
|
|
|
//#nosec:G404 // Okay to generate non-deterministic strings
|
2017-04-17 14:25:59 +00:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
|
|
|
result := make([]byte, 32)
|
|
|
|
for i := range result {
|
|
|
|
result[i] = chars[r.Intn(len(chars))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GetFunctionMap(t *testing.T) {
|
|
|
|
f := GetFunctionMap()
|
2024-02-28 22:54:18 +00:00
|
|
|
require.NotNil(t, f)
|
2017-04-17 14:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 13:55:52 +00:00
|
|
|
func Test_base64(t *testing.T) {
|
|
|
|
var (
|
|
|
|
b64 = "aGVsbG8="
|
|
|
|
plain = "hello"
|
|
|
|
)
|
|
|
|
|
2024-02-28 22:54:18 +00:00
|
|
|
assert.Equal(t, plain, renderHelper(t, fmt.Sprintf(`{{ b64dec "%s" }}`, b64), nil))
|
|
|
|
assert.Equal(t, b64, renderHelper(t, fmt.Sprintf(`{{ b64enc "%s" }}`, plain), nil))
|
2021-08-30 13:55:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 14:25:59 +00:00
|
|
|
func Test_env(t *testing.T) {
|
|
|
|
result := randomString()
|
2024-02-28 22:54:18 +00:00
|
|
|
require.NoError(t, os.Setenv("KORVIKE_TESTING", result))
|
2017-04-17 14:25:59 +00:00
|
|
|
|
2024-02-28 22:54:18 +00:00
|
|
|
assert.Equal(t, result, renderHelper(t, `{{ env "KORVIKE_TESTING" }}`, nil))
|
2017-04-17 14:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_file(t *testing.T) {
|
2024-02-28 22:54:18 +00:00
|
|
|
f, err := os.CreateTemp("", "")
|
|
|
|
require.NoError(t, err)
|
2017-04-17 14:25:59 +00:00
|
|
|
|
|
|
|
p := f.Name()
|
|
|
|
result := randomString()
|
|
|
|
fmt.Fprint(f, result)
|
2024-02-28 22:54:18 +00:00
|
|
|
require.NoError(t, f.Close())
|
2017-04-17 14:25:59 +00:00
|
|
|
|
2024-02-28 22:54:18 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
require.NoError(t, os.Remove(p))
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, result, renderHelper(t, fmt.Sprintf("{{ file %q }}", p), nil))
|
2017-04-17 14:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 21:45:58 +00:00
|
|
|
func Test_hash(t *testing.T) {
|
|
|
|
input := "I'm a string to hash"
|
|
|
|
for algo, exp := range map[string]string{
|
2024-02-28 22:54:18 +00:00
|
|
|
"sha1sum": "bd41599338445f401b8d3751fbe718e8a0b52004",
|
|
|
|
"sha256sum": "ba32f090baf28862816a10da05509b31393704184ae49c68f9eb2933afa9e4d1",
|
|
|
|
"sha512sum": "3288edcff4f28526fe8ecfb6d5182f2a446ab0572550c9591d1f5cacd377397af25c0274c9c2428c35422d215ccdc304d0353c093c76e750f9d7c4d54e64eed8",
|
2021-04-11 21:45:58 +00:00
|
|
|
} {
|
2024-02-28 22:54:18 +00:00
|
|
|
assert.Equal(t, exp, renderHelper(t, fmt.Sprintf("{{ %s %q }}", algo, input), nil), algo)
|
2021-04-11 21:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 14:25:59 +00:00
|
|
|
func Test_now(t *testing.T) {
|
2024-02-28 22:54:18 +00:00
|
|
|
_, err := time.Parse(
|
|
|
|
time.RFC3339Nano,
|
|
|
|
renderHelper(t, fmt.Sprintf("{{ now | date %q }}", time.RFC3339Nano), nil),
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
2017-04-17 14:25:59 +00:00
|
|
|
}
|
2021-03-22 15:22:23 +00:00
|
|
|
|
|
|
|
func Test_tplexec(t *testing.T) {
|
|
|
|
result := randomString()
|
2024-02-28 22:54:18 +00:00
|
|
|
require.NoError(t, os.Setenv("KORVIKE_TESTING", result))
|
|
|
|
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
strings.Join([]string{
|
|
|
|
"test",
|
|
|
|
result,
|
|
|
|
}, ":"),
|
|
|
|
renderHelper(t, `{{ tplexec "{{ .var }}:{{ env \"KORVIKE_TESTING\" }}" }}`, map[string]any{
|
|
|
|
"var": "test",
|
|
|
|
}),
|
|
|
|
)
|
2021-03-22 15:22:23 +00:00
|
|
|
}
|