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{
|
|
|
|
coreBuildProvisioner{&TestProvisioner{}, 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)
|
|
|
|
|
|
|
|
build := testBuild()
|
2013-05-22 19:46:23 -04:00
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
coreB := build.(*coreBuild)
|
|
|
|
builder := coreB.builder.(*TestBuilder)
|
2013-04-20 21:55:02 -04:00
|
|
|
|
2013-05-22 19:46:23 -04:00
|
|
|
build.Prepare(ui)
|
2013-04-20 21:55:02 -04:00
|
|
|
assert.True(builder.prepareCalled, "prepare should be called")
|
|
|
|
assert.Equal(builder.prepareConfig, 42, "prepare config should be 42")
|
2013-05-22 19:46:23 -04:00
|
|
|
|
|
|
|
// Verify provisioners were prepared
|
|
|
|
coreProv := coreB.provisioners[0]
|
|
|
|
prov := coreProv.provisioner.(*TestProvisioner)
|
|
|
|
assert.True(prov.prepCalled, "prepare should be called")
|
|
|
|
assert.Equal(prov.prepConfig, 42, "prepare should be called with proper config")
|
|
|
|
assert.Equal(prov.prepUi, ui, "prepare should be called with proper ui")
|
2013-04-20 21:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuild_Run(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
ui := testUi()
|
|
|
|
|
|
|
|
build := testBuild()
|
2013-05-22 19:20:40 -04:00
|
|
|
build.Prepare(ui)
|
2013-04-20 21:55:02 -04:00
|
|
|
build.Run(ui)
|
|
|
|
|
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-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")
|
|
|
|
}()
|
|
|
|
|
|
|
|
testBuild().Run(testUi())
|
|
|
|
}
|