github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/command_test.go (about) 1 package rpc 2 3 import ( 4 "cgl.tideland.biz/asserts" 5 "github.com/mitchellh/packer/packer" 6 "net/rpc" 7 "testing" 8 ) 9 10 type TestCommand struct { 11 runArgs []string 12 runCalled bool 13 runEnv packer.Environment 14 } 15 16 func (tc *TestCommand) Help() string { 17 return "bar" 18 } 19 20 func (tc *TestCommand) Run(env packer.Environment, args []string) int { 21 tc.runCalled = true 22 tc.runArgs = args 23 tc.runEnv = env 24 return 0 25 } 26 27 func (tc *TestCommand) Synopsis() string { 28 return "foo" 29 } 30 31 func TestRPCCommand(t *testing.T) { 32 assert := asserts.NewTestingAsserts(t, true) 33 34 // Create the command 35 command := new(TestCommand) 36 37 // Start the server 38 server := rpc.NewServer() 39 RegisterCommand(server, command) 40 address := serveSingleConn(server) 41 42 // Create the command client over RPC and run some methods to verify 43 // we get the proper behavior. 44 client, err := rpc.Dial("tcp", address) 45 assert.Nil(err, "should be no error") 46 47 clientComm := Command(client) 48 49 //Test Help 50 help := clientComm.Help() 51 assert.Equal(help, "bar", "helps hould be correct") 52 53 // Test run 54 runArgs := []string{"foo", "bar"} 55 testEnv := &testEnvironment{} 56 exitCode := clientComm.Run(testEnv, runArgs) 57 assert.Equal(command.runArgs, runArgs, "Correct args should be sent") 58 assert.Equal(exitCode, 0, "Exit code should be correct") 59 60 assert.NotNil(command.runEnv, "should have an env") 61 if command.runEnv != nil { 62 command.runEnv.Ui() 63 assert.True(testEnv.uiCalled, "UI should be called on env") 64 } 65 66 // Test Synopsis 67 synopsis := clientComm.Synopsis() 68 assert.Equal(synopsis, "foo", "Synopsis should be correct") 69 } 70 71 func TestCommand_Implements(t *testing.T) { 72 assert := asserts.NewTestingAsserts(t, true) 73 74 var r packer.Command 75 c := Command(nil) 76 77 assert.Implementor(c, &r, "should be a Builder") 78 }