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
}
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
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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) {