2015-05-08 18:17:57 +02:00
|
|
|
package communicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-17 19:00:12 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/communicator/remote"
|
2020-11-25 19:27:10 +01:00
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
2015-05-08 18:17:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MockCommunicator is an implementation of Communicator that can be used for tests.
|
|
|
|
type MockCommunicator struct {
|
|
|
|
RemoteScriptPath string
|
|
|
|
Commands map[string]bool
|
|
|
|
Uploads map[string]string
|
|
|
|
UploadScripts map[string]string
|
|
|
|
UploadDirs map[string]string
|
2018-03-20 19:23:32 +01:00
|
|
|
CommandFunc func(*remote.Cmd) error
|
|
|
|
DisconnectFunc func() error
|
|
|
|
ConnTimeout time.Duration
|
2015-05-08 18:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect implementation of communicator.Communicator interface
|
2020-11-25 19:27:10 +01:00
|
|
|
func (c *MockCommunicator) Connect(o provisioners.UIOutput) error {
|
2015-05-08 18:17:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) Disconnect() error {
|
2018-03-20 19:23:32 +01:00
|
|
|
if c.DisconnectFunc != nil {
|
|
|
|
return c.DisconnectFunc()
|
|
|
|
}
|
2015-05-08 18:17:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) Timeout() time.Duration {
|
2018-03-20 19:23:32 +01:00
|
|
|
if c.ConnTimeout != 0 {
|
|
|
|
return c.ConnTimeout
|
|
|
|
}
|
2015-05-08 18:17:57 +02:00
|
|
|
return time.Duration(5 * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScriptPath implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) ScriptPath() string {
|
|
|
|
return c.RemoteScriptPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) Start(r *remote.Cmd) error {
|
2018-03-15 15:50:17 +01:00
|
|
|
r.Init()
|
|
|
|
|
2018-03-20 19:23:32 +01:00
|
|
|
if c.CommandFunc != nil {
|
|
|
|
return c.CommandFunc(r)
|
|
|
|
}
|
|
|
|
|
2015-05-08 18:17:57 +02:00
|
|
|
if !c.Commands[r.Command] {
|
|
|
|
return fmt.Errorf("Command not found!")
|
|
|
|
}
|
|
|
|
|
2018-03-15 15:50:17 +01:00
|
|
|
r.SetExitStatus(0, nil)
|
2015-05-08 18:17:57 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) Upload(path string, input io.Reader) error {
|
|
|
|
f, ok := c.Uploads[path]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Path %q not found!", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.ReadFrom(input)
|
|
|
|
content := strings.TrimSpace(buf.String())
|
|
|
|
|
|
|
|
f = strings.TrimSpace(f)
|
|
|
|
if f != content {
|
|
|
|
return fmt.Errorf("expected: %q\n\ngot: %q\n", f, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadScript implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) UploadScript(path string, input io.Reader) error {
|
|
|
|
c.Uploads = c.UploadScripts
|
|
|
|
return c.Upload(path, input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadDir implementation of communicator.Communicator interface
|
|
|
|
func (c *MockCommunicator) UploadDir(dst string, src string) error {
|
|
|
|
v, ok := c.UploadDirs[src]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Directory not found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if v != dst {
|
|
|
|
return fmt.Errorf("expected: %q\n\ngot: %q\n", v, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|