github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/environment_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  var testEnvBuilder = &testBuilder{}
    11  var testEnvCache = &testCache{}
    12  var testEnvUi = &testUi{}
    13  
    14  type testEnvironment struct {
    15  	builderCalled bool
    16  	builderName   string
    17  	cliCalled     bool
    18  	cliArgs       []string
    19  	hookCalled    bool
    20  	hookName      string
    21  	ppCalled      bool
    22  	ppName        string
    23  	provCalled    bool
    24  	provName      string
    25  	uiCalled      bool
    26  }
    27  
    28  func (e *testEnvironment) Builder(name string) (packer.Builder, error) {
    29  	e.builderCalled = true
    30  	e.builderName = name
    31  	return testEnvBuilder, nil
    32  }
    33  
    34  func (e *testEnvironment) Cache() packer.Cache {
    35  	return testEnvCache
    36  }
    37  
    38  func (e *testEnvironment) Cli(args []string) (int, error) {
    39  	e.cliCalled = true
    40  	e.cliArgs = args
    41  	return 42, nil
    42  }
    43  
    44  func (e *testEnvironment) Hook(name string) (packer.Hook, error) {
    45  	e.hookCalled = true
    46  	e.hookName = name
    47  	return nil, nil
    48  }
    49  
    50  func (e *testEnvironment) PostProcessor(name string) (packer.PostProcessor, error) {
    51  	e.ppCalled = true
    52  	e.ppName = name
    53  	return nil, nil
    54  }
    55  
    56  func (e *testEnvironment) Provisioner(name string) (packer.Provisioner, error) {
    57  	e.provCalled = true
    58  	e.provName = name
    59  	return nil, nil
    60  }
    61  
    62  func (e *testEnvironment) Ui() packer.Ui {
    63  	e.uiCalled = true
    64  	return testEnvUi
    65  }
    66  
    67  func TestEnvironmentRPC(t *testing.T) {
    68  	assert := asserts.NewTestingAsserts(t, true)
    69  
    70  	// Create the interface to test
    71  	e := &testEnvironment{}
    72  
    73  	// Start the server
    74  	server := rpc.NewServer()
    75  	RegisterEnvironment(server, e)
    76  	address := serveSingleConn(server)
    77  
    78  	// Create the client over RPC and run some methods to verify it works
    79  	client, err := rpc.Dial("tcp", address)
    80  	assert.Nil(err, "should be able to connect")
    81  	eClient := &Environment{client}
    82  
    83  	// Test Builder
    84  	builder, _ := eClient.Builder("foo")
    85  	assert.True(e.builderCalled, "Builder should be called")
    86  	assert.Equal(e.builderName, "foo", "Correct name for Builder")
    87  
    88  	builder.Prepare(nil)
    89  	assert.True(testEnvBuilder.prepareCalled, "Prepare should be called")
    90  
    91  	// Test Cache
    92  	cache := eClient.Cache()
    93  	cache.Lock("foo")
    94  	assert.True(testEnvCache.lockCalled, "lock should be called")
    95  
    96  	// Test Cli
    97  	cliArgs := []string{"foo", "bar"}
    98  	result, _ := eClient.Cli(cliArgs)
    99  	assert.True(e.cliCalled, "CLI should be called")
   100  	assert.Equal(e.cliArgs, cliArgs, "args should match")
   101  	assert.Equal(result, 42, "result shuld be 42")
   102  
   103  	// Test Provisioner
   104  	_, _ = eClient.Provisioner("foo")
   105  	assert.True(e.provCalled, "provisioner should be called")
   106  	assert.Equal(e.provName, "foo", "should have proper name")
   107  
   108  	// Test Ui
   109  	ui := eClient.Ui()
   110  	assert.True(e.uiCalled, "Ui should've been called")
   111  
   112  	// Test calls on the Ui
   113  	ui.Say("format")
   114  	assert.True(testEnvUi.sayCalled, "Say should be called")
   115  	assert.Equal(testEnvUi.sayMessage, "format", "message should match")
   116  }
   117  
   118  func TestEnvironment_ImplementsEnvironment(t *testing.T) {
   119  	assert := asserts.NewTestingAsserts(t, true)
   120  
   121  	var realVar packer.Environment
   122  	e := &Environment{nil}
   123  
   124  	assert.Implementor(e, &realVar, "should be an Environment")
   125  }