summaryrefslogtreecommitdiff
path: root/integrationtests/dgrep_test.go
blob: 7d237c786d12fbb938bd3d01517c991aa6c019aa (plain)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
package integrationtests

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/mimecast/dtail/internal/config"
)

func TestDGrep1(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrep1")
	defer testLogger.WriteLogFile()

	// Test in serverless mode
	t.Run("Serverless", func(t *testing.T) {
		testDGrep1Serverless(t, testLogger)
	})

	// Test in server mode
	t.Run("ServerMode", func(t *testing.T) {
		testDGrep1WithServer(t, testLogger)
	})
}

func testDGrep1Serverless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep.stdout.tmp"
	expectedOutFile := "dgrep1.txt.expected"
	ctx := WithTestLogger(context.Background(), logger)

	_, err := runCommand(ctx, t, outFile,
		"../dgrep",
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		inFile)

	if err != nil {
		t.Error(err)
		return
	}

	if err := compareFilesWithContext(ctx, t, outFile, expectedOutFile); err != nil {
		t.Error(err)
		return
	}
}

func testDGrep1WithServer(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep.stdout.tmp"
	expectedOutFile := "dgrep1.txt.expected"
	port := getUniquePortNumber()
	bindAddress := "localhost"

	ctx, cancel := context.WithCancel(context.Background())
	ctx = WithTestLogger(ctx, logger)
	defer cancel()

	// Start dserver
	_, _, _, err := startCommand(ctx, t,
		"", "../dserver",
		"--cfg", "none",
		"--logger", "stdout",
		"--logLevel", "error",
		"--bindAddress", bindAddress,
		"--port", fmt.Sprintf("%d", port),
	)
	if err != nil {
		t.Error(err)
		return
	}

	if err := waitForServerReady(ctx, bindAddress, port); err != nil {
		t.Error(err)
		return
	}

	err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
		return compareFilesWithContext(ctx, t, outFile, expectedOutFile)
	},
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
		"--trustAllHosts",
		"--noColor",
		"--files", inFile)
	cancel()
	if err != nil {
		t.Error(err)
		return
	}
}

func TestDGrep1Colors(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrep1Colors")
	defer testLogger.WriteLogFile()

	// Test in serverless mode
	t.Run("Serverless", func(t *testing.T) {
		testDGrep1ColorsServerless(t, testLogger)
	})

	// Test in server mode
	t.Run("ServerMode", func(t *testing.T) {
		testDGrep1ColorsWithServer(t, testLogger)
	})
}

func testDGrep1ColorsServerless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep1colors.stdout.tmp"
	ctx := WithTestLogger(context.Background(), logger)

	// Run without --plain to get colored output
	_, err := runCommand(ctx, t, outFile,
		"../dgrep",
		"--cfg", "none",
		"--grep", "1002-071947",
		inFile)

	if err != nil {
		t.Error(err)
		return
	}

	// Verify it ran successfully and produced output
	info, err := os.Stat(outFile)
	if err != nil {
		t.Error("Output file not created:", err)
		return
	}
	if info.Size() == 0 {
		t.Error("Output file is empty")
		return
	}

	// Verify output contains ANSI color codes
	content, err := os.ReadFile(outFile)
	if err != nil {
		t.Error("Failed to read output file:", err)
		return
	}
	if !strings.Contains(string(content), "\033[") {
		t.Error("Output does not contain ANSI color codes")
		return
	}

	// Log verification
	logger.LogFileComparison(outFile, "ANSI color codes", "contains check")
}

