feat(panel): give a delivery its history and its own log lines
test / test (push) Has been cancelled
test / test (push) Has been cancelled
The delivery page was a list of the fields the send-log table has no column for, stacked one per line down the reading measure. Six values of a few characters each — domain, application, queue id, journal id and two timestamps — came to a page of mostly empty rows, and none of them answered the question the log raises when a row is opened: what actually happened to this message. So the page states that instead. The subject heads it and the sender, recipient and outcome are the line under it, which puts what the message was and how it ended on the first line. Below, two columns: what the journal recorded on the left, as a grid of tiles rather than a stack, and on the right the two timestamps stated as the steps they stand for — accepted and queued, then delivered, deferred, bounced, or refused before queueing. Each step carries its status in the panel's own ok/warn/error/unknown vocabulary, so a colour means here what it means on the status page. A message still queued shows the report it is waiting for as a step that has not happened, rather than dating it with the moment the row was written. Under both, at full width, the mail.log lines for the message's queue id. The queue id was printed on this page as something to go and search the system log for by hand; logtail.QueueLines does that search. It scans a bounded tail of the current file — finding one message's lines means reading rather than seeking — and anchors the match on the character before the id, since queue ids are hexadecimal runs and a shorter one is regularly the tail of a longer one. Send-log rows outlive mail.log (retention ninety days, rotation fourteen files), so a message with nothing left to show says so; that is the normal end state, not a fault, and only a log that cannot be read at all is reported as one. Two cards abreast and a block of raw log lines do not fit the reading measure, so the page now declares itself wide — the opposite of what it did when the column width was unified, where it was the page that stayed prose. The mechanism is unchanged and is why the reversal costs one line: how wide a page needs to be is the page's own property, not the navigation entry's. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -333,3 +334,123 @@ func waitFor(t *testing.T, cond func() bool) {
|
||||
}
|
||||
t.Fatal("condition not met within timeout")
|
||||
}
|
||||
|
||||
// A delivery's page shows what Postfix wrote about that one message, so the
|
||||
// read has to pick its queue-id's lines out of everything else the mail path
|
||||
// logged around them — and only its own: queue ids are hexadecimal runs, and a
|
||||
// shorter one is regularly the tail of a longer one.
|
||||
func TestQueueLinesPicksOutOneMessage(t *testing.T) {
|
||||
path := writeLog(t,
|
||||
"host postfix/smtpd[20]: 41E862C00D9E: client=mail.example.com[203.0.113.4]",
|
||||
"host postfix/qmgr[10]: 5900C2C00D9E: from=<other@example.com>, size=500, nrcpt=1 (queue active)",
|
||||
"host postfix/cleanup[15]: 41E862C00D9E: message-id=<abc@example.com>",
|
||||
// Same run of characters, longer id: not this message.
|
||||
"host postfix/smtp[26]: FF41E862C00D9E: to=<z@example.net>, status=sent (250 OK)",
|
||||
"host opendkim[30]: 41E862C00D9E: DKIM-Signature field added (s=mail d=example.com)",
|
||||
"host postfix/smtp[26]: 41E862C00D9E: to=<a@example.net>, relay=mx.example.net[203.0.113.9]:25, dsn=2.0.0, status=sent (250 OK)",
|
||||
)
|
||||
|
||||
lines, err := QueueLines(path, "41E862C00D9E", 200)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueLines: %v", err)
|
||||
}
|
||||
if len(lines) != 4 {
|
||||
t.Fatalf("got %d lines, want 4:\n%s", len(lines), strings.Join(lines, "\n"))
|
||||
}
|
||||
// Oldest first: the page reads the message's history downwards.
|
||||
if !strings.Contains(lines[0], "client=") || !strings.Contains(lines[3], "status=sent") {
|
||||
t.Errorf("lines are not in the order they were logged:\n%s", strings.Join(lines, "\n"))
|
||||
}
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "FF41E862C00D9E") || strings.Contains(line, "5900C2C00D9E") {
|
||||
t.Errorf("another message's line came back: %q", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A message the milter refused has no queue id, so there is nothing to match
|
||||
// on — every line in the log would be someone else's.
|
||||
func TestQueueLinesWithoutAQueueIDMatchesNothing(t *testing.T) {
|
||||
path := writeLog(t, "host postfix/smtp[26]: 41E862C00D9E: to=<a@example.net>, status=sent (250 OK)")
|
||||
|
||||
lines, err := QueueLines(path, "", 200)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueLines: %v", err)
|
||||
}
|
||||
if lines != nil {
|
||||
t.Errorf("got %v, want no lines", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// The cap keeps the newest lines, not the first ones: what a message did last
|
||||
// is what its page is opened for.
|
||||
func TestQueueLinesCapKeepsTheLatest(t *testing.T) {
|
||||
var log []string
|
||||
for i := 0; i < 10; i++ {
|
||||
log = append(log, "host postfix/smtp[26]: ABC123: attempt "+itoa(i))
|
||||
}
|
||||
path := writeLog(t, log...)
|
||||
|
||||
lines, err := QueueLines(path, "ABC123", 3)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueLines: %v", err)
|
||||
}
|
||||
if len(lines) != 3 || !strings.HasSuffix(lines[0], "attempt 7") || !strings.HasSuffix(lines[2], "attempt 9") {
|
||||
t.Errorf("cap did not keep the last three:\n%s", strings.Join(lines, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
// Send-log rows outlive mail.log — retention is ninety days and rotation keeps
|
||||
// fourteen files — so a message whose lines are gone, or a log that is between
|
||||
// rotations and absent altogether, is an empty answer for the page to explain,
|
||||
// not an error for it to report.
|
||||
func TestQueueLinesOnAMessageWithNoLinesLeft(t *testing.T) {
|
||||
path := writeLog(t, "host postfix/smtp[26]: 5900C2C00D9E: to=<z@example.net>, status=sent (250 OK)")
|
||||
|
||||
lines, err := QueueLines(path, "41E862C00D9E", 200)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueLines: %v", err)
|
||||
}
|
||||
if lines != nil {
|
||||
t.Errorf("got %v, want no lines", lines)
|
||||
}
|
||||
}
|
||||
|
||||
// writeLog creates a mail.log holding the given lines and returns its path.
|
||||
func writeLog(t *testing.T, lines ...string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "mail.log")
|
||||
body := ""
|
||||
for _, line := range lines {
|
||||
body += line + "\n"
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
||||
t.Fatalf("write log: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func itoa(n int) string { return strconv.Itoa(n) }
|
||||
|
||||
// The read is bounded, so on a log that has grown all day it starts in the
|
||||
// middle of a line. That fragment is not a line and must not come back as one,
|
||||
// and anything before the budget is out of reach — which the page reports the
|
||||
// same way as a message whose lines have rotated away.
|
||||
func TestQueueLinesReadsABoundedTail(t *testing.T) {
|
||||
old := queueScanBytes
|
||||
queueScanBytes = 120
|
||||
t.Cleanup(func() { queueScanBytes = old })
|
||||
|
||||
path := writeLog(t,
|
||||
"host postfix/smtp[26]: ABC123: too far back to reach, padded out past the budget with this run of filler text",
|
||||
"host postfix/smtp[26]: ABC123: within the budget",
|
||||
)
|
||||
|
||||
lines, err := QueueLines(path, "ABC123", 200)
|
||||
if err != nil {
|
||||
t.Fatalf("QueueLines: %v", err)
|
||||
}
|
||||
if len(lines) != 1 || !strings.HasSuffix(lines[0], "within the budget") {
|
||||
t.Errorf("got %d lines, want only the one inside the budget:\n%s", len(lines), strings.Join(lines, "\n"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user