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

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"net/rpc"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  var testPostProcessorArtifact = new(testArtifact)
    11  
    12  type TestPostProcessor struct {
    13  	configCalled bool
    14  	configVal    []interface{}
    15  	ppCalled     bool
    16  	ppArtifact   packer.Artifact
    17  	ppUi         packer.Ui
    18  }
    19  
    20  func (pp *TestPostProcessor) Configure(v ...interface{}) error {
    21  	pp.configCalled = true
    22  	pp.configVal = v
    23  	return nil
    24  }
    25  
    26  func (pp *TestPostProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) {
    27  	pp.ppCalled = true
    28  	pp.ppArtifact = a
    29  	pp.ppUi = ui
    30  	return testPostProcessorArtifact, false, nil
    31  }
    32  
    33  func TestPostProcessorRPC(t *testing.T) {
    34  	// Create the interface to test
    35  	p := new(TestPostProcessor)
    36  
    37  	// Start the server
    38  	server := rpc.NewServer()
    39  	RegisterPostProcessor(server, p)
    40  	address := serveSingleConn(server)
    41  
    42  	// Create the client over RPC and run some methods to verify it works
    43  	client, err := rpc.Dial("tcp", address)
    44  	if err != nil {
    45  		t.Fatalf("Error connecting to rpc: %s", err)
    46  	}
    47  
    48  	// Test Configure
    49  	config := 42
    50  	pClient := PostProcessor(client)
    51  	err = pClient.Configure(config)
    52  	if err != nil {
    53  		t.Fatalf("error: %s", err)
    54  	}
    55  
    56  	if !p.configCalled {
    57  		t.Fatal("config should be called")
    58  	}
    59  
    60  	if !reflect.DeepEqual(p.configVal, []interface{}{42}) {
    61  		t.Fatalf("unknown config value: %#v", p.configVal)
    62  	}
    63  
    64  	// Test PostProcess
    65  	a := new(testArtifact)
    66  	ui := new(testUi)
    67  	artifact, _, err := pClient.PostProcess(ui, a)
    68  	if err != nil {
    69  		t.Fatalf("err: %s", err)
    70  	}
    71  
    72  	if !p.ppCalled {
    73  		t.Fatal("postprocess should be called")
    74  	}
    75  
    76  	if p.ppArtifact.BuilderId() != "bid" {
    77  		t.Fatal("unknown artifact")
    78  	}
    79  
    80  	if artifact.BuilderId() != "bid" {
    81  		t.Fatal("unknown result artifact")
    82  	}
    83  }
    84  
    85  func TestPostProcessor_Implements(t *testing.T) {
    86  	var raw interface{}
    87  	raw = PostProcessor(nil)
    88  	if _, ok := raw.(packer.PostProcessor); !ok {
    89  		t.Fatal("not a postprocessor")
    90  	}
    91  }