func testDGrep1ColorsWithServer(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep1colors.stdout.tmp"
	port := getUniquePortNumber()
	bindAddress := "localhost"

	ctx, cancel := context.WithCancel(context.Background())
	ctx = WithTestLogger(ctx, logger)
	defer cancel()

	// Start dserver
	_, _, _, err := startCommand(ctx, t,
		"", "../dserver",
		"--cfg", "none",
		"--logger", "stdout",
		"--logLevel", "error",
		"--bindAddress", bindAddress,
		"--port", fmt.Sprintf("%d", port),
	)
	if err != nil {
		t.Error(err)
		return
	}

	if err := waitForServerReady(ctx, bindAddress, port); err != nil {
		t.Error(err)
		return
	}

	err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
		info, statErr := os.Stat(outFile)
		if statErr != nil {
			return fmt.Errorf("output file not created: %w", statErr)
		}
		if info.Size() == 0 {
			return fmt.Errorf("output file is empty")
		}
		content, readErr := os.ReadFile(outFile)
		if readErr != nil {
			return fmt.Errorf("failed to read output file: %w", readErr)
		}
		if !strings.Contains(string(content), "REMOTE") && !strings.Contains(string(content), "SERVER") {
			preview := string(content)
			if len(preview) > 500 {
				preview = preview[:500]
			}
			return fmt.Errorf("server mode output does not contain server metadata. First 500 chars:\n%s", preview)
		}
		return nil
	},
		"--cfg", "none",
		"--grep", "1002-071947",
		"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
		"--trustAllHosts",
		"--files", inFile)
	cancel()
	if err != nil {
		t.Error(err)
		return
	}
	logger.LogFileComparison(outFile, "server metadata (REMOTE/SERVER)", "contains check")
}

func TestDGrep2(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrep2")
	defer testLogger.WriteLogFile()

	// Test in serverless mode
	t.Run("Serverless", func(t *testing.T) {
		testDGrep2Serverless(t, testLogger)
	})

	// Test in server mode
	t.Run("ServerMode", func(t *testing.T) {
		testDGrep2WithServer(t, testLogger)
	})
}

func testDGrep2Serverless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep.stdout.tmp"
	expectedOutFile := "dgrep2.txt.expected"
	ctx := WithTestLogger(context.Background(), logger)

	_, err := runCommand(ctx, t, outFile,
		"../dgrep",
		"--plain",
		"--cfg", "none",
		"--grep", "1002-07194[789]",
		inFile)

	if err != nil {
		t.Error(err)
		return
	}

	if err := compareFilesWithContext(ctx, t, outFile, expectedOutFile); err != nil {
		t.Error(err)
		return
	}
}

func testDGrep2WithServer(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrep.stdout.tmp"
	expectedOutFile := "dgrep2.txt.expected"
	port := getUniquePortNumber()
	bindAddress := "localhost"

	ctx, cancel := context.WithCancel(context.Background())
	ctx = WithTestLogger(ctx, logger)
	defer cancel()

	// Start dserver
	_, _, _, err := startCommand(ctx, t,
		"", "../dserver",
		"--cfg", "none",
		"--logger", "stdout",
		"--logLevel", "error",
		"--bindAddress", bindAddress,
		"--port", fmt.Sprintf("%d", port),
	)
	if err != nil {
		t.Error(err)
		return
	}

	if err := waitForServerReady(ctx, bindAddress, port); err != nil {
		t.Error(err)
		return
	}

	err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
		return compareFilesWithContext(ctx, t, outFile, expectedOutFile)
	},
		"--plain",
		"--cfg", "none",
		"--grep", "1002-07194[789]",
		"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
		"--trustAllHosts",
		"--noColor",
		"--files", inFile)
	cancel()
	if err != nil {
		t.Error(err)
		return
	}
}

func TestDGrepContext1(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrepContext1")
	defer testLogger.WriteLogFile()

	// Test in serverless mode
	t.Run("Serverless", func(t *testing.T) {
		testDGrepContext1Serverless(t, testLogger)
	})

	// Test in server mode
	t.Run("ServerMode", func(t *testing.T) {
		testDGrepContext1WithServer(t, testLogger)
	})
}

