github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/builder_test.go (about) 1 package rpc 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "reflect" 6 "testing" 7 ) 8 9 var testBuilderArtifact = &packer.MockArtifact{} 10 11 func TestBuilderPrepare(t *testing.T) { 12 b := new(packer.MockBuilder) 13 client, server := testClientServer(t) 14 defer client.Close() 15 defer server.Close() 16 server.RegisterBuilder(b) 17 bClient := client.Builder() 18 19 // Test Prepare 20 config := 42 21 warnings, err := bClient.Prepare(config) 22 if err != nil { 23 t.Fatalf("bad: %s", err) 24 } 25 if len(warnings) > 0 { 26 t.Fatalf("bad: %#v", warnings) 27 } 28 29 if !b.PrepareCalled { 30 t.Fatal("should be called") 31 } 32 33 expected := []interface{}{int64(42)} 34 if !reflect.DeepEqual(b.PrepareConfig, expected) { 35 t.Fatalf("bad: %#v != %#v", b.PrepareConfig, expected) 36 } 37 } 38 39 func TestBuilderPrepare_Warnings(t *testing.T) { 40 b := new(packer.MockBuilder) 41 client, server := testClientServer(t) 42 defer client.Close() 43 defer server.Close() 44 server.RegisterBuilder(b) 45 bClient := client.Builder() 46 47 expected := []string{"foo"} 48 b.PrepareWarnings = expected 49 50 // Test Prepare 51 warnings, err := bClient.Prepare(nil) 52 if err != nil { 53 t.Fatalf("bad: %s", err) 54 } 55 if !reflect.DeepEqual(warnings, expected) { 56 t.Fatalf("bad: %#v", warnings) 57 } 58 } 59 60 func TestBuilderRun(t *testing.T) { 61 b := new(packer.MockBuilder) 62 client, server := testClientServer(t) 63 defer client.Close() 64 defer server.Close() 65 server.RegisterBuilder(b) 66 bClient := client.Builder() 67 68 // Test Run 69 cache := new(testCache) 70 hook := &packer.MockHook{} 71 ui := &testUi{} 72 artifact, err := bClient.Run(ui, hook, cache) 73 if err != nil { 74 t.Fatalf("err: %s", err) 75 } 76 77 if !b.RunCalled { 78 t.Fatal("run should be called") 79 } 80 81 if artifact.Id() != testBuilderArtifact.Id() { 82 t.Fatalf("bad: %s", artifact.Id()) 83 } 84 } 85 86 func TestBuilderRun_nilResult(t *testing.T) { 87 b := new(packer.MockBuilder) 88 b.RunNilResult = true 89 90 client, server := testClientServer(t) 91 defer client.Close() 92 defer server.Close() 93 server.RegisterBuilder(b) 94 bClient := client.Builder() 95 96 cache := new(testCache) 97 hook := &packer.MockHook{} 98 ui := &testUi{} 99 artifact, err := bClient.Run(ui, hook, cache) 100 if artifact != nil { 101 t.Fatalf("bad: %#v", artifact) 102 } 103 if err != nil { 104 t.Fatalf("bad: %#v", err) 105 } 106 } 107 108 func TestBuilderRun_ErrResult(t *testing.T) { 109 b := new(packer.MockBuilder) 110 client, server := testClientServer(t) 111 defer client.Close() 112 defer server.Close() 113 server.RegisterBuilder(b) 114 bClient := client.Builder() 115 116 b.RunErrResult = true 117 118 cache := new(testCache) 119 hook := &packer.MockHook{} 120 ui := &testUi{} 121 artifact, err := bClient.Run(ui, hook, cache) 122 if artifact != nil { 123 t.Fatalf("bad: %#v", artifact) 124 } 125 if err == nil { 126 t.Fatal("should have error") 127 } 128 } 129 130 func TestBuilderCancel(t *testing.T) { 131 b := new(packer.MockBuilder) 132 client, server := testClientServer(t) 133 defer client.Close() 134 defer server.Close() 135 server.RegisterBuilder(b) 136 bClient := client.Builder() 137 138 bClient.Cancel() 139 if !b.CancelCalled { 140 t.Fatal("cancel should be called") 141 } 142 } 143 144 func TestBuilder_ImplementsBuilder(t *testing.T) { 145 var _ packer.Builder = new(builder) 146 }