diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
| commit | 849951be1d1a7ee9f9302006ccb187bf5b4e36f3 (patch) | |
| tree | 496c924a03a9ea6212e29bb4699e268066ebad81 /internal/clients/session_spec_test.go | |
| parent | bf78b3abffee6d49c08ca2980156afc455994969 (diff) | |
feat: DTail fork — server/client feature development
Squashed development of the snonux/dtail fork's product code (internal/, cmd/)
since diverging from mimecast/dtail. Major areas:
- Read/output path: the former "turbo" channel-less path is now the single,
default server-side read/output path for cat/grep/tail and MapReduce; the old
channel-based path and its config/env toggles were removed.
- MapReduce: single aggregate implementation (server + serverless) fed directly
by a processor pipeline, with input-exhausted finalization via the shutdown
coordinator; high-concurrency and data-race fixes.
- Journal source reads (journal:unit.service) via journalctl, Linux-gated behind
a journal-v1 capability.
- Auth-key fast reconnect: in-memory per-user public-key cache with TTL/max-keys,
registered over an authenticated session (AUTHKEY), checked before
authorized_keys.
- Interactive query reload (--interactive-query) with SESSION START/UPDATE
generation boundaries and capability negotiation.
- Client-side deadlines: --timeout / --shutdownAfter as context deadlines;
follow shutdown handling.
- Client logging: diagnostics-only daily log by default, opt-in payload tee via
--log-payload.
- Numerous correctness fixes (buffer-pool double-recycle races, EOF-sentinel
leaks, glob-expansion cap, TOCTOU in CSV parsing) with accompanying unit tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/clients/session_spec_test.go')
| -rw-r--r-- | internal/clients/session_spec_test.go | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/internal/clients/session_spec_test.go b/internal/clients/session_spec_test.go new file mode 100644 index 0000000..8133bc9 --- /dev/null +++ b/internal/clients/session_spec_test.go @@ -0,0 +1,147 @@ +package clients + +import ( + "testing" + + "github.com/mimecast/dtail/internal/config" + "github.com/mimecast/dtail/internal/omode" +) + +func TestSessionSpecCommands(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + spec SessionSpec + want []string + wantErr bool + }{ + { + name: "tail read commands", + spec: SessionSpec{ + Mode: omode.TailClient, + Files: []string{"/var/log/app.log"}, + Options: "plain=true", + Regex: "ERROR", + }, + want: []string{"tail:plain=true /var/log/app.log regex:default,literal ERROR"}, + }, + { + name: "map client commands", + spec: SessionSpec{ + Mode: omode.MapClient, + Files: []string{"/var/log/app.log"}, + Options: "plain=true", + Query: "from STATS select count(*)", + Regex: ".", + }, + want: []string{ + "map:plain=true from STATS select count(*)", + "cat:plain=true /var/log/app.log regex:noop ", + }, + }, + { + name: "tail query with timeout", + spec: SessionSpec{ + Mode: omode.TailClient, + Files: []string{"/var/log/app.log"}, + Options: "plain=true", + Query: "from STATS select count(*)", + Regex: "WARN", + Timeout: 15, + }, + want: []string{ + "map:plain=true from STATS select count(*)", + "timeout 15 tail /var/log/app.log regex:default,literal WARN", + }, + }, + { + name: "health command", + spec: SessionSpec{ + Mode: omode.HealthClient, + }, + want: []string{"health"}, + }, + { + name: "invalid regex returns error", + spec: SessionSpec{ + Mode: omode.GrepClient, + Files: []string{"/var/log/app.log"}, + Regex: "[", + }, + wantErr: true, + }, + { + name: "query with unsupported mode returns error", + spec: SessionSpec{ + Mode: omode.GrepClient, + Files: []string{"/var/log/app.log"}, + Query: "from STATS select count(*)", + Regex: ".", + }, + wantErr: true, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + got, err := tc.spec.Commands() + if tc.wantErr { + if err == nil { + t.Fatalf("expected error, got nil") + } + return + } + if err != nil { + t.Fatalf("Commands() error = %v", err) + } + if len(got) != len(tc.want) { + t.Fatalf("unexpected command count: got %d want %d (%v)", len(got), len(tc.want), got) + } + for i := range tc.want { + if got[i] != tc.want[i] { + t.Fatalf("command %d mismatch: got %q want %q", i, got[i], tc.want[i]) + } + } + }) + } +} + +func TestNewSessionSpecSplitsFiles(t *testing.T) { + t.Parallel() + + spec := NewSessionSpec(config.Args{ + Mode: omode.GrepClient, + What: " a.log , , b.log ", + RegexStr: "ERROR", + RegexInvert: true, + Timeout: 10, + }) + + if len(spec.Files) != 2 || spec.Files[0] != "a.log" || spec.Files[1] != "b.log" { + t.Fatalf("unexpected files: %#v", spec.Files) + } + if !spec.RegexInvert { + t.Fatalf("expected RegexInvert to be true") + } + if spec.Timeout != 10 { + t.Fatalf("unexpected timeout: %d", spec.Timeout) + } +} + +func TestNewSessionSpecUsesPipeSentinelForServerlessStdin(t *testing.T) { + t.Parallel() + + spec := NewSessionSpec(config.Args{ + Mode: omode.GrepClient, + Serverless: true, + RegexStr: "ERROR", + }) + + if len(spec.Files) != 1 || spec.Files[0] != "-" { + t.Fatalf("unexpected files for serverless stdin: %#v", spec.Files) + } +} |
