packer/rpc: Setup the Environment properly for Command

This commit is contained in:
Mitchell Hashimoto 2013-05-08 13:11:47 -07:00
parent adb533fd3e
commit ac83cf652a
2 changed files with 25 additions and 6 deletions

View File

@ -18,7 +18,7 @@ type ServerCommand struct {
}
type CommandRunArgs struct {
Env packer.Environment
RPCAddress string
Args []string
}
@ -29,8 +29,11 @@ func Command(client *rpc.Client) *ClientCommand {
}
func (c *ClientCommand) Run(env packer.Environment, args []string) (result int) {
// TODO: Environment
rpcArgs := &CommandRunArgs{nil, args}
// Create and start the server for the Environment
server := rpc.NewServer()
RegisterEnvironment(server, env)
rpcArgs := &CommandRunArgs{serveSingleConn(server), args}
err := c.client.Call("Command.Run", rpcArgs, &result)
if err != nil {
panic(err)
@ -49,7 +52,14 @@ func (c *ClientCommand) Synopsis() (result string) {
}
func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error {
*reply = c.command.Run(args.Env, args.Args)
client, err := rpc.Dial("tcp", args.RPCAddress)
if err != nil {
return err
}
env := &Environment{client}
*reply = c.command.Run(env, args.Args)
return nil
}

View File

@ -85,12 +85,21 @@ func TestRPCCommand(t *testing.T) {
}
clientComm := &ClientCommand{client}
// Test run
runArgs := []string{"foo", "bar"}
testEnv := &testEnvironment{}
exitCode := clientComm.Run(testEnv, runArgs)
synopsis := clientComm.Synopsis()
assert.Equal(command.runArgs, runArgs, "Correct args should be sent")
assert.Equal(exitCode, 0, "Exit code should be correct")
assert.NotNil(command.runEnv, "should have an env")
if command.runEnv != nil {
command.runEnv.Ui()
assert.True(testEnv.uiCalled, "UI should be called on env")
}
// Test Synopsis
synopsis := clientComm.Synopsis()
assert.Equal(synopsis, "foo", "Synopsis should be correct")
}