2013-05-03 17:26:21 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
2013-10-16 23:04:57 -04:00
|
|
|
"reflect"
|
2013-05-03 17:26:21 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestCommand struct {
|
2013-05-10 20:01:24 -04:00
|
|
|
runArgs []string
|
2013-05-03 17:26:21 -04:00
|
|
|
runCalled bool
|
2013-05-10 20:01:24 -04:00
|
|
|
runEnv packer.Environment
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
func (tc *TestCommand) Help() string {
|
|
|
|
return "bar"
|
|
|
|
}
|
|
|
|
|
2013-05-03 17:26:21 -04:00
|
|
|
func (tc *TestCommand) Run(env packer.Environment, args []string) int {
|
|
|
|
tc.runCalled = true
|
|
|
|
tc.runArgs = args
|
|
|
|
tc.runEnv = env
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TestCommand) Synopsis() string {
|
|
|
|
return "foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRPCCommand(t *testing.T) {
|
|
|
|
// Create the command
|
|
|
|
command := new(TestCommand)
|
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
// Start the server
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterCommand(server, command)
|
|
|
|
address := serveSingleConn(server)
|
2013-05-03 17:26:21 -04:00
|
|
|
|
|
|
|
// Create the command client over RPC and run some methods to verify
|
|
|
|
// we get the proper behavior.
|
2013-05-08 16:42:25 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
2013-10-16 23:04:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2013-05-03 17:26:21 -04:00
|
|
|
|
2013-05-08 16:42:25 -04:00
|
|
|
clientComm := Command(client)
|
2013-05-08 16:11:47 -04:00
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
//Test Help
|
|
|
|
help := clientComm.Help()
|
2013-10-16 23:04:57 -04:00
|
|
|
if help != "bar" {
|
|
|
|
t.Fatalf("bad: %s", help)
|
|
|
|
}
|
2013-06-02 14:41:12 -04:00
|
|
|
|
2013-05-08 16:11:47 -04:00
|
|
|
// Test run
|
2013-05-03 17:26:21 -04:00
|
|
|
runArgs := []string{"foo", "bar"}
|
2013-05-04 18:58:42 -04:00
|
|
|
testEnv := &testEnvironment{}
|
2013-05-03 17:26:21 -04:00
|
|
|
exitCode := clientComm.Run(testEnv, runArgs)
|
2013-10-16 23:04:57 -04:00
|
|
|
if !reflect.DeepEqual(command.runArgs, runArgs) {
|
|
|
|
t.Fatalf("bad: %#v", command.runArgs)
|
|
|
|
}
|
|
|
|
if exitCode != 0 {
|
|
|
|
t.Fatalf("bad: %d", exitCode)
|
|
|
|
}
|
2013-05-08 16:11:47 -04:00
|
|
|
|
2013-10-16 23:04:57 -04:00
|
|
|
if command.runEnv == nil {
|
|
|
|
t.Fatal("runEnv should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
command.runEnv.Ui()
|
|
|
|
if !testEnv.uiCalled {
|
|
|
|
t.Fatal("ui should be called")
|
2013-05-08 16:11:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test Synopsis
|
|
|
|
synopsis := clientComm.Synopsis()
|
2013-10-16 23:04:57 -04:00
|
|
|
if synopsis != "foo" {
|
|
|
|
t.Fatalf("bad: %#v", synopsis)
|
|
|
|
}
|
2013-05-03 17:26:21 -04:00
|
|
|
}
|
2013-06-02 14:41:12 -04:00
|
|
|
|
|
|
|
func TestCommand_Implements(t *testing.T) {
|
2013-10-16 23:04:57 -04:00
|
|
|
var _ packer.Command = Command(nil)
|
2013-06-02 14:41:12 -04:00
|
|
|
}
|