Commit ac83cf65 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/rpc: Setup the Environment properly for Command

parent adb533fd
...@@ -18,7 +18,7 @@ type ServerCommand struct { ...@@ -18,7 +18,7 @@ type ServerCommand struct {
} }
type CommandRunArgs struct { type CommandRunArgs struct {
Env packer.Environment RPCAddress string
Args []string Args []string
} }
...@@ -29,8 +29,11 @@ func Command(client *rpc.Client) *ClientCommand { ...@@ -29,8 +29,11 @@ func Command(client *rpc.Client) *ClientCommand {
} }
func (c *ClientCommand) Run(env packer.Environment, args []string) (result int) { func (c *ClientCommand) Run(env packer.Environment, args []string) (result int) {
// TODO: Environment // Create and start the server for the Environment
rpcArgs := &CommandRunArgs{nil, args} server := rpc.NewServer()
RegisterEnvironment(server, env)
rpcArgs := &CommandRunArgs{serveSingleConn(server), args}
err := c.client.Call("Command.Run", rpcArgs, &result) err := c.client.Call("Command.Run", rpcArgs, &result)
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -49,7 +52,14 @@ func (c *ClientCommand) Synopsis() (result string) { ...@@ -49,7 +52,14 @@ func (c *ClientCommand) Synopsis() (result string) {
} }
func (c *ServerCommand) Run(args *CommandRunArgs, reply *int) error { 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 return nil
} }
......
...@@ -85,12 +85,21 @@ func TestRPCCommand(t *testing.T) { ...@@ -85,12 +85,21 @@ func TestRPCCommand(t *testing.T) {
} }
clientComm := &ClientCommand{client} clientComm := &ClientCommand{client}
// Test run
runArgs := []string{"foo", "bar"} runArgs := []string{"foo", "bar"}
testEnv := &testEnvironment{} testEnv := &testEnvironment{}
exitCode := clientComm.Run(testEnv, runArgs) exitCode := clientComm.Run(testEnv, runArgs)
synopsis := clientComm.Synopsis()
assert.Equal(command.runArgs, runArgs, "Correct args should be sent") assert.Equal(command.runArgs, runArgs, "Correct args should be sent")
assert.Equal(exitCode, 0, "Exit code should be correct") 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") assert.Equal(synopsis, "foo", "Synopsis should be correct")
} }
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