packer/plugin: Actually try more ports for plugins

This commit is contained in:
Mitchell Hashimoto 2013-05-08 22:25:47 -07:00
parent a519de21b9
commit c164b4c23c
5 changed files with 70 additions and 16 deletions

View File

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

View File

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

View File

@ -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...")

View File

@ -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":

View File

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