Build/Builder take a Cache object now

This commit is contained in:
Mitchell Hashimoto 2013-06-09 22:00:47 -07:00
parent a23500e0a4
commit 62309cb6de
14 changed files with 55 additions and 25 deletions

View File

@ -87,7 +87,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
// Basic sanity checks. These are panics now because the Prepare
// method should verify these exist and such.
if b.config.AccessKey == "" {

View File

@ -172,7 +172,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return nil
}
func (b *Builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
// Seed the random number generator
rand.Seed(time.Now().UTC().UnixNano())

View File

@ -138,7 +138,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
log.Printf("Starting build run: %s", b.Name())
ui := buildUis[b.Name()]
artifacts[b.Name()] = b.Run(ui)
artifacts[b.Name()] = b.Run(ui, env.Cache())
ui.Say("Build finished.")
}(b)
}

View File

@ -7,7 +7,7 @@ import "log"
type Build interface {
Name() string
Prepare(Ui) error
Run(Ui) Artifact
Run(Ui, Cache) Artifact
Cancel()
}
@ -60,7 +60,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) {
}
// Runs the actual build. Prepare must be called prior to running this.
func (b *coreBuild) Run(ui Ui) Artifact {
func (b *coreBuild) Run(ui Ui, cache Cache) Artifact {
if !b.prepareCalled {
panic("Prepare must be called first")
}
@ -87,7 +87,7 @@ func (b *coreBuild) Run(ui Ui) Artifact {
}
hook := &DispatchHook{hooks}
return b.builder.Run(ui, hook)
return b.builder.Run(ui, hook, cache)
}
// Cancels the build if it is running.

View File

@ -53,11 +53,12 @@ func TestBuild_Prepare(t *testing.T) {
func TestBuild_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
cache := &TestCache{}
ui := testUi()
build := testBuild()
build.Prepare(ui)
build.Run(ui)
build.Run(ui, cache)
coreB := build.(*coreBuild)
@ -89,7 +90,7 @@ func TestBuild_RunBeforePrepare(t *testing.T) {
assert.Equal(p.(string), "Prepare must be called first", "right panic")
}()
testBuild().Run(testUi())
testBuild().Run(testUi(), &TestCache{})
}
func TestBuild_Cancel(t *testing.T) {

View File

@ -10,7 +10,7 @@ type Builder interface {
Prepare(config interface{}) error
// Run is where the actual build should take place. It takes a Build and a Ui.
Run(ui Ui, hook Hook) Artifact
Run(ui Ui, hook Hook, cache Cache) Artifact
// Cancel cancels a possibly running Builder. This should block until
// the builder actually cancels and cleans up after itself.

View File

@ -4,6 +4,7 @@ type TestBuilder struct {
prepareCalled bool
prepareConfig interface{}
runCalled bool
runCache Cache
runHook Hook
runUi Ui
cancelCalled bool
@ -15,10 +16,11 @@ func (tb *TestBuilder) Prepare(config interface{}) error {
return nil
}
func (tb *TestBuilder) Run(ui Ui, h Hook) Artifact {
func (tb *TestBuilder) Run(ui Ui, h Hook, c Cache) Artifact {
tb.runCalled = true
tb.runHook = h
tb.runUi = ui
tb.runCache = c
return nil
}

View File

@ -6,6 +6,20 @@ import (
"testing"
)
type TestCache struct{}
func (TestCache) Lock(string) string {
return ""
}
func (TestCache) Unlock(string) {}
func (TestCache) RLock(string) (string, bool) {
return "", false
}
func (TestCache) RUnlock(string) {}
func TestFileCache_Implements(t *testing.T) {
var raw interface{}
raw = &FileCache{}

View File

@ -22,13 +22,13 @@ func (b *cmdBuilder) Prepare(config interface{}) error {
return b.builder.Prepare(config)
}
func (b *cmdBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
func (b *cmdBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
defer func() {
r := recover()
b.checkExit(r, nil)
}()
return b.builder.Run(ui, hook)
return b.builder.Run(ui, hook, cache)
}
func (b *cmdBuilder) Cancel() {

View File

@ -13,7 +13,7 @@ func (helperBuilder) Prepare(interface{}) error {
return nil
}
func (helperBuilder) Run(packer.Ui, packer.Hook) packer.Artifact {
func (helperBuilder) Run(packer.Ui, packer.Hook, packer.Cache) packer.Artifact {
return nil
}

View File

@ -48,10 +48,10 @@ func (b *build) Prepare(ui packer.Ui) (err error) {
return
}
func (b *build) Run(ui packer.Ui) packer.Artifact {
func (b *build) Run(ui packer.Ui, cache packer.Cache) packer.Artifact {
// Create and start the server for the UI
// TODO: Error handling
server := rpc.NewServer()
RegisterCache(server, cache)
RegisterUi(server, ui)
args := &BuildRunArgs{serveSingleConn(server)}
@ -95,7 +95,7 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error {
return err
}
artifact := b.build.Run(&Ui{client})
artifact := b.build.Run(&Ui{client}, Cache(client))
// Wrap the artifact
server := rpc.NewServer()

View File

@ -14,6 +14,7 @@ type testBuild struct {
prepareCalled bool
prepareUi packer.Ui
runCalled bool
runCache packer.Cache
runUi packer.Ui
cancelCalled bool
}
@ -29,8 +30,9 @@ func (b *testBuild) Prepare(ui packer.Ui) error {
return nil
}
func (b *testBuild) Run(ui packer.Ui) packer.Artifact {
func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) packer.Artifact {
b.runCalled = true
b.runCache = cache
b.runUi = ui
return testBuildArtifact
}
@ -65,12 +67,16 @@ func TestBuildRPC(t *testing.T) {
assert.True(b.prepareCalled, "prepare should be called")
// Test Run
cache := new(testCache)
ui = new(testUi)
bClient.Run(ui)
bClient.Run(ui, cache)
assert.True(b.runCalled, "run should be called")
// Test the UI given to run, which should be fully functional
if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayMessage, "format", "message should be correct")

View File

@ -46,12 +46,12 @@ func (b *builder) Prepare(config interface{}) (err error) {
return
}
func (b *builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
// Create and start the server for the Build and UI
// TODO: Error handling
server := rpc.NewServer()
RegisterUi(server, ui)
RegisterCache(server, cache)
RegisterHook(server, hook)
RegisterUi(server, ui)
// Create a server for the response
responseL := netListenerInRange(portRangeMin, portRangeMax)
@ -129,9 +129,10 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
go func() {
defer responseC.Close()
cache := Cache(client)
hook := Hook(client)
ui := &Ui{client}
artifact := b.builder.Run(ui, hook)
artifact := b.builder.Run(ui, hook, cache)
responseAddress := ""
if artifact != nil {

View File

@ -13,6 +13,7 @@ type testBuilder struct {
prepareCalled bool
prepareConfig interface{}
runCalled bool
runCache packer.Cache
runHook packer.Hook
runUi packer.Ui
cancelCalled bool
@ -26,7 +27,8 @@ func (b *testBuilder) Prepare(config interface{}) error {
return nil
}
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact {
b.runCache = cache
b.runCalled = true
b.runHook = hook
b.runUi = ui
@ -65,12 +67,16 @@ func TestBuilderRPC(t *testing.T) {
assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg")
// Test Run
cache := new(testCache)
hook := &testHook{}
ui := &testUi{}
artifact := bClient.Run(ui, hook)
artifact := bClient.Run(ui, hook, cache)
assert.True(b.runCalled, "runs hould be called")
if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
b.runHook.Run("foo", nil, nil, nil)
assert.True(hook.runCalled, "run should be called")
@ -83,7 +89,7 @@ func TestBuilderRPC(t *testing.T) {
// Test run with nil result
b.nilRunResult = true
artifact = bClient.Run(ui, hook)
artifact = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
// Test Cancel