packer: Add UploadDir to the Communicator interface
This commit is contained in:
parent
e589bf2724
commit
f5c7c6ea90
|
@ -59,6 +59,11 @@ type Communicator interface {
|
||||||
// it completes.
|
// it completes.
|
||||||
Upload(string, io.Reader) error
|
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
|
// Download downloads a file from the machine from the given remote path
|
||||||
// with the contents writing to the given writer. This method will
|
// with the contents writing to the given writer. This method will
|
||||||
// block until it completes.
|
// block until it completes.
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,42 +2,11 @@ package packer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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) {
|
func TestRemoteCmd_StartWithUi(t *testing.T) {
|
||||||
data := "hello\nworld\nthere"
|
data := "hello\nworld\nthere"
|
||||||
|
|
||||||
|
@ -46,7 +15,7 @@ func TestRemoteCmd_StartWithUi(t *testing.T) {
|
||||||
uiOutput := new(bytes.Buffer)
|
uiOutput := new(bytes.Buffer)
|
||||||
rcOutput.WriteString(data)
|
rcOutput.WriteString(data)
|
||||||
|
|
||||||
testComm := &TestCommunicator{
|
testComm := &MockCommunicator{
|
||||||
Stdout: rcOutput,
|
Stdout: rcOutput,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue