2013-05-03 17:26:21 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
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
|
2013-12-10 15:02:01 -05:00
|
|
|
client, server := testClientServer(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
server.RegisterCommand(command)
|
|
|
|
commClient := client.Command()
|
2013-05-08 16:11:47 -04:00
|
|
|
|
2013-06-02 14:41:12 -04:00
|
|
|
//Test Help
|
2013-12-10 15:02:01 -05:00
|
|
|
help := commClient.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-12-10 15:02:01 -05:00
|
|
|
exitCode := commClient.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")
|
|
|
|
}
|
|
|
|
|
2013-05-08 16:11:47 -04:00
|
|
|
// Test Synopsis
|
2013-12-10 15:02:01 -05:00
|
|
|
synopsis := commClient.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-12-10 16:26:07 -05:00
|
|
|
var _ packer.Command = new(command)
|
2013-06-02 14:41:12 -04:00
|
|
|
}
|