func testDGrepContext1Serverless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrepcontext.stdout.tmp"
	expectedOutFile := "dgrepcontext1.txt.expected"
	ctx := WithTestLogger(context.Background(), logger)

	_, err := runCommand(ctx, t, outFile,
		"../dgrep",
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		"--before", "2",
		"--after", "3",
		inFile)

	if err != nil {
		t.Error(err)
		return
	}

	if err := compareFilesWithContext(ctx, t, outFile, expectedOutFile); err != nil {
		t.Error(err)
		return
	}
}

func testDGrepContext1WithServer(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrepcontext.stdout.tmp"
	expectedOutFile := "dgrepcontext1.txt.expected"
	port := getUniquePortNumber()
	bindAddress := "localhost"

	ctx, cancel := context.WithCancel(context.Background())
	ctx = WithTestLogger(ctx, logger)
	defer cancel()

	// Start dserver
	_, _, _, err := startCommand(ctx, t,
		"", "../dserver",
		"--cfg", "none",
		"--logger", "stdout",
		"--logLevel", "error",
		"--bindAddress", bindAddress,
		"--port", fmt.Sprintf("%d", port),
	)
	if err != nil {
		t.Error(err)
		return
	}

	if err := waitForServerReady(ctx, bindAddress, port); err != nil {
		t.Error(err)
		return
	}

	err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
		return compareFilesWithContext(ctx, t, outFile, expectedOutFile)
	},
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		"--before", "2",
		"--after", "3",
		"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
		"--trustAllHosts",
		"--noColor",
		"--files", inFile)
	cancel()
	if err != nil {
		t.Error(err)
		return
	}
}

func TestDGrepContext2(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrepContext2")
	defer testLogger.WriteLogFile()

	// Test in serverless mode
	t.Run("Serverless", func(t *testing.T) {
		testDGrepContext2Serverless(t, testLogger)
	})

	// Test in server mode
	t.Run("ServerMode", func(t *testing.T) {
		testDGrepContext2WithServer(t, testLogger)
	})
}

func testDGrepContext2Serverless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrepcontext.stdout.tmp"
	expectedOutFile := "dgrepcontext2.txt.expected"
	ctx := WithTestLogger(context.Background(), logger)

	_, err := runCommand(ctx, t, outFile,
		"../dgrep",
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		"--before", "2",
		"--after", "3",
		"--invert",
		inFile)

	if err != nil {
		t.Error(err)
		return
	}

	if err := compareFilesWithContext(ctx, t, outFile, expectedOutFile); err != nil {
		t.Error(err)
		return
	}
}

func testDGrepContext2WithServer(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrepcontext.stdout.tmp"
	expectedOutFile := "dgrepcontext2.txt.expected"
	port := getUniquePortNumber()
	bindAddress := "localhost"

	ctx, cancel := context.WithCancel(context.Background())
	ctx = WithTestLogger(ctx, logger)
	defer cancel()

	// Start dserver
	_, _, _, err := startCommand(ctx, t,
		"", "../dserver",
		"--cfg", "none",
		"--logger", "stdout",
		"--logLevel", "error",
		"--bindAddress", bindAddress,
		"--port", fmt.Sprintf("%d", port),
	)
	if err != nil {
		t.Error(err)
		return
	}

	if err := waitForServerReady(ctx, bindAddress, port); err != nil {
		t.Error(err)
		return
	}

	err = runCommandUntilValid(ctx, t, 5, 200*time.Millisecond, outFile, "../dgrep", func() error {
		return compareFilesWithContext(ctx, t, outFile, expectedOutFile)
	},
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947",
		"--before", "2",
		"--after", "3",
		"--invert",
		"--servers", fmt.Sprintf("%s:%d", bindAddress, port),
		"--trustAllHosts",
		"--noColor",
		"--files", inFile)
	cancel()
	if err != nil {
		t.Error(err)
		return
	}
}

