github.com/jbronn/packer@v0.1.6-0.20140120165540-8a1364dbd817/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 if !reflect.DeepEqual(p.configVal, []interface{}{42}) { 58 t.Fatalf("unknown config value: %#v", p.configVal) 59 } 60 61 // Test PostProcess 62 a := &packer.MockArtifact{ 63 IdValue: "ppTestId", 64 } 65 ui := new(testUi) 66 artifact, _, err := ppClient.PostProcess(ui, a) 67 if err != nil { 68 t.Fatalf("err: %s", err) 69 } 70 71 if !p.ppCalled { 72 t.Fatal("postprocess should be called") 73 } 74 75 if p.ppArtifactId != "ppTestId" { 76 t.Fatalf("unknown artifact: %s", p.ppArtifact.Id()) 77 } 78 79 if artifact.Id() != "id" { 80 t.Fatalf("unknown artifact: %s", artifact.Id()) 81 } 82 } 83 84 func TestPostProcessor_Implements(t *testing.T) { 85 var raw interface{} 86 raw = new(postProcessor) 87 if _, ok := raw.(packer.PostProcessor); !ok { 88 t.Fatal("not a postprocessor") 89 } 90 }