github.com/rothwerx/packer@v0.9.0/packer/rpc/post_processor_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  var testPostProcessorArtifact = new(packer.MockArtifact)
    10  
    11  type TestPostProcessor struct {
    12  	configCalled bool
    13  	configVal    []interface{}
    14  	ppCalled     bool
    15  	ppArtifact   packer.Artifact
    16  	ppArtifactId string
    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.ppArtifactId = a.Id()
    30  	pp.ppUi = ui
    31  	return testPostProcessorArtifact, false, nil
    32  }
    33  
    34  func TestPostProcessorRPC(t *testing.T) {
    35  	// Create the interface to test
    36  	p := new(TestPostProcessor)
    37  
    38  	// Start the server
    39  	client, server := testClientServer(t)
    40  	defer client.Close()
    41  	defer server.Close()
    42  	server.RegisterPostProcessor(p)
    43  
    44  	ppClient := client.PostProcessor()
    45  
    46  	// Test Configure
    47  	config := 42
    48  	err := ppClient.Configure(config)
    49  	if err != nil {
    50  		t.Fatalf("error: %s", err)
    51  	}
    52  
    53  	if !p.configCalled {
    54  		t.Fatal("config should be called")
    55  	}
    56  
    57  	expected := []interface{}{int64(42)}
    58  	if !reflect.DeepEqual(p.configVal, expected) {
    59  		t.Fatalf("unknown config value: %#v", p.configVal)
    60  	}
    61  
    62  	// Test PostProcess
    63  	a := &packer.MockArtifact{
    64  		IdValue: "ppTestId",
    65  	}
    66  	ui := new(testUi)
    67  	artifact, _, err := ppClient.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.ppArtifactId != "ppTestId" {
    77  		t.Fatalf("unknown artifact: %s", p.ppArtifact.Id())
    78  	}
    79  
    80  	if artifact.Id() != "id" {
    81  		t.Fatalf("unknown artifact: %s", artifact.Id())
    82  	}
    83  }
    84  
    85  func TestPostProcessor_Implements(t *testing.T) {
    86  	var raw interface{}
    87  	raw = new(postProcessor)
    88  	if _, ok := raw.(packer.PostProcessor); !ok {
    89  		t.Fatal("not a postprocessor")
    90  	}
    91  }