-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner_test.go
More file actions
422 lines (343 loc) · 9.73 KB
/
Copy pathrunner_test.go
File metadata and controls
422 lines (343 loc) · 9.73 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
package trexec_test
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
"github.com/Chokqu/trexec"
)
var helperBin string
func TestMain(m *testing.M) {
tempDir, err := os.MkdirTemp("", "trexec-test-*")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create temp dir: %v\n", err)
os.Exit(1)
}
defer os.RemoveAll(tempDir)
binName := "testhelper"
if os.Getenv("GOOS") == "windows" || filepath.Ext(os.Args[0]) == ".exe" {
binName += ".exe"
}
helperBin = filepath.Join(tempDir, binName)
buildCmd := exec.Command("go", "build", "-o", helperBin, "./testdata/helper")
buildCmd.Stdout = os.Stdout
buildCmd.Stderr = os.Stderr
if err := buildCmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "failed to build testhelper: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestSimpleExecution(t *testing.T) {
ctx := context.Background()
var stdout bytes.Buffer
result, err := trexec.RunWithResult(ctx, helperBin, []string{"-stdout=hello world"},
trexec.WithStdout(&stdout),
)
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if !result.Success() {
t.Errorf("expected success, got: %s", result)
}
if result.ExitCode != 0 {
t.Errorf("ExitCode = %d, want 0", result.ExitCode)
}
if result.Cancelled {
t.Error("should not be cancelled")
}
got := strings.TrimSpace(stdout.String())
if got != "hello world" {
t.Errorf("stdout = %q, want %q", got, "hello world")
}
}
func TestNonZeroExit(t *testing.T) {
ctx := context.Background()
result, err := trexec.RunWithResult(ctx, helperBin, []string{"-exit=42"})
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if result.Success() {
t.Error("should not be success")
}
if result.ExitCode != 42 {
t.Errorf("ExitCode = %d, want 42", result.ExitCode)
}
if result.Error == nil {
t.Error("Error should not be nil")
}
}
func TestStderrCapture(t *testing.T) {
ctx := context.Background()
var stderr bytes.Buffer
result, err := trexec.RunWithResult(ctx, helperBin, []string{"-stderr=error_msg"},
trexec.WithStderr(&stderr),
)
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if !result.Success() {
t.Errorf("expected success, got: %s", result)
}
got := strings.TrimSpace(stderr.String())
if got != "error_msg" {
t.Errorf("stderr = %q, want %q", got, "error_msg")
}
}
func TestContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cmd := trexec.CommandContext(ctx, helperBin, []string{"-sleep=60s"},
trexec.WithGracePeriod(1*time.Second),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
// Give the process a moment to start.
time.Sleep(100 * time.Millisecond)
// Cancel the context.
cancel()
result := cmd.Wait()
if !result.Cancelled {
t.Error("result should be cancelled")
}
if result.Duration < 50*time.Millisecond {
t.Errorf("duration too short: %v", result.Duration)
}
}
func TestGracefulShutdown(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cmd := trexec.CommandContext(ctx, helperBin, []string{"-graceful"},
trexec.WithGracePeriod(5*time.Second),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
cancel()
result := cmd.Wait()
if !result.Cancelled {
t.Error("result should be cancelled")
}
if !result.GracefullyTerminated {
t.Error("result should be gracefully terminated")
}
if result.ForceKilled {
t.Error("result should not be force-killed")
}
}
func TestWithGracefulSignalINT(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
// Helper traps os.Interrupt / SIGINT
cmd := trexec.CommandContext(ctx, helperBin, []string{"-graceful"},
trexec.WithGracePeriod(5*time.Second),
trexec.WithGracefulSignal(trexec.SIGINT),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
cancel()
result := cmd.Wait()
if !result.Cancelled {
t.Error("result should be cancelled")
}
if !result.GracefullyTerminated {
t.Error("result should be gracefully terminated via SIGINT")
}
}
func TestForceKill(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
// Helper configured to ignore SIGTERM
cmd := trexec.CommandContext(ctx, helperBin, []string{"-ignore-sig", "-sleep=60s"},
trexec.WithGracePeriod(500*time.Millisecond),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
time.Sleep(200 * time.Millisecond)
start := time.Now()
cancel()
result := cmd.Wait()
elapsed := time.Since(start)
if !result.Cancelled {
t.Error("result should be cancelled")
}
if !result.ForceKilled {
t.Error("result should be force-killed")
}
if result.GracefullyTerminated {
t.Error("result should not be gracefully terminated")
}
// Should take approximately the grace period.
if elapsed < 400*time.Millisecond || elapsed > 3*time.Second {
t.Errorf("force kill took %v, expected ~500ms", elapsed)
}
}
func TestZeroGracePeriod(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cmd := trexec.CommandContext(ctx, helperBin, []string{"-sleep=60s"},
trexec.WithGracePeriod(0),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
time.Sleep(100 * time.Millisecond)
start := time.Now()
cancel()
result := cmd.Wait()
elapsed := time.Since(start)
if !result.Cancelled {
t.Error("result should be cancelled")
}
if !result.ForceKilled {
t.Error("result should be force-killed (zero grace period)")
}
if elapsed > 2*time.Second {
t.Errorf("zero grace period kill took %v, expected < 2s", elapsed)
}
}
func TestProcessTreeKill(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
// Recursively spawns child processes to depth 2
cmd := trexec.CommandContext(ctx, helperBin, []string{"-depth=2", "-sleep=60s"},
trexec.WithGracePeriod(1*time.Second),
)
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
time.Sleep(300 * time.Millisecond)
cancel()
result := cmd.Wait()
if !result.Cancelled {
t.Error("result should be cancelled")
}
}
func TestRunConvenienceFunction(t *testing.T) {
err := trexec.Run(context.Background(), helperBin, []string{"-stdout=hello"})
if err != nil {
t.Errorf("Run() returned error: %v", err)
}
}
func TestRunConvenienceFunctionError(t *testing.T) {
err := trexec.Run(context.Background(), helperBin, []string{"-exit=1"})
if err == nil {
t.Error("Run() should return error for non-zero exit")
}
}
func TestBinaryNotFound(t *testing.T) {
_, err := trexec.RunWithResult(context.Background(),
"nonexistent-binary-that-does-not-exist-12345",
nil,
)
if err == nil {
t.Error("expected error for nonexistent binary")
}
}
func TestDoubleStart(t *testing.T) {
ctx := context.Background()
cmd := trexec.CommandContext(ctx, helperBin, []string{"-stdout=hello"})
if err := cmd.Start(); err != nil {
t.Fatalf("first Start() failed: %v", err)
}
err := cmd.Start()
if err != trexec.ErrAlreadyStarted {
t.Errorf("second Start() = %v, want ErrAlreadyStarted", err)
}
cmd.Wait() // cleanup
}
func TestWorkingDirectory(t *testing.T) {
targetDir := os.TempDir()
var stdout bytes.Buffer
result, err := trexec.RunWithResult(context.Background(), helperBin, []string{"-pwd"},
trexec.WithDir(targetDir),
trexec.WithStdout(&stdout),
)
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if !result.Success() {
t.Errorf("expected success, got: %s", result)
}
got := strings.TrimSpace(stdout.String())
// Resolve symlinks if any (e.g. /var vs /private/var on macOS)
targetResolved, _ := filepath.EvalSymlinks(targetDir)
gotResolved, _ := filepath.EvalSymlinks(got)
if !strings.EqualFold(gotResolved, targetResolved) {
t.Errorf("pwd = %q (resolved %q), want %q (resolved %q)", got, gotResolved, targetDir, targetResolved)
}
}
func TestWithSysProcAttr(t *testing.T) {
ctx := context.Background()
customAttr := &syscall.SysProcAttr{}
result, err := trexec.RunWithResult(ctx, helperBin, []string{"-stdout=test_attr"},
trexec.WithSysProcAttr(customAttr),
)
if err != nil {
t.Fatalf("RunWithResult with custom SysProcAttr failed: %v", err)
}
if !result.Success() {
t.Errorf("expected success with WithSysProcAttr, got: %s", result)
}
}
func TestAlreadyCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel before start
result, err := trexec.RunWithResult(ctx, helperBin, []string{"-stdout=hello"})
if err != nil {
return
}
if result.ExitCode == 0 && !result.Cancelled {
return
}
if result.Cancelled {
return
}
t.Errorf("unexpected result: %s", result)
}
func TestStateChangeCallback(t *testing.T) {
var mu sync.Mutex
var states []trexec.State
result, err := trexec.RunWithResult(context.Background(), helperBin, []string{"-stdout=hello"},
trexec.WithOnStateChange(func(s trexec.State) {
mu.Lock()
states = append(states, s)
mu.Unlock()
}),
)
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if !result.Success() {
t.Errorf("expected success, got: %s", result)
}
time.Sleep(200 * time.Millisecond)
mu.Lock()
count := len(states)
mu.Unlock()
if count < 2 {
mu.Lock()
t.Errorf("expected at least 2 state changes, got %d: %v", count, states)
mu.Unlock()
}
}
func TestResultDuration(t *testing.T) {
start := time.Now()
result, err := trexec.RunWithResult(context.Background(), helperBin, []string{"-sleep=100ms"})
elapsed := time.Since(start)
if err != nil {
t.Fatalf("RunWithResult failed: %v", err)
}
if result.Duration < 50*time.Millisecond {
t.Errorf("Duration = %v, expected >= 50ms", result.Duration)
}
if result.Duration > elapsed+500*time.Millisecond {
t.Errorf("Duration %v much larger than elapsed %v", result.Duration, elapsed)
}
}