mirror of https://github.com/hashicorp/packer
parent
8ed313e7b5
commit
30ab944437
@ -0,0 +1,50 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
)
|
||||
|
||||
// An implementation of packer.Hook where the hook is actually executed
|
||||
// over an RPC connection.
|
||||
type hook struct {
|
||||
client *rpc.Client
|
||||
}
|
||||
|
||||
// HookServer wraps a packer.Hook implementation and makes it exportable
|
||||
// as part of a Golang RPC server.
|
||||
type HookServer struct {
|
||||
hook packer.Hook
|
||||
}
|
||||
|
||||
type HookRunArgs struct {
|
||||
Name string
|
||||
Data interface{}
|
||||
RPCAddress string
|
||||
}
|
||||
|
||||
func Hook(client *rpc.Client) *hook {
|
||||
return &hook{client}
|
||||
}
|
||||
|
||||
func (h *hook) Run(name string, data interface{}, ui packer.Ui) {
|
||||
server := rpc.NewServer()
|
||||
RegisterUi(server, ui)
|
||||
address := serveSingleConn(server)
|
||||
|
||||
args := &HookRunArgs{name, data, address}
|
||||
h.client.Call("Hook.Run", args, new(interface{}))
|
||||
return
|
||||
}
|
||||
|
||||
func (h *HookServer) Run(args *HookRunArgs, reply *interface{}) error {
|
||||
client, err := rpc.Dial("tcp", args.RPCAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
h.hook.Run(args.Name, args.Data, &Ui{client})
|
||||
|
||||
*reply = nil
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"cgl.tideland.biz/asserts"
|
||||
"github.com/mitchellh/packer/packer"
|
||||
"net/rpc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type testHook struct {
|
||||
runCalled bool
|
||||
runUi packer.Ui
|
||||
}
|
||||
|
||||
func (h *testHook) Run(name string, data interface{}, ui packer.Ui) {
|
||||
h.runCalled = true
|
||||
}
|
||||
|
||||
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", 42, ui)
|
||||
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")
|
||||
}
|
||||
Loading…
Reference in new issue