From d2023c69bebe97e600c4619b1e409f02f9caaccf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 12 Jun 2013 16:01:42 -0700 Subject: [PATCH] packer/rpc: returning errors from builds works properly --- packer/rpc/build.go | 6 +++--- packer/rpc/build_test.go | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/packer/rpc/build.go b/packer/rpc/build.go index a816947d2..ed6050af9 100644 --- a/packer/rpc/build.go +++ b/packer/rpc/build.go @@ -57,12 +57,12 @@ func (b *build) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, error) { var reply string if err := b.client.Call("Build.Run", args, &reply); err != nil { - panic(err) + return nil, err } client, err := rpc.Dial("tcp", reply) if err != nil { - panic(err) + return nil, err } return Artifact(client), nil @@ -97,7 +97,7 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error { artifact, err := b.build.Run(&Ui{client}, Cache(client)) if err != nil { - return err + return NewBasicError(err) } // Wrap the artifact diff --git a/packer/rpc/build_test.go b/packer/rpc/build_test.go index 33d1631c3..e7aed364d 100644 --- a/packer/rpc/build_test.go +++ b/packer/rpc/build_test.go @@ -2,6 +2,7 @@ package rpc import ( "cgl.tideland.biz/asserts" + "errors" "github.com/mitchellh/packer/packer" "net/rpc" "testing" @@ -17,6 +18,8 @@ type testBuild struct { runCache packer.Cache runUi packer.Ui cancelCalled bool + + errRunResult bool } func (b *testBuild) Name() string { @@ -34,7 +37,12 @@ func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, erro b.runCalled = true b.runCache = cache b.runUi = ui - return testBuildArtifact, nil + + if b.errRunResult { + return nil, errors.New("foo") + } else { + return testBuildArtifact, nil + } } func (b *testBuild) Cancel() { @@ -69,8 +77,9 @@ func TestBuildRPC(t *testing.T) { // Test Run cache := new(testCache) ui = new(testUi) - bClient.Run(ui, cache) + _, err = bClient.Run(ui, cache) assert.True(b.runCalled, "run should be called") + assert.Nil(err, "should not error") // Test the UI given to run, which should be fully functional if b.runCalled { @@ -82,6 +91,11 @@ func TestBuildRPC(t *testing.T) { assert.Equal(ui.sayMessage, "format", "message should be correct") } + // Test run with an error + b.errRunResult = true + _, err = bClient.Run(ui, cache) + assert.NotNil(err, "should not nil") + // Test Cancel bClient.Cancel() assert.True(b.cancelCalled, "cancel should be called")