github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/artifact_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  type testArtifact struct{}
    11  
    12  func (testArtifact) BuilderId() string {
    13  	return "bid"
    14  }
    15  
    16  func (testArtifact) Files() []string {
    17  	return []string{"a", "b"}
    18  }
    19  
    20  func (testArtifact) Id() string {
    21  	return "id"
    22  }
    23  
    24  func (testArtifact) String() string {
    25  	return "string"
    26  }
    27  
    28  func (testArtifact) Destroy() error {
    29  	return nil
    30  }
    31  
    32  func TestArtifactRPC(t *testing.T) {
    33  	assert := asserts.NewTestingAsserts(t, true)
    34  
    35  	// Create the interface to test
    36  	a := new(testArtifact)
    37  
    38  	// Start the server
    39  	server := rpc.NewServer()
    40  	RegisterArtifact(server, a)
    41  	address := serveSingleConn(server)
    42  
    43  	// Create the client over RPC and run some methods to verify it works
    44  	client, err := rpc.Dial("tcp", address)
    45  	assert.Nil(err, "should be able to connect")
    46  	aClient := Artifact(client)
    47  
    48  	// Test
    49  	assert.Equal(aClient.BuilderId(), "bid", "should have correct builder ID")
    50  	assert.Equal(aClient.Files(), []string{"a", "b"}, "should have correct builder ID")
    51  	assert.Equal(aClient.Id(), "id", "should have correct builder ID")
    52  	assert.Equal(aClient.String(), "string", "should have correct builder ID")
    53  }
    54  
    55  func TestArtifact_Implements(t *testing.T) {
    56  	assert := asserts.NewTestingAsserts(t, true)
    57  
    58  	var r packer.Artifact
    59  	a := Artifact(nil)
    60  
    61  	assert.Implementor(a, &r, "should be an Artifact")
    62  }