From 0b61e50621d505087550efc4704a4b055f8d8755 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 2 Nov 2013 22:40:06 -0500 Subject: [PATCH] packer/rpc: implement new warnings interfaces --- packer/rpc/build.go | 22 +++++++++++++----- packer/rpc/build_test.go | 47 +++++++++++++++++++++++++++----------- packer/rpc/builder.go | 24 +++++++++++++------ packer/rpc/builder_test.go | 4 ++-- 4 files changed, 69 insertions(+), 28 deletions(-) diff --git a/packer/rpc/build.go b/packer/rpc/build.go index 8f21b4ce9..2df03a6ab 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -21,6 +21,11 @@ type BuildRunArgs struct { UiRPCAddress string } +type BuildPrepareResponse struct { + Warnings []string + Error error +} + func Build(client *rpc.Client) *build { return &build{client} } @@ -30,12 +35,13 @@ func (b *build) Name() (result string) { return } -func (b *build) Prepare(v map[string]string) (err error) { - if cerr := b.client.Call("Build.Prepare", v, &err); cerr != nil { - return cerr +func (b *build) Prepare(v map[string]string) ([]string, error) { + var resp BuildPrepareResponse + 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) { @@ -86,8 +92,12 @@ func (b *BuildServer) Name(args *interface{}, reply *string) error { return nil } -func (b *BuildServer) Prepare(v map[string]string, reply *error) error { - *reply = b.build.Prepare(v) +func (b *BuildServer) Prepare(v map[string]string, resp *BuildPrepareResponse) error { + warnings, err := b.build.Prepare(v) + *resp = BuildPrepareResponse{ + Warnings: warnings, + Error: err, + } return nil } diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index bee9b6796..3cb5e1fb2 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -4,21 +4,23 @@ import ( "errors" "github.com/mitchellh/packer/packer" "net/rpc" + "reflect" "testing" ) var testBuildArtifact = &testArtifact{} type testBuild struct { - nameCalled bool - prepareCalled bool - prepareVars map[string]string - runCalled bool - runCache packer.Cache - runUi packer.Ui - setDebugCalled bool - setForceCalled bool - cancelCalled bool + nameCalled bool + prepareCalled bool + prepareVars map[string]string + prepareWarnings []string + runCalled bool + runCache packer.Cache + runUi packer.Ui + setDebugCalled bool + setForceCalled bool + cancelCalled bool errRunResult bool } @@ -28,10 +30,10 @@ func (b *testBuild) Name() string { 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.prepareVars = v - return nil + return b.prepareWarnings, nil } func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) { @@ -58,7 +60,7 @@ func (b *testBuild) Cancel() { b.cancelCalled = true } -func TestBuildRPC(t *testing.T) { +func buildRPCClient(t *testing.T) (*testBuild, packer.Build) { // Create the interface to test b := new(testBuild) @@ -72,7 +74,11 @@ func TestBuildRPC(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } - bClient := Build(client) + return b, Build(client) +} + +func TestBuild(t *testing.T) { + b, bClient := buildRPCClient(t) // Test 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) { var _ packer.Build = Build(nil) } diff --git a/packer/rpc/builder.go b/packer/rpc/builder.go index 5e2c20d77..42f92c4d4 100644 --- a/packer/rpc/builder.go +++ b/packer/rpc/builder.go @@ -29,6 +29,11 @@ type BuilderRunArgs struct { ResponseAddress string } +type BuilderPrepareResponse struct { + Warnings []string + Error error +} + type BuilderRunResponse struct { Err error RPCAddress string @@ -38,13 +43,14 @@ func Builder(client *rpc.Client) *builder { return &builder{client} } -func (b *builder) Prepare(config ...interface{}) (err error) { - cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err) +func (b *builder) Prepare(config ...interface{}) ([]string, error) { + var resp BuilderPrepareResponse + cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp) 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) { @@ -108,12 +114,16 @@ func (b *builder) Cancel() { } } -func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error { - err := b.builder.Prepare(args.Configs...) +func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error { + warnings, err := b.builder.Prepare(args.Configs...) if err != nil { - *reply = NewBasicError(err) + err = NewBasicError(err) } + *reply = BuilderPrepareResponse{ + Warnings: warnings, + Error: err, + } return nil } diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 6ce8df78e..b56c8afaf 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -23,10 +23,10 @@ type testBuilder struct { nilRunResult bool } -func (b *testBuilder) Prepare(config ...interface{}) error { +func (b *testBuilder) Prepare(config ...interface{}) ([]string, error) { b.prepareCalled = true b.prepareConfig = config - return nil + return nil, nil } func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {