Commit c164b4c2 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/plugin: Actually try more ports for plugins

parent a519de21
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")
}
...@@ -2,6 +2,7 @@ package plugin ...@@ -2,6 +2,7 @@ package plugin
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"os/exec" "os/exec"
"testing" "testing"
) )
...@@ -9,6 +10,16 @@ import ( ...@@ -9,6 +10,16 @@ import (
// TODO: Test command cleanup functionality // TODO: Test command cleanup functionality
// TODO: Test timeout 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) { func TestCommand_NoExist(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
...@@ -22,8 +33,11 @@ func TestCommand_Good(t *testing.T) { ...@@ -22,8 +33,11 @@ func TestCommand_Good(t *testing.T) {
command, err := Command(helperProcess("command")) command, err := Command(helperProcess("command"))
assert.Nil(err, "should start command properly") assert.Nil(err, "should start command properly")
assert.NotNil(command, "should have a command")
if command != nil {
result := command.Synopsis() result := command.Synopsis()
assert.Equal(result, "1", "should return result") assert.Equal(result, "1", "should return result")
}
} }
func TestCommand_CommandExited(t *testing.T) { func TestCommand_CommandExited(t *testing.T) {
......
...@@ -16,6 +16,7 @@ import ( ...@@ -16,6 +16,7 @@ import (
"os" "os"
packrpc "github.com/mitchellh/packer/packer/rpc" packrpc "github.com/mitchellh/packer/packer/rpc"
"strconv" "strconv"
"strings"
) )
// This serves a single RPC connection on the given RPC server on // This serves a single RPC connection on the given RPC server on
...@@ -40,7 +41,14 @@ func serve(server *rpc.Server) (err error) { ...@@ -40,7 +41,14 @@ func serve(server *rpc.Server) (err error) {
address = fmt.Sprintf(":%d", port) address = fmt.Sprintf(":%d", port)
listener, err = net.Listen("tcp", address) listener, err = net.Listen("tcp", address)
if err != nil { if err != nil {
if !strings.Contains(err.Error(), "address already in use") {
// Not an address already in use error, return.
return return
} else {
// Address is in use, just try another
err = nil
continue
}
} }
break break
...@@ -67,6 +75,18 @@ func serve(server *rpc.Server) (err error) { ...@@ -67,6 +75,18 @@ func serve(server *rpc.Server) (err error) {
return 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. // Serves a command from a plugin.
func ServeCommand(command packer.Command) { func ServeCommand(command packer.Command) {
log.Println("Preparing to serve a command plugin...") log.Println("Preparing to serve a command plugin...")
......
...@@ -2,23 +2,12 @@ package plugin ...@@ -2,23 +2,12 @@ package plugin
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/packer"
"os" "os"
"os/exec" "os/exec"
"testing" "testing"
"time" "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 { func helperProcess(s... string) *exec.Cmd {
cs := []string{"-test.run=TestHelperProcess", "--"} cs := []string{"-test.run=TestHelperProcess", "--"}
cs = append(cs, s...) cs = append(cs, s...)
...@@ -59,6 +48,8 @@ func TestHelperProcess(*testing.T) { ...@@ -59,6 +48,8 @@ func TestHelperProcess(*testing.T) {
cmd, args := args[0], args[1:] cmd, args := args[0], args[1:]
switch cmd { switch cmd {
case "builder":
ServeBuilder(new(helperBuilder))
case "command": case "command":
ServeCommand(new(helperCommand)) ServeCommand(new(helperCommand))
case "invalid-rpc-address": case "invalid-rpc-address":
......
...@@ -43,7 +43,7 @@ func TestBuilderRPC(t *testing.T) { ...@@ -43,7 +43,7 @@ func TestBuilderRPC(t *testing.T) {
// Test Prepare // Test Prepare
config := 42 config := 42
bClient := &Builder{client} bClient := Builder(client)
bClient.Prepare(config) bClient.Prepare(config)
assert.True(b.prepareCalled, "prepare should be called") assert.True(b.prepareCalled, "prepare should be called")
assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg") assert.Equal(b.prepareConfig, 42, "prepare should be called with right arg")
...@@ -68,7 +68,7 @@ func TestBuilder_ImplementsBuild(t *testing.T) { ...@@ -68,7 +68,7 @@ func TestBuilder_ImplementsBuild(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
var realBuilder packer.Builder var realBuilder packer.Builder
b := &Builder{nil} b := Builder(nil)
assert.Implementor(b, &realBuilder, "should be a Builder") assert.Implementor(b, &realBuilder, "should be a Builder")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment