packer-cn/packer/rpc/hook_test.go

51 lines
1014 B
Go
Raw Normal View History

2013-05-11 12:51:49 -04:00
package rpc
import (
"cgl.tideland.biz/asserts"
"github.com/mitchellh/packer/packer"
"net/rpc"
"testing"
)
type testHook struct {
2013-05-20 19:50:35 -04:00
runCalled bool
runUi packer.Ui
2013-05-11 12:51:49 -04:00
}
func (h *testHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
2013-05-11 12:51:49 -04:00
h.runCalled = true
return nil
2013-05-11 12:51:49 -04:00
}
func TestHookRPC(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
// Create the UI to test
h := new(testHook)
// Serve
server := rpc.NewServer()
RegisterHook(server, h)
address := serveSingleConn(server)
// Create the client over RPC and run some methods to verify it works
client, err := rpc.Dial("tcp", address)
assert.Nil(err, "should be able to connect")
hClient := Hook(client)
// Test Run
ui := &testUi{}
hClient.Run("foo", ui, nil, 42)
2013-05-11 12:51:49 -04:00
assert.True(h.runCalled, "run should be called")
}
func TestHook_Implements(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
var r packer.Hook
h := &hook{nil}
assert.Implementor(h, &r, "should be a Hook")
}