packer-cn/packer/rpc/command_test.go

72 lines
1.3 KiB
Go
Raw Normal View History

package rpc
import (
"github.com/mitchellh/packer/packer"
2013-10-16 23:04:57 -04:00
"reflect"
"testing"
)
type TestCommand struct {
2013-05-10 20:01:24 -04:00
runArgs []string
runCalled bool
2013-05-10 20:01:24 -04:00
runEnv packer.Environment
}
func (tc *TestCommand) Help() string {
return "bar"
}
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)
// 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()
//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)
}
// Test run
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-10-16 23:04:57 -04:00
if command.runEnv == nil {
t.Fatal("runEnv should not be nil")
}
// 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)
}
}
func TestCommand_Implements(t *testing.T) {
2013-12-10 16:26:07 -05:00
var _ packer.Command = new(command)
}