From 19e8a67331c9eb67947dc4555d2b842b050d8300 Mon Sep 17 00:00:00 2001 From: Teodor Calin Date: Mon, 15 Jun 2026 18:08:42 +0000 Subject: [PATCH] test(daemon): isolate HOME for the package test binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Many pkg/daemon tests construct New(Config{}) with an empty IdentityPath, so the daemon resolves managed-engine state (~/.pilot/managed_.json), the hostname cache, and the network snapshot under os.UserHomeDir() — the real home. Run in parallel (within the package and across the packages go test ./... runs concurrently on a shared CI runner) those tests race on the same ~/.pilot files, flaking on macOS (the snapshot / hostname-cache / managed-engine tests, which pass in isolation). Add a TestMain that points HOME (and USERPROFILE) at a throwaway dir before any test runs, making the package hermetic. Verified: tests no longer touch the real ~/.pilot; suite green; repeated concurrent ./pkg/... runs clean. --- pkg/daemon/zz_testmain_test.go | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pkg/daemon/zz_testmain_test.go diff --git a/pkg/daemon/zz_testmain_test.go b/pkg/daemon/zz_testmain_test.go new file mode 100644 index 00000000..c0996b29 --- /dev/null +++ b/pkg/daemon/zz_testmain_test.go @@ -0,0 +1,36 @@ +package daemon + +import ( + "os" + "testing" +) + +// TestMain isolates HOME for the entire package test binary. +// +// Many tests here construct a daemon with New(Config{}) (empty +// IdentityPath). Such a daemon resolves its on-disk state — managed-engine +// files (~/.pilot/managed_.json), the hostname cache, the network +// snapshot — under os.UserHomeDir(). Without isolation those tests read and +// write the *real* home concurrently: both within this package's parallel +// tests and across the packages that `go test ./...` runs in parallel on a +// shared CI runner. That races on the same files and flakes (observed +// reddening macOS CI on the snapshot / hostname-cache / managed-engine +// tests, which pass in isolation). +// +// Pointing HOME (and USERPROFILE, for os.UserHomeDir on Windows) at a +// throwaway directory before any test runs makes every test in this +// package hermetic. Tests that set their own HOME via t.Setenv still work — +// they override and restore relative to this base. +func TestMain(m *testing.M) { + tmp, err := os.MkdirTemp("", "pilot-daemon-test-home-") + if err != nil { + panic("daemon test: create temp HOME: " + err.Error()) + } + _ = os.Setenv("HOME", tmp) + _ = os.Setenv("USERPROFILE", tmp) + + code := m.Run() + + _ = os.RemoveAll(tmp) + os.Exit(code) +}