github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/build_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 testBuildArtifact = &testArtifact{}
    12  
    13  type testBuild struct {
    14  	nameCalled     bool
    15  	prepareCalled  bool
    16  	prepareVars    map[string]string
    17  	runCalled      bool
    18  	runCache       packer.Cache
    19  	runUi          packer.Ui
    20  	setDebugCalled bool
    21  	setForceCalled bool
    22  	cancelCalled   bool
    23  
    24  	errRunResult bool
    25  }
    26  
    27  func (b *testBuild) Name() string {
    28  	b.nameCalled = true
    29  	return "name"
    30  }
    31  
    32  func (b *testBuild) Prepare(v map[string]string) error {
    33  	b.prepareCalled = true
    34  	b.prepareVars = v
    35  	return nil
    36  }
    37  
    38  func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) ([]packer.Artifact, error) {
    39  	b.runCalled = true
    40  	b.runCache = cache
    41  	b.runUi = ui
    42  
    43  	if b.errRunResult {
    44  		return nil, errors.New("foo")
    45  	} else {
    46  		return []packer.Artifact{testBuildArtifact}, nil
    47  	}
    48  }
    49  
    50  func (b *testBuild) SetDebug(bool) {
    51  	b.setDebugCalled = true
    52  }
    53  
    54  func (b *testBuild) SetForce(bool) {
    55  	b.setForceCalled = true
    56  }
    57  
    58  func (b *testBuild) Cancel() {
    59  	b.cancelCalled = true
    60  }
    61  
    62  func TestBuildRPC(t *testing.T) {
    63  	assert := asserts.NewTestingAsserts(t, true)
    64  
    65  	// Create the interface to test
    66  	b := new(testBuild)
    67  
    68  	// Start the server
    69  	server := rpc.NewServer()
    70  	RegisterBuild(server, b)
    71  	address := serveSingleConn(server)
    72  
    73  	// Create the client over RPC and run some methods to verify it works
    74  	client, err := rpc.Dial("tcp", address)
    75  	assert.Nil(err, "should be able to connect")
    76  	bClient := Build(client)
    77  
    78  	// Test Name
    79  	bClient.Name()
    80  	assert.True(b.nameCalled, "name should be called")
    81  
    82  	// Test Prepare
    83  	bClient.Prepare(map[string]string{"foo": "bar"})
    84  	assert.True(b.prepareCalled, "prepare should be called")
    85  	if len(b.prepareVars) != 1 {
    86  		t.Fatalf("bad vars: %#v", b.prepareVars)
    87  	}
    88  
    89  	if b.prepareVars["foo"] != "bar" {
    90  		t.Fatalf("bad vars: %#v", b.prepareVars)
    91  	}
    92  
    93  	// Test Run
    94  	cache := new(testCache)
    95  	ui := new(testUi)
    96  	artifacts, err := bClient.Run(ui, cache)
    97  	assert.True(b.runCalled, "run should be called")
    98  	assert.Nil(err, "should not error")
    99  	assert.Equal(len(artifacts), 1, "should have one artifact")
   100  	assert.Equal(artifacts[0].BuilderId(), "bid", "should have proper builder id")
   101  
   102  	// Test the UI given to run, which should be fully functional
   103  	if b.runCalled {
   104  		b.runCache.Lock("foo")
   105  		assert.True(cache.lockCalled, "lock should be called")
   106  
   107  		b.runUi.Say("format")
   108  		assert.True(ui.sayCalled, "say should be called")
   109  		assert.Equal(ui.sayMessage, "format", "message should be correct")
   110  	}
   111  
   112  	// Test run with an error
   113  	b.errRunResult = true
   114  	_, err = bClient.Run(ui, cache)
   115  	assert.NotNil(err, "should not nil")
   116  
   117  	// Test SetDebug
   118  	bClient.SetDebug(true)
   119  	assert.True(b.setDebugCalled, "should be called")
   120  
   121  	// Test SetForce
   122  	bClient.SetForce(true)
   123  	assert.True(b.setForceCalled, "should be called")
   124  
   125  	// Test Cancel
   126  	bClient.Cancel()
   127  	assert.True(b.cancelCalled, "cancel should be called")
   128  }
   129  
   130  func TestBuild_ImplementsBuild(t *testing.T) {
   131  	assert := asserts.NewTestingAsserts(t, true)
   132  
   133  	var realBuild packer.Build
   134  	b := Build(nil)
   135  
   136  	assert.Implementor(b, &realBuild, "should be a Build")
   137  }