packer-cn/packer/core_test.go

102 lines
1.6 KiB
Go
Raw Normal View History

2015-05-23 17:48:07 -04:00
package packer
import (
"os"
2015-05-23 19:12:32 -04:00
"reflect"
2015-05-23 17:48:07 -04:00
"testing"
"github.com/mitchellh/packer/template"
)
2015-05-23 19:12:32 -04:00
func TestCoreBuildNames(t *testing.T) {
cases := []struct {
File string
Vars map[string]string
Result []string
}{
{
"build-names-basic.json",
nil,
[]string{"something"},
},
{
"build-names-func.json",
nil,
[]string{"TUBES"},
},
}
for _, tc := range cases {
tpl, err := template.ParseFile(fixtureDir(tc.File))
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
core, err := NewCore(&CoreConfig{
Template: tpl,
Variables: tc.Vars,
})
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
names := core.BuildNames()
if !reflect.DeepEqual(names, tc.Result) {
t.Fatalf("err: %s\n\n%#v", tc.File, names)
}
}
}
2015-05-23 17:48:07 -04:00
func TestCoreValidate(t *testing.T) {
cases := []struct {
File string
Vars map[string]string
Err bool
}{
{
"validate-dup-builder.json",
nil,
true,
},
// Required variable not set
{
"validate-req-variable.json",
nil,
true,
},
{
"validate-req-variable.json",
map[string]string{"foo": "bar"},
false,
},
}
for _, tc := range cases {
f, err := os.Open(fixtureDir(tc.File))
if err != nil {
t.Fatalf("err: %s", err)
}
tpl, err := template.Parse(f)
f.Close()
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
core, err := NewCore(&CoreConfig{
Template: tpl,
Variables: tc.Vars,
})
if err != nil {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
if err := core.Validate(); (err != nil) != tc.Err {
t.Fatalf("err: %s\n\n%s", tc.File, err)
}
}
}