github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/provisioner_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  func TestProvisionerRPC(t *testing.T) {
    11  	assert := asserts.NewTestingAsserts(t, true)
    12  
    13  	// Create the interface to test
    14  	p := new(packer.MockProvisioner)
    15  
    16  	// Start the server
    17  	server := rpc.NewServer()
    18  	RegisterProvisioner(server, p)
    19  	address := serveSingleConn(server)
    20  
    21  	// Create the client over RPC and run some methods to verify it works
    22  	client, err := rpc.Dial("tcp", address)
    23  	assert.Nil(err, "should be able to connect")
    24  
    25  	// Test Prepare
    26  	config := 42
    27  	pClient := Provisioner(client)
    28  	pClient.Prepare(config)
    29  	assert.True(p.PrepCalled, "prepare should be called")
    30  	assert.Equal(p.PrepConfigs, []interface{}{42}, "prepare should be called with right arg")
    31  
    32  	// Test Provision
    33  	ui := &testUi{}
    34  	comm := &packer.MockCommunicator{}
    35  	pClient.Provision(ui, comm)
    36  	assert.True(p.ProvCalled, "provision should be called")
    37  
    38  	p.ProvUi.Say("foo")
    39  	assert.True(ui.sayCalled, "say should be called")
    40  
    41  	// Test Cancel
    42  	pClient.Cancel()
    43  	if !p.CancelCalled {
    44  		t.Fatal("cancel should be called")
    45  	}
    46  }
    47  
    48  func TestProvisioner_Implements(t *testing.T) {
    49  	assert := asserts.NewTestingAsserts(t, true)
    50  
    51  	var r packer.Provisioner
    52  	p := Provisioner(nil)
    53  
    54  	assert.Implementor(p, &r, "should be a provisioner")
    55  }