forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
communicator_mock.go
91 lines (73 loc) · 2.18 KB
/
communicator_mock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package communicator
import (
"bytes"
"fmt"
"io"
"strings"
"time"
"github.com/hashicorp/terraform/communicator/remote"
"github.com/hashicorp/terraform/terraform"
)
// 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
}
// Connect implementation of communicator.Communicator interface
func (c *MockCommunicator) Connect(o terraform.UIOutput) error {
return nil
}
// Disconnect implementation of communicator.Communicator interface
func (c *MockCommunicator) Disconnect() error {
return nil
}
// Timeout implementation of communicator.Communicator interface
func (c *MockCommunicator) Timeout() time.Duration {
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 {
if !c.Commands[r.Command] {
return fmt.Errorf("Command not found!")
}
r.SetExited(0)
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
}