-
Notifications
You must be signed in to change notification settings - Fork 29
/
main_test.go
333 lines (301 loc) · 8.04 KB
/
main_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
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
package main
import (
"bytes"
"context"
"errors"
"io"
"io/fs"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
)
type testRequest struct {
name string
path string
queryParams []string
expectedOutput string
}
func TestPushup(t *testing.T) {
// do a `kill -QUIT <pid>` to get a stack trace of all goroutines, useful
// if the test seems to hang
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGQUIT)
buf := make([]byte, 1<<20)
for {
<-sigs
stacklen := runtime.Stack(buf, true)
log.Printf("=== received SIGQUIT ===\n*** goroutine dump...\n%s\n*** end\n", buf[:stacklen])
}
}()
if testing.Short() {
t.Skip("skipping test in short mode")
}
tmpdir := t.TempDir()
pushup := filepath.Join(tmpdir, "pushup.exe")
// build Pushup executable
if err := exec.Command("go", "build", "-o", pushup, ".").Run(); err != nil {
t.Fatalf("building Pushup exe: %v", err)
}
testdataDir := "./testdata"
entries, err := os.ReadDir(testdataDir)
if err != nil {
t.Fatalf("reading testdata dir: %v", err)
}
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), upFileExt) {
t.Run(entry.Name(), func(t *testing.T) {
// FIXME(paulsmith): remove this once we have been panic
// handling in layouts
if entry.Name() == "panicking.up" {
t.Skip()
}
basename, _ := splitExt(entry.Name())
var requests []testRequest
extendedDir := filepath.Join(testdataDir, basename)
if dirExists(extendedDir) {
entries, err := os.ReadDir(extendedDir)
if err != nil {
t.Fatalf("reading extended dir: %v", err)
}
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".conf") {
config := testRequest{name: entry.Name()}
req, _ := splitExt(entry.Name())
outFile := filepath.Join(extendedDir, req+".out")
if !fileExists(outFile) {
t.Fatalf("request file %v needs a matching output file", entry.Name())
}
reqFile := filepath.Join(extendedDir, entry.Name())
{
b, err := os.ReadFile(reqFile)
if err != nil {
t.Fatalf("reading request file %v: %v", reqFile, err)
}
s := string(b)
lines := strings.Split(s, "\n")
for _, line := range lines {
line := strings.TrimSpace(line)
if line != "" {
pair := strings.SplitN(line, "=", 2)
if len(pair) != 2 {
t.Fatalf("illegal request key-value pair: %q", line)
}
switch pair[0] {
case "requestPath":
config.path = pair[1]
case "queryParam":
config.queryParams = append(config.queryParams, pair[1])
default:
log.Printf("unhandled request config key: %q", pair[0])
}
}
}
}
{
b, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("reading output file %v: %v", outFile, err)
}
config.expectedOutput = string(b)
}
requests = append(requests, config)
}
}
}
pushupFile := filepath.Join(testdataDir, entry.Name())
outFile := filepath.Join(testdataDir, basename+".out")
if _, err := os.Stat(outFile); err != nil {
if errors.Is(err, fs.ErrNotExist) {
t.Fatalf("no matching output file %s", outFile)
} else {
t.Fatalf("stat'ing output file: %v", err)
}
}
output, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("reading output file: %v", err)
}
// FIXME(paulsmith): add metadata to the testdata files with the
// desired path to avoid these hacks
// TODO(paulsmith): strip /testdata/ from request paths so tests
// don't need to be aware of where they live
requestPath := "/testdata/" + basename
if basename == "index" {
requestPath = "/testdata/"
} else if basename == "$name" {
requestPath = "/testdata/world"
}
requests = append(requests, testRequest{
path: requestPath,
expectedOutput: string(output),
})
for _, request := range requests {
t.Run(request.name, func(t *testing.T) {
g, ctx0 := errgroup.WithContext(context.Background())
ctx, cancel := context.WithTimeout(ctx0, 10*time.Second)
defer cancel()
ready := make(chan bool)
done := make(chan bool)
tmpdir, err := os.MkdirTemp("", "pushup")
if err != nil {
t.Fatalf("creating temp dir: %v", err)
}
defer os.RemoveAll(tmpdir)
socketPath := filepath.Join(tmpdir, "sock")
var (
errb bytes.Buffer
allgood bool
)
cmd := exec.Command(pushup, "run", "-page", pushupFile, "-unix-socket", socketPath)
sysProcAttr(cmd)
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
cmd.Stderr = &errb
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
g.Go(func() error {
if err := cmd.Wait(); err != nil {
return err
}
return nil
})
g.Go(func() error {
var buf [256]byte
// NOTE(paulsmith): keep this in sync with the string in main.go
needle := []byte("Pushup ready and listening on ")
for {
select {
case <-ctx.Done():
err := ctx.Err()
return err
default:
if stdout != nil {
for {
n, err := stdout.Read(buf[:])
if n > 0 {
if bytes.Contains(buf[:], needle) {
ready <- true
return nil
}
} else {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
}
}
}
}
})
g.Go(func() error {
select {
case <-done:
if cmd != nil {
if err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); err != nil {
return err
}
}
return nil
case <-ctx.Done():
err := ctx.Err()
return err
}
})
g.Go(func() error {
select {
case <-ready:
case <-ctx.Done():
err := ctx.Err()
return err
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(proto, addr string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}
var queryParams string
if len(request.queryParams) > 0 {
queryParams = "?" + strings.Join(request.queryParams, "&")
}
reqUrl := "http://dummy" + request.path + queryParams
resp, err := client.Get(reqUrl)
if err != nil {
return nil
}
defer resp.Body.Close()
got, err := io.ReadAll(resp.Body)
if err != nil {
return nil
}
done <- true
if diff := cmp.Diff(request.expectedOutput, string(got)); diff != "" {
t.Errorf("expected render diff (-want +got)\n%s", diff)
} else {
allgood = true
}
return nil
})
go func() {
//nolint:errcheck
g.Wait()
close(ready)
close(done)
}()
if err := g.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok && allgood {
// no-op
} else if _, ok := err.(*os.SyscallError); ok && allgood {
// no-op
} else {
t.Logf("stderr:\n%s\n", errb.String())
t.Fatalf("error: %T %v", err, err)
}
}
})
}
})
}
}
}
func splitExt(path string) (name string, ext string) {
ext = filepath.Ext(path)
name = strings.TrimSuffix(path, ext)
return
}
func TestTypenameFromPath(t *testing.T) {
tests := []struct {
path string
want string
}{
{"", ""},
{"index", "Index"},
{"$name", "DollarSignName"},
{"default", "Default"},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
got := typenameFromPath(test.path)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("(-want, +got)\n%s", diff)
}
})
}
}