github.phpd.cn/hashicorp/packer@v1.3.2/packer/rpc/post_processor_test.go (about)

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