packer-cn/packer/rpc/communicator_test.go

171 lines
3.4 KiB
Go
Raw Normal View History

2013-05-12 17:47:55 -04:00
package rpc
import (
"bufio"
"context"
2013-05-12 17:47:55 -04:00
"io"
2013-08-23 22:18:15 -04:00
"reflect"
2013-05-12 17:47:55 -04:00
"testing"
2018-01-22 20:21:10 -05:00
"github.com/hashicorp/packer/packer"
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
2013-05-12 17:47:55 -04:00
)
func TestCommunicatorRPC(t *testing.T) {
// Create the interface to test
c := new(packer.MockCommunicator)
2013-05-12 17:47:55 -04:00
// Start the server
2013-12-10 14:43:02 -05:00
client, server := testClientServer(t)
defer client.Close()
defer server.Close()
server.RegisterCommunicator(c)
remote := client.Communicator()
// The remote command we'll use
stdin_r, stdin_w := io.Pipe()
stdout_r, stdout_w := io.Pipe()
stderr_r, stderr_w := io.Pipe()
var cmd packersdk.RemoteCmd
cmd.Command = "foo"
cmd.Stdin = stdin_r
cmd.Stdout = stdout_w
cmd.Stderr = stderr_w
2013-05-12 17:47:55 -04:00
// Send some data on stdout and stderr from the mock
c.StartStdout = "outfoo\n"
c.StartStderr = "errfoo\n"
c.StartExitStatus = 42
ctx := context.Background()
2013-05-12 17:47:55 -04:00
// Test Start
err := remote.Start(ctx, &cmd)
if err != nil {
t.Fatalf("err: %s", err)
}
2013-05-12 17:47:55 -04:00
// Test that we can read from stdout
bufOut := bufio.NewReader(stdout_r)
2013-05-12 17:47:55 -04:00
data, err := bufOut.ReadString('\n')
if err != nil {
t.Fatalf("err: %s", err)
}
if data != "outfoo\n" {
t.Fatalf("bad data: %s", data)
}
2013-05-12 17:47:55 -04:00
// Test that we can read from stderr
bufErr := bufio.NewReader(stderr_r)
2013-05-12 17:47:55 -04:00
data, err = bufErr.ReadString('\n')
if err != nil {
t.Fatalf("err: %s", err)
}
if data != "errfoo\n" {
t.Fatalf("bad data: %s", data)
}
2013-05-12 17:47:55 -04:00
// Test that we can write to stdin
stdin_w.Write([]byte("info\n"))
stdin_w.Close()
cmd.Wait()
if c.StartStdin != "info\n" {
2013-12-10 14:43:02 -05:00
t.Fatalf("bad data: %s", c.StartStdin)
}
// Test that we can get the exit status properly
if cmd.ExitStatus() != 42 {
t.Fatalf("bad exit: %d", cmd.ExitStatus())
}
2013-05-12 19:48:46 -04:00
// Test that we can upload things
uploadR, uploadW := io.Pipe()
go func() {
defer uploadW.Close()
uploadW.Write([]byte("uploadfoo\n"))
}()
err = remote.Upload("foo", uploadR, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if !c.UploadCalled {
t.Fatal("should have uploaded")
}
if c.UploadPath != "foo" {
t.Fatalf("path: %s", c.UploadPath)
}
if c.UploadData != "uploadfoo\n" {
t.Fatalf("bad: %s", c.UploadData)
}
2013-08-23 22:18:15 -04:00
// Test that we can upload directories
dirDst := "foo"
dirSrc := "bar"
dirExcl := []string{"foo"}
err = remote.UploadDir(dirDst, dirSrc, dirExcl)
if err != nil {
t.Fatalf("err: %s", err)
}
if c.UploadDirDst != dirDst {
t.Fatalf("bad: %s", c.UploadDirDst)
}
if c.UploadDirSrc != dirSrc {
t.Fatalf("bad: %s", c.UploadDirSrc)
}
if !reflect.DeepEqual(c.UploadDirExclude, dirExcl) {
t.Fatalf("bad: %#v", c.UploadDirExclude)
}
// Test that we can download things
downloadR, downloadW := io.Pipe()
downloadDone := make(chan bool)
var downloadData string
var downloadErr error
2013-05-12 19:48:46 -04:00
go func() {
bufDownR := bufio.NewReader(downloadR)
downloadData, downloadErr = bufDownR.ReadString('\n')
downloadDone <- true
}()
c.DownloadData = "download\n"
err = remote.Download("bar", downloadW)
if err != nil {
t.Fatalf("err: %s", err)
}
if !c.DownloadCalled {
t.Fatal("download should be called")
}
if c.DownloadPath != "bar" {
t.Fatalf("bad: %s", c.DownloadPath)
}
<-downloadDone
if downloadErr != nil {
t.Fatalf("err: %s", downloadErr)
}
if downloadData != "download\n" {
t.Fatalf("bad: %s", downloadData)
}
2013-05-12 17:47:55 -04:00
}
func TestCommunicator_ImplementsCommunicator(t *testing.T) {
var raw interface{}
raw = Communicator(nil)
if _, ok := raw.(packersdk.Communicator); !ok {
t.Fatal("should be a Communicator")
}
2013-05-12 17:47:55 -04:00
}