func TestDGrepPipeToStdin(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrepPipeToStdin")
	defer testLogger.WriteLogFile()

	// Only test in serverless mode (stdin piping doesn't work with server mode)
	t.Run("Serverless", func(t *testing.T) {
		testDGrepStdinServerless(t, testLogger)
	})
}

func testDGrepStdinServerless(t *testing.T, logger *TestLogger) {
	inFile := "mapr_testdata.log"
	outFile := "dgrepstdin.stdout.tmp"
	expectedOutFile := "dgrep1.txt.expected"
	ctx := WithTestLogger(context.Background(), logger)

	// Use startCommand with stdin piping
	stdoutCh, stderrCh, cmdErrCh, err := startCommand(ctx, t,
		inFile, // This will be piped to stdin
		"../dgrep",
		"--plain",
		"--cfg", "none",
		"--grep", "1002-071947")

	if err != nil {
		t.Error(err)
		return
	}

	// Collect output
	fd, err := os.Create(outFile)
	if err != nil {
		t.Error(err)
		return
	}
	defer fd.Close()

	// Read from stdout channel
	go func() {
		for line := range stdoutCh {
			fmt.Fprintln(fd, line)
		}
	}()

	// Drain stderr channel
	go func() {
		for line := range stderrCh {
			t.Log("stderr:", line)
		}
	}()

	// Wait for command to complete
	select {
	case <-ctx.Done():
		t.Error("Context cancelled")
		return
	case cmdErr := <-cmdErrCh:
		if cmdErr != nil {
			t.Error("Command failed:", cmdErr)
			return
		}
	case <-time.After(5 * time.Second):
		t.Error("Command timed out")
		return
	}

	// Give time for output to be written
	time.Sleep(100 * time.Millisecond)

	if err := compareFilesWithContext(ctx, t, outFile, expectedOutFile); err != nil {
		t.Error(err)
		return
	}
}

// TestDGrepMaxCountNoEOFError is a regression test for the (former "turbo")
// direct read path leaking the io.EOF early-stop sentinel that
// processWithContext returns once a -max (MaxCount) limit is reached. The leak
// surfaced as a spurious "SERVER|...|ERROR|...|EOF" line.
//
// The direct read path is now the one and only runtime path, so this runs a
// single server. The test asserts the server logged "Using turbo mode for
// reading" (proving the optimized path actually ran) and that the -max output
// carries no ERROR/EOF line and matches the exact expected payload.
func TestDGrepMaxCountNoEOFError(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	cleanupTmpFiles(t)
	testLogger := NewTestLogger("TestDGrepMaxCountNoEOFError")
	defer testLogger.WriteLogFile()

	// Many lines match "INFO"; -max 3 forces an early stop well before EOF, which
	// is what makes processWithContext return the io.EOF sentinel.
	dataFile := filepath.Join(t.TempDir(), "maxcount_input.log")
	var sb strings.Builder
	for i := 0; i < 20; i++ {
		fmt.Fprintf(&sb, "INFO line %d\n", i)
	}
	if err := os.WriteFile(dataFile, []byte(sb.String()), 0o600); err != nil {
		t.Fatalf("write input file: %v", err)
	}
	configFile := writeTurboFileServerConfig(t, dataFile)

	// run starts a dserver and greps the data file with -max 3.
	run := func(name string, serverEnv map[string]string) (string, *safeLineLog) {
		serverEnv["DTAIL_HOSTNAME_OVERRIDE"] = "integrationtest"
		server := startDJournalExtendedServer(t, testLogger, configFile, serverEnv, "debug", true)
		outFile := fmt.Sprintf("dgrep_maxcount_%s.stdout.tmp", name)
		if _, err := runCommand(server.ctx, t, outFile, "../dgrep",
			"--plain",
			"--cfg", "none",
			"--logLevel", "error",
			"--grep", "INFO",
			"--max", "3",
			"--servers", server.address,
			"--trustAllHosts",
			"--noColor",
			"--files", dataFile,
		); err != nil {
			t.Fatalf("dgrep -max (%s) failed: %v", name, err)
		}
		return readTestFile(t, outFile), server.logs
	}

	turboOut, turboLogs := run("turbo", map[string]string{})

	// In SERVER mode the leaked max-count sentinel does NOT reach the client
	// output (unlike serverless): the server logs it via dlog.Server.Error in
	// executeReadLoop as an "ERROR|...|<file>|<basename>|EOF" line on its own
	// stdout. So the regression is asserted against the server logs, not the
	// client output. assertNoReadEOFError distinguishes that leak (an ERROR line
	// ending in "|EOF") from the benign "ssh: parse error in message type 0"
	// ERROR line the handshake always emits.
	assertNoReadEOFError := func(t *testing.T, label string, logs *safeLineLog) {
		t.Helper()
		for _, ln := range strings.Split(logs.String(), "\n") {
			if strings.Contains(ln, "ERROR") && strings.HasSuffix(ln, "|EOF") {
				t.Fatalf("%s server leaked the max-count io.EOF sentinel as a spurious "+
					"ERROR log line: %q\nfull logs:\n%s", label, ln, logs.String())
			}
		}
	}

	// Prove the run genuinely exercised the optimized path.
	turboLogs.waitContains(t, "Using turbo mode for reading", 5*time.Second)
	// "File processing complete" is logged strictly AFTER the read returns (and
	// thus after any ERROR|...|EOF line from executeReadLoop), so once it appears
	// the absence check below cannot pass merely because the log has not flushed.
	turboLogs.waitContains(t, "File processing complete", 5*time.Second)

	assertNoReadEOFError(t, "turbo", turboLogs)

	if want := "INFO line 0\nINFO line 1\nINFO line 2\n"; turboOut != want {
		t.Fatalf("turbo -max output: got:\n%swant:\n%s", turboOut, want)
	}
}

// writeTurboFileServerConfig creates a dserver config for a turbo-enabled
// integration server that is permitted to read readableFile. It mirrors the auth
// setup used by the journal-extended turbo tests: a CacheDir holding the
// integration public key as <user>.authorized_keys, so the server can run with
// the integration-test force-disable dropped (turbo ON) while the client still
// authenticates with the integration key. Returns the config file path.
func writeTurboFileServerConfig(t *testing.T, readableFile string) string {
	t.Helper()

	tmpDir := t.TempDir()
	cwd, err := os.Getwd()
	if err != nil {
		t.Fatalf("get test working directory: %v", err)
	}
	// CacheDir is resolved relative to the server's working directory, so the key
	// cache must live under cwd and be referenced by its basename (as the journal
	// harness does).
	cacheDirAbs, err := os.MkdirTemp(cwd, "dgrep-maxcount-key-cache-")
	if err != nil {
		t.Fatalf("create key cache dir: %v", err)
	}
	t.Cleanup(func() { os.RemoveAll(cacheDirAbs) })
	if err := os.WriteFile(filepath.Join(cacheDirAbs, currentUsername(t)+".authorized_keys"),
		readIntegrationPublicKey(t), 0o600); err != nil {
		t.Fatalf("write authorized_keys cache: %v", err)
	}

	content, err := json.MarshalIndent(map[string]any{
		"Common": map[string]any{
			"CacheDir": filepath.Base(cacheDirAbs),
		},
		"Server": map[string]any{
			"HostKeyFile": filepath.Join(tmpDir, "ssh_host_key"),
			"Permissions": map[string]any{
				"Default": []string{permissionForPath(readableFile)},
			},
		},
	}, "", "  ")
	if err != nil {
		t.Fatalf("marshal server config: %v", err)
	}
	configFile := filepath.Join(tmpDir, "dtail.json")
	if err := os.WriteFile(configFile, append(content, '\n'), 0o600); err != nil {
		t.Fatalf("write server config: %v", err)
	}
	return configFile
}