2020-11-18 05:28:07 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2022-06-14 19:59:53 +00:00
|
|
|
"errors"
|
2020-11-18 05:28:07 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2022-06-14 19:59:53 +00:00
|
|
|
|
|
|
|
"github.com/matryer/is"
|
2020-11-18 05:28:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
tmplDir string
|
|
|
|
regDir string
|
|
|
|
reg *Registry
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Set up template
|
|
|
|
setupTemplate()
|
|
|
|
|
|
|
|
// Set up registry
|
|
|
|
setupRegistry()
|
|
|
|
|
|
|
|
status := m.Run()
|
|
|
|
|
|
|
|
if err = os.RemoveAll(tmplDir); err != nil {
|
|
|
|
fmt.Printf("could not clean up temp directory %s\n", tmplDir)
|
|
|
|
}
|
|
|
|
if err = os.RemoveAll(regDir); err != nil {
|
|
|
|
fmt.Printf("could not clean up temp directory %s\n", regDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemplate(t *testing.T) {
|
|
|
|
t.Run("save", testSave)
|
|
|
|
t.Run("get", testGet)
|
|
|
|
t.Run("get-fail", testGetFail)
|
|
|
|
t.Run("execute", testExecute)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSave(t *testing.T) {
|
2022-06-14 19:59:53 +00:00
|
|
|
assert := is.New(t)
|
|
|
|
_, err := reg.SaveTemplate("test", tmplDir)
|
|
|
|
assert.NoErr(err) // Should save template
|
2020-11-18 05:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testGet(t *testing.T) {
|
2022-06-14 19:59:53 +00:00
|
|
|
assert := is.New(t)
|
2020-11-18 05:28:07 +00:00
|
|
|
_, err := reg.GetTemplate("test")
|
2022-06-14 19:59:53 +00:00
|
|
|
assert.NoErr(err) // Should get template
|
2020-11-18 05:28:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testGetFail(t *testing.T) {
|
2022-06-14 19:59:53 +00:00
|
|
|
assert := is.New(t)
|
2020-11-18 05:28:07 +00:00
|
|
|
_, err := reg.GetTemplate("fail")
|
2022-06-14 19:59:53 +00:00
|
|
|
if !errors.As(err, &ErrTemplateNotFound{}) {
|
|
|
|
assert.Fail() // Template should not exist
|
2020-11-18 05:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTemplate() {
|
|
|
|
var err error
|
2023-08-20 20:01:58 +00:00
|
|
|
tmplDir, err = os.MkdirTemp(os.TempDir(), "tmpl-setup")
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template config
|
2022-06-14 19:59:53 +00:00
|
|
|
fi, err := os.Create(filepath.Join(tmplDir, "tmpl.yaml"))
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = fi.WriteString(tmplTemplate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
// Template directories
|
|
|
|
pkgPath := filepath.Join(tmplDir, "template", "{{upper package}}")
|
|
|
|
if err := os.MkdirAll(pkgPath, os.ModePerm); err != nil {
|
2020-11-18 05:28:07 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-03 03:06:32 +00:00
|
|
|
// .tmplkeep file
|
|
|
|
fi, err = os.Create(filepath.Join(pkgPath, ".tmplkeep"))
|
2020-11-22 05:25:24 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Template file
|
2020-11-18 05:28:07 +00:00
|
|
|
fi, err = os.Create(filepath.Join(tmplDir, "template", "TEST"))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
_, err = fi.WriteString(tmplContents)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRegistry() {
|
|
|
|
var err error
|
2023-08-20 20:01:58 +00:00
|
|
|
regDir, err = os.MkdirTemp(os.TempDir(), "tmpl-reg")
|
2020-11-18 05:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reg, err = Open(regDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|