packer: Add UploadDir to the Communicator interface

This commit is contained in:
Mitchell Hashimoto 2013-08-23 17:05:06 -07:00
parent d6824a728f
commit 46e02209a9
4 changed files with 60 additions and 32 deletions

View File

@ -59,6 +59,11 @@ type Communicator interface {
// it completes.
Upload(string, io.Reader) error
// UploadDir uploads the contents of a directory recursively to
// the remote path. It also takes an optional slice of paths to
// ignore when uploading.
UploadDir(string, string, []string) error
// Download downloads a file from the machine from the given remote path
// with the contents writing to the given writer. This method will
// block until it completes.

View File

@ -0,0 +1,41 @@
package packer
import (
"io"
)
// MockCommunicator is a valid Communicator implementation that can be
// used for tests.
type MockCommunicator struct {
Stderr io.Reader
Stdout io.Reader
}
func (c *MockCommunicator) Start(rc *RemoteCmd) error {
go func() {
rc.Lock()
defer rc.Unlock()
if rc.Stdout != nil && c.Stdout != nil {
io.Copy(rc.Stdout, c.Stdout)
}
if rc.Stderr != nil && c.Stderr != nil {
io.Copy(rc.Stderr, c.Stderr)
}
}()
return nil
}
func (c *MockCommunicator) Upload(string, io.Reader) error {
return nil
}
func (c *MockCommunicator) UploadDir(string, string, []string) error {
return nil
}
func (c *MockCommunicator) Download(string, io.Writer) error {
return nil
}

View File

@ -0,0 +1,13 @@
package packer
import (
"testing"
)
func TestMockCommunicator_impl(t *testing.T) {
var raw interface{}
raw = new(MockCommunicator)
if _, ok := raw.(Communicator); !ok {
t.Fatal("should be a communicator")
}
}

View File

@ -2,42 +2,11 @@ package packer
import (
"bytes"
"io"
"strings"
"testing"
"time"
)
type TestCommunicator struct {
Stderr io.Reader
Stdout io.Reader
}
func (c *TestCommunicator) Start(rc *RemoteCmd) error {
go func() {
rc.Lock()
defer rc.Unlock()
if rc.Stdout != nil && c.Stdout != nil {
io.Copy(rc.Stdout, c.Stdout)
}
if rc.Stderr != nil && c.Stderr != nil {
io.Copy(rc.Stderr, c.Stderr)
}
}()
return nil
}
func (c *TestCommunicator) Upload(string, io.Reader) error {
return nil
}
func (c *TestCommunicator) Download(string, io.Writer) error {
return nil
}
func TestRemoteCmd_StartWithUi(t *testing.T) {
data := "hello\nworld\nthere"
@ -46,7 +15,7 @@ func TestRemoteCmd_StartWithUi(t *testing.T) {
uiOutput := new(bytes.Buffer)
rcOutput.WriteString(data)
testComm := &TestCommunicator{
testComm := &MockCommunicator{
Stdout: rcOutput,
}