From 73d35835c5d38a1fed2af9bb29f331e4a32b9788 Mon Sep 17 00:00:00 2001 From: BaaRai <130491490+BaaRai@users.noreply.github.com> Date: Fri, 20 Nov 2026 06:38:10 +0100 Subject: [PATCH] test(agent,server): run the tests in a temp directory of their own Norma keeps background-task output in os.TempDir()/norma. A test that starts a Norma session without a private TMPDIR wrote into the host's /tmp/norma. TestMain of the agent and server packages now creates a directory with os.MkdirTemp("", "artex-test-tmp-"), sets TMPDIR to it before m.Run and removes it afterwards on every exit path. The directory is world-writable and sticky like /tmp: MkdirTemp creates it 0700, which the executor of the root tests could not traverse. In the server package the setup comes after RunExecShimIfRequested, which stays the first call; the database suffix, the test key and the advisory lock run in the same order as before. A test that sets TMPDIR itself keeps its own. --- agent/testmain_test.go | 28 ++++++++++++++++++++++++---- server/testmain_test.go | 28 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/agent/testmain_test.go b/agent/testmain_test.go index aa088a7..c3184bb 100644 --- a/agent/testmain_test.go +++ b/agent/testmain_test.go @@ -2,6 +2,7 @@ package agent import ( "database/sql" + "log" "os" "testing" @@ -13,22 +14,41 @@ import ( // agent test suite so cross-package DELETE cleanup races with db/server // packages are avoided when running `go test ./...`. func TestMain(m *testing.M) { + // Norma keeps background-task output in os.TempDir()/norma. The tests get a temp + // directory of their own, so a session they start never writes the host's + // /norma. It is world-writable and sticky like /tmp: MkdirTemp makes it + // 0700, which an executor process could not traverse. + tmp, err := os.MkdirTemp("", "artex-test-tmp-") + if err != nil { + log.Fatalf("test temp directory: %v", err) + } + if err := os.Chmod(tmp, 0o777|os.ModeSticky); err != nil { + os.RemoveAll(tmp) + log.Fatalf("test temp directory: %v", err) + } + os.Setenv("TMPDIR", tmp) + // run removes the temp directory on every exit path: os.Exit runs no defer. + run := func() int { + code := m.Run() + os.RemoveAll(tmp) + return code + } // Own database per package (#80): with separate databases, packages stop // contending on shared tables and the advisory lock below stops // serializing go test ./... across packages. db.SuffixTestDatabase("agent") dsn, _, err := db.DSN() if err != nil { - os.Exit(m.Run()) + os.Exit(run()) } conn, err := sql.Open("pgx", dsn) if err != nil || conn.Ping() != nil { - os.Exit(m.Run()) + os.Exit(run()) } defer conn.Close() if _, err := conn.Exec(`SELECT pg_advisory_lock(7337741002)`); err != nil { - os.Exit(m.Run()) + os.Exit(run()) } defer conn.Exec(`SELECT pg_advisory_unlock(7337741002)`) //nolint:errcheck - os.Exit(m.Run()) + os.Exit(run()) } diff --git a/server/testmain_test.go b/server/testmain_test.go index d0cfb4a..05a0dec 100644 --- a/server/testmain_test.go +++ b/server/testmain_test.go @@ -2,6 +2,7 @@ package server import ( "database/sql" + "log" "os" "testing" @@ -16,6 +17,25 @@ func TestMain(m *testing.M) { // The executor tests (execpriv_root_linux_test.go) start this binary as the bash // shim and as the MCP drop shim, the way cmd/artex's main does. RunExecShimIfRequested(os.Args) + // Norma keeps background-task output in os.TempDir()/norma. The tests get a temp + // directory of their own, so a session they start never writes the host's + // /norma. It is world-writable and sticky like /tmp: MkdirTemp makes it + // 0700, which the executor of the root tests could not traverse. + tmp, err := os.MkdirTemp("", "artex-test-tmp-") + if err != nil { + log.Fatalf("test temp directory: %v", err) + } + if err := os.Chmod(tmp, 0o777|os.ModeSticky); err != nil { + os.RemoveAll(tmp) + log.Fatalf("test temp directory: %v", err) + } + os.Setenv("TMPDIR", tmp) + // run removes the temp directory on every exit path: os.Exit runs no defer. + run := func() int { + code := m.Run() + os.RemoveAll(tmp) + return code + } // Own database per package (#80): with separate databases, packages stop // contending on shared tables and the advisory lock below stops // serializing go test ./... across packages. @@ -28,18 +48,18 @@ func TestMain(m *testing.M) { } dsn, _, err := db.DSN() if err != nil { - os.Exit(m.Run()) + os.Exit(run()) } conn, err := sql.Open("pgx", dsn) if err != nil || conn.Ping() != nil { - os.Exit(m.Run()) + os.Exit(run()) } defer conn.Close() if _, err := conn.Exec(`SELECT pg_advisory_lock(7337741002)`); err != nil { - os.Exit(m.Run()) + os.Exit(run()) } defer conn.Exec(`SELECT pg_advisory_unlock(7337741002)`) //nolint:errcheck - os.Exit(m.Run()) + os.Exit(run()) } // skipWithoutDSN skips the test when neither ARTEX_PG_DSN nor a config file