From 62309cb6deb4c3afa12a0479eb7d29a7670702b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 9 Jun 2013 22:00:47 -0700 Subject: [PATCH] Build/Builder take a Cache object now --- builder/amazonebs/builder.go | 2 +- builder/vmware/builder.go | 2 +- command/build/command.go | 2 +- packer/build.go | 6 +++--- packer/build_test.go | 5 +++-- packer/builder.go | 2 +- packer/builder_test.go | 4 +++- packer/cache_test.go | 14 ++++++++++++++ packer/plugin/builder.go | 4 ++-- packer/plugin/builder_test.go | 2 +- packer/rpc/build.go | 6 +++--- packer/rpc/build_test.go | 10 ++++++++-- packer/rpc/builder.go | 9 +++++---- packer/rpc/builder_test.go | 12 +++++++++--- 14 files changed, 55 insertions(+), 25 deletions(-) diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index b42fe487e..1a5c4bcd9 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -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 == "" { diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index 95028d744..b84779ce6 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -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()) diff --git a/command/build/command.go b/command/build/command.go index 844df8219..60cdbe290 100644 --- a/command/build/command.go +++ b/command/build/command.go @@ -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) } diff --git a/packer/build.go b/packer/build.go index 8b610296e..da09ccbc2 100644 --- a/packer/build.go +++ b/packer/build.go @@ -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. diff --git a/packer/build_test.go b/packer/build_test.go index ef48757c0..8852319c6 100644 --- a/packer/build_test.go +++ b/packer/build_test.go @@ -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) { diff --git a/packer/builder.go b/packer/builder.go index 341c77247..284277b5d 100644 --- a/packer/builder.go +++ b/packer/builder.go @@ -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. diff --git a/packer/builder_test.go b/packer/builder_test.go index e6fba7997..a7351e06a 100644 --- a/packer/builder_test.go +++ b/packer/builder_test.go @@ -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 } diff --git a/packer/cache_test.go b/packer/cache_test.go index d4c3a1c69..1be344ecc 100644 --- a/packer/cache_test.go +++ b/packer/cache_test.go @@ -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{} diff --git a/packer/plugin/builder.go b/packer/plugin/builder.go index 5113b459c..88255e36b 100644 --- a/packer/plugin/builder.go +++ b/packer/plugin/builder.go @@ -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() { diff --git a/packer/plugin/builder_test.go b/packer/plugin/builder_test.go index 43ebd6e56..b149c18c4 100644 --- a/packer/plugin/builder_test.go +++ b/packer/plugin/builder_test.go @@ -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 } diff --git a/packer/rpc/build.go b/packer/rpc/build.go index e8f0efed5..ebaa30628 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -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() diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index 387c187e4..2890433a9 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -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") diff --git a/packer/rpc/builder.go b/packer/rpc/builder.go index a9a685b07..6679a025c 100644 --- a/packer/rpc/builder.go +++ b/packer/rpc/builder.go @@ -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 { diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 678d70f75..2b9cc1eaf 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -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