packer-cn/packer-plugin-sdk/template/template_test.go

143 lines
1.8 KiB
Go
Raw Permalink Normal View History

2015-05-19 17:25:56 -04:00
package template
import (
2015-05-21 17:29:45 -04:00
"os"
2015-05-19 17:25:56 -04:00
"path/filepath"
2015-05-21 17:29:45 -04:00
"testing"
2015-05-19 17:25:56 -04:00
)
const FixturesDir = "./test-fixtures"
// fixtureDir returns the path to a test fixtures directory
func fixtureDir(n string) string {
return filepath.Join(FixturesDir, n)
}
2015-05-21 17:29:45 -04:00
func TestTemplateValidate(t *testing.T) {
cases := []struct {
File string
Err bool
}{
{
"validate-good-prov-timeout.json",
false,
},
2015-05-21 17:29:45 -04:00
{
"validate-no-builders.json",
true,
},
{
"validate-bad-override.json",
true,
},
{
"validate-good-override.json",
false,
},
2015-05-21 17:39:32 -04:00
{
"validate-bad-prov-only.json",
true,
},
{
"validate-good-prov-only.json",
false,
},
{
"validate-bad-prov-except.json",
true,
},
{
"validate-good-prov-except.json",
false,
},
{
"validate-bad-pp-only.json",
true,
},
{
"validate-good-pp-only.json",
false,
},
{
"validate-bad-pp-except.json",
true,
},
{
"validate-good-pp-except.json",
false,
},
2015-05-21 17:29:45 -04:00
}
for _, tc := range cases {
f, err := os.Open(fixtureDir(tc.File))
if err != nil {
t.Fatalf("err: %s", err)
}
tpl, err := Parse(f)
f.Close()
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
err = tpl.Validate()
if (err != nil) != tc.Err {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
}
}
2015-05-26 12:07:16 -04:00
func TestOnlyExceptSkip(t *testing.T) {
cases := []struct {
Only, Except []string
Input string
Result bool
}{
{
[]string{"foo"},
nil,
"foo",
false,
},
{
nil,
[]string{"foo"},
"foo",
true,
},
{
nil,
nil,
"foo",
false,
},
}
for _, tc := range cases {
oe := &OnlyExcept{
Only: tc.Only,
Except: tc.Except,
}
actual := oe.Skip(tc.Input)
if actual != tc.Result {
t.Fatalf(
"bad: %#v\n\n%#v\n\n%#v\n\n%#v",
actual, tc.Only, tc.Except, tc.Input)
}
}
}