packer-cn/packer/rpc/ui_test.go

52 lines
991 B
Go
Raw Normal View History

2013-05-03 18:49:15 -04:00
package rpc
import (
"cgl.tideland.biz/asserts"
"net/rpc"
"testing"
)
type testUi struct {
errorCalled bool
errorMessage string
sayCalled bool
sayMessage string
2013-05-03 18:49:15 -04:00
}
func (u *testUi) Error(message string) {
2013-05-08 18:12:48 -04:00
u.errorCalled = true
u.errorMessage = message
2013-05-08 18:12:48 -04:00
}
func (u *testUi) Say(message string) {
2013-05-03 18:49:15 -04:00
u.sayCalled = true
u.sayMessage = message
2013-05-03 18:49:15 -04:00
}
func TestUiRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the UI to test
ui := new(testUi)
// Start the RPC server
server := rpc.NewServer()
RegisterUi(server, ui)
address := serveSingleConn(server)
2013-05-03 18:49:15 -04:00
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
2013-05-03 18:49:15 -04:00
if err != nil {
panic(err)
}
uiClient := &Ui{client}
// Basic error and say tests
uiClient.Error("message")
assert.Equal(ui.errorMessage, "message", "message should be correct")
2013-05-08 18:12:48 -04:00
uiClient.Say("message")
assert.Equal(ui.sayMessage, "message", "message should be correct")
2013-05-03 18:49:15 -04:00
}