2013-08-23 20:05:06 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
2013-08-23 21:29:46 -04:00
|
|
|
"bytes"
|
2013-08-23 20:05:06 -04:00
|
|
|
"io"
|
2014-05-10 00:03:35 -04:00
|
|
|
"os"
|
2013-08-23 21:29:46 -04:00
|
|
|
"sync"
|
2013-08-23 20:05:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// MockCommunicator is a valid Communicator implementation that can be
|
|
|
|
// used for tests.
|
|
|
|
type MockCommunicator struct {
|
2013-08-23 21:29:46 -04:00
|
|
|
StartCalled bool
|
|
|
|
StartCmd *RemoteCmd
|
|
|
|
StartStderr string
|
|
|
|
StartStdout string
|
|
|
|
StartStdin string
|
|
|
|
StartExitStatus int
|
|
|
|
|
|
|
|
UploadCalled bool
|
|
|
|
UploadPath string
|
|
|
|
UploadData string
|
|
|
|
|
2013-08-23 22:18:15 -04:00
|
|
|
UploadDirDst string
|
|
|
|
UploadDirSrc string
|
|
|
|
UploadDirExclude []string
|
|
|
|
|
2015-11-02 06:22:52 -05:00
|
|
|
DownloadDirDst string
|
|
|
|
DownloadDirSrc string
|
|
|
|
DownloadDirExclude []string
|
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
DownloadCalled bool
|
|
|
|
DownloadPath string
|
|
|
|
DownloadData string
|
2013-08-23 20:05:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockCommunicator) Start(rc *RemoteCmd) error {
|
2013-08-23 21:29:46 -04:00
|
|
|
c.StartCalled = true
|
|
|
|
c.StartCmd = rc
|
|
|
|
|
2013-08-23 20:05:06 -04:00
|
|
|
go func() {
|
2013-08-23 21:29:46 -04:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
if rc.Stdout != nil && c.StartStdout != "" {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
rc.Stdout.Write([]byte(c.StartStdout))
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
2013-08-23 20:05:06 -04:00
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
if rc.Stderr != nil && c.StartStderr != "" {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
rc.Stderr.Write([]byte(c.StartStderr))
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2013-08-23 20:05:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
if rc.Stdin != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
var data bytes.Buffer
|
|
|
|
io.Copy(&data, rc.Stdin)
|
|
|
|
c.StartStdin = data.String()
|
|
|
|
}()
|
2013-08-23 20:05:06 -04:00
|
|
|
}
|
2013-08-23 21:29:46 -04:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
rc.SetExited(c.StartExitStatus)
|
2013-08-23 20:05:06 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-10 00:03:35 -04:00
|
|
|
func (c *MockCommunicator) Upload(path string, r io.Reader, fi *os.FileInfo) error {
|
2013-08-23 21:29:46 -04:00
|
|
|
c.UploadCalled = true
|
|
|
|
c.UploadPath = path
|
|
|
|
|
|
|
|
var data bytes.Buffer
|
|
|
|
if _, err := io.Copy(&data, r); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UploadData = data.String()
|
|
|
|
|
2013-08-23 20:05:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 22:18:15 -04:00
|
|
|
func (c *MockCommunicator) UploadDir(dst string, src string, excl []string) error {
|
|
|
|
c.UploadDirDst = dst
|
|
|
|
c.UploadDirSrc = src
|
|
|
|
c.UploadDirExclude = excl
|
|
|
|
|
2013-08-23 20:05:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:29:46 -04:00
|
|
|
func (c *MockCommunicator) Download(path string, w io.Writer) error {
|
|
|
|
c.DownloadCalled = true
|
|
|
|
c.DownloadPath = path
|
|
|
|
w.Write([]byte(c.DownloadData))
|
|
|
|
|
2013-08-23 20:05:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-11-02 06:22:52 -05:00
|
|
|
|
|
|
|
func (c *MockCommunicator) DownloadDir(src string, dst string, excl []string) error {
|
|
|
|
c.DownloadDirDst = dst
|
|
|
|
c.DownloadDirSrc = src
|
|
|
|
c.DownloadDirExclude = excl
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|