2013-08-09 18:10:24 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2013-08-22 14:32:59 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-08-09 18:10:24 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-08-22 14:32:59 -04:00
|
|
|
func testTemplate() (*packer.Template, *packer.ComponentFinder) {
|
|
|
|
tplData := `{
|
|
|
|
"builders": [
|
|
|
|
{
|
|
|
|
"type": "foo"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "bar"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
tpl, err := packer.ParseTemplate([]byte(tplData))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cf := &packer.ComponentFinder{
|
|
|
|
Builder: func(string) (packer.Builder, error) { return new(packer.MockBuilder), nil },
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpl, cf
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildOptionsBuilds(t *testing.T) {
|
|
|
|
opts := new(BuildOptions)
|
|
|
|
bs, err := opts.Builds(testTemplate())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bs) != 2 {
|
|
|
|
t.Fatalf("bad: %d", len(bs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 14:34:51 -04:00
|
|
|
func TestBuildOptionsBuilds_except(t *testing.T) {
|
|
|
|
opts := new(BuildOptions)
|
|
|
|
opts.Except = []string{"foo"}
|
|
|
|
|
|
|
|
bs, err := opts.Builds(testTemplate())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bs) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(bs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if bs[0].Name() != "bar" {
|
|
|
|
t.Fatalf("bad: %s", bs[0].Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildOptionsBuilds_only(t *testing.T) {
|
|
|
|
opts := new(BuildOptions)
|
|
|
|
opts.Only = []string{"foo"}
|
|
|
|
|
|
|
|
bs, err := opts.Builds(testTemplate())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bs) != 1 {
|
|
|
|
t.Fatalf("bad: %d", len(bs))
|
|
|
|
}
|
|
|
|
|
|
|
|
if bs[0].Name() != "foo" {
|
|
|
|
t.Fatalf("bad: %s", bs[0].Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-22 14:40:30 -04:00
|
|
|
func TestBuildOptionsBuilds_exceptNonExistent(t *testing.T) {
|
|
|
|
opts := new(BuildOptions)
|
|
|
|
opts.Except = []string{"i-dont-exist"}
|
|
|
|
|
|
|
|
_, err := opts.Builds(testTemplate())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildOptionsBuilds_onlyNonExistent(t *testing.T) {
|
|
|
|
opts := new(BuildOptions)
|
|
|
|
opts.Only = []string{"i-dont-exist"}
|
|
|
|
|
|
|
|
_, err := opts.Builds(testTemplate())
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("err should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 18:21:24 -04:00
|
|
|
func TestBuildOptionsValidate(t *testing.T) {
|
|
|
|
bf := new(BuildOptions)
|
2013-08-09 18:10:24 -04:00
|
|
|
|
|
|
|
err := bf.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Both set
|
|
|
|
bf.Except = make([]string, 1)
|
|
|
|
bf.Only = make([]string, 1)
|
|
|
|
err = bf.Validate()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// One set
|
|
|
|
bf.Except = make([]string, 1)
|
|
|
|
bf.Only = make([]string, 0)
|
|
|
|
err = bf.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bf.Except = make([]string, 0)
|
|
|
|
bf.Only = make([]string, 1)
|
|
|
|
err = bf.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-08-09 19:45:29 -04:00
|
|
|
|
|
|
|
func TestBuildOptionsValidate_userVarFiles(t *testing.T) {
|
|
|
|
bf := new(BuildOptions)
|
|
|
|
|
|
|
|
err := bf.Validate()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-existent file
|
|
|
|
bf.UserVarFiles = []string{"ireallyshouldntexistanywhere"}
|
|
|
|
err = bf.Validate()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|