2013-04-20 21:55:02 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-05-03 23:45:38 -04:00
|
|
|
func testBuild() Build {
|
|
|
|
return &coreBuild{
|
2013-05-22 19:15:57 -04:00
|
|
|
name: "test",
|
|
|
|
builder: &TestBuilder{},
|
|
|
|
builderConfig: 42,
|
2013-05-22 19:46:23 -04:00
|
|
|
hooks: map[string][]Hook{
|
|
|
|
"foo": []Hook{&TestHook{}},
|
|
|
|
},
|
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{
|
|
|
|
coreBuildPostProcessor{&TestPostProcessor{}, 42},
|
|
|
|
},
|
|
|
|
},
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-21 15:36:55 -04:00
|
|
|
func testBuilder() *TestBuilder {
|
|
|
|
return &TestBuilder{}
|
|
|
|
}
|
|
|
|
|
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-06-14 15:38:54 -04:00
|
|
|
debugFalseConfig := map[string]interface{}{DebugConfigKey: false}
|
|
|
|
|
2013-04-20 21:55:02 -04:00
|
|
|
build := testBuild()
|
2013-06-14 15:38:54 -04:00
|
|
|
coreB := build.(*coreBuild)
|
|
|
|
builder := coreB.builder.(*TestBuilder)
|
|
|
|
|
|
|
|
build.Prepare()
|
|
|
|
assert.True(builder.prepareCalled, "prepare should be called")
|
|
|
|
assert.Equal(builder.prepareConfig, []interface{}{42, debugFalseConfig}, "prepare config should be 42")
|
2013-05-22 19:46:23 -04:00
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
coreProv := coreB.provisioners[0]
|
|
|
|
prov := coreProv.provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.prepCalled, "prepare should be called")
|
|
|
|
assert.Equal(prov.prepConfigs, []interface{}{42, debugFalseConfig}, "prepare should be called with proper config")
|
2013-06-18 13:31:52 -04:00
|
|
|
|
|
|
|
corePP := coreB.postProcessors[0][0]
|
|
|
|
pp := corePP.processor.(*TestPostProcessor)
|
|
|
|
assert.True(pp.configCalled, "config should be called")
|
|
|
|
assert.Equal(pp.configVal, 42, "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()
|
|
|
|
if err := build.Prepare(); err != nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
build.Prepare()
|
|
|
|
}
|
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
func TestBuild_Prepare_Debug(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
debugConfig := map[string]interface{}{DebugConfigKey: true}
|
|
|
|
|
|
|
|
build := testBuild()
|
2013-05-22 19:46:23 -04:00
|
|
|
coreB := build.(*coreBuild)
|
|
|
|
builder := coreB.builder.(*TestBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-06-14 15:38:54 -04:00
|
|
|
build.SetDebug(true)
|
2013-06-13 13:08:31 -04:00
|
|
|
build.Prepare()
|
2013-04-20 21:55:02 -04:00
|
|
|
assert.True(builder.prepareCalled, "prepare should be called")
|
2013-06-14 15:38:54 -04:00
|
|
|
assert.Equal(builder.prepareConfig, []interface{}{42, debugConfig}, "prepare config should be 42")
|
2013-05-22 19:46:23 -04:00
|
|
|
|
|
|
|
coreProv := coreB.provisioners[0]
|
|
|
|
prov := coreProv.provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.prepCalled, "prepare should be called")
|
2013-06-14 15:38:54 -04:00
|
|
|
assert.Equal(prov.prepConfigs, []interface{}{42, debugConfig}, "prepare should be called with proper config")
|
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-06-13 13:08:31 -04:00
|
|
|
build.Prepare()
|
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:29:07 -04:00
|
|
|
coreB := build.(*coreBuild)
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
// Verify builder was run
|
2013-05-22 19:29:07 -04:00
|
|
|
builder := coreB.builder.(*TestBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
assert.True(builder.runCalled, "run should be called")
|
|
|
|
assert.Equal(builder.runUi, ui, "run should be called with ui")
|
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)
|
|
|
|
|
|
|
|
hook := coreB.hooks["foo"][0].(*TestHook)
|
|
|
|
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)
|
|
|
|
prov := coreB.provisioners[0].provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.provCalled, "provision should be called")
|
2013-06-18 13:54:22 -04:00
|
|
|
|
|
|
|
// Verify post-processor was run
|
|
|
|
pp := coreB.postProcessors[0][0].processor.(*TestPostProcessor)
|
|
|
|
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
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
coreB := build.(*coreBuild)
|
|
|
|
|
|
|
|
builder := coreB.builder.(*TestBuilder)
|
|
|
|
assert.True(builder.cancelCalled, "cancel should be called")
|
|
|
|
}
|