-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
396 lines (345 loc) · 10.8 KB
/
Copy pathmain_test.go
File metadata and controls
396 lines (345 loc) · 10.8 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
// End-to-end tests: build the fly binary once and run it against a fake
// docker command, so that exit codes and output are the ones users see.
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/flywp/server-cli/internal/testutil"
)
var flyBin string
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "fly-e2e")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
flyBin = filepath.Join(dir, "fly")
if out, err := exec.Command("go", "build", "-o", flyBin, ".").CombinedOutput(); err != nil {
fmt.Fprintf(os.Stderr, "building fly: %v\n%s", err, out)
os.Exit(1)
}
code := m.Run()
_ = os.RemoveAll(dir)
os.Exit(code)
}
type result struct {
code int
stdout, stderr string
}
// env is a test environment: a home directory with one site in it and a
// fake docker command.
type env struct {
home, site string
docker *testutil.FakeDocker
vars []string
}
func newEnv(t *testing.T, services ...string) *env {
t.Helper()
if os.Geteuid() == 0 {
t.Skip("fly refuses to run as root")
}
if len(services) == 0 {
services = []string{"php", "nginx"}
}
home := testutil.TempDir(t)
site := filepath.Join(home, "example.com")
testutil.WriteSite(t, site, services...)
return &env{home: home, site: site, docker: testutil.NewFakeDocker(t)}
}
// run executes fly in dir with the test environment. It kills fly and fails
// the test if fly does not finish within 10 seconds.
func (e *env) run(t *testing.T, dir string, args ...string) result {
t.Helper()
return runFly(t, dir, append(append(e.docker.Env(), "HOME="+e.home), e.vars...), args...)
}
// runFly executes fly in dir with the environment environ. It kills fly and
// fails the test if fly does not finish within 10 seconds.
func runFly(t *testing.T, dir string, environ []string, args ...string) result {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, flyBin, args...)
cmd.Dir = dir
cmd.Env = environ
var stdout, stderr bytes.Buffer
cmd.Stdout, cmd.Stderr = &stdout, &stderr
err := cmd.Run()
res := result{stdout: stdout.String(), stderr: stderr.String()}
var exitErr *exec.ExitError
switch {
case ctx.Err() != nil:
t.Fatalf("fly %s did not finish within 10s", strings.Join(args, " "))
case errors.As(err, &exitErr):
res.code = exitErr.ExitCode()
case err != nil:
t.Fatalf("running fly %s: %v", strings.Join(args, " "), err)
}
return res
}
func TestChildExitStatusPassesThrough(t *testing.T) {
e := newEnv(t)
e.vars = append(e.vars, testutil.EnvExit+"=7")
res := e.run(t, e.site, "exec", "--", "php", "sh", "-c", "exit 7")
if res.code != 7 {
t.Errorf("exit code = %d, want 7 (stderr %q)", res.code, res.stderr)
}
if res.stderr != "" {
t.Errorf("stderr = %q, want nothing: the child reports its own error", res.stderr)
}
}
func TestDockerFailureExitsNonZero(t *testing.T) {
commands := [][]string{
{"start"},
{"stop"},
{"restart"},
{"restart", "php"},
{"wp", "--", "plugin", "list"},
{"exec", "--", "php", "ls"},
{"logs"},
{"base", "start"},
{"base", "stop"},
{"base", "restart"},
}
for _, args := range commands {
t.Run(strings.Join(args, " "), func(t *testing.T) {
e := newEnv(t)
e.vars = append(e.vars, testutil.EnvExit+"=3")
res := e.run(t, e.site, args...)
if res.code != 3 {
t.Errorf("exit code = %d, want 3 (stderr %q)", res.code, res.stderr)
}
if strings.Contains(res.stdout, "successfully") {
t.Errorf("stdout = %q, want no success message after a failure", res.stdout)
}
if strings.Contains(res.stdout+res.stderr, "%!") {
t.Errorf("output has a format error: stdout %q, stderr %q", res.stdout, res.stderr)
}
})
}
}
func TestSuccessExitsZero(t *testing.T) {
e := newEnv(t)
res := e.run(t, e.site, "start")
if res.code != 0 {
t.Fatalf("exit code = %d, want 0 (stderr %q)", res.code, res.stderr)
}
want := "compose -f " + filepath.Join(e.site, "docker-compose.yml") + " up -d"
if calls := e.docker.ComposeCalls(t); len(calls) != 1 || calls[0] != want {
t.Errorf("docker calls = %q, want [%q]", calls, want)
}
}
func TestNoSiteIsAnError(t *testing.T) {
e := newEnv(t)
dir := filepath.Join(e.home, "not-a-site")
if err := os.Mkdir(dir, 0o755); err != nil {
t.Fatal(err)
}
res := e.run(t, dir, "start")
if res.code != 1 {
t.Errorf("exit code = %d, want 1", res.code)
}
if !strings.Contains(res.stderr, "no docker-compose.yml file found") {
t.Errorf("stderr = %q, want the no-site error", res.stderr)
}
if res.stdout != "" {
t.Errorf("stdout = %q, want errors on stderr only", res.stdout)
}
}
func TestOutsideHomeDoesNotHang(t *testing.T) {
e := newEnv(t)
outside := testutil.TempDir(t) // not inside e.home
// run fails the test if fly does not stop.
res := e.run(t, outside, "start")
if res.code != 1 || !strings.Contains(res.stderr, "no docker-compose.yml file found") {
t.Errorf("got exit %d, stderr %q, want exit 1 and the no-site error", res.code, res.stderr)
}
}
func TestDomainFlag(t *testing.T) {
e := newEnv(t)
outside := testutil.TempDir(t)
res := e.run(t, outside, "start", "--domain", "example.com")
if res.code != 0 {
t.Fatalf("--domain example.com: exit code = %d, want 0 (stderr %q)", res.code, res.stderr)
}
for _, bad := range []string{"../", "..", "example.com/../..", "a/b"} {
res := e.run(t, outside, "start", "--domain", bad)
if res.code != 1 || !strings.Contains(res.stderr, "invalid domain") {
t.Errorf("--domain %q: exit %d, stderr %q, want exit 1 and an invalid domain error", bad, res.code, res.stderr)
}
}
if calls := e.docker.ComposeCalls(t); len(calls) != 1 {
t.Errorf("docker calls = %q, want only the call for example.com", calls)
}
}
func TestFlagsPassThrough(t *testing.T) {
tests := []struct {
name string
services []string
outside bool // run outside the site directory
args []string
want string // docker arguments after "compose -f <file>"
}{
{
name: "wp-cli flags",
args: []string{"wp", "plugin", "list", "--format=json"},
want: "exec -T php wp plugin list --format=json",
},
{
name: "domain before the wp-cli command",
outside: true,
args: []string{"--domain", "example.com", "wp", "plugin", "list", "--format=json"},
want: "exec -T php wp plugin list --format=json",
},
{
name: "exec flags",
args: []string{"exec", "php", "ls", "-la"},
want: "exec -T php ls -la",
},
{
name: "exec in the default service",
args: []string{"exec", "ls", "-la"},
want: "exec -T php ls -la",
},
{
name: "exec on an OpenLiteSpeed site",
services: []string{"openlitespeed"},
args: []string{"exec", "ls"},
want: "exec -T openlitespeed ls",
},
{
name: "wp on an OpenLiteSpeed site",
services: []string{"openlitespeed"},
args: []string{"wp", "plugin", "list"},
want: "exec -T --user www-data openlitespeed wp plugin list",
},
{
name: "follow logs of one service",
args: []string{"logs", "-f", "php"},
want: "logs --follow php",
},
{
name: "tail logs",
args: []string{"logs", "--tail", "50"},
want: "logs --tail 50",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := newEnv(t, tt.services...)
dir := e.site
if tt.outside {
dir = testutil.TempDir(t)
}
res := e.run(t, dir, tt.args...)
if res.code != 0 {
t.Fatalf("exit code = %d, want 0 (stderr %q)", res.code, res.stderr)
}
want := "compose -f " + filepath.Join(e.site, "docker-compose.yml") + " " + tt.want
if calls := e.docker.ComposeCalls(t); len(calls) != 1 || calls[0] != want {
t.Errorf("docker calls = %q, want [%q]", calls, want)
}
})
}
}
func TestExecNeedsACommand(t *testing.T) {
e := newEnv(t)
res := e.run(t, e.site, "exec", "php")
if res.code != 1 || !strings.Contains(res.stderr, "no command given") {
t.Errorf("exit %d, stderr %q, want exit 1 and a missing-command error", res.code, res.stderr)
}
}
func TestDockerUnavailable(t *testing.T) {
modes := []struct {
name string
mode string // fake docker failure mode
noDocker bool // no docker command in PATH at all
want string
}{
{name: "no docker CLI", noDocker: true, want: "Warning: Docker CLI is not available"},
{name: "no compose plugin", mode: "no-compose", want: "Warning: Docker Compose plugin is not available"},
{name: "daemon down", mode: "daemon-down", want: "Warning: Docker daemon is not available"},
}
commands := [][]string{
{"start"},
{"wp", "plugin", "list"},
{"exec", "php", "ls"},
{"base", "stop"},
{"sites", "start"},
}
for _, m := range modes {
for _, args := range commands {
t.Run(m.name+"/"+strings.Join(args, " "), func(t *testing.T) {
e := newEnv(t)
if m.noDocker {
e.vars = append(e.vars, "PATH="+testutil.TempDir(t))
} else {
e.vars = append(e.vars, testutil.EnvMode+"="+m.mode)
}
res := e.run(t, e.site, args...)
if res.code != 69 {
t.Errorf("exit code = %d, want 69", res.code)
}
lines := strings.Split(strings.TrimSpace(res.stderr), "\n")
if len(lines) != 1 || !strings.HasPrefix(lines[0], m.want) {
t.Errorf("stderr = %q, want one line that starts with %q", res.stderr, m.want)
}
if strings.Contains(res.stderr, "exec:") || strings.Contains(res.stdout, "successfully") {
t.Errorf("output has a raw error or a false success: stdout %q, stderr %q", res.stdout, res.stderr)
}
if calls := e.docker.ComposeCalls(t); len(calls) != 0 {
t.Errorf("compose calls = %q, want none", calls)
}
})
}
t.Run(m.name+"/status", func(t *testing.T) {
e := newEnv(t)
if m.noDocker {
e.vars = append(e.vars, "PATH="+testutil.TempDir(t))
} else {
e.vars = append(e.vars, testutil.EnvMode+"="+m.mode)
}
res := e.run(t, e.site, "status")
if res.code != 0 {
t.Errorf("exit code = %d, want 0", res.code)
}
for _, part := range []string{"Docker CLI is", "Docker Compose plugin is", "Docker daemon is"} {
if !strings.Contains(res.stdout, part) {
t.Errorf("stdout = %q, want a line for %q", res.stdout, part)
}
}
if !strings.Contains(res.stdout, strings.TrimPrefix(m.want, "Warning: ")) {
t.Errorf("stdout = %q, want %q", res.stdout, strings.TrimPrefix(m.want, "Warning: "))
}
})
}
}
func TestUsageErrors(t *testing.T) {
tests := []struct {
args []string
want string
}{
{args: []string{"start", "--bogus"}, want: "Run 'fly start --help' for usage"},
{args: []string{"exec"}, want: "requires at least 1 arg"},
{args: []string{"update"}, want: "sudo fly update"},
}
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
e := newEnv(t)
res := e.run(t, e.site, tt.args...)
if res.code != 1 {
t.Errorf("exit code = %d, want 1", res.code)
}
if !strings.Contains(res.stderr, tt.want) {
t.Errorf("stderr = %q, want it to contain %q", res.stderr, tt.want)
}
})
}
}