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