packer, packer/rpc: Update Build interface to allow variable overrides

This commit is contained in:
Mitchell Hashimoto 2013-08-09 13:40:00 -07:00
parent d87f878a44
commit 7408558a55
4 changed files with 29 additions and 19 deletions

View File

@ -35,7 +35,7 @@ type Build interface {
// Prepare configures the various components of this build and reports // Prepare configures the various components of this build and reports
// any errors in doing so (such as syntax errors, validation errors, etc.) // any errors in doing so (such as syntax errors, validation errors, etc.)
Prepare() error Prepare(v map[string]string) error
// Run runs the actual builder, returning an artifact implementation // Run runs the actual builder, returning an artifact implementation
// of what is built. If anything goes wrong, an error is returned. // of what is built. If anything goes wrong, an error is returned.
@ -103,8 +103,9 @@ func (b *coreBuild) Name() string {
} }
// Prepare prepares the build by doing some initialization for the builder // Prepare prepares the build by doing some initialization for the builder
// and any hooks. This _must_ be called prior to Run. // and any hooks. This _must_ be called prior to Run. The parameter is the
func (b *coreBuild) Prepare() (err error) { // overrides for the variables within the template (if any).
func (b *coreBuild) Prepare(v map[string]string) (err error) {
b.l.Lock() b.l.Lock()
defer b.l.Unlock() defer b.l.Unlock()

View File

@ -50,7 +50,7 @@ func TestBuild_Prepare(t *testing.T) {
build := testBuild() build := testBuild()
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
build.Prepare() build.Prepare(nil)
assert.True(builder.prepareCalled, "prepare should be called") assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42") assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42")
@ -67,7 +67,7 @@ func TestBuild_Prepare(t *testing.T) {
func TestBuild_Prepare_Twice(t *testing.T) { func TestBuild_Prepare_Twice(t *testing.T) {
build := testBuild() build := testBuild()
if err := build.Prepare(); err != nil { if err := build.Prepare(nil); err != nil {
t.Fatalf("bad error: %s", err) t.Fatalf("bad error: %s", err)
} }
@ -82,7 +82,7 @@ func TestBuild_Prepare_Twice(t *testing.T) {
} }
}() }()
build.Prepare() build.Prepare(nil)
} }
func TestBuild_Prepare_Debug(t *testing.T) { func TestBuild_Prepare_Debug(t *testing.T) {
@ -99,7 +99,7 @@ func TestBuild_Prepare_Debug(t *testing.T) {
builder := build.builder.(*TestBuilder) builder := build.builder.(*TestBuilder)
build.SetDebug(true) build.SetDebug(true)
build.Prepare() build.Prepare(nil)
assert.True(builder.prepareCalled, "prepare should be called") assert.True(builder.prepareCalled, "prepare should be called")
assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42") assert.Equal(builder.prepareConfig, []interface{}{42, packerConfig}, "prepare config should be 42")
@ -116,7 +116,7 @@ func TestBuild_Run(t *testing.T) {
ui := testUi() ui := testUi()
build := testBuild() build := testBuild()
build.Prepare() build.Prepare(nil)
artifacts, err := build.Run(ui, cache) artifacts, err := build.Run(ui, cache)
assert.Nil(err, "should not error") assert.Nil(err, "should not error")
assert.Equal(len(artifacts), 2, "should have two artifacts") assert.Equal(len(artifacts), 2, "should have two artifacts")
@ -152,7 +152,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
build := testBuild() build := testBuild()
build.postProcessors = [][]coreBuildPostProcessor{} build.postProcessors = [][]coreBuildPostProcessor{}
build.Prepare() build.Prepare(nil)
artifacts, err := build.Run(ui, cache) artifacts, err := build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -177,7 +177,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
}, },
} }
build.Prepare() build.Prepare(nil)
artifacts, err = build.Run(ui, cache) artifacts, err = build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -205,7 +205,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
}, },
} }
build.Prepare() build.Prepare(nil)
artifacts, err = build.Run(ui, cache) artifacts, err = build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -235,7 +235,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
}, },
} }
build.Prepare() build.Prepare(nil)
artifacts, err = build.Run(ui, cache) artifacts, err = build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -262,7 +262,7 @@ func TestBuild_Run_Artifacts(t *testing.T) {
}, },
} }
build.Prepare() build.Prepare(nil)
artifacts, err = build.Run(ui, cache) artifacts, err = build.Run(ui, cache)
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)

View File

@ -30,8 +30,8 @@ func (b *build) Name() (result string) {
return return
} }
func (b *build) Prepare() (err error) { func (b *build) Prepare(v map[string]string) (err error) {
if cerr := b.client.Call("Build.Prepare", new(interface{}), &err); cerr != nil { if cerr := b.client.Call("Build.Prepare", v, &err); cerr != nil {
return cerr return cerr
} }
@ -86,8 +86,8 @@ func (b *BuildServer) Name(args *interface{}, reply *string) error {
return nil return nil
} }
func (b *BuildServer) Prepare(args interface{}, reply *error) error { func (b *BuildServer) Prepare(v map[string]string, reply *error) error {
*reply = b.build.Prepare() *reply = b.build.Prepare(v)
return nil return nil
} }

View File

@ -13,6 +13,7 @@ var testBuildArtifact = &testArtifact{}
type testBuild struct { type testBuild struct {
nameCalled bool nameCalled bool
prepareCalled bool prepareCalled bool
prepareVars map[string]string
runCalled bool runCalled bool
runCache packer.Cache runCache packer.Cache
runUi packer.Ui runUi packer.Ui
@ -28,8 +29,9 @@ func (b *testBuild) Name() string {
return "name" return "name"
} }
func (b *testBuild) Prepare() error { func (b *testBuild) Prepare(v map[string]string) error {
b.prepareCalled = true b.prepareCalled = true
b.prepareVars = v
return nil return nil
} }
@ -78,8 +80,15 @@ func TestBuildRPC(t *testing.T) {
assert.True(b.nameCalled, "name should be called") assert.True(b.nameCalled, "name should be called")
// Test Prepare // Test Prepare
bClient.Prepare() bClient.Prepare(map[string]string{"foo": "bar"})
assert.True(b.prepareCalled, "prepare should be called") assert.True(b.prepareCalled, "prepare should be called")
if len(b.prepareVars) != 1 {
t.Fatalf("bad vars: %#v", b.prepareVars)
}
if b.prepareVars["foo"] != "bar" {
t.Fatalf("bad vars: %#v", b.prepareVars)
}
// Test Run // Test Run
cache := new(testCache) cache := new(testCache)