carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3import (
4 "errors"
5 "net"
6 "os"
7 "path/filepath"
8 "syscall"
9 "testing"
10 "time"
11)
12
13// TestNotifyReadyWritesToTheSocket: the whole contract. systemd hands the
14// process a unixgram socket path in NOTIFY_SOCKET and waits for READY=1
15// before calling the unit started — which is what turns "exec'd" into
16// "actually serving" for the adopt pass's restart check.
17//
18// Uses os.MkdirTemp directly (NOT t.TempDir): t.TempDir embeds the test
19// name in the path it hands back, and AF_UNIX socket paths are capped at
20// sizeof(sockaddr_un.sun_path) — 108 bytes on Linux. A long test name under
21// a long TMPDIR pushes that over the limit and net.ListenPacket fails with
22// "invalid argument", which is a test-harness artifact, not evidence
23// unixgram sockets are unsupported here — so only the genuinely-unsupported
24// errors (EAFNOSUPPORT, EPERM) are allowed to skip the test; anything else,
25// including a too-long path, must fail loudly.
26func TestNotifyReadyWritesToTheSocket(t *testing.T) {
27 dir, err := os.MkdirTemp("", "n")
28 if err != nil {
29 t.Fatalf("MkdirTemp: %v", err)
30 }
31 t.Cleanup(func() { os.RemoveAll(dir) })
32 sock := filepath.Join(dir, "n.sock")
33 conn, err := net.ListenPacket("unixgram", sock)
34 if err != nil {
35 if errors.Is(err, syscall.EAFNOSUPPORT) || errors.Is(err, syscall.EPERM) {
36 t.Skipf("unixgram unavailable here: %v", err)
37 }
38 t.Fatalf("ListenPacket(unixgram, %s): %v", sock, err)
39 }
40 defer conn.Close()
41 t.Setenv("NOTIFY_SOCKET", sock)
42
43 if err := NotifyReady(); err != nil {
44 t.Fatalf("NotifyReady: %v", err)
45 }
46 buf := make([]byte, 64)
47 conn.SetReadDeadline(time.Now().Add(2 * time.Second))
48 n, _, err := conn.ReadFrom(buf)
49 if err != nil {
50 t.Fatalf("nothing arrived on the notify socket: %v", err)
51 }
52 if got := string(buf[:n]); got != "READY=1" {
53 t.Fatalf("sent %q, want READY=1", got)
54 }
55}
56
57// TestNotifyReadyIsANoOpOutsideSystemd: the console runs under `go run`, in
58// tests and on a laptop, and none of those set NOTIFY_SOCKET. Absent is
59// normal, not an error.
60func TestNotifyReadyIsANoOpOutsideSystemd(t *testing.T) {
61 t.Setenv("NOTIFY_SOCKET", "")
62 if err := NotifyReady(); err != nil {
63 t.Fatalf("NotifyReady with no socket = %v, want nil", err)
64 }
65}
66