forked from hashicorp/go-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
275 lines (226 loc) · 5.24 KB
/
client_test.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package plugin
import (
"bytes"
"io/ioutil"
"net"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestClient(t *testing.T) {
process := helperProcess("mock")
c := NewClient(&ClientConfig{Cmd: process, HandshakeConfig: testHandshake})
defer c.Kill()
// Test that it parses the proper address
addr, err := c.Start()
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}
if addr.Network() != "tcp" {
t.Fatalf("bad: %#v", addr)
}
if addr.String() != ":1234" {
t.Fatalf("bad: %#v", addr)
}
// Test that it exits properly if killed
c.Kill()
if process.ProcessState == nil {
t.Fatal("should have process state")
}
// Test that it knows it is exited
if !c.Exited() {
t.Fatal("should say client has exited")
}
}
func TestClient_testCleanup(t *testing.T) {
// Create a temporary dir to store the result file
td, err := ioutil.TempDir("", "plugin")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
// Create a path that the helper process will write on cleanup
path := filepath.Join(td, "output")
// Test the cleanup
process := helperProcess("cleanup", path)
c := NewClient(&ClientConfig{
Cmd: process,
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Grab the client so the process starts
if _, err := c.Client(); err != nil {
c.Kill()
t.Fatalf("err: %s", err)
}
// Kill it gracefully
c.Kill()
// Test for the file
if _, err := os.Stat(path); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestClient_testInterface(t *testing.T) {
process := helperProcess("test-interface")
c := NewClient(&ClientConfig{
Cmd: process,
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
defer c.Kill()
// Grab the RPC client
client, err := c.Client()
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}
// Grab the impl
raw, err := client.Dispense("test")
if err != nil {
t.Fatalf("err should be nil, got %s", err)
}
impl, ok := raw.(testInterface)
if !ok {
t.Fatalf("bad: %#v", raw)
}
result := impl.Double(21)
if result != 42 {
t.Fatalf("bad: %#v", result)
}
// Kill it
c.Kill()
// Test that it knows it is exited
if !c.Exited() {
t.Fatal("should say client has exited")
}
}
func TestClient_cmdAndReattach(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("start-timeout"),
Reattach: &ReattachConfig{},
}
c := NewClient(config)
defer c.Kill()
_, err := c.Start()
if err == nil {
t.Fatal("err should not be nil")
}
}
func TestClient_reattachNotFound(t *testing.T) {
// Find a bad pid
var pid int = 5000
for i := pid; i < 32000; i++ {
if _, err := os.FindProcess(i); err != nil {
pid = i
break
}
}
// Addr that won't work
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("err: %s", err)
}
addr := l.Addr()
l.Close()
// Reattach
c := NewClient(&ClientConfig{
Reattach: &ReattachConfig{
Addr: addr,
Pid: pid,
},
HandshakeConfig: testHandshake,
Plugins: testPluginMap,
})
// Start shouldn't error
if _, err := c.Start(); err == nil {
t.Fatal("should error")
} else if err != ErrProcessNotFound {
t.Fatalf("err: %s", err)
}
}
func TestClientStart_badVersion(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("bad-version"),
StartTimeout: 50 * time.Millisecond,
HandshakeConfig: testHandshake,
}
c := NewClient(config)
defer c.Kill()
_, err := c.Start()
if err == nil {
t.Fatal("err should not be nil")
}
}
func TestClient_Start_Timeout(t *testing.T) {
config := &ClientConfig{
Cmd: helperProcess("start-timeout"),
StartTimeout: 50 * time.Millisecond,
HandshakeConfig: testHandshake,
}
c := NewClient(config)
defer c.Kill()
_, err := c.Start()
if err == nil {
t.Fatal("err should not be nil")
}
}
func TestClient_Stderr(t *testing.T) {
stderr := new(bytes.Buffer)
process := helperProcess("stderr")
c := NewClient(&ClientConfig{
Cmd: process,
Stderr: stderr,
HandshakeConfig: testHandshake,
})
defer c.Kill()
if _, err := c.Start(); err != nil {
t.Fatalf("err: %s", err)
}
for !c.Exited() {
time.Sleep(10 * time.Millisecond)
}
if !strings.Contains(stderr.String(), "HELLO\n") {
t.Fatalf("bad log data: '%s'", stderr.String())
}
if !strings.Contains(stderr.String(), "WORLD\n") {
t.Fatalf("bad log data: '%s'", stderr.String())
}
}
func TestClient_Stdin(t *testing.T) {
// Overwrite stdin for this test with a temporary file
tf, err := ioutil.TempFile("", "terraform")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
if _, err = tf.WriteString("hello"); err != nil {
t.Fatalf("error: %s", err)
}
if err = tf.Sync(); err != nil {
t.Fatalf("error: %s", err)
}
if _, err = tf.Seek(0, 0); err != nil {
t.Fatalf("error: %s", err)
}
oldStdin := os.Stdin
defer func() { os.Stdin = oldStdin }()
os.Stdin = tf
process := helperProcess("stdin")
c := NewClient(&ClientConfig{Cmd: process, HandshakeConfig: testHandshake})
defer c.Kill()
_, err = c.Start()
if err != nil {
t.Fatalf("error: %s", err)
}
for {
if c.Exited() {
break
}
time.Sleep(50 * time.Millisecond)
}
if !process.ProcessState.Success() {
t.Fatal("process didn't exit cleanly")
}
}