From c164b4c23c1490fd1bedb47917d1070cd6862322 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 May 2013 22:25:47 -0700 Subject: [PATCH] packer/plugin: Actually try more ports for plugins --- packer/plugin/builder_test.go | 29 +++++++++++++++++++++++++++++ packer/plugin/command_test.go | 18 ++++++++++++++++-- packer/plugin/plugin.go | 22 +++++++++++++++++++++- packer/plugin/plugin_test.go | 13 ++----------- packer/rpc/builder_test.go | 4 ++-- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 packer/plugin/builder_test.go diff --git a/packer/plugin/builder_test.go b/packer/plugin/builder_test.go new file mode 100644 index 000000000..251999eb7 --- /dev/null +++ b/packer/plugin/builder_test.go @@ -0,0 +1,29 @@ +package plugin + +import ( + "cgl.tideland.biz/asserts" + "github.com/mitchellh/packer/packer" + "os/exec" + "testing" +) + +type helperBuilder byte + +func (helperBuilder) Prepare(interface{}) {} + +func (helperBuilder) Run(packer.Build, packer.Ui) {} + +func TestBuilder_NoExist(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + _, err := Builder(exec.Command("i-should-never-ever-ever-exist")) + assert.NotNil(err, "should have an error") +} + +func TestBuilder_Good(t *testing.T) { + assert := asserts.NewTestingAsserts(t, true) + + _, err := Builder(helperProcess("builder")) + assert.Nil(err, "should start builder properly") +} + diff --git a/packer/plugin/command_test.go b/packer/plugin/command_test.go index b99168065..9091ec0ba 100644 --- a/packer/plugin/command_test.go +++ b/packer/plugin/command_test.go @@ -2,6 +2,7 @@ package plugin import ( "cgl.tideland.biz/asserts" + "github.com/mitchellh/packer/packer" "os/exec" "testing" ) @@ -9,6 +10,16 @@ import ( // TODO: Test command cleanup functionality // TODO: Test timeout functionality +type helperCommand byte + +func (helperCommand) Run(packer.Environment, []string) int { + return 42 +} + +func (helperCommand) Synopsis() string { + return "1" +} + func TestCommand_NoExist(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) @@ -22,8 +33,11 @@ func TestCommand_Good(t *testing.T) { command, err := Command(helperProcess("command")) assert.Nil(err, "should start command properly") - result := command.Synopsis() - assert.Equal(result, "1", "should return result") + assert.NotNil(command, "should have a command") + if command != nil { + result := command.Synopsis() + assert.Equal(result, "1", "should return result") + } } func TestCommand_CommandExited(t *testing.T) { diff --git a/packer/plugin/plugin.go b/packer/plugin/plugin.go index 3990c9653..7f9b58a32 100644 --- a/packer/plugin/plugin.go +++ b/packer/plugin/plugin.go @@ -16,6 +16,7 @@ import ( "os" packrpc "github.com/mitchellh/packer/packer/rpc" "strconv" + "strings" ) // This serves a single RPC connection on the given RPC server on @@ -40,7 +41,14 @@ func serve(server *rpc.Server) (err error) { address = fmt.Sprintf(":%d", port) listener, err = net.Listen("tcp", address) if err != nil { - return + if !strings.Contains(err.Error(), "address already in use") { + // Not an address already in use error, return. + return + } else { + // Address is in use, just try another + err = nil + continue + } } break @@ -67,6 +75,18 @@ func serve(server *rpc.Server) (err error) { return } +// Serves a builder from a plugin. +func ServeBuilder(builder packer.Builder) { + log.Println("Preparing to serve a builder plugin...") + + server := rpc.NewServer() + packrpc.RegisterBuilder(server, builder) + + if err := serve(server); err != nil { + log.Panic(err) + } +} + // Serves a command from a plugin. func ServeCommand(command packer.Command) { log.Println("Preparing to serve a command plugin...") diff --git a/packer/plugin/plugin_test.go b/packer/plugin/plugin_test.go index 35cb84145..3a1a999b8 100644 --- a/packer/plugin/plugin_test.go +++ b/packer/plugin/plugin_test.go @@ -2,23 +2,12 @@ package plugin import ( "fmt" - "github.com/mitchellh/packer/packer" "os" "os/exec" "testing" "time" ) -type helperCommand byte - -func (helperCommand) Run(packer.Environment, []string) int { - return 42 -} - -func (helperCommand) Synopsis() string { - return "1" -} - func helperProcess(s... string) *exec.Cmd { cs := []string{"-test.run=TestHelperProcess", "--"} cs = append(cs, s...) @@ -59,6 +48,8 @@ func TestHelperProcess(*testing.T) { cmd, args := args[0], args[1:] switch cmd { + case "builder": + ServeBuilder(new(helperBuilder)) case "command": ServeCommand(new(helperCommand)) case "invalid-rpc-address": diff --git a/packer/rpc/builder_test.go b/packer/rpc/builder_test.go index 586769569..68342813f 100644 --- a/packer/rpc/builder_test.go +++ b/packer/rpc/builder_test.go @@ -43,7 +43,7 @@ func TestBuilderRPC(t *testing.T) { // Test Prepare config := 42 - bClient := &Builder{client} + bClient := Builder(client) bClient.Prepare(config) assert.True(b.prepareCalled, "prepare should be called") assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg") @@ -68,7 +68,7 @@ func TestBuilder_ImplementsBuild(t *testing.T) { assert := asserts.NewTestingAsserts(t, true) var realBuilder packer.Builder - b := &Builder{nil} + b := Builder(nil) assert.Implementor(b, &realBuilder, "should be a Builder") }