github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/packer/rpc/hook_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"cgl.tideland.biz/asserts"
     5  	"github.com/mitchellh/packer/packer"
     6  	"net/rpc"
     7  	"testing"
     8  )
     9  
    10  type testHook struct {
    11  	runCalled bool
    12  	runUi     packer.Ui
    13  }
    14  
    15  func (h *testHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) error {
    16  	h.runCalled = true
    17  	return nil
    18  }
    19  
    20  func TestHookRPC(t *testing.T) {
    21  	assert := asserts.NewTestingAsserts(t, true)
    22  
    23  	// Create the UI to test
    24  	h := new(testHook)
    25  
    26  	// Serve
    27  	server := rpc.NewServer()
    28  	RegisterHook(server, h)
    29  	address := serveSingleConn(server)
    30  
    31  	// Create the client over RPC and run some methods to verify it works
    32  	client, err := rpc.Dial("tcp", address)
    33  	assert.Nil(err, "should be able to connect")
    34  
    35  	hClient := Hook(client)
    36  
    37  	// Test Run
    38  	ui := &testUi{}
    39  	hClient.Run("foo", ui, nil, 42)
    40  	assert.True(h.runCalled, "run should be called")
    41  }
    42  
    43  func TestHook_Implements(t *testing.T) {
    44  	assert := asserts.NewTestingAsserts(t, true)
    45  
    46  	var r packer.Hook
    47  	h := &hook{nil}
    48  
    49  	assert.Implementor(h, &r, "should be a Hook")
    50  }