github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/environment_test.go (about) 1 package rpc 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "reflect" 6 "testing" 7 ) 8 9 var testEnvBuilder = &packer.MockBuilder{} 10 var testEnvCache = &testCache{} 11 var testEnvUi = &testUi{} 12 13 type testEnvironment struct { 14 builderCalled bool 15 builderName string 16 cliCalled bool 17 cliArgs []string 18 hookCalled bool 19 hookName string 20 ppCalled bool 21 ppName string 22 provCalled bool 23 provName string 24 uiCalled bool 25 } 26 27 func (e *testEnvironment) Builder(name string) (packer.Builder, error) { 28 e.builderCalled = true 29 e.builderName = name 30 return testEnvBuilder, nil 31 } 32 33 func (e *testEnvironment) Cache() packer.Cache { 34 return testEnvCache 35 } 36 37 func (e *testEnvironment) Cli(args []string) (int, error) { 38 e.cliCalled = true 39 e.cliArgs = args 40 return 42, nil 41 } 42 43 func (e *testEnvironment) Hook(name string) (packer.Hook, error) { 44 e.hookCalled = true 45 e.hookName = name 46 return nil, nil 47 } 48 49 func (e *testEnvironment) PostProcessor(name string) (packer.PostProcessor, error) { 50 e.ppCalled = true 51 e.ppName = name 52 return nil, nil 53 } 54 55 func (e *testEnvironment) Provisioner(name string) (packer.Provisioner, error) { 56 e.provCalled = true 57 e.provName = name 58 return nil, nil 59 } 60 61 func (e *testEnvironment) Ui() packer.Ui { 62 e.uiCalled = true 63 return testEnvUi 64 } 65 66 func TestEnvironmentRPC(t *testing.T) { 67 // Create the interface to test 68 e := &testEnvironment{} 69 70 // Start the server 71 client, server := testClientServer(t) 72 defer client.Close() 73 defer server.Close() 74 server.RegisterEnvironment(e) 75 eClient := client.Environment() 76 77 // Test Builder 78 builder, _ := eClient.Builder("foo") 79 if !e.builderCalled { 80 t.Fatal("builder should be called") 81 } 82 if e.builderName != "foo" { 83 t.Fatalf("bad: %#v", e.builderName) 84 } 85 86 builder.Prepare(nil) 87 if !testEnvBuilder.PrepareCalled { 88 t.Fatal("should be called") 89 } 90 91 // Test Cache 92 cache := eClient.Cache() 93 cache.Lock("foo") 94 if !testEnvCache.lockCalled { 95 t.Fatal("should be called") 96 } 97 98 // Test Cli 99 cliArgs := []string{"foo", "bar"} 100 result, _ := eClient.Cli(cliArgs) 101 if !e.cliCalled { 102 t.Fatal("should be called") 103 } 104 if !reflect.DeepEqual(e.cliArgs, cliArgs) { 105 t.Fatalf("bad: %#v", e.cliArgs) 106 } 107 if result != 42 { 108 t.Fatalf("bad: %#v", result) 109 } 110 111 // Test Provisioner 112 _, _ = eClient.Provisioner("foo") 113 if !e.provCalled { 114 t.Fatal("should be called") 115 } 116 if e.provName != "foo" { 117 t.Fatalf("bad: %s", e.provName) 118 } 119 120 // Test Ui 121 ui := eClient.Ui() 122 if !e.uiCalled { 123 t.Fatal("should be called") 124 } 125 126 // Test calls on the Ui 127 ui.Say("format") 128 if !testEnvUi.sayCalled { 129 t.Fatal("should be called") 130 } 131 if testEnvUi.sayMessage != "format" { 132 t.Fatalf("bad: %#v", testEnvUi.sayMessage) 133 } 134 } 135 136 func TestEnvironment_ImplementsEnvironment(t *testing.T) { 137 var _ packer.Environment = new(Environment) 138 }