| 1 |
package plugin |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"strings" |
| 8 |
"sync" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
type fakePlugin struct { |
| 14 |
mu sync.Mutex |
| 15 |
closed bool |
| 16 |
} |
| 17 |
|
| 18 |
func (p *fakePlugin) Info() PluginInfo { |
| 19 |
return PluginInfo{ |
| 20 |
RequiredEnv: map[string]bool{"WORK": true}, |
| 21 |
ResultEnv: map[string]bool{"GREETING": true}, |
| 22 |
Cmds: map[string]CmdInfo{"hello": {WritesOutput: true}}, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
func (p *fakePlugin) NewTestInstance(tp TestParams) (TestInstance, error) { |
| 27 |
if tp.TestName == "fail" { |
| 28 |
return nil, fmt.Errorf("instance creation refused") |
| 29 |
} |
| 30 |
return &fakeInstance{work: tp.Env["WORK"]}, nil |
| 31 |
} |
| 32 |
|
| 33 |
func (p *fakePlugin) Close() { |
| 34 |
p.mu.Lock() |
| 35 |
defer p.mu.Unlock() |
| 36 |
p.closed = true |
| 37 |
} |
| 38 |
|
| 39 |
type fakeInstance struct { |
| 40 |
work string |
| 41 |
} |
| 42 |
|
| 43 |
func (i *fakeInstance) Env() map[string]string { |
| 44 |
return map[string]string{"GREETING": "hi from " + i.work} |
| 45 |
} |
| 46 |
|
| 47 |
func (i *fakeInstance) RunCmd(p CmdParams) (CmdResult, error) { |
| 48 |
switch p.Name { |
| 49 |
case "hello": |
| 50 |
return CmdResult{Stdout: []byte("hello " + p.Args[0] + "\n")}, nil |
| 51 |
case "boom": |
| 52 |
return CmdResult{}, fmt.Errorf("plugin transport boom") |
| 53 |
case "fail": |
| 54 |
return CmdResult{Stderr: []byte("oops\n"), Err: "exit status 1"}, nil |
| 55 |
} |
| 56 |
return CmdResult{}, fmt.Errorf("unknown command %q", p.Name) |
| 57 |
} |
| 58 |
|
| 59 |
func (i *fakeInstance) Close() {} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
type rwc struct { |
| 64 |
r io.ReadCloser |
| 65 |
w io.WriteCloser |
| 66 |
} |
| 67 |
|
| 68 |
func (c rwc) Read(b []byte) (int, error) { return c.r.Read(b) } |
| 69 |
func (c rwc) Write(b []byte) (int, error) { return c.w.Write(b) } |
| 70 |
func (c rwc) Close() error { |
| 71 |
c.r.Close() |
| 72 |
c.w.Close() |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
func startInProcess(t *testing.T, impl Interface) (Interface, func()) { |
| 79 |
t.Helper() |
| 80 |
|
| 81 |
cr, cw := io.Pipe() |
| 82 |
|
| 83 |
sr, sw := io.Pipe() |
| 84 |
|
| 85 |
serveDone := make(chan struct{}) |
| 86 |
go func() { |
| 87 |
NewServer(impl, rwc{r: cr, w: sw}) |
| 88 |
close(serveDone) |
| 89 |
}() |
| 90 |
|
| 91 |
c, err := NewClient(rwc{r: sr, w: cw}) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("NewClient: %v", err) |
| 94 |
} |
| 95 |
|
| 96 |
cleanup := func() { |
| 97 |
c.Close() |
| 98 |
<-serveDone |
| 99 |
} |
| 100 |
return c, cleanup |
| 101 |
} |
| 102 |
|
| 103 |
func TestRoundTrip(t *testing.T) { |
| 104 |
impl := &fakePlugin{} |
| 105 |
c, cleanup := startInProcess(t, impl) |
| 106 |
defer cleanup() |
| 107 |
|
| 108 |
if cmds := c.Info().Cmds; len(cmds) != 1 || !cmds["hello"].WritesOutput { |
| 109 |
t.Fatalf("unexpected Info().Cmds: %v", cmds) |
| 110 |
} |
| 111 |
|
| 112 |
inst, err := c.NewTestInstance(TestParams{ |
| 113 |
TestName: "t1", |
| 114 |
Env: map[string]string{"WORK": "/tmp/work"}, |
| 115 |
}) |
| 116 |
if err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
if got, want := inst.Env()["GREETING"], "hi from /tmp/work"; got != want { |
| 120 |
t.Errorf("Env()[GREETING] = %q, want %q", got, want) |
| 121 |
} |
| 122 |
|
| 123 |
res, err := inst.RunCmd(CmdParams{Name: "hello", Args: []string{"world"}}) |
| 124 |
if err != nil { |
| 125 |
t.Fatal(err) |
| 126 |
} |
| 127 |
if got, want := string(res.Stdout), "hello world\n"; got != want { |
| 128 |
t.Errorf("stdout = %q, want %q", got, want) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
res, err = inst.RunCmd(CmdParams{Name: "fail"}) |
| 133 |
if err != nil { |
| 134 |
t.Fatalf("unexpected transport error for command failure: %v", err) |
| 135 |
} |
| 136 |
if res.Err != "exit status 1" { |
| 137 |
t.Errorf("res.Err = %q, want %q", res.Err, "exit status 1") |
| 138 |
} |
| 139 |
if got := string(res.Stderr); got != "oops\n" { |
| 140 |
t.Errorf("stderr = %q, want %q", got, "oops\n") |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
if _, err := inst.RunCmd(CmdParams{Name: "boom"}); err == nil || !strings.Contains(err.Error(), "plugin transport boom") { |
| 145 |
t.Errorf("RunCmd(boom) err = %v, want one containing %q", err, "plugin transport boom") |
| 146 |
} |
| 147 |
|
| 148 |
inst.Close() |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
func TestProtocolVersionTooNew(t *testing.T) { |
| 154 |
|
| 155 |
cr, cw := io.Pipe() |
| 156 |
|
| 157 |
sr, sw := io.Pipe() |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
go func() { |
| 162 |
dec := json.NewDecoder(cr) |
| 163 |
enc := json.NewEncoder(sw) |
| 164 |
var req rpcRequest |
| 165 |
if err := dec.Decode(&req); err != nil { |
| 166 |
return |
| 167 |
} |
| 168 |
enc.Encode(rpcResponse{ |
| 169 |
ID: req.ID, |
| 170 |
Result: mustMarshal(infoResult{Version: protocolVersion + 1}), |
| 171 |
}) |
| 172 |
}() |
| 173 |
|
| 174 |
_, err := NewClient(rwc{r: sr, w: cw}) |
| 175 |
if err == nil || !strings.Contains(err.Error(), "protocol version") { |
| 176 |
t.Fatalf("NewClient err = %v, want one mentioning protocol version", err) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
func TestNewInstanceError(t *testing.T) { |
| 181 |
c, cleanup := startInProcess(t, &fakePlugin{}) |
| 182 |
defer cleanup() |
| 183 |
|
| 184 |
if _, err := c.NewTestInstance(TestParams{TestName: "fail"}); err == nil || !strings.Contains(err.Error(), "instance creation refused") { |
| 185 |
t.Errorf("NewTestInstance err = %v, want one containing %q", err, "instance creation refused") |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestConcurrentInstances(t *testing.T) { |
| 190 |
c, cleanup := startInProcess(t, &fakePlugin{}) |
| 191 |
defer cleanup() |
| 192 |
|
| 193 |
const n = 20 |
| 194 |
var wg sync.WaitGroup |
| 195 |
for i := range n { |
| 196 |
wg.Add(1) |
| 197 |
go func() { |
| 198 |
defer wg.Done() |
| 199 |
inst, err := c.NewTestInstance(TestParams{ |
| 200 |
TestName: fmt.Sprintf("t%d", i), |
| 201 |
Env: map[string]string{"WORK": fmt.Sprintf("/w%d", i)}, |
| 202 |
}) |
| 203 |
if err != nil { |
| 204 |
t.Errorf("NewTestInstance: %v", err) |
| 205 |
return |
| 206 |
} |
| 207 |
defer inst.Close() |
| 208 |
res, err := inst.RunCmd(CmdParams{Name: "hello", Args: []string{fmt.Sprintf("%d", i)}}) |
| 209 |
if err != nil { |
| 210 |
t.Errorf("RunCmd: %v", err) |
| 211 |
return |
| 212 |
} |
| 213 |
if got, want := string(res.Stdout), fmt.Sprintf("hello %d\n", i); got != want { |
| 214 |
t.Errorf("stdout = %q, want %q", got, want) |
| 215 |
} |
| 216 |
}() |
| 217 |
} |
| 218 |
wg.Wait() |
| 219 |
} |