github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/builder_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"cgl.tideland.biz/asserts"
     5  	"errors"
     6  	"github.com/mitchellh/packer/packer"
     7  	"net/rpc"
     8  	"testing"
     9  )
    10  
    11  var testBuilderArtifact = &testArtifact{}
    12  
    13  type testBuilder struct {
    14  	prepareCalled bool
    15  	prepareConfig []interface{}
    16  	runCalled     bool
    17  	runCache      packer.Cache
    18  	runHook       packer.Hook
    19  	runUi         packer.Ui
    20  	cancelCalled  bool
    21  
    22  	errRunResult bool
    23  	nilRunResult bool
    24  }
    25  
    26  func (b *testBuilder) Prepare(config ...interface{}) error {
    27  	b.prepareCalled = true
    28  	b.prepareConfig = config
    29  	return nil
    30  }
    31  
    32  func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
    33  	b.runCache = cache
    34  	b.runCalled = true
    35  	b.runHook = hook
    36  	b.runUi = ui
    37  
    38  	if b.errRunResult {
    39  		return nil, errors.New("foo")
    40  	} else if b.nilRunResult {
    41  		return nil, nil
    42  	} else {
    43  		return testBuilderArtifact, nil
    44  	}
    45  }
    46  
    47  func (b *testBuilder) Cancel() {
    48  	b.cancelCalled = true
    49  }
    50  
    51  func TestBuilderRPC(t *testing.T) {
    52  	assert := asserts.NewTestingAsserts(t, true)
    53  
    54  	// Create the interface to test
    55  	b := new(testBuilder)
    56  
    57  	// Start the server
    58  	server := rpc.NewServer()
    59  	RegisterBuilder(server, b)
    60  	address := serveSingleConn(server)
    61  
    62  	// Create the client over RPC and run some methods to verify it works
    63  	client, err := rpc.Dial("tcp", address)
    64  	assert.Nil(err, "should be able to connect")
    65  
    66  	// Test Prepare
    67  	config := 42
    68  	bClient := Builder(client)
    69  	bClient.Prepare(config)
    70  	assert.True(b.prepareCalled, "prepare should be called")
    71  	assert.Equal(b.prepareConfig, []interface{}{42}, "prepare should be called with right arg")
    72  
    73  	// Test Run
    74  	cache := new(testCache)
    75  	hook := &packer.MockHook{}
    76  	ui := &testUi{}
    77  	artifact, err := bClient.Run(ui, hook, cache)
    78  	assert.Nil(err, "should have no error")
    79  	assert.True(b.runCalled, "runs hould be called")
    80  
    81  	if b.runCalled {
    82  		b.runCache.Lock("foo")
    83  		assert.True(cache.lockCalled, "lock should be called")
    84  
    85  		b.runHook.Run("foo", nil, nil, nil)
    86  		assert.True(hook.RunCalled, "run should be called")
    87  
    88  		b.runUi.Say("format")
    89  		assert.True(ui.sayCalled, "say should be called")
    90  		assert.Equal(ui.sayMessage, "format", "message should be correct")
    91  
    92  		assert.Equal(artifact.Id(), testBuilderArtifact.Id(), "should have artifact Id")
    93  	}
    94  
    95  	// Test run with nil result
    96  	b.nilRunResult = true
    97  	artifact, err = bClient.Run(ui, hook, cache)
    98  	assert.Nil(artifact, "should be nil")
    99  	assert.Nil(err, "should have no error")
   100  
   101  	// Test with an error
   102  	b.errRunResult = true
   103  	b.nilRunResult = false
   104  	artifact, err = bClient.Run(ui, hook, cache)
   105  	assert.Nil(artifact, "should be nil")
   106  	assert.NotNil(err, "should have error")
   107  
   108  	// Test Cancel
   109  	bClient.Cancel()
   110  	assert.True(b.cancelCalled, "cancel should be called")
   111  }
   112  
   113  func TestBuilder_ImplementsBuilder(t *testing.T) {
   114  	assert := asserts.NewTestingAsserts(t, true)
   115  
   116  	var realBuilder packer.Builder
   117  	b := Builder(nil)
   118  
   119  	assert.Implementor(b, &realBuilder, "should be a Builder")
   120  }