github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/communicator_test.go (about) 1 package rpc 2 3 import ( 4 "bufio" 5 "github.com/mitchellh/packer/packer" 6 "io" 7 "reflect" 8 "testing" 9 ) 10 11 func TestCommunicatorRPC(t *testing.T) { 12 // Create the interface to test 13 c := new(packer.MockCommunicator) 14 15 // Start the server 16 client, server := testClientServer(t) 17 defer client.Close() 18 defer server.Close() 19 server.RegisterCommunicator(c) 20 remote := client.Communicator() 21 22 // The remote command we'll use 23 stdin_r, stdin_w := io.Pipe() 24 stdout_r, stdout_w := io.Pipe() 25 stderr_r, stderr_w := io.Pipe() 26 27 var cmd packer.RemoteCmd 28 cmd.Command = "foo" 29 cmd.Stdin = stdin_r 30 cmd.Stdout = stdout_w 31 cmd.Stderr = stderr_w 32 33 // Send some data on stdout and stderr from the mock 34 c.StartStdout = "outfoo\n" 35 c.StartStderr = "errfoo\n" 36 c.StartExitStatus = 42 37 38 // Test Start 39 err := remote.Start(&cmd) 40 if err != nil { 41 t.Fatalf("err: %s", err) 42 } 43 44 // Test that we can read from stdout 45 bufOut := bufio.NewReader(stdout_r) 46 data, err := bufOut.ReadString('\n') 47 if err != nil { 48 t.Fatalf("err: %s", err) 49 } 50 51 if data != "outfoo\n" { 52 t.Fatalf("bad data: %s", data) 53 } 54 55 // Test that we can read from stderr 56 bufErr := bufio.NewReader(stderr_r) 57 data, err = bufErr.ReadString('\n') 58 if err != nil { 59 t.Fatalf("err: %s", err) 60 } 61 62 if data != "errfoo\n" { 63 t.Fatalf("bad data: %s", data) 64 } 65 66 // Test that we can write to stdin 67 stdin_w.Write([]byte("info\n")) 68 stdin_w.Close() 69 cmd.Wait() 70 if c.StartStdin != "info\n" { 71 t.Fatalf("bad data: %s", c.StartStdin) 72 } 73 74 // Test that we can get the exit status properly 75 if cmd.ExitStatus != 42 { 76 t.Fatalf("bad exit: %d", cmd.ExitStatus) 77 } 78 79 // Test that we can upload things 80 uploadR, uploadW := io.Pipe() 81 go func() { 82 defer uploadW.Close() 83 uploadW.Write([]byte("uploadfoo\n")) 84 }() 85 err = remote.Upload("foo", uploadR, nil) 86 if err != nil { 87 t.Fatalf("err: %s", err) 88 } 89 90 if !c.UploadCalled { 91 t.Fatal("should have uploaded") 92 } 93 94 if c.UploadPath != "foo" { 95 t.Fatalf("path: %s", c.UploadPath) 96 } 97 98 if c.UploadData != "uploadfoo\n" { 99 t.Fatalf("bad: %s", c.UploadData) 100 } 101 102 // Test that we can upload directories 103 dirDst := "foo" 104 dirSrc := "bar" 105 dirExcl := []string{"foo"} 106 err = remote.UploadDir(dirDst, dirSrc, dirExcl) 107 if err != nil { 108 t.Fatalf("err: %s", err) 109 } 110 111 if c.UploadDirDst != dirDst { 112 t.Fatalf("bad: %s", c.UploadDirDst) 113 } 114 115 if c.UploadDirSrc != dirSrc { 116 t.Fatalf("bad: %s", c.UploadDirSrc) 117 } 118 119 if !reflect.DeepEqual(c.UploadDirExclude, dirExcl) { 120 t.Fatalf("bad: %#v", c.UploadDirExclude) 121 } 122 123 // Test that we can download things 124 downloadR, downloadW := io.Pipe() 125 downloadDone := make(chan bool) 126 var downloadData string 127 var downloadErr error 128 129 go func() { 130 bufDownR := bufio.NewReader(downloadR) 131 downloadData, downloadErr = bufDownR.ReadString('\n') 132 downloadDone <- true 133 }() 134 135 c.DownloadData = "download\n" 136 err = remote.Download("bar", downloadW) 137 if err != nil { 138 t.Fatalf("err: %s", err) 139 } 140 141 if !c.DownloadCalled { 142 t.Fatal("download should be called") 143 } 144 145 if c.DownloadPath != "bar" { 146 t.Fatalf("bad: %s", c.DownloadPath) 147 } 148 149 <-downloadDone 150 if downloadErr != nil { 151 t.Fatalf("err: %s", downloadErr) 152 } 153 154 if downloadData != "download\n" { 155 t.Fatalf("bad: %s", downloadData) 156 } 157 } 158 159 func TestCommunicator_ImplementsCommunicator(t *testing.T) { 160 var raw interface{} 161 raw = Communicator(nil) 162 if _, ok := raw.(packer.Communicator); !ok { 163 t.Fatal("should be a Communicator") 164 } 165 }