Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
378 lines (336 loc) · 11.8 KB
/
Copy pathmain_test.go
File metadata and controls
378 lines (336 loc) · 11.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
package main
import (
"bytes"
"context"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
// runMainEnv makes the test binary run the CLI instead of the tests, so a test
// can run it as a separate process and see its exit status, stdout and stderr
// as a script wrapping it would.
const runMainEnv = "LIBRESPEED_CLI_TEST_RUN_MAIN"
func TestMain(m *testing.M) {
if os.Getenv(runMainEnv) == "1" {
main()
os.Exit(0)
}
os.Exit(m.Run())
}
type cliResult struct {
code int
stdout string
stderr string
elapsed time.Duration
}
// failure is the error the CLI terminated with, from the line main prints
// for it. The test fails when there is no such line.
func (r cliResult) failure(t *testing.T) string {
t.Helper()
const prefix = "Terminated due to error: "
for _, line := range strings.Split(r.stderr, "\n") {
if msg, ok := strings.CutPrefix(line, prefix); ok {
return msg
}
}
t.Fatalf("stderr has no %q line:\n%s", prefix, r.stderr)
return ""
}
func runCLI(t *testing.T, args ...string) cliResult {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], args...)
cmd.Env = append(os.Environ(), runMainEnv+"=1")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
start := time.Now()
err := cmd.Run()
elapsed := time.Since(start)
code := 0
if exitErr, ok := err.(*exec.ExitError); ok {
code = exitErr.ExitCode()
} else if err != nil {
t.Fatalf("running the CLI: %v", err)
}
return cliResult{code: code, stdout: stdout.String(), stderr: stderr.String(), elapsed: elapsed}
}
// mockBackend answers like a speed test backend, with ping URLs that answer
// the probe wrongly in the ways a misconfigured or broken one does.
func mockBackend(t *testing.T) *httptest.Server {
t.Helper()
release := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/empty.php":
case "/getIP.php":
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"processedString":"127.0.0.1 - localhost","rawIspInfo":""}`))
case "/body.php":
w.Write([]byte("hello"))
case "/error-page.php":
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("<html><body><h1>404 Not Found</h1></body></html>\n"))
case "/server-error.php":
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("<html><body><h1>500 Internal Server Error</h1></body></html>\n"))
case "/hang.php":
select {
case <-r.Context().Done():
case <-release:
}
default:
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(func() {
close(release)
srv.Close()
})
return srv
}
// deadURL is a server URL nothing listens on.
func deadURL(t *testing.T) string {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer ln.Close()
return "http://" + ln.Addr().String() + "/"
}
type listedServer struct {
id int
name string
url string
ping string
}
// writeServerList writes a server list for --local-json and returns its path.
func writeServerList(t *testing.T, servers ...listedServer) string {
t.Helper()
type entry struct {
ID int `json:"id"`
Name string `json:"name"`
Server string `json:"server"`
DownloadURL string `json:"dlURL"`
UploadURL string `json:"ulURL"`
PingURL string `json:"pingURL"`
GetIPURL string `json:"getIpURL"`
}
entries := []entry{}
for _, s := range servers {
entries = append(entries, entry{s.id, s.name, s.url, "garbage.php", "empty.php", s.ping, "getIP.php"})
}
b, err := json.Marshal(entries)
if err != nil {
t.Fatalf("marshal server list: %v", err)
}
path := filepath.Join(t.TempDir(), "servers.json")
if err := os.WriteFile(path, b, 0o644); err != nil {
t.Fatalf("write server list: %v", err)
}
return path
}
// outputModes are the ways the CLI prints its report: the default one and
// each output option.
var outputModes = []string{"", "--json", "--json-stream", "--csv", "--simple"}
// withMode is args with the option for an output mode from outputModes.
func withMode(mode string, args ...string) []string {
if mode != "" {
args = append(args, mode)
}
return args
}
// A server from a cached list that cannot be reached, as on an uplink that is
// down, fails the run in every mode: exit status 1, the server and the cause
// on stderr, and no report on stdout -- not a null a script would take for a
// result.
func TestDownServerFailsTheRunInEveryMode(t *testing.T) {
const down = "Selected server Dead (127.0.0.1) is not responding: "
list := writeServerList(t, listedServer{1, "Dead", deadURL(t), "empty.php"})
for _, mode := range outputModes {
t.Run("mode="+mode, func(t *testing.T) {
r := runCLI(t, withMode(mode, "--local-json", list, "--server", "1", "--duration", "1", "--no-icmp")...)
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
// the cause's wording is Go's and the OS's; defs checks its type
if msg := r.failure(t); !strings.HasPrefix(msg, down) || len(msg) == len(down) {
t.Errorf("error = %q, want %q and a cause", msg, down)
}
if mode == "" && !strings.Contains(r.stderr, "\nSelected server Dead (127.0.0.1) is not responding at the moment, try again later\n") {
t.Errorf("the usual line is missing from stderr:\n%s", r.stderr)
}
})
}
}
// A backend that answers its probe wrongly fails the run and says what it
// answered: the status when it is wrong, even with an error page along, and
// the body otherwise.
func TestServerAnsweringItsProbeWronglyFailsTheRun(t *testing.T) {
backend := mockBackend(t)
for _, c := range []struct{ ping, answered string }{
{"missing.php", "HTTP 404 Not Found"},
{"error-page.php", "HTTP 404 Not Found"},
{"server-error.php", "HTTP 500 Internal Server Error"},
{"body.php", "a non-empty body"},
} {
list := writeServerList(t, listedServer{1, "Wrong", backend.URL, c.ping})
for _, mode := range outputModes {
t.Run(c.ping+"/mode="+mode, func(t *testing.T) {
r := runCLI(t, withMode(mode, "--local-json", list, "--server", "1", "--no-icmp")...)
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
want := "Selected server Wrong (127.0.0.1) is not responding: the ping request returned " + c.answered
if got := r.failure(t); got != want {
t.Errorf("error = %q, want %q", got, want)
}
})
}
}
}
// A backend that does not answer within --timeout fails the run when the
// timeout runs out, not the default 15 seconds.
func TestHangingServerFailsTheRunWithTheTimeout(t *testing.T) {
const down = "Selected server Hang (127.0.0.1) is not responding: "
backend := mockBackend(t)
list := writeServerList(t, listedServer{1, "Hang", backend.URL, "hang.php"})
for _, mode := range outputModes {
t.Run("mode="+mode, func(t *testing.T) {
t.Parallel() // each run waits out the timeout
r := runCLI(t, withMode(mode, "--local-json", list, "--server", "1", "--no-icmp", "--timeout", "1")...)
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
if msg := r.failure(t); !strings.HasPrefix(msg, down) || len(msg) == len(down) {
t.Errorf("error = %q, want %q and a cause", msg, down)
}
if r.elapsed >= 10*time.Second {
t.Errorf("the run took %s, want about the 1 second --timeout", r.elapsed)
}
})
}
}
// With more than one server and none of them answering, the error names every
// one with its own cause, not only the last server tried.
func TestRunWhoseServersAreAllDownNamesEach(t *testing.T) {
backend := mockBackend(t)
list := writeServerList(t,
listedServer{1, "Dead", deadURL(t), "empty.php"},
listedServer{2, "Wrong", backend.URL, "error-page.php"},
)
r := runCLI(t, "--local-json", list, "--server", "1", "--server", "2", "--duration", "1", "--no-icmp", "--json")
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
msg := r.failure(t)
causes, ok := strings.CutPrefix(msg, "None of the 2 selected servers is responding: ")
dead, wrong, split := strings.Cut(causes, "; ")
if !ok || !split {
t.Fatalf("error = %q, want both servers named", msg)
}
if !strings.HasPrefix(dead, "Dead (127.0.0.1): ") || len(dead) == len("Dead (127.0.0.1): ") {
t.Errorf("first cause = %q, want Dead and a cause", dead)
}
if want := "Wrong (127.0.0.1): the ping request returned HTTP 404 Not Found"; wrong != want {
t.Errorf("second cause = %q, want %q", wrong, want)
}
}
// With more than one server, a run that measured any of them reports those
// and succeeds.
func TestDownServerAmongOthersLeavesTheRunToTheOnesThatAnswer(t *testing.T) {
backend := mockBackend(t)
list := writeServerList(t,
listedServer{1, "Live", backend.URL, "empty.php"},
listedServer{2, "Dead", deadURL(t), "empty.php"},
)
r := runCLI(t, "--local-json", list, "--server", "1", "--server", "2",
"--duration", "1", "--no-download", "--no-upload", "--no-icmp", "--json")
if r.code != 0 {
t.Fatalf("exit status %d, want 0; stderr:\n%s", r.code, r.stderr)
}
var reports []struct {
Server struct {
Name string `json:"name"`
} `json:"server"`
}
if err := json.Unmarshal([]byte(r.stdout), &reports); err != nil {
t.Fatalf("stdout is not a JSON report: %v\n%s", err, r.stdout)
}
if len(reports) != 1 || reports[0].Server.Name != "Live" {
t.Errorf("reports = %s, want the one for Live", r.stdout)
}
}
// Without --server the fastest server is picked and a down one is skipped,
// as before: with every server down the run fails before any test.
func TestAutomaticSelectionWithEveryServerDownFailsAsBefore(t *testing.T) {
backend := mockBackend(t)
list := writeServerList(t,
listedServer{1, "Dead", deadURL(t), "empty.php"},
listedServer{2, "Wrong", backend.URL, "error-page.php"},
)
r := runCLI(t, "--local-json", list, "--no-icmp", "--json")
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
if !strings.Contains(r.stderr, "No server is currently available, please try again later.\n") {
t.Errorf("stderr does not say no server is available:\n%s", r.stderr)
}
}
// A run with no server to test, as with --server -1 and an empty list, fails
// in every mode like one whose servers are all down, without an empty report.
func TestRunWithNoServerToTestFails(t *testing.T) {
list := writeServerList(t)
for _, mode := range outputModes {
t.Run("mode="+mode, func(t *testing.T) {
r := runCLI(t, withMode(mode, "--local-json", list, "--server", "-1")...)
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want no report", r.stdout)
}
if got, want := r.failure(t), "No server to test: the server list is empty"; got != want {
t.Errorf("error = %q, want %q", got, want)
}
})
}
}
// A server list that cannot be fetched fails the run as it always did.
func TestUnreachableServerListStillFailsTheRun(t *testing.T) {
r := runCLI(t, "--server-json", deadURL(t)+"servers.json", "--server", "1", "--json")
if r.code != 1 {
t.Errorf("exit status %d, want 1; stderr:\n%s", r.code, r.stderr)
}
if r.stdout != "" {
t.Errorf("stdout = %q, want nothing", r.stdout)
}
r.failure(t)
if !strings.Contains(r.stderr, "Error when fetching server list: ") {
t.Errorf("stderr does not say the list could not be fetched:\n%s", r.stderr)
}
}