packer/rpc: implement new warnings interfaces

This commit is contained in:
Mitchell Hashimoto 2013-11-02 22:40:06 -05:00
parent 336051e316
commit 0b61e50621
4 changed files with 69 additions and 28 deletions

View File

@ -21,6 +21,11 @@ type BuildRunArgs struct {
UiRPCAddress string UiRPCAddress string
} }
type BuildPrepareResponse struct {
Warnings []string
Error error
}
func Build(client *rpc.Client) *build { func Build(client *rpc.Client) *build {
return &build{client} return &build{client}
} }
@ -30,12 +35,13 @@ func (b *build) Name() (result string) {
return return
} }
func (b *build) Prepare(v map[string]string) (err error) { func (b *build) Prepare(v map[string]string) ([]string, error) {
if cerr := b.client.Call("Build.Prepare", v, &err); cerr != nil { var resp BuildPrepareResponse
return cerr if cerr := b.client.Call("Build.Prepare", v, &resp); cerr != nil {
return nil, cerr
} }
return return resp.Warnings, resp.Error
} }
func (b *build) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) { func (b *build) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
@ -86,8 +92,12 @@ func (b *BuildServer) Name(args *interface{}, reply *string) error {
return nil return nil
} }
func (b *BuildServer) Prepare(v map[string]string, reply *error) error { func (b *BuildServer) Prepare(v map[string]string, resp *BuildPrepareResponse) error {
*reply = b.build.Prepare(v) warnings, err := b.build.Prepare(v)
*resp = BuildPrepareResponse{
Warnings: warnings,
Error: err,
}
return nil return nil
} }

View File

@ -4,21 +4,23 @@ import (
"errors" "errors"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net/rpc" "net/rpc"
"reflect"
"testing" "testing"
) )
var testBuildArtifact = &testArtifact{} var testBuildArtifact = &testArtifact{}
type testBuild struct { type testBuild struct {
nameCalled bool nameCalled bool
prepareCalled bool prepareCalled bool
prepareVars map[string]string prepareVars map[string]string
runCalled bool prepareWarnings []string
runCache packer.Cache runCalled bool
runUi packer.Ui runCache packer.Cache
setDebugCalled bool runUi packer.Ui
setForceCalled bool setDebugCalled bool
cancelCalled bool setForceCalled bool
cancelCalled bool
errRunResult bool errRunResult bool
} }
@ -28,10 +30,10 @@ func (b *testBuild) Name() string {
return "name" return "name"
} }
func (b *testBuild) Prepare(v map[string]string) error { func (b *testBuild) Prepare(v map[string]string) ([]string, error) {
b.prepareCalled = true b.prepareCalled = true
b.prepareVars = v b.prepareVars = v
return nil return b.prepareWarnings, nil
} }
func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) { func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
@ -58,7 +60,7 @@ func (b *testBuild) Cancel() {
b.cancelCalled = true b.cancelCalled = true
} }
func TestBuildRPC(t *testing.T) { func buildRPCClient(t *testing.T) (*testBuild, packer.Build) {
// Create the interface to test // Create the interface to test
b := new(testBuild) b := new(testBuild)
@ -72,7 +74,11 @@ func TestBuildRPC(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
bClient := Build(client) return b, Build(client)
}
func TestBuild(t *testing.T) {
b, bClient := buildRPCClient(t)
// Test Name // Test Name
bClient.Name() bClient.Name()
@ -157,6 +163,21 @@ func TestBuildRPC(t *testing.T) {
} }
} }
func TestBuildPrepare_Warnings(t *testing.T) {
b, bClient := buildRPCClient(t)
expected := []string{"foo"}
b.prepareWarnings = expected
warnings, err := bClient.Prepare(nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if !reflect.DeepEqual(warnings, expected) {
t.Fatalf("bad: %#v", warnings)
}
}
func TestBuild_ImplementsBuild(t *testing.T) { func TestBuild_ImplementsBuild(t *testing.T) {
var _ packer.Build = Build(nil) var _ packer.Build = Build(nil)
} }

View File

@ -29,6 +29,11 @@ type BuilderRunArgs struct {
ResponseAddress string ResponseAddress string
} }
type BuilderPrepareResponse struct {
Warnings []string
Error error
}
type BuilderRunResponse struct { type BuilderRunResponse struct {
Err error Err error
RPCAddress string RPCAddress string
@ -38,13 +43,14 @@ func Builder(client *rpc.Client) *builder {
return &builder{client} return &builder{client}
} }
func (b *builder) Prepare(config ...interface{}) (err error) { func (b *builder) Prepare(config ...interface{}) ([]string, error) {
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err) var resp BuilderPrepareResponse
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp)
if cerr != nil { if cerr != nil {
err = cerr return nil, cerr
} }
return return resp.Warnings, resp.Error
} }
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
@ -108,12 +114,16 @@ func (b *builder) Cancel() {
} }
} }
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error { func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
err := b.builder.Prepare(args.Configs...) warnings, err := b.builder.Prepare(args.Configs...)
if err != nil { if err != nil {
*reply = NewBasicError(err) err = NewBasicError(err)
} }
*reply = BuilderPrepareResponse{
Warnings: warnings,
Error: err,
}
return nil return nil
} }

View File

@ -23,10 +23,10 @@ type testBuilder struct {
nilRunResult bool nilRunResult bool
} }
func (b *testBuilder) Prepare(config ...interface{}) error { func (b *testBuilder) Prepare(config ...interface{}) ([]string, error) {
b.prepareCalled = true b.prepareCalled = true
b.prepareConfig = config b.prepareConfig = config
return nil return nil, nil
} }
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) { func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {