-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
520 lines (415 loc) · 12.7 KB
/
Copy pathprocess.go
File metadata and controls
520 lines (415 loc) · 12.7 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package testastic
import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"time"
)
// Default configuration values for [Binary.Start].
const (
defaultReadyTimeout = 10 * time.Second
defaultReadyInterval = 100 * time.Millisecond
defaultShutdownTimeout = 5 * time.Second
httpCheckTimeout = 1 * time.Second
)
// ReadyChecker determines whether a process is ready to accept work.
// Implementations are polled repeatedly by [Binary.Start] until they return
// true or the readiness timeout expires.
type ReadyChecker interface {
Check(ctx context.Context) bool
}
// ReadyCheckFunc is an adapter to allow use of ordinary functions as [ReadyChecker].
// This follows the same pattern as [http.HandlerFunc] for [http.Handler].
type ReadyCheckFunc func(ctx context.Context) bool
var _ ReadyChecker = ReadyCheckFunc(nil)
// Check calls f(ctx).
func (f ReadyCheckFunc) Check(ctx context.Context) bool {
return f(ctx)
}
// HTTPCheck returns a [ReadyChecker] that polls an HTTP endpoint until it
// returns a 2xx status code. The endpoint is constructed as
// http://localhost:<port><path> (e.g., HTTPCheck(8080, "/health")).
func HTTPCheck(port int, path string) ReadyChecker {
url := fmt.Sprintf("http://localhost:%d%s", port, path)
client := &http.Client{Timeout: httpCheckTimeout}
return ReadyCheckFunc(func(ctx context.Context) bool {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false
}
resp, err := client.Do(req)
if err != nil {
return false
}
_ = resp.Body.Close()
return resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices
})
}
// BuildOption configures optional behavior for [BuildBinary] and
// [BuildBinaryMain]. Use the provided option constructors (such as
// [WithBuildArgs] and [WithWorkDir]) to create options.
type BuildOption interface {
applyBuild(cfg *buildConfig)
}
// ProcessOption configures optional behavior for [Binary.Start]. Use the
// provided option constructors (such as [WithPort] and [WithEnv]) to create
// options.
type ProcessOption interface {
applyProcess(cfg *processConfig)
}
// buildConfig holds configuration for `go build`.
type buildConfig struct {
importPath string
buildArgs []string
workDir string
}
// processConfig holds configuration for process startup.
type processConfig struct {
binaryPath string
args []string
env []string
port int
readyCheck ReadyChecker
readyTimeout time.Duration
readyInterval time.Duration
shutdownTimeout time.Duration
coverDir string
workDir string
}
type argsOption struct{ args []string }
func (o argsOption) applyProcess(c *processConfig) {
c.args = o.args
}
type envOption struct{ env []string }
func (o envOption) applyProcess(c *processConfig) {
c.env = o.env
}
type portOption struct{ port int }
func (o portOption) applyProcess(c *processConfig) {
c.port = o.port
}
type readyTimeoutOption struct{ duration time.Duration }
func (o readyTimeoutOption) applyProcess(c *processConfig) {
c.readyTimeout = o.duration
}
type readyIntervalOption struct{ duration time.Duration }
func (o readyIntervalOption) applyProcess(c *processConfig) {
c.readyInterval = o.duration
}
type shutdownTimeoutOption struct{ duration time.Duration }
func (o shutdownTimeoutOption) applyProcess(c *processConfig) {
c.shutdownTimeout = o.duration
}
type coverDirOption struct{ dir string }
func (o coverDirOption) applyProcess(c *processConfig) {
c.coverDir = o.dir
}
type buildArgsOption struct{ args []string }
func (o buildArgsOption) applyBuild(c *buildConfig) {
c.buildArgs = o.args
}
type workDirOption struct{ dir string }
func (o workDirOption) applyBuild(c *buildConfig) {
c.workDir = o.dir
}
func (o workDirOption) applyProcess(c *processConfig) {
c.workDir = o.dir
}
// WithArgs sets command-line arguments passed to the binary.
func WithArgs(args ...string) ProcessOption {
return argsOption{args: args}
}
// WithEnv sets additional environment variables in "KEY=VALUE" format.
// These are appended to the current process environment.
// GOCOVERDIR is set automatically and must not be included here.
func WithEnv(env ...string) ProcessOption {
return envOption{env: env}
}
// WithPort sets the TCP port the process listens on.
// When set, [Process.URL] returns http://localhost:<port>.
// When not set, [Process.URL] returns an empty string.
func WithPort(port int) ProcessOption {
return portOption{port: port}
}
// WithReadyTimeout sets how long to wait for the process to become ready.
// Default: 10 seconds.
func WithReadyTimeout(d time.Duration) ProcessOption {
return readyTimeoutOption{duration: d}
}
// WithReadyInterval sets the polling interval between readiness checks.
// Default: 100 milliseconds.
func WithReadyInterval(d time.Duration) ProcessOption {
return readyIntervalOption{duration: d}
}
// WithShutdownTimeout sets how long to wait after requesting shutdown before
// forcing termination and closing inherited output pipes. On Windows, the
// process is terminated immediately because Go cannot deliver a graceful
// interrupt.
// Default: 5 seconds.
func WithShutdownTimeout(d time.Duration) ProcessOption {
return shutdownTimeoutOption{duration: d}
}
// WithCoverDir overrides the directory where coverage data files are written.
// Default: a subdirectory created under t.TempDir().
func WithCoverDir(dir string) ProcessOption {
return coverDirOption{dir: dir}
}
// WithBuildArgs sets additional arguments passed to `go build`
// (e.g., "-ldflags", "-tags"). The flags -cover and -o are always added
// automatically.
func WithBuildArgs(args ...string) BuildOption {
return buildArgsOption{args: args}
}
// WithWorkDir sets the working directory for `go build` and [Binary.Start].
// Default: the directory containing the test file (detected via
// runtime.Caller).
func WithWorkDir(dir string) interface {
BuildOption
ProcessOption
} {
return workDirOption{dir: dir}
}
// Process represents a running process with coverage instrumentation.
// It is created by [Binary.Start] and should not be constructed directly.
type Process struct {
tb testing.TB
cmd *exec.Cmd
cancel context.CancelFunc
baseURL string
coverDir string
stdout *capturedOutput
stderr *capturedOutput
exited chan struct{}
mu sync.Mutex
stopped bool
}
func newProcessConfig(opts []ProcessOption) *processConfig {
cfg := &processConfig{}
for _, opt := range opts {
opt.applyProcess(cfg)
}
return cfg
}
func newBuildConfig(opts []BuildOption) *buildConfig {
cfg := &buildConfig{}
for _, opt := range opts {
opt.applyBuild(cfg)
}
return cfg
}
// defaultWorkDir returns the directory of the file that called the public
// function which called defaultWorkDir. The caller depth of 2 is correct
// because every call site follows the pattern: user code → public API
// (BuildBinary, NewBinary, Binary.Start, etc.) → defaultWorkDir.
// If a new internal wrapper is added between the public API and this function,
// the depth must be adjusted or the caller should resolve the directory at the
// public boundary and pass it explicitly.
func defaultWorkDir() string {
const callerDepth = 2
_, file, _, ok := runtime.Caller(callerDepth)
if !ok {
return ""
}
return filepath.Dir(file)
}
func startProcess(ctx context.Context, tb testing.TB, cfg *processConfig) *Process {
tb.Helper()
validateProcessConfig(tb, cfg)
applyProcessDefaults(cfg)
coverDir := setupCoverDir(tb, cfg.coverDir)
ctx, cancel := context.WithCancel(ctx)
proc := &Process{
tb: tb,
cancel: cancel,
baseURL: formatBaseURL(cfg.port),
coverDir: coverDir,
stdout: &capturedOutput{},
stderr: &capturedOutput{},
exited: make(chan struct{}),
}
//nolint:gosec // args are from test config, not user input
proc.cmd = exec.CommandContext(ctx, cfg.binaryPath, cfg.args...)
proc.cmd.Cancel = func() error {
return interruptProcess(proc.cmd.Process)
}
proc.cmd.WaitDelay = cfg.shutdownTimeout
proc.cmd.Stdout = proc.stdout
proc.cmd.Stderr = proc.stderr
proc.cmd.Env = append(append(os.Environ(), cfg.env...), "GOCOVERDIR="+coverDir)
if cfg.workDir != "" {
proc.cmd.Dir = cfg.workDir
}
err := proc.cmd.Start()
if err != nil {
cancel()
tb.Fatalf("testastic: failed to start process: %v", err)
return nil
}
go func() {
_ = proc.cmd.Wait()
close(proc.exited)
}()
tb.Cleanup(proc.Stop)
waitForReady(ctx, tb, proc, cfg)
return proc
}
// URL returns the base URL of the running process (e.g., "http://localhost:8080").
// Returns an empty string when [WithPort] is not set.
func (p *Process) URL() string {
return p.baseURL
}
// CoverDir returns the path to the directory containing raw coverage data files.
// Use `go tool covdata` to process the files:
//
// go tool covdata textfmt -i=<coverdir> -o=coverage.out
func (p *Process) CoverDir() string {
return p.coverDir
}
// Stop cancels the process context to shut the process down. On Unix it sends
// SIGTERM and the process has until ShutdownTimeout to exit gracefully, writing
// coverage data to CoverDir. On Windows, the process is terminated immediately
// because Go cannot deliver a graceful interrupt, so coverage data may not be
// flushed.
//
// Stop is idempotent, so calling it multiple times is safe.
// It is called automatically by the t.Cleanup handler registered by [Binary.Start].
func (p *Process) Stop() {
p.mu.Lock()
if p.stopped {
p.mu.Unlock()
return
}
p.stopped = true
p.mu.Unlock()
p.cancel()
<-p.exited
p.logOutput()
}
func (p *Process) logOutput() {
if !p.tb.Failed() {
return
}
if p.stdout.Len() > 0 {
p.tb.Logf("testastic: process stdout:\n%s", p.stdout.String())
}
if p.stderr.Len() > 0 {
p.tb.Logf("testastic: process stderr:\n%s", p.stderr.String())
}
}
func validateProcessConfig(tb testing.TB, cfg *processConfig) {
tb.Helper()
if cfg.binaryPath == "" {
tb.Fatalf("testastic: Binary.Start requires a non-empty binary path")
}
if cfg.readyCheck == nil {
tb.Fatalf("testastic: Binary.Start requires readyCheck")
}
for _, e := range cfg.env {
if strings.HasPrefix(e, "GOCOVERDIR=") {
tb.Fatalf("testastic: WithEnv must not include GOCOVERDIR; use WithCoverDir instead")
}
}
if cfg.readyTimeout < 0 {
tb.Fatalf("testastic: WithReadyTimeout must not be negative")
}
if cfg.readyInterval < 0 {
tb.Fatalf("testastic: WithReadyInterval must not be negative")
}
if cfg.shutdownTimeout < 0 {
tb.Fatalf("testastic: WithShutdownTimeout must not be negative")
}
}
func applyProcessDefaults(cfg *processConfig) {
if cfg.readyTimeout == 0 {
cfg.readyTimeout = defaultReadyTimeout
}
if cfg.readyInterval == 0 {
cfg.readyInterval = defaultReadyInterval
}
if cfg.shutdownTimeout == 0 {
cfg.shutdownTimeout = defaultShutdownTimeout
}
}
func formatBaseURL(port int) string {
if port > 0 {
return fmt.Sprintf("http://localhost:%d", port)
}
return ""
}
func setupCoverDir(tb testing.TB, coverDir string) string {
tb.Helper()
if coverDir != "" {
return coverDir
}
if shared := getSharedCoverDir(); shared != "" {
// Per-run subdir avoids covmeta.<hash> race between parallel subprocesses.
sub, err := os.MkdirTemp(shared, "run-*") //nolint:usetesting // must live under sharedCoverDir
if err != nil {
tb.Fatalf("testastic: failed to create per-run coverage directory: %v", err)
}
return sub
}
coverDir = filepath.Join(tb.TempDir(), "coverage")
const dirPerm = 0o750
err := os.MkdirAll(coverDir, dirPerm)
if err != nil {
tb.Fatalf("testastic: failed to create coverage directory: %v", err)
}
return coverDir
}
func waitForReady(ctx context.Context, tb testing.TB, proc *Process, cfg *processConfig) {
tb.Helper()
readyCtx, cancel := context.WithTimeout(ctx, cfg.readyTimeout)
defer cancel()
ticker := time.NewTicker(cfg.readyInterval)
defer ticker.Stop()
for {
select {
case <-proc.exited:
tb.Fatalf(
"testastic: process exited before becoming ready\nstdout:\n%s\nstderr:\n%s",
proc.stdout.String(), proc.stderr.String(),
)
return
default:
}
ready := make(chan bool, 1)
go func() {
ready <- cfg.readyCheck.Check(readyCtx)
}()
select {
case isReady := <-ready:
if isReady {
return
}
case <-readyCtx.Done():
tb.Fatalf(
"testastic: process not ready after %v\nstdout:\n%s\nstderr:\n%s",
cfg.readyTimeout, proc.stdout.String(), proc.stderr.String(),
)
return
}
select {
case <-ticker.C:
case <-readyCtx.Done():
tb.Fatalf(
"testastic: process not ready after %v\nstdout:\n%s\nstderr:\n%s",
cfg.readyTimeout, proc.stdout.String(), proc.stderr.String(),
)
return
case <-proc.exited:
tb.Fatalf(
"testastic: process exited before becoming ready\nstdout:\n%s\nstderr:\n%s",
proc.stdout.String(), proc.stderr.String(),
)
return
}
}
}