packer-cn/packer/rpc/ui_test.go

61 lines
1.3 KiB
Go
Raw Normal View History

2013-05-03 18:49:15 -04:00
package rpc
import (
"cgl.tideland.biz/asserts"
"errors"
2013-05-03 18:49:15 -04:00
"net/rpc"
"testing"
)
type testUi struct {
2013-05-08 18:12:48 -04:00
errorCalled bool
errorFormat string
2013-05-10 20:01:24 -04:00
errorVars []interface{}
sayCalled bool
sayFormat string
sayVars []interface{}
2013-05-03 18:49:15 -04:00
}
2013-05-08 18:12:48 -04:00
func (u *testUi) Error(format string, a ...interface{}) {
u.errorCalled = true
u.errorFormat = format
u.errorVars = a
}
2013-05-03 18:49:15 -04:00
func (u *testUi) Say(format string, a ...interface{}) {
u.sayCalled = true
u.sayFormat = format
u.sayVars = a
}
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
2013-05-08 18:12:48 -04:00
uiClient.Error("format", "arg0", 42)
assert.Equal(ui.errorFormat, "format", "format should be correct")
uiClient.Say("format", "arg0", 42)
2013-05-03 18:49:15 -04:00
assert.Equal(ui.sayFormat, "format", "format should be correct")
// Test that errors are properly converted to strings
uiClient.Say("format", errors.New("foo"))
assert.Equal(ui.sayVars, []interface{}{"foo"}, "should have correct vars")
2013-05-03 18:49:15 -04:00
}