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"
|
|
|
|
|
2015-05-28 17:40:45 -04:00
|
|
|
configHelper "github.com/mitchellh/packer/helper/config"
|
2015-05-23 17:48:07 -04:00
|
|
|
"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-25 20:58:59 -04:00
|
|
|
func TestCoreBuild_basic(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-basic.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:15:07 -04:00
|
|
|
func TestCoreBuild_basicInterpolated(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-basic-interpolated.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("NAME")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:40:45 -04:00
|
|
|
func TestCoreBuild_env(t *testing.T) {
|
|
|
|
os.Setenv("PACKER_TEST_ENV", "test")
|
|
|
|
defer os.Setenv("PACKER_TEST_ENV", "")
|
|
|
|
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-env.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the config
|
|
|
|
var result map[string]interface{}
|
|
|
|
err = configHelper.Decode(&result, nil, b.PrepareConfig...)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result["value"] != "test" {
|
|
|
|
t.Fatalf("bad: %#v", result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 21:15:07 -04:00
|
|
|
func TestCoreBuild_nonExist(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-basic.json"))
|
|
|
|
TestBuilder(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
_, err := core.Build("nope")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:14:29 -04:00
|
|
|
func TestCoreBuild_prov(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-prov.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
p := TestProvisioner(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
if !p.ProvCalled {
|
|
|
|
t.Fatal("provisioner not called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:16:39 -04:00
|
|
|
func TestCoreBuild_provSkip(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-prov-skip.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
p := TestProvisioner(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
if p.ProvCalled {
|
|
|
|
t.Fatal("provisioner should not be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoreBuild_provSkipInclude(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-prov-skip-include.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
p := TestProvisioner(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
if !p.ProvCalled {
|
|
|
|
t.Fatal("provisioner should be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:46:04 -04:00
|
|
|
func TestCoreBuild_provOverride(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-prov-override.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
p := TestProvisioner(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
if !p.ProvCalled {
|
|
|
|
t.Fatal("provisioner not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, raw := range p.PrepConfigs {
|
|
|
|
if m, ok := raw.(map[string]interface{}); ok {
|
|
|
|
if _, ok := m["foo"]; ok {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatal("override not called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 12:28:59 -04:00
|
|
|
func TestCoreBuild_postProcess(t *testing.T) {
|
|
|
|
config := TestCoreConfig(t)
|
|
|
|
testCoreTemplate(t, config, fixtureDir("build-pp.json"))
|
|
|
|
b := TestBuilder(t, config, "test")
|
|
|
|
p := TestPostProcessor(t, config, "test")
|
|
|
|
core := TestCore(t, config)
|
|
|
|
ui := TestUi(t)
|
|
|
|
|
|
|
|
b.ArtifactId = "hello"
|
|
|
|
p.ArtifactId = "goodbye"
|
|
|
|
|
|
|
|
build, err := core.Build("test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := build.Prepare(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact, err := build.Run(ui, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if len(artifact) != 1 {
|
|
|
|
t.Fatalf("bad: %#v", artifact)
|
|
|
|
}
|
|
|
|
|
|
|
|
if artifact[0].Id() != p.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", artifact[0].Id())
|
|
|
|
}
|
|
|
|
if p.PostProcessArtifact.Id() != b.ArtifactId {
|
|
|
|
t.Fatalf("bad: %s", p.PostProcessArtifact.Id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:42:53 -04:00
|
|
|
_, err = NewCore(&CoreConfig{
|
2015-05-23 17:48:07 -04:00
|
|
|
Template: tpl,
|
|
|
|
Variables: tc.Vars,
|
|
|
|
})
|
2015-05-28 17:42:53 -04:00
|
|
|
if (err != nil) != tc.Err {
|
2015-05-23 17:48:07 -04:00
|
|
|
t.Fatalf("err: %s\n\n%s", tc.File, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 20:29:10 -04:00
|
|
|
|
|
|
|
func testComponentFinder() *ComponentFinder {
|
|
|
|
builderFactory := func(n string) (Builder, error) { return new(MockBuilder), nil }
|
2015-05-26 12:28:59 -04:00
|
|
|
ppFactory := func(n string) (PostProcessor, error) { return new(MockPostProcessor), nil }
|
2015-05-25 20:29:10 -04:00
|
|
|
provFactory := func(n string) (Provisioner, error) { return new(MockProvisioner), nil }
|
|
|
|
return &ComponentFinder{
|
|
|
|
Builder: builderFactory,
|
|
|
|
PostProcessor: ppFactory,
|
|
|
|
Provisioner: provFactory,
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 20:58:59 -04:00
|
|
|
|
|
|
|
func testCoreTemplate(t *testing.T, c *CoreConfig, p string) {
|
|
|
|
tpl, err := template.ParseFile(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s\n\n%s", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Template = tpl
|
|
|
|
}
|