github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/ui_test.go (about) 1 package rpc 2 3 import ( 4 "cgl.tideland.biz/asserts" 5 "net/rpc" 6 "reflect" 7 "testing" 8 ) 9 10 type testUi struct { 11 askCalled bool 12 askQuery string 13 errorCalled bool 14 errorMessage string 15 machineCalled bool 16 machineType string 17 machineArgs []string 18 messageCalled bool 19 messageMessage string 20 sayCalled bool 21 sayMessage string 22 } 23 24 func (u *testUi) Ask(query string) (string, error) { 25 u.askCalled = true 26 u.askQuery = query 27 return "foo", nil 28 } 29 30 func (u *testUi) Error(message string) { 31 u.errorCalled = true 32 u.errorMessage = message 33 } 34 35 func (u *testUi) Machine(t string, args ...string) { 36 u.machineCalled = true 37 u.machineType = t 38 u.machineArgs = args 39 } 40 41 func (u *testUi) Message(message string) { 42 u.messageCalled = true 43 u.messageMessage = message 44 } 45 46 func (u *testUi) Say(message string) { 47 u.sayCalled = true 48 u.sayMessage = message 49 } 50 51 func TestUiRPC(t *testing.T) { 52 assert := asserts.NewTestingAsserts(t, true) 53 54 // Create the UI to test 55 ui := new(testUi) 56 57 // Start the RPC server 58 server := rpc.NewServer() 59 RegisterUi(server, ui) 60 address := serveSingleConn(server) 61 62 // Create the client over RPC and run some methods to verify it works 63 client, err := rpc.Dial("tcp", address) 64 if err != nil { 65 panic(err) 66 } 67 68 uiClient := &Ui{client} 69 70 // Basic error and say tests 71 result, err := uiClient.Ask("query") 72 assert.Nil(err, "should not error") 73 assert.True(ui.askCalled, "ask should be called") 74 assert.Equal(ui.askQuery, "query", "should be correct") 75 assert.Equal(result, "foo", "should have correct result") 76 77 uiClient.Error("message") 78 assert.Equal(ui.errorMessage, "message", "message should be correct") 79 80 uiClient.Message("message") 81 assert.Equal(ui.messageMessage, "message", "message should be correct") 82 83 uiClient.Say("message") 84 assert.Equal(ui.sayMessage, "message", "message should be correct") 85 86 uiClient.Machine("foo", "bar", "baz") 87 if !ui.machineCalled { 88 t.Fatal("machine should be called") 89 } 90 91 if ui.machineType != "foo" { 92 t.Fatalf("bad type: %#v", ui.machineType) 93 } 94 95 expected := []string{"bar", "baz"} 96 if !reflect.DeepEqual(ui.machineArgs, expected) { 97 t.Fatalf("bad: %#v", ui.machineArgs) 98 } 99 }