Commit 62309cb6 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Build/Builder take a Cache object now

parent a23500e0
...@@ -87,7 +87,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -87,7 +87,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return 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 // Basic sanity checks. These are panics now because the Prepare
// method should verify these exist and such. // method should verify these exist and such.
if b.config.AccessKey == "" { if b.config.AccessKey == "" {
......
...@@ -172,7 +172,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -172,7 +172,7 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
return nil 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 // Seed the random number generator
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
......
...@@ -138,7 +138,7 @@ func (c Command) Run(env packer.Environment, args []string) int { ...@@ -138,7 +138,7 @@ func (c Command) Run(env packer.Environment, args []string) int {
log.Printf("Starting build run: %s", b.Name()) log.Printf("Starting build run: %s", b.Name())
ui := buildUis[b.Name()] ui := buildUis[b.Name()]
artifacts[b.Name()] = b.Run(ui) artifacts[b.Name()] = b.Run(ui, env.Cache())
ui.Say("Build finished.") ui.Say("Build finished.")
}(b) }(b)
} }
......
...@@ -7,7 +7,7 @@ import "log" ...@@ -7,7 +7,7 @@ import "log"
type Build interface { type Build interface {
Name() string Name() string
Prepare(Ui) error Prepare(Ui) error
Run(Ui) Artifact Run(Ui, Cache) Artifact
Cancel() Cancel()
} }
...@@ -60,7 +60,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) { ...@@ -60,7 +60,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) {
} }
// Runs the actual build. Prepare must be called prior to running this. // 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 { if !b.prepareCalled {
panic("Prepare must be called first") panic("Prepare must be called first")
} }
...@@ -87,7 +87,7 @@ func (b *coreBuild) Run(ui Ui) Artifact { ...@@ -87,7 +87,7 @@ func (b *coreBuild) Run(ui Ui) Artifact {
} }
hook := &DispatchHook{hooks} hook := &DispatchHook{hooks}
return b.builder.Run(ui, hook) return b.builder.Run(ui, hook, cache)
} }
// Cancels the build if it is running. // Cancels the build if it is running.
......
...@@ -53,11 +53,12 @@ func TestBuild_Prepare(t *testing.T) { ...@@ -53,11 +53,12 @@ func TestBuild_Prepare(t *testing.T) {
func TestBuild_Run(t *testing.T) { func TestBuild_Run(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
cache := &TestCache{}
ui := testUi() ui := testUi()
build := testBuild() build := testBuild()
build.Prepare(ui) build.Prepare(ui)
build.Run(ui) build.Run(ui, cache)
coreB := build.(*coreBuild) coreB := build.(*coreBuild)
...@@ -89,7 +90,7 @@ func TestBuild_RunBeforePrepare(t *testing.T) { ...@@ -89,7 +90,7 @@ func TestBuild_RunBeforePrepare(t *testing.T) {
assert.Equal(p.(string), "Prepare must be called first", "right panic") assert.Equal(p.(string), "Prepare must be called first", "right panic")
}() }()
testBuild().Run(testUi()) testBuild().Run(testUi(), &TestCache{})
} }
func TestBuild_Cancel(t *testing.T) { func TestBuild_Cancel(t *testing.T) {
......
...@@ -10,7 +10,7 @@ type Builder interface { ...@@ -10,7 +10,7 @@ type Builder interface {
Prepare(config interface{}) error Prepare(config interface{}) error
// Run is where the actual build should take place. It takes a Build and a Ui. // 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 // Cancel cancels a possibly running Builder. This should block until
// the builder actually cancels and cleans up after itself. // the builder actually cancels and cleans up after itself.
......
...@@ -4,6 +4,7 @@ type TestBuilder struct { ...@@ -4,6 +4,7 @@ type TestBuilder struct {
prepareCalled bool prepareCalled bool
prepareConfig interface{} prepareConfig interface{}
runCalled bool runCalled bool
runCache Cache
runHook Hook runHook Hook
runUi Ui runUi Ui
cancelCalled bool cancelCalled bool
...@@ -15,10 +16,11 @@ func (tb *TestBuilder) Prepare(config interface{}) error { ...@@ -15,10 +16,11 @@ func (tb *TestBuilder) Prepare(config interface{}) error {
return nil 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.runCalled = true
tb.runHook = h tb.runHook = h
tb.runUi = ui tb.runUi = ui
tb.runCache = c
return nil return nil
} }
......
...@@ -6,6 +6,20 @@ import ( ...@@ -6,6 +6,20 @@ import (
"testing" "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) { func TestFileCache_Implements(t *testing.T) {
var raw interface{} var raw interface{}
raw = &FileCache{} raw = &FileCache{}
......
...@@ -22,13 +22,13 @@ func (b *cmdBuilder) Prepare(config interface{}) error { ...@@ -22,13 +22,13 @@ func (b *cmdBuilder) Prepare(config interface{}) error {
return b.builder.Prepare(config) 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() { defer func() {
r := recover() r := recover()
b.checkExit(r, nil) b.checkExit(r, nil)
}() }()
return b.builder.Run(ui, hook) return b.builder.Run(ui, hook, cache)
} }
func (b *cmdBuilder) Cancel() { func (b *cmdBuilder) Cancel() {
......
...@@ -13,7 +13,7 @@ func (helperBuilder) Prepare(interface{}) error { ...@@ -13,7 +13,7 @@ func (helperBuilder) Prepare(interface{}) error {
return nil return nil
} }
func (helperBuilder) Run(packer.Ui, packer.Hook) packer.Artifact { func (helperBuilder) Run(packer.Ui, packer.Hook, packer.Cache) packer.Artifact {
return nil return nil
} }
......
...@@ -48,10 +48,10 @@ func (b *build) Prepare(ui packer.Ui) (err error) { ...@@ -48,10 +48,10 @@ func (b *build) Prepare(ui packer.Ui) (err error) {
return 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 // Create and start the server for the UI
// TODO: Error handling
server := rpc.NewServer() server := rpc.NewServer()
RegisterCache(server, cache)
RegisterUi(server, ui) RegisterUi(server, ui)
args := &BuildRunArgs{serveSingleConn(server)} args := &BuildRunArgs{serveSingleConn(server)}
...@@ -95,7 +95,7 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error { ...@@ -95,7 +95,7 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error {
return err return err
} }
artifact := b.build.Run(&Ui{client}) artifact := b.build.Run(&Ui{client}, Cache(client))
// Wrap the artifact // Wrap the artifact
server := rpc.NewServer() server := rpc.NewServer()
......
...@@ -14,6 +14,7 @@ type testBuild struct { ...@@ -14,6 +14,7 @@ type testBuild struct {
prepareCalled bool prepareCalled bool
prepareUi packer.Ui prepareUi packer.Ui
runCalled bool runCalled bool
runCache packer.Cache
runUi packer.Ui runUi packer.Ui
cancelCalled bool cancelCalled bool
} }
...@@ -29,8 +30,9 @@ func (b *testBuild) Prepare(ui packer.Ui) error { ...@@ -29,8 +30,9 @@ func (b *testBuild) Prepare(ui packer.Ui) error {
return nil 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.runCalled = true
b.runCache = cache
b.runUi = ui b.runUi = ui
return testBuildArtifact return testBuildArtifact
} }
...@@ -65,12 +67,16 @@ func TestBuildRPC(t *testing.T) { ...@@ -65,12 +67,16 @@ func TestBuildRPC(t *testing.T) {
assert.True(b.prepareCalled, "prepare should be called") assert.True(b.prepareCalled, "prepare should be called")
// Test Run // Test Run
cache := new(testCache)
ui = new(testUi) ui = new(testUi)
bClient.Run(ui) bClient.Run(ui, cache)
assert.True(b.runCalled, "run should be called") assert.True(b.runCalled, "run should be called")
// Test the UI given to run, which should be fully functional // Test the UI given to run, which should be fully functional
if b.runCalled { if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
b.runUi.Say("format") b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called") assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayMessage, "format", "message should be correct") assert.Equal(ui.sayMessage, "format", "message should be correct")
......
...@@ -46,12 +46,12 @@ func (b *builder) Prepare(config interface{}) (err error) { ...@@ -46,12 +46,12 @@ func (b *builder) Prepare(config interface{}) (err error) {
return 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 // Create and start the server for the Build and UI
// TODO: Error handling
server := rpc.NewServer() server := rpc.NewServer()
RegisterUi(server, ui) RegisterCache(server, cache)
RegisterHook(server, hook) RegisterHook(server, hook)
RegisterUi(server, ui)
// Create a server for the response // Create a server for the response
responseL := netListenerInRange(portRangeMin, portRangeMax) responseL := netListenerInRange(portRangeMin, portRangeMax)
...@@ -129,9 +129,10 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error { ...@@ -129,9 +129,10 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
go func() { go func() {
defer responseC.Close() defer responseC.Close()
cache := Cache(client)
hook := Hook(client) hook := Hook(client)
ui := &Ui{client} ui := &Ui{client}
artifact := b.builder.Run(ui, hook) artifact := b.builder.Run(ui, hook, cache)
responseAddress := "" responseAddress := ""
if artifact != nil { if artifact != nil {
......
...@@ -13,6 +13,7 @@ type testBuilder struct { ...@@ -13,6 +13,7 @@ type testBuilder struct {
prepareCalled bool prepareCalled bool
prepareConfig interface{} prepareConfig interface{}
runCalled bool runCalled bool
runCache packer.Cache
runHook packer.Hook runHook packer.Hook
runUi packer.Ui runUi packer.Ui
cancelCalled bool cancelCalled bool
...@@ -26,7 +27,8 @@ func (b *testBuilder) Prepare(config interface{}) error { ...@@ -26,7 +27,8 @@ func (b *testBuilder) Prepare(config interface{}) error {
return nil 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.runCalled = true
b.runHook = hook b.runHook = hook
b.runUi = ui b.runUi = ui
...@@ -65,12 +67,16 @@ func TestBuilderRPC(t *testing.T) { ...@@ -65,12 +67,16 @@ func TestBuilderRPC(t *testing.T) {
assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg") assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg")
// Test Run // Test Run
cache := new(testCache)
hook := &testHook{} hook := &testHook{}
ui := &testUi{} ui := &testUi{}
artifact := bClient.Run(ui, hook) artifact := bClient.Run(ui, hook, cache)
assert.True(b.runCalled, "runs hould be called") assert.True(b.runCalled, "runs hould be called")
if b.runCalled { if b.runCalled {
b.runCache.Lock("foo")
assert.True(cache.lockCalled, "lock should be called")
b.runHook.Run("foo", nil, nil, nil) b.runHook.Run("foo", nil, nil, nil)
assert.True(hook.runCalled, "run should be called") assert.True(hook.runCalled, "run should be called")
...@@ -83,7 +89,7 @@ func TestBuilderRPC(t *testing.T) { ...@@ -83,7 +89,7 @@ func TestBuilderRPC(t *testing.T) {
// Test run with nil result // Test run with nil result
b.nilRunResult = true b.nilRunResult = true
artifact = bClient.Run(ui, hook) artifact = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil") assert.Nil(artifact, "should be nil")
// Test Cancel // Test Cancel
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment