2013-04-20 21:55:02 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
2013-06-19 01:45:53 -04:00
|
|
|
"reflect"
|
2013-04-20 21:55:02 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
func testBuild() *coreBuild {
|
2013-05-03 23:45:38 -04:00
|
|
|
return &coreBuild{
|
2013-05-22 19:15:57 -04:00
|
|
|
name: "test",
|
2013-06-19 01:45:53 -04:00
|
|
|
builder: &TestBuilder{artifactId: "b"},
|
2013-05-22 19:15:57 -04:00
|
|
|
builderConfig: 42,
|
2013-07-14 20:58:32 -04:00
|
|
|
builderType: "foo",
|
2013-05-22 19:46:23 -04:00
|
|
|
hooks: map[string][]Hook{
|
2013-08-30 20:03:55 -04:00
|
|
|
"foo": []Hook{&MockHook{}},
|
2013-05-22 19:46:23 -04:00
|
|
|
},
|
2013-05-22 19:29:07 -04:00
|
|
|
provisioners: []coreBuildProvisioner{
|
2013-06-07 13:35:26 -04:00
|
|
|
coreBuildProvisioner{&TestProvisioner{}, []interface{}{42}},
|
2013-05-22 19:29:07 -04:00
|
|
|
},
|
2013-06-18 13:31:52 -04:00
|
|
|
postProcessors: [][]coreBuildPostProcessor{
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp"}, "testPP", make(map[string]interface{}), true},
|
2013-06-18 13:31:52 -04:00
|
|
|
},
|
|
|
|
},
|
2013-08-09 17:07:49 -04:00
|
|
|
variables: make(map[string]string),
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 15:36:55 -04:00
|
|
|
func testBuilder() *TestBuilder {
|
|
|
|
return &TestBuilder{}
|
|
|
|
}
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
func testDefaultPackerConfig() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
BuildNameConfigKey: "test",
|
|
|
|
BuilderTypeConfigKey: "foo",
|
|
|
|
DebugConfigKey: false,
|
|
|
|
ForceConfigKey: false,
|
|
|
|
UserVariablesConfigKey: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 14:32:03 -04:00
|
|
|
func TestBuild_Name(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
build := testBuild()
|
|
|
|
assert.Equal(build.Name(), "test", "should have a name")
|
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
func TestBuild_Prepare(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
2013-08-09 17:07:49 -04:00
|
|
|
packerConfig := testDefaultPackerConfig()
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
build := testBuild()
|
2013-06-19 01:45:53 -04:00
|
|
|
builder := build.builder.(*TestBuilder)
|
2013-06-14 15:38:54 -04:00
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-14 15:38:54 -04:00
|
|
|
assert.True(builder.prepareCalled, "prepare should be called")
|
2013-07-01 14:06:28 -04:00
|
|
|
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42")
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
coreProv := build.provisioners[0]
|
2013-06-14 15:38:54 -04:00
|
|
|
prov := coreProv.provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.prepCalled, "prepare should be called")
|
2013-07-01 14:06:28 -04:00
|
|
|
assert.Equal(prov.prepConfigs, []interface{}{42, packerConfig}, "prepare should be called with proper config")
|
2013-06-18 13:31:52 -04:00
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
corePP := build.postProcessors[0][0]
|
2013-06-18 13:31:52 -04:00
|
|
|
pp := corePP.processor.(*TestPostProcessor)
|
|
|
|
assert.True(pp.configCalled, "config should be called")
|
2013-08-19 19:00:13 -04:00
|
|
|
assert.Equal(pp.configVal, []interface{}{make(map[string]interface{}), packerConfig}, "config should have right value")
|
2013-06-14 15:38:54 -04:00
|
|
|
}
|
|
|
|
|
2013-06-18 12:37:14 -04:00
|
|
|
func TestBuild_Prepare_Twice(t *testing.T) {
|
|
|
|
build := testBuild()
|
2013-08-09 16:40:00 -04:00
|
|
|
if err := build.Prepare(nil); err != nil {
|
2013-06-18 12:37:14 -04:00
|
|
|
t.Fatalf("bad error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
|
|
|
if p == nil {
|
|
|
|
t.Fatalf("should've paniced")
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.(string) != "prepare already called" {
|
|
|
|
t.Fatalf("Invalid panic: %s", p)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-18 12:37:14 -04:00
|
|
|
}
|
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
func TestBuild_Prepare_Debug(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
packerConfig := testDefaultPackerConfig()
|
|
|
|
packerConfig[DebugConfigKey] = true
|
2013-06-14 15:38:54 -04:00
|
|
|
|
|
|
|
build := testBuild()
|
2013-06-19 01:45:53 -04:00
|
|
|
builder := build.builder.(*TestBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
build.SetDebug(true)
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-04-20 21:55:02 -04:00
|
|
|
assert.True(builder.prepareCalled, "prepare should be called")
|
2013-07-01 14:06:28 -04:00
|
|
|
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42")
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
coreProv := build.provisioners[0]
|
2013-05-22 19:46:23 -04:00
|
|
|
prov := coreProv.provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.prepCalled, "prepare should be called")
|
2013-07-01 14:06:28 -04:00
|
|
|
assert.Equal(prov.prepConfigs, []interface{}{42, packerConfig}, "prepare should be called with proper config")
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
|
2013-08-09 17:07:49 -04:00
|
|
|
func TestBuildPrepare_variables_default(t *testing.T) {
|
|
|
|
packerConfig := testDefaultPackerConfig()
|
|
|
|
packerConfig[UserVariablesConfigKey] = map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}
|
|
|
|
|
|
|
|
build := testBuild()
|
|
|
|
build.variables["foo"] = "bar"
|
|
|
|
builder := build.builder.(*TestBuilder)
|
|
|
|
|
|
|
|
err := build.Prepare(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !builder.prepareCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(builder.prepareConfig[1], packerConfig) {
|
|
|
|
t.Fatalf("prepare bad: %#v", builder.prepareConfig[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPrepare_variables_nonexist(t *testing.T) {
|
|
|
|
build := testBuild()
|
|
|
|
build.variables["foo"] = "bar"
|
|
|
|
|
|
|
|
err := build.Prepare(map[string]string{"bar": "baz"})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have had error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPrepare_variables_override(t *testing.T) {
|
|
|
|
packerConfig := testDefaultPackerConfig()
|
|
|
|
packerConfig[UserVariablesConfigKey] = map[string]string{
|
|
|
|
"foo": "baz",
|
|
|
|
}
|
|
|
|
|
|
|
|
build := testBuild()
|
|
|
|
build.variables["foo"] = "bar"
|
|
|
|
builder := build.builder.(*TestBuilder)
|
|
|
|
|
|
|
|
err := build.Prepare(map[string]string{"foo": "baz"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !builder.prepareCalled {
|
|
|
|
t.Fatal("prepare should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(builder.prepareConfig[1], packerConfig) {
|
|
|
|
t.Fatalf("prepare bad: %#v", builder.prepareConfig[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
func TestBuild_Run(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
2013-06-10 01:00:47 -04:00
|
|
|
cache := &TestCache{}
|
2013-04-20 21:55:02 -04:00
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
build := testBuild()
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-18 13:54:22 -04:00
|
|
|
artifacts, err := build.Run(ui, cache)
|
|
|
|
assert.Nil(err, "should not error")
|
|
|
|
assert.Equal(len(artifacts), 2, "should have two artifacts")
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// Verify builder was run
|
2013-06-19 01:45:53 -04:00
|
|
|
builder := build.builder.(*TestBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
assert.True(builder.runCalled, "run should be called")
|
2013-05-22 19:29:07 -04:00
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// Verify hooks are disapatchable
|
|
|
|
dispatchHook := builder.runHook
|
|
|
|
dispatchHook.Run("foo", nil, nil, 42)
|
|
|
|
|
2013-08-30 20:03:55 -04:00
|
|
|
hook := build.hooks["foo"][0].(*MockHook)
|
|
|
|
assert.True(hook.RunCalled, "run should be called")
|
|
|
|
assert.Equal(hook.RunData, 42, "should have correct data")
|
2013-05-24 00:26:24 -04:00
|
|
|
|
|
|
|
// Verify provisioners run
|
|
|
|
dispatchHook.Run(HookProvision, nil, nil, 42)
|
2013-06-19 01:45:53 -04:00
|
|
|
prov := build.provisioners[0].provisioner.(*TestProvisioner)
|
2013-05-24 00:26:24 -04:00
|
|
|
assert.True(prov.provCalled, "provision should be called")
|
2013-06-18 13:54:22 -04:00
|
|
|
|
|
|
|
// Verify post-processor was run
|
2013-06-19 01:45:53 -04:00
|
|
|
pp := build.postProcessors[0][0].processor.(*TestPostProcessor)
|
2013-06-18 13:54:22 -04:00
|
|
|
assert.True(pp.ppCalled, "post processor should be called")
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
2013-04-20 22:03:53 -04:00
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
func TestBuild_Run_Artifacts(t *testing.T) {
|
|
|
|
cache := &TestCache{}
|
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
// Test case: Test that with no post-processors, we only get the
|
|
|
|
// main build.
|
|
|
|
build := testBuild()
|
|
|
|
build.postProcessors = [][]coreBuildPostProcessor{}
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-19 01:45:53 -04:00
|
|
|
artifacts, err := build.Run(ui, cache)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds := []string{"b"}
|
|
|
|
artifactIds := make([]string, len(artifacts))
|
|
|
|
for i, artifact := range artifacts {
|
|
|
|
artifactIds[i] = artifact.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(artifactIds, expectedIds) {
|
|
|
|
t.Fatalf("unexpected ids: %#v", artifactIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case: Test that with a single post-processor that doesn't keep
|
|
|
|
// inputs, only that post-processors results are returned.
|
|
|
|
build = testBuild()
|
|
|
|
build.postProcessors = [][]coreBuildPostProcessor{
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp"}, "pp", make(map[string]interface{}), false},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-19 01:45:53 -04:00
|
|
|
artifacts, err = build.Run(ui, cache)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"pp"}
|
|
|
|
artifactIds = make([]string, len(artifacts))
|
|
|
|
for i, artifact := range artifacts {
|
|
|
|
artifactIds[i] = artifact.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(artifactIds, expectedIds) {
|
|
|
|
t.Fatalf("unexpected ids: %#v", artifactIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case: Test that with multiple post-processors, as long as one
|
|
|
|
// keeps the original, the original is kept.
|
|
|
|
build = testBuild()
|
|
|
|
build.postProcessors = [][]coreBuildPostProcessor{
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1"}, "pp", make(map[string]interface{}), false},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2"}, "pp", make(map[string]interface{}), true},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-19 01:45:53 -04:00
|
|
|
artifacts, err = build.Run(ui, cache)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"b", "pp1", "pp2"}
|
|
|
|
artifactIds = make([]string, len(artifacts))
|
|
|
|
for i, artifact := range artifacts {
|
|
|
|
artifactIds[i] = artifact.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(artifactIds, expectedIds) {
|
|
|
|
t.Fatalf("unexpected ids: %#v", artifactIds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case: Test that with sequences, intermediaries are kept if they
|
|
|
|
// want to be.
|
|
|
|
build = testBuild()
|
|
|
|
build.postProcessors = [][]coreBuildPostProcessor{
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1a"}, "pp", make(map[string]interface{}), false},
|
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp1b"}, "pp", make(map[string]interface{}), true},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
[]coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2a"}, "pp", make(map[string]interface{}), false},
|
|
|
|
coreBuildPostProcessor{&TestPostProcessor{artifactId: "pp2b"}, "pp", make(map[string]interface{}), false},
|
2013-06-19 01:45:53 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-06-19 01:45:53 -04:00
|
|
|
artifacts, err = build.Run(ui, cache)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"pp1a", "pp1b", "pp2b"}
|
|
|
|
artifactIds = make([]string, len(artifacts))
|
|
|
|
for i, artifact := range artifacts {
|
|
|
|
artifactIds[i] = artifact.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(artifactIds, expectedIds) {
|
|
|
|
t.Fatalf("unexpected ids: %#v", artifactIds)
|
|
|
|
}
|
2013-07-01 14:30:39 -04:00
|
|
|
|
|
|
|
// Test case: Test that with a single post-processor that forcibly
|
|
|
|
// keeps inputs, that the artifacts are kept.
|
|
|
|
build = testBuild()
|
|
|
|
build.postProcessors = [][]coreBuildPostProcessor{
|
|
|
|
[]coreBuildPostProcessor{
|
|
|
|
coreBuildPostProcessor{
|
2013-08-19 19:00:13 -04:00
|
|
|
&TestPostProcessor{artifactId: "pp", keep: true}, "pp", make(map[string]interface{}), false,
|
2013-07-01 14:30:39 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:40:00 -04:00
|
|
|
build.Prepare(nil)
|
2013-07-01 14:30:39 -04:00
|
|
|
artifacts, err = build.Run(ui, cache)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIds = []string{"b", "pp"}
|
|
|
|
artifactIds = make([]string, len(artifacts))
|
|
|
|
for i, artifact := range artifacts {
|
|
|
|
artifactIds[i] = artifact.Id()
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(artifactIds, expectedIds) {
|
|
|
|
t.Fatalf("unexpected ids: %#v", artifactIds)
|
|
|
|
}
|
2013-06-19 01:45:53 -04:00
|
|
|
}
|
|
|
|
|
2013-04-20 22:03:53 -04:00
|
|
|
func TestBuild_RunBeforePrepare(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
p := recover()
|
|
|
|
assert.NotNil(p, "should panic")
|
|
|
|
assert.Equal(p.(string), "Prepare must be called first", "right panic")
|
|
|
|
}()
|
|
|
|
|
2013-06-10 01:00:47 -04:00
|
|
|
testBuild().Run(testUi(), &TestCache{})
|
2013-04-20 22:03:53 -04:00
|
|
|
}
|
2013-06-03 19:06:04 -04:00
|
|
|
|
|
|
|
func TestBuild_Cancel(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
build := testBuild()
|
|
|
|
build.Cancel()
|
|
|
|
|
2013-06-19 01:45:53 -04:00
|
|
|
builder := build.builder.(*TestBuilder)
|
2013-06-03 19:06:04 -04:00
|
|
|
assert.True(builder.cancelCalled, "cancel should be called")
|
|
|
|
}
|