Commit 86f1fbe9 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: Support the new Builder func on Environment

parent a6aafde0
......@@ -19,6 +19,7 @@ type CommandFunc func(name string) Command
// It allows for things such as executing CLI commands, getting the
// list of available builders, and more.
type Environment interface {
Builder(name string) Builder
Cli(args []string) int
Ui() Ui
}
......@@ -68,6 +69,12 @@ func NewEnvironment(config *EnvironmentConfig) (resultEnv Environment, err error
return
}
// Returns a builder of the given name that is registered with this
// environment.
func (e *coreEnvironment) Builder(name string) Builder {
return e.builderFunc(name)
}
// Executes a command as if it was typed on the command-line interface.
// The return value is the exit code of the command.
func (e *coreEnvironment) Cli(args []string) int {
......
......@@ -50,6 +50,20 @@ func TestNewEnvironment_NoConfig(t *testing.T) {
assert.NotNil(err, "should be an error")
}
func TestEnvironment_Builder(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
builder := &TestBuilder{}
builders := make(map[string]Builder)
builders["foo"] = builder
config := DefaultEnvironmentConfig()
config.BuilderFunc = func(n string) Builder { return builders[n] }
env, _ := NewEnvironment(config)
assert.Equal(env.Builder("foo"), builder, "should return correct builder")
}
func TestEnvironment_Cli_CallsRun(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
......
......@@ -21,6 +21,15 @@ type EnvironmentCliArgs struct {
Args []string
}
func (e *Environment) Builder(name string) packer.Builder {
var reply string
e.client.Call("Environment.Builder", name, &reply)
// TODO: error handling
client, _ := rpc.Dial("tcp", reply)
return &Builder{client}
}
func (e *Environment) Cli(args []string) (result int) {
rpcArgs := &EnvironmentCliArgs{args}
e.client.Call("Environment.Cli", rpcArgs, &result)
......@@ -36,6 +45,18 @@ func (e *Environment) Ui() packer.Ui {
return &Ui{client}
}
func (e *EnvironmentServer) Builder(name *string, reply *string) error {
builder := e.env.Builder(*name)
// Wrap it
server := NewServer()
server.RegisterBuilder(builder)
server.StartSingle()
*reply = server.Address()
return nil
}
func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) error {
*reply = e.env.Cli(args.Args)
return nil
......
......@@ -7,15 +7,23 @@ import (
"testing"
)
var testEnvBuilder = &testBuilder{}
var testEnvUi = &testUi{}
type testEnvironment struct {
bfCalled bool
builderCalled bool
builderName string
cliCalled bool
cliArgs []string
uiCalled bool
}
func (e *testEnvironment) Builder(name string) packer.Builder {
e.builderCalled = true
e.builderName = name
return testEnvBuilder
}
func (e *testEnvironment) Cli(args []string) int {
e.cliCalled = true
e.cliArgs = args
......@@ -44,6 +52,14 @@ func TestEnvironmentRPC(t *testing.T) {
assert.Nil(err, "should be able to connect")
eClient := &Environment{client}
// Test Builder
builder := eClient.Builder("foo")
assert.True(e.builderCalled, "Builder should be called")
assert.Equal(e.builderName, "foo", "Correct name for Builder")
builder.Prepare(nil)
assert.True(testEnvBuilder.prepareCalled, "Prepare should be called")
// Test Cli
cliArgs := []string{"foo", "bar"}
result := eClient.Cli(cliArgs)
......
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