Merge pull request 'End-to-end test with a real Postfix' (#17) from test/e2e-postfix into main

This commit is contained in:
squidd 2026-11-09 16:48:22 +01:00
commit 77fcdb3bbe
9 changed files with 592 additions and 1 deletions

View file

@ -1,4 +1,4 @@
.PHONY: all admin test build clean
.PHONY: all admin test build e2e clean
all: admin build
@ -13,6 +13,11 @@ test:
go test ./...
cd admin && npm test
# End-to-end test: parsimony behind a real Postfix, both in Docker (needs a running Docker daemon).
# Set E2E_PARSIMONY_IMAGE to reuse an image instead of building one.
e2e:
go test -tags e2e -count=1 -timeout 15m -v ./e2e
clean:
rm -f parsimony
node admin/scripts/clean.mjs

View file

@ -83,11 +83,20 @@ It is served on the API listener together with `/api`, so it needs no CORS, and
make admin # npm ci && npm run build
make build # go build, embeds the UI
make test # go test + vitest
make e2e # end-to-end with a real Postfix in Docker (about 2 minutes, mostly the image build)
```
Without `make admin` the binary still builds and the UI answers 503 with a hint.
For frontend work run the daemon, then `cd admin && npm run dev` (Vite proxies `/api` to `127.0.0.1:8080`).
## End-to-end test
`make e2e` starts a real Postfix and parsimony in Docker and walks the whole path: Postfix hands list mail to
parsimony over LMTP, parsimony queues it and relays the copies back through Postfix, which delivers them to
maildirs. It checks the list headers and VERP return path, a real `550 5.1.1` from Postfix disabling a member after
three hard bounces, a real LMTP `550` for a non-member, join and leave by email, moderation, the public archive
and one-click unsubscribe. The files are in `e2e/` and behind the `e2e` build tag, so `go test ./...` stays fast.
## Postfix
Deliver the list domains to parsimony over LMTP and keep the `+` extensions intact, for example:

525
e2e/e2e_test.go Normal file
View file

@ -0,0 +1,525 @@
//go:build e2e
// Package e2e runs parsimony behind a real Postfix in Docker.
//
// make e2e
//
// One Postfix container plays every MTA role: it receives list mail and hands
// it to parsimony over LMTP, accepts the relayed copies, and delivers them to
// local maildirs. members.test has three mailboxes (alice, bob, carol); any
// other user gets a real "550 5.1.1 User unknown" from Postfix.
package e2e
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/smtp"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
)
const (
token = "e2e-token"
domain = "lists.example.test"
)
var (
suffix = fmt.Sprint(os.Getpid())
network = "pm-e2e-net-" + suffix
postfixC = "pm-e2e-postfix-" + suffix
parsimonyC = "pm-e2e-parsimony-" + suffix
smtpAddr string
apiBase string
webBase string
)
func docker(args ...string) (string, error) {
out, err := exec.Command("docker", args...).CombinedOutput()
return strings.TrimSpace(string(out)), err
}
func mustDocker(args ...string) string {
out, err := docker(args...)
if err != nil {
fmt.Fprintf(os.Stderr, "docker %s: %v\n%s\n", strings.Join(args, " "), err, out)
cleanup()
os.Exit(2)
}
return out
}
func freePort() int {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer ln.Close()
return ln.Addr().(*net.TCPAddr).Port
}
func cleanup() {
docker("rm", "-f", parsimonyC, postfixC)
docker("network", "rm", network)
}
func TestMain(m *testing.M) {
root, _ := filepath.Abs("..")
image := os.Getenv("E2E_PARSIMONY_IMAGE")
if image == "" {
image = "parsimony:e2e"
mustDocker("build", "-t", image, root)
}
mustDocker("build", "-t", "parsimony-e2e-postfix", filepath.Join(root, "e2e", "postfix"))
mustDocker("network", "create", network)
apiPort, webPort := freePort(), freePort()
apiBase = fmt.Sprintf("http://127.0.0.1:%d", apiPort)
webBase = fmt.Sprintf("http://127.0.0.1:%d", webPort)
cfg := filepath.Join(os.TempDir(), "pm-e2e-"+suffix+".yaml")
os.WriteFile(cfg, []byte(fmt.Sprintf(`db: /data/parsimony.db
base_url: %s
lmtp:
network: tcp
addr: 0.0.0.0:2424
relay:
addr: postfix:25
concurrency: 4
api:
listen: 0.0.0.0:8080
web:
listen: 0.0.0.0:8081
`, webBase)), 0o644)
defer os.Remove(cfg)
mustDocker("run", "-d", "--name", postfixC, "--network", network, "--network-alias", "postfix",
"-p", "127.0.0.1::25", "parsimony-e2e-postfix")
mustDocker("run", "-d", "--name", parsimonyC, "--network", network, "--network-alias", "parsimony",
"--read-only", "--cap-drop", "ALL", "-e", "PARSIMONY_API_TOKEN="+token,
"-v", cfg+":/etc/parsimony/parsimony.yaml:ro",
"-p", fmt.Sprintf("127.0.0.1:%d:8080", apiPort), "-p", fmt.Sprintf("127.0.0.1:%d:8081", webPort), image)
smtpAddr = mustDocker("port", postfixC, "25/tcp")
smtpAddr = strings.Split(strings.Split(smtpAddr, "\n")[0], " ")[0] // "127.0.0.1:49153"
if !waitFor(60*time.Second, func() bool {
resp, err := http.Get(apiBase + "/api/v1/health")
if err != nil {
return false
}
resp.Body.Close()
return resp.StatusCode == 200 && smtpBanner()
}) {
logs, _ := docker("logs", parsimonyC)
plogs, _ := docker("logs", postfixC)
fmt.Fprintf(os.Stderr, "stack did not start\n--- parsimony\n%s\n--- postfix\n%s\n", logs, plogs)
cleanup()
os.Exit(2)
}
code := m.Run()
if code != 0 {
logs, _ := docker("logs", parsimonyC)
plogs, _ := docker("logs", postfixC)
fmt.Fprintf(os.Stderr, "\n=== parsimony log ===\n%s\n=== postfix log ===\n%s\n", logs, plogs)
}
cleanup()
os.Exit(code)
}
// smtpBanner reports whether Postfix answers with its 220 greeting. A plain TCP
// connect is not enough: Docker's proxy accepts connections before Postfix listens.
func smtpBanner() bool {
conn, err := net.DialTimeout("tcp", smtpAddr, 2*time.Second)
if err != nil {
return false
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
buf := make([]byte, 64)
n, _ := conn.Read(buf)
return strings.HasPrefix(string(buf[:n]), "220")
}
func waitFor(d time.Duration, cond func() bool) bool {
deadline := time.Now().Add(d)
for time.Now().Before(deadline) {
if cond() {
return true
}
time.Sleep(200 * time.Millisecond)
}
return false
}
// --- helpers ---------------------------------------------------------------
func call(t *testing.T, method, path, body string, out any) int {
t.Helper()
req, _ := http.NewRequest(method, apiBase+path, strings.NewReader(body))
req.Header.Set("Authorization", "Bearer "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
if out != nil && len(b) > 0 {
if err := json.Unmarshal(b, out); err != nil {
t.Fatalf("%s %s: %v: %s", method, path, err, b)
}
}
return resp.StatusCode
}
func mustCall(t *testing.T, want int, method, path, body string, out any) {
t.Helper()
if got := call(t, method, path, body, out); got != want {
t.Fatalf("%s %s = %d, want %d", method, path, got, want)
}
}
var msgCounter int
// send submits a message to Postfix like any MTA would.
func send(t *testing.T, from, to, subject, body string, headers ...string) {
t.Helper()
msgCounter++
msg := fmt.Sprintf("From: %s\r\nTo: %s\r\nSubject: %s\r\nMessage-ID: <e2e-%s-%d@%s>\r\nDate: %s\r\n%s\r\n%s\r\n",
from, to, subject, suffix, msgCounter, "tests.example", time.Now().Format(time.RFC1123Z),
strings.Join(headers, ""), body)
if err := submit(from, to, msg); err != nil {
t.Fatalf("send %q to %s: %v", subject, to, err)
}
}
// submit speaks SMTP to Postfix with timeouts, so a stuck server fails the test instead of hanging it.
func submit(from, to, msg string) error {
conn, err := net.DialTimeout("tcp", smtpAddr, 5*time.Second)
if err != nil {
return err
}
conn.SetDeadline(time.Now().Add(30 * time.Second))
c, err := smtp.NewClient(conn, "127.0.0.1")
if err != nil {
conn.Close()
return err
}
defer c.Close()
if err := c.Mail(from); err != nil {
return err
}
if err := c.Rcpt(to); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
if _, err := w.Write([]byte(msg)); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return c.Quit()
}
// mailbox returns the raw messages delivered to a members.test user.
func mailbox(user string) []string {
script := fmt.Sprintf(`for f in /var/mail/vhosts/members.test/%s/new/*; do [ -f "$f" ] && { echo "=====FILE====="; cat "$f"; }; done; true`, user)
out, _ := docker("exec", postfixC, "sh", "-c", script)
var msgs []string
for _, part := range strings.Split(out, "=====FILE=====\n") {
if strings.TrimSpace(part) != "" {
msgs = append(msgs, part)
}
}
return msgs
}
func waitMail(t *testing.T, user, contains string) string {
t.Helper()
var found string
ok := waitFor(30*time.Second, func() bool {
for _, m := range mailbox(user) {
if strings.Contains(m, contains) {
found = m
return true
}
}
return false
})
if !ok {
t.Fatalf("%s never received a message containing %q; mailbox has %d message(s)", user, contains, len(mailbox(user)))
}
return found
}
func header(msg, name string) string {
re := regexp.MustCompile(`(?im)^` + regexp.QuoteMeta(name) + `:[ \t]*(.*(?:\r?\n[ \t]+.*)*)`)
if m := re.FindStringSubmatch(msg); m != nil {
return strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(m[1], "\r\n", " "), "\n", " "))
}
return ""
}
func postfixLog() string {
out, _ := docker("logs", postfixC)
return out
}
type member struct {
Address string `json:"address"`
Disabled bool `json:"disabled"`
BounceCount int `json:"bounce_count"`
}
func members(t *testing.T) map[string]member {
t.Helper()
var ms []member
mustCall(t, 200, "GET", "/api/v1/lists/1/members", "", &ms)
out := map[string]member{}
for _, m := range ms {
out[m.Address] = m
}
return out
}
func queuePending(t *testing.T) int {
t.Helper()
var q struct {
Pending int `json:"pending"`
}
mustCall(t, 200, "GET", "/api/v1/queue", "", &q)
return q.Pending
}
func waitQueueEmpty(t *testing.T) {
t.Helper()
if !waitFor(30*time.Second, func() bool { return queuePending(t) == 0 }) {
t.Fatalf("queue did not drain: %d pending", queuePending(t))
}
}
// --- the scenario ------------------------------------------------------------
// The subtests build on each other (members, bounce counts, archive), so they run in order.
func TestWholePath(t *testing.T) {
list := "dev@" + domain
var unsubURL string
t.Run("create the list and its members", func(t *testing.T) {
mustCall(t, 201, "POST", "/api/v1/lists", `{"name":"dev","domain":"`+domain+`","title":"Dev"}`, nil)
mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"archive_policy":"public"}`, nil)
for _, a := range []string{"alice", "bob", "ghost"} { // ghost has no mailbox: Postfix will say 5.1.1
mustCall(t, 201, "POST", "/api/v1/lists/1/members", `{"address":"`+a+`@members.test","display_name":"`+a+`"}`, nil)
}
})
t.Run("a post reaches the members through Postfix with the right headers", func(t *testing.T) {
send(t, "Alice Smith <alice@members.test>", list, "First post", "hello everyone, write to carol@members.test")
msg := waitMail(t, "bob", "First post")
waitMail(t, "alice", "First post")
if got := header(msg, "List-Id"); got != "Dev <dev."+domain+">" {
t.Errorf("List-Id = %q", got)
}
if got := header(msg, "From"); got != `"Alice Smith via Dev" <`+list+`>` {
t.Errorf("From = %q", got)
}
if got := header(msg, "Reply-To"); !strings.Contains(got, "alice@members.test") {
t.Errorf("Reply-To = %q", got)
}
if got := header(msg, "Return-Path"); !regexp.MustCompile(`^<dev\+bounce-\d+-[0-9a-f]{12}@` + regexp.QuoteMeta(domain) + `>$`).MatchString(got) {
t.Errorf("Return-Path must be a signed VERP address, got %q", got)
}
if header(msg, "Precedence") != "list" || header(msg, "List-Post") != "<mailto:"+list+">" {
t.Errorf("list headers missing:\n%s", msg)
}
lu := header(msg, "List-Unsubscribe")
m := regexp.MustCompile(`<(` + regexp.QuoteMeta(webBase) + `/unsubscribe/\d+-[0-9a-f]{12})>`).FindStringSubmatch(lu)
if m == nil || !strings.Contains(lu, "<mailto:dev+leave@"+domain+">") {
t.Fatalf("List-Unsubscribe = %q", lu)
}
unsubURL = m[1]
if header(msg, "List-Unsubscribe-Post") != "List-Unsubscribe=One-Click" {
t.Error("one-click header missing")
}
if !strings.Contains(msg, "hello everyone") {
t.Error("body missing")
}
// Each recipient has its own unsubscribe link.
aliceMsg := waitMail(t, "alice", "First post")
if header(aliceMsg, "List-Unsubscribe") == lu {
t.Error("recipients must not share an unsubscribe link")
}
})
t.Run("the post is archived and public pages hide addresses", func(t *testing.T) {
var page struct {
Total int `json:"total"`
}
mustCall(t, 200, "GET", "/api/v1/lists/1/archive?q=first", "", &page)
if page.Total != 1 {
t.Fatalf("archive total = %d", page.Total)
}
resp, err := http.Get(webBase + "/archive/" + list + "/1")
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
body := string(b)
if resp.StatusCode != 200 || !strings.Contains(body, "First post") || !strings.Contains(body, "carol@…") ||
strings.Contains(body, "carol@members.test") {
t.Fatalf("public page: %d\n%.600s", resp.StatusCode, body)
}
})
t.Run("a real 5.1.1 from Postfix counts as a hard bounce and disables the member after three", func(t *testing.T) {
if !waitFor(30*time.Second, func() bool { return members(t)["ghost@members.test"].BounceCount == 1 }) {
t.Fatalf("ghost: %+v\n%s", members(t)["ghost@members.test"], postfixLog())
}
if m := members(t); m["alice@members.test"].BounceCount != 0 || m["bob@members.test"].BounceCount != 0 {
t.Fatalf("only the unknown mailbox may bounce: %+v", m)
}
send(t, "alice@members.test", list, "Second post", "two")
send(t, "alice@members.test", list, "Third post", "three")
if !waitFor(30*time.Second, func() bool { return members(t)["ghost@members.test"].Disabled }) {
t.Fatalf("ghost: %+v", members(t)["ghost@members.test"])
}
waitQueueEmpty(t)
waitMail(t, "bob", "Third post")
// Once disabled, nobody tries ghost again: the count stays at 3.
send(t, "alice@members.test", list, "Fourth post", "four")
waitMail(t, "bob", "Fourth post")
waitQueueEmpty(t)
if g := members(t)["ghost@members.test"]; g.BounceCount != 3 || !g.Disabled {
t.Fatalf("ghost: %+v", g)
}
})
t.Run("a non-member is refused with a real LMTP 550", func(t *testing.T) {
before := len(mailbox("bob"))
send(t, "mallory@elsewhere.test", list, "Spam", "buy now")
if !waitFor(30*time.Second, func() bool {
l := postfixLog()
return strings.Contains(l, "only list members may post") && strings.Contains(l, "status=bounced")
}) {
t.Fatalf("Postfix never reported the refusal:\n%s", postfixLog())
}
time.Sleep(time.Second)
if len(mailbox("bob")) != before {
t.Fatal("a refused post must not be delivered")
}
})
t.Run("join and confirm by email", func(t *testing.T) {
send(t, "carol@members.test", "dev+join@"+domain, "join", "please")
confirm := waitMail(t, "carol", "Confirm your subscription")
reply := regexp.MustCompile(`dev\+confirm-[0-9a-f]+@` + regexp.QuoteMeta(domain)).FindString(header(confirm, "Reply-To"))
if reply == "" {
t.Fatalf("no confirm address in Reply-To:\n%s", confirm)
}
if _, ok := members(t)["carol@members.test"]; ok {
t.Fatal("carol must not be a member before confirming")
}
send(t, "carol@members.test", reply, "Re: Confirm", "ok")
if !waitFor(30*time.Second, func() bool { _, ok := members(t)["carol@members.test"]; return ok }) {
t.Fatalf("carol never became a member\n%s", postfixLog())
}
send(t, "alice@members.test", list, "Welcome carol", "hi")
waitMail(t, "carol", "Welcome carol")
})
t.Run("leave by email", func(t *testing.T) {
send(t, "carol@members.test", "dev+leave@"+domain, "leave", "bye")
confirm := waitMail(t, "carol", "Confirm your unsubscription")
reply := regexp.MustCompile(`dev\+confirm-[0-9a-f]+@` + regexp.QuoteMeta(domain)).FindString(header(confirm, "Reply-To"))
send(t, "carol@members.test", reply, "Re: Confirm", "yes")
if !waitFor(30*time.Second, func() bool { _, ok := members(t)["carol@members.test"]; return !ok }) {
t.Fatal("carol is still a member")
}
})
t.Run("moderation holds a stranger's post until approved", func(t *testing.T) {
mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"post_policy":"moderated"}`, nil)
send(t, "dave@elsewhere.test", list, "Needs approval", "pending")
var held []struct {
ID int64 `json:"id"`
Subject string `json:"subject"`
}
if !waitFor(30*time.Second, func() bool {
call(t, "GET", "/api/v1/lists/1/held", "", &held)
return len(held) == 1
}) {
t.Fatalf("held: %+v", held)
}
if held[0].Subject != "Needs approval" {
t.Fatalf("held: %+v", held)
}
time.Sleep(time.Second)
for _, m := range mailbox("alice") {
if strings.Contains(m, "Needs approval") {
t.Fatal("held mail must not be delivered")
}
}
mustCall(t, 204, "POST", fmt.Sprintf("/api/v1/held/%d/approve", held[0].ID), "", nil)
waitMail(t, "alice", "Needs approval")
mustCall(t, 200, "PATCH", "/api/v1/lists/1", `{"post_policy":"members"}`, nil)
})
t.Run("one-click unsubscribe from a real message", func(t *testing.T) {
if _, ok := members(t)["bob@members.test"]; !ok {
t.Fatal("bob should still be subscribed")
}
// A GET (link prefetch) must not unsubscribe.
resp, err := http.Get(unsubURL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("confirm page: %d", resp.StatusCode)
}
if _, ok := members(t)["bob@members.test"]; !ok {
t.Fatal("GET must not unsubscribe")
}
resp, err = http.Post(unsubURL, "application/x-www-form-urlencoded", strings.NewReader("List-Unsubscribe=One-Click"))
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("one-click: %d", resp.StatusCode)
}
// The link came from bob's copy, so bob (and only bob) is gone.
ms := members(t)
if _, ok := ms["bob@members.test"]; ok {
t.Fatal("bob must be unsubscribed")
}
if _, ok := ms["alice@members.test"]; !ok {
t.Fatal("alice must stay")
}
before := len(mailbox("bob"))
send(t, "alice@members.test", list, "After bob left", "x")
waitMail(t, "alice", "After bob left")
waitQueueEmpty(t)
if len(mailbox("bob")) != before {
t.Fatal("bob must not receive mail after unsubscribing")
}
})
t.Run("the queue ends empty", func(t *testing.T) {
waitQueueEmpty(t)
})
}

20
e2e/postfix/Dockerfile Normal file
View file

@ -0,0 +1,20 @@
# A small Postfix used only by the end-to-end test.
FROM debian:stable-slim
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends postfix rsyslog \
&& rm -rf /var/lib/apt/lists/* \
&& groupadd -g 5000 vmail && useradd -u 5000 -g 5000 -d /var/mail/vhosts -s /usr/sbin/nologin vmail \
&& mkdir -p /var/mail/vhosts && chown vmail:vmail /var/mail/vhosts
COPY main.cf.append /tmp/main.cf.append
COPY transport vmailbox /etc/postfix/
COPY rsyslog.conf /etc/rsyslog.conf
COPY entrypoint.sh /entrypoint.sh
RUN cat /tmp/main.cf.append >> /etc/postfix/main.cf \
# no chroot: the smtp and lmtp clients must resolve docker network names
&& postconf -F '*/*/chroot=n' \
&& chmod 644 /etc/postfix/transport /etc/postfix/vmailbox \
&& chmod +x /entrypoint.sh \
&& postmap /etc/postfix/transport /etc/postfix/vmailbox
EXPOSE 25
# Postfix logs to syslog; the test reads /var/log/mail.log
CMD ["/entrypoint.sh"]

View file

@ -0,0 +1,6 @@
#!/bin/sh
# rsyslog gives Postfix a /dev/log; its output is mirrored to the container log.
rsyslogd
touch /var/log/mail.log
tail -F /var/log/mail.log &
exec postfix start-fg

View file

@ -0,0 +1,20 @@
myhostname = mx.lists.example.test
mydomain = lists.example.test
mydestination =
inet_interfaces = all
inet_protocols = ipv4
compatibility_level = 3.6
recipient_delimiter =
mynetworks = 127.0.0.0/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
smtpd_relay_restrictions = permit_mynetworks, reject_unauth_destination
# The list domain is handed to parsimony over LMTP, extensions (+join, +confirm-...) intact.
relay_domains = lists.example.test
transport_maps = hash:/etc/postfix/transport
# Members live in local maildirs; unknown users are rejected with 550 5.1.1.
virtual_mailbox_domains = members.test
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

2
e2e/postfix/rsyslog.conf Normal file
View file

@ -0,0 +1,2 @@
module(load="imuxsock")
*.* /var/log/mail.log

1
e2e/postfix/transport Normal file
View file

@ -0,0 +1 @@
lists.example.test lmtp:inet:parsimony:2424

3
e2e/postfix/vmailbox Normal file
View file

@ -0,0 +1,3 @@
alice@members.test members.test/alice/
bob@members.test members.test/bob/
carol@members.test members.test/carol/