2013-05-12 17:47:55 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"io"
|
|
|
|
"net/rpc"
|
|
|
|
"testing"
|
2013-06-03 02:21:55 -04:00
|
|
|
"time"
|
2013-05-12 17:47:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type testCommunicator struct {
|
|
|
|
startCalled bool
|
2013-06-03 02:08:40 -04:00
|
|
|
startCmd *packer.RemoteCmd
|
2013-05-12 19:48:46 -04:00
|
|
|
|
|
|
|
uploadCalled bool
|
2013-05-20 19:50:35 -04:00
|
|
|
uploadPath string
|
|
|
|
uploadData string
|
2013-05-12 20:09:14 -04:00
|
|
|
|
|
|
|
downloadCalled bool
|
2013-05-20 19:50:35 -04:00
|
|
|
downloadPath string
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 02:08:40 -04:00
|
|
|
func (t *testCommunicator) Start(cmd *packer.RemoteCmd) error {
|
2013-05-12 17:47:55 -04:00
|
|
|
t.startCalled = true
|
|
|
|
t.startCmd = cmd
|
2013-06-03 02:08:40 -04:00
|
|
|
return nil
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
func (t *testCommunicator) Upload(path string, reader io.Reader) (err error) {
|
2013-05-12 19:48:46 -04:00
|
|
|
t.uploadCalled = true
|
|
|
|
t.uploadPath = path
|
2013-05-12 20:09:14 -04:00
|
|
|
t.uploadData, err = bufio.NewReader(reader).ReadString('\n')
|
|
|
|
return
|
2013-05-12 19:25:40 -04:00
|
|
|
}
|
2013-05-12 17:47:55 -04:00
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
func (t *testCommunicator) Download(path string, writer io.Writer) error {
|
|
|
|
t.downloadCalled = true
|
|
|
|
t.downloadPath = path
|
|
|
|
writer.Write([]byte("download\n"))
|
|
|
|
|
2013-05-12 19:25:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2013-05-12 17:47:55 -04:00
|
|
|
|
|
|
|
func TestCommunicatorRPC(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// Create the interface to test
|
|
|
|
c := new(testCommunicator)
|
|
|
|
|
|
|
|
// Start the server
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterCommunicator(server, c)
|
|
|
|
address := serveSingleConn(server)
|
|
|
|
|
|
|
|
// Create the client over RPC and run some methods to verify it works
|
|
|
|
client, err := rpc.Dial("tcp", address)
|
|
|
|
assert.Nil(err, "should be able to connect")
|
2013-06-03 02:08:40 -04:00
|
|
|
remote := Communicator(client)
|
|
|
|
|
|
|
|
// 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 packer.RemoteCmd
|
|
|
|
cmd.Command = "foo"
|
|
|
|
cmd.Stdin = stdin_r
|
|
|
|
cmd.Stdout = stdout_w
|
|
|
|
cmd.Stderr = stderr_w
|
2013-05-12 17:47:55 -04:00
|
|
|
|
|
|
|
// Test Start
|
2013-06-03 02:08:40 -04:00
|
|
|
err = remote.Start(&cmd)
|
2013-05-12 17:47:55 -04:00
|
|
|
assert.Nil(err, "should not have an error")
|
|
|
|
|
|
|
|
// Test that we can read from stdout
|
2013-06-03 02:08:40 -04:00
|
|
|
c.startCmd.Stdout.Write([]byte("outfoo\n"))
|
|
|
|
bufOut := bufio.NewReader(stdout_r)
|
2013-05-12 17:47:55 -04:00
|
|
|
data, err := bufOut.ReadString('\n')
|
|
|
|
assert.Nil(err, "should have no problem reading stdout")
|
|
|
|
assert.Equal(data, "outfoo\n", "should be correct stdout")
|
|
|
|
|
|
|
|
// Test that we can read from stderr
|
2013-06-03 02:08:40 -04:00
|
|
|
c.startCmd.Stderr.Write([]byte("errfoo\n"))
|
|
|
|
bufErr := bufio.NewReader(stderr_r)
|
2013-05-12 17:47:55 -04:00
|
|
|
data, err = bufErr.ReadString('\n')
|
2013-06-03 02:08:40 -04:00
|
|
|
assert.Nil(err, "should have no problem reading stderr")
|
|
|
|
assert.Equal(data, "errfoo\n", "should be correct stderr")
|
2013-05-12 17:47:55 -04:00
|
|
|
|
|
|
|
// Test that we can write to stdin
|
2013-06-03 02:08:40 -04:00
|
|
|
stdin_w.Write([]byte("infoo\n"))
|
|
|
|
bufIn := bufio.NewReader(c.startCmd.Stdin)
|
2013-05-12 17:47:55 -04:00
|
|
|
data, err = bufIn.ReadString('\n')
|
|
|
|
assert.Nil(err, "should have no problem reading stdin")
|
|
|
|
assert.Equal(data, "infoo\n", "should be correct stdin")
|
2013-05-12 19:18:17 -04:00
|
|
|
|
|
|
|
// Test that we can get the exit status properly
|
2013-06-03 02:21:55 -04:00
|
|
|
c.startCmd.ExitStatus = 42
|
|
|
|
c.startCmd.Exited = true
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
if cmd.Exited {
|
|
|
|
assert.Equal(cmd.ExitStatus, 42, "should have proper exit status")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(cmd.Exited, "should have exited")
|
2013-05-12 19:48:46 -04:00
|
|
|
|
|
|
|
// Test that we can upload things
|
|
|
|
uploadR, uploadW := io.Pipe()
|
2013-05-12 20:09:14 -04:00
|
|
|
go uploadW.Write([]byte("uploadfoo\n"))
|
2013-05-12 19:48:46 -04:00
|
|
|
err = remote.Upload("foo", uploadR)
|
|
|
|
assert.Nil(err, "should not error")
|
|
|
|
assert.True(c.uploadCalled, "should be called")
|
|
|
|
assert.Equal(c.uploadPath, "foo", "should be correct path")
|
2013-05-12 20:09:14 -04:00
|
|
|
assert.Equal(c.uploadData, "uploadfoo\n", "should have the proper data")
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2013-05-12 20:09:14 -04:00
|
|
|
go func() {
|
|
|
|
bufDownR := bufio.NewReader(downloadR)
|
|
|
|
downloadData, downloadErr = bufDownR.ReadString('\n')
|
|
|
|
downloadDone <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = remote.Download("bar", downloadW)
|
2013-05-12 19:48:46 -04:00
|
|
|
assert.Nil(err, "should not error")
|
2013-05-12 20:09:14 -04:00
|
|
|
assert.True(c.downloadCalled, "should have called download")
|
|
|
|
assert.Equal(c.downloadPath, "bar", "should have correct download path")
|
|
|
|
|
|
|
|
<-downloadDone
|
|
|
|
assert.Nil(downloadErr, "should not error reading download data")
|
|
|
|
assert.Equal(downloadData, "download\n", "should have the proper data")
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommunicator_ImplementsCommunicator(t *testing.T) {
|
2013-05-12 19:19:34 -04:00
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
2013-05-12 17:47:55 -04:00
|
|
|
|
2013-05-12 19:19:34 -04:00
|
|
|
var r packer.Communicator
|
|
|
|
c := Communicator(nil)
|
2013-05-12 17:47:55 -04:00
|
|
|
|
2013-05-12 19:19:34 -04:00
|
|
|
assert.Implementor(c, &r, "should be a Communicator")
|
2013-05-12 17:47:55 -04:00
|
|
|
}
|