2013-05-03 17:26:21 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"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) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
assert.Nil(err, "should be no error")
|
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()
|
|
|
|
assert.Equal(help, "bar", "helps hould be correct")
|
|
|
|
|
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)
|
|
|
|
assert.Equal(command.runArgs, runArgs, "Correct args should be sent")
|
|
|
|
assert.Equal(exitCode, 0, "Exit code should be correct")
|
2013-05-08 16:11:47 -04:00
|
|
|
|
|
|
|
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()
|
2013-05-03 17:26:21 -04:00
|
|
|
assert.Equal(synopsis, "foo", "Synopsis should be correct")
|
|
|
|
}
|
2013-06-02 14:41:12 -04:00
|
|
|
|
|
|
|
func TestCommand_Implements(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
var r packer.Command
|
|
|
|
c := Command(nil)
|
|
|
|
|
|
|
|
assert.Implementor(c, &r, "should be a Builder")
|
|
|
|
}
|