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 95b598f748
commit 3f0a268e1e
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
// 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
// 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
// and any hooks. This _must_ be called prior to Run.
func (b *coreBuild) Prepare() (err error) {
// and any hooks. This _must_ be called prior to Run. The parameter is the
// overrides for the variables within the template (if any).
func (b *coreBuild) Prepare(v map[string]string) (err error) {
b.l.Lock()
defer b.l.Unlock()

View File

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

View File

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

View File

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