packer-cn/packer/build_test.go

85 lines
1.7 KiB
Go
Raw Normal View History

2013-04-20 21:55:02 -04:00
package packer
import (
"cgl.tideland.biz/asserts"
"testing"
)
type TestBuilder struct {
prepareCalled bool
prepareConfig interface{}
2013-05-10 20:01:24 -04:00
runCalled bool
runBuild Build
runUi Ui
2013-04-20 21:55:02 -04:00
}
func (tb *TestBuilder) Prepare(config interface{}) error {
2013-04-20 21:55:02 -04:00
tb.prepareCalled = true
tb.prepareConfig = config
return nil
2013-04-20 21:55:02 -04:00
}
2013-05-03 23:45:38 -04:00
func (tb *TestBuilder) Run(b Build, ui Ui) {
2013-04-20 21:55:02 -04:00
tb.runCalled = true
tb.runBuild = b
tb.runUi = ui
}
2013-05-03 23:45:38 -04:00
func testBuild() Build {
return &coreBuild{
2013-05-10 20:01:24 -04:00
name: "test",
builder: &TestBuilder{},
2013-04-21 15:36:55 -04:00
rawConfig: 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-03 23:45:38 -04:00
builder := build.(*coreBuild).builder.(*TestBuilder)
2013-04-20 21:55:02 -04:00
2013-04-21 15:36:55 -04:00
build.Prepare()
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")
}
func TestBuild_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
ui := testUi()
build := testBuild()
2013-04-21 15:36:55 -04:00
build.Prepare()
2013-04-20 21:55:02 -04:00
build.Run(ui)
2013-05-03 23:45:38 -04:00
builder := build.(*coreBuild).builder.(*TestBuilder)
2013-04-20 21:55:02 -04:00
assert.True(builder.runCalled, "run should be called")
assert.Equal(builder.runBuild, build, "run should be called with build")
assert.Equal(builder.runUi, ui, "run should be called with ui")
}
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())
}