2013-05-22 18:35:52 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProvisionerRPC(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// Create the interface to test
|
2013-08-31 02:21:15 -04:00
|
|
|
p := new(packer.MockProvisioner)
|
2013-05-22 18:35:52 -04:00
|
|
|
|
|
|
|
// Start the server
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterProvisioner(server, p)
|
|
|
|
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")
|
|
|
|
|
|
|
|
// Test Prepare
|
|
|
|
config := 42
|
|
|
|
pClient := Provisioner(client)
|
2013-06-06 20:01:12 -04:00
|
|
|
pClient.Prepare(config)
|
2013-08-31 02:21:15 -04:00
|
|
|
assert.True(p.PrepCalled, "prepare should be called")
|
|
|
|
assert.Equal(p.PrepConfigs, []interface{}{42}, "prepare should be called with right arg")
|
2013-05-22 18:35:52 -04:00
|
|
|
|
|
|
|
// Test Provision
|
2013-06-06 20:01:12 -04:00
|
|
|
ui := &testUi{}
|
2013-08-31 02:21:15 -04:00
|
|
|
comm := &packer.MockCommunicator{}
|
2013-05-22 18:35:52 -04:00
|
|
|
pClient.Provision(ui, comm)
|
2013-08-31 02:21:15 -04:00
|
|
|
assert.True(p.ProvCalled, "provision should be called")
|
2013-05-22 18:35:52 -04:00
|
|
|
|
2013-08-31 02:21:15 -04:00
|
|
|
p.ProvUi.Say("foo")
|
2013-05-22 18:35:52 -04:00
|
|
|
assert.True(ui.sayCalled, "say should be called")
|
2013-08-31 02:21:15 -04:00
|
|
|
|
|
|
|
// Test Cancel
|
|
|
|
pClient.Cancel()
|
|
|
|
if !p.CancelCalled {
|
|
|
|
t.Fatal("cancel should be called")
|
|
|
|
}
|
2013-05-22 18:35:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProvisioner_Implements(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
var r packer.Provisioner
|
|
|
|
p := Provisioner(nil)
|
|
|
|
|
|
|
|
assert.Implementor(p, &r, "should be a provisioner")
|
|
|
|
}
|