packer/rpc: Builders return valid artifacts

This commit is contained in:
Mitchell Hashimoto 2013-05-21 22:13:29 -07:00
parent 53c3d330e4
commit caa2b7711b
2 changed files with 24 additions and 8 deletions

View File

@ -46,10 +46,18 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
RegisterHook(server, hook)
args := &BuilderRunArgs{serveSingleConn(server)}
b.client.Call("Builder.Run", args, new(interface{}))
// TODO: artifact
return nil
var reply string
if err := b.client.Call("Builder.Run", args, &reply); err != nil {
panic(err)
}
client, err := rpc.Dial("tcp", reply)
if err != nil {
panic(err)
}
return Artifact(client)
}
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
@ -61,7 +69,7 @@ func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
return nil
}
func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
func (b *BuilderServer) Run(args *BuilderRunArgs, reply *string) error {
client, err := rpc.Dial("tcp", args.RPCAddress)
if err != nil {
return err
@ -69,8 +77,12 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
hook := Hook(client)
ui := &Ui{client}
b.builder.Run(ui, hook)
artifact := b.builder.Run(ui, hook)
*reply = nil
// Wrap the artifact
server := rpc.NewServer()
RegisterArtifact(server, artifact)
*reply = serveSingleConn(server)
return nil
}

View File

@ -7,6 +7,8 @@ import (
"testing"
)
var testBuilderArtifact = &testArtifact{}
type testBuilder struct {
prepareCalled bool
prepareConfig interface{}
@ -25,7 +27,7 @@ func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
b.runCalled = true
b.runHook = hook
b.runUi = ui
return nil
return testBuilderArtifact
}
func TestBuilderRPC(t *testing.T) {
@ -53,7 +55,7 @@ func TestBuilderRPC(t *testing.T) {
// Test Run
hook := &testHook{}
ui := &testUi{}
bClient.Run(ui, hook)
artifact := bClient.Run(ui, hook)
assert.True(b.runCalled, "runs hould be called")
if b.runCalled {
@ -63,6 +65,8 @@ func TestBuilderRPC(t *testing.T) {
b.runUi.Say("format")
assert.True(ui.sayCalled, "say should be called")
assert.Equal(ui.sayFormat, "format", "format should be correct")
assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have artifact Id")
}
}