packer/rpc: Setup the Environment properly for Command
This commit is contained in:
parent
adb533fd3e
commit
ac83cf652a
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue