Lightweight server monitoring with historical data, docker stats, and alerts.
fix(tests): make test suites runnable on macOS (#2729)
Metronom94fab6cb6f92
diff --git a/agent/health/health.go b/agent/health/health.goindex 0fa1abee1..f6d00404f 100644--- a/agent/health/health.go+++ b/agent/health/health.go@@ -6,6 +6,7 @@ package healthimport ("errors"+ "io/fs""log""os""path/filepath"@@ -53,7 +54,12 @@ func Update() error {return updateHealthFile(healthFile)}-// CleanUp removes the health file+// CleanUp removes the health file. The path is shared by every agent on the host, so a file that+// is already gone - another agent removed it, or a test ran two agents - is not a failure to+// report: it would surface as the error from Start.func CleanUp() error {- return os.Remove(healthFile)+ if err := os.Remove(healthFile); err != nil && !errors.Is(err, fs.ErrNotExist) {+ return err+ }+ return nil}diff --git a/agent/health/health_test.go b/agent/health/health_test.goindex 2e9c48df7..1b5a86fb9 100644--- a/agent/health/health_test.go+++ b/agent/health/health_test.go@@ -63,3 +63,18 @@ func TestHealth(t *testing.T) {})})}++// The health file path is shared by every agent on the host, so a second removal must not be+// reported as a failure: it reaches the caller as the error returned by Agent.Start.+func TestCleanUpIsIdempotent(t *testing.T) {+ dir := t.TempDir()+ original := healthFile+ healthFile = filepath.Join(dir, "beszel_health")+ t.Cleanup(func() { healthFile = original })++ require.NoError(t, Update())+ require.FileExists(t, healthFile)+ require.NoError(t, CleanUp())+ require.NoFileExists(t, healthFile)+ assert.NoError(t, CleanUp(), "removing a health file that is already gone is not an error")+}diff --git a/agent/lifecycle_test.go b/agent/lifecycle_test.goindex a62fe8747..15c95607a 100644--- a/agent/lifecycle_test.go+++ b/agent/lifecycle_test.go@@ -32,15 +32,8 @@ func TestAgentCancellationReleasesListener(t *testing.T) {return true}, 2*time.Second, 10*time.Millisecond)cancel()- select {- case err := <-done:- require.NoError(t, err)- case <-time.After(2 * time.Second):- t.Fatal("agent did not stop after cancellation")- }- listener, err := net.Listen("tcp", opts.Addr)- require.NoError(t, err, "stopped agent must release its listening port")- require.NoError(t, listener.Close())+ requireAgentStopped(t, done, "agent did not stop after cancellation")+ requirePortReleased(t, opts.Addr)}}@@ -79,16 +72,38 @@ func TestAgentCancellationJoinsReconnect(t *testing.T) {require.NoError(t, client.Close())require.Eventually(t, cm.isConnectingNow, time.Second, time.Millisecond)cancel()+ requireAgentStopped(t, done, "agent did not join its pending reconnect")+ require.False(t, cm.isConnectingNow())+ requirePortReleased(t, opts.Addr)+}++// requireAgentStopped waits for Start to return after the context is cancelled.+// The window is a liveness bound, not a performance assertion: `go test ./...`+// runs packages in parallel, and on a loaded machine shutdown can take several+// seconds, which made a 2s wait flake.+func requireAgentStopped(t *testing.T, done <-chan error, msg string) {+ t.Helper()select {case err := <-done:require.NoError(t, err)- case <-time.After(2 * time.Second):- t.Fatal("agent did not join its pending reconnect")+ case <-time.After(30 * time.Second):+ t.Fatal(msg)}- require.False(t, cm.isConnectingNow())- listener, err := net.Listen("tcp", opts.Addr)- require.NoError(t, err)- require.NoError(t, listener.Close())+}++// requirePortReleased fails unless addr becomes bindable again. The kernel can+// hold the port for a moment after the agent closes its listener, so a single+// immediate bind flakes with "address already in use".+func requirePortReleased(t *testing.T, addr string) {+ t.Helper()+ require.Eventually(t, func() bool {+ listener, err := net.Listen("tcp", addr)+ if err != nil {+ return false+ }+ _ = listener.Close()+ return true+ }, 2*time.Second, 10*time.Millisecond, "stopped agent must release its listening port %s", addr)}func TestAgentCancellationDuringHandshake(t *testing.T) {@@ -112,10 +127,5 @@ func TestAgentCancellationDuringHandshake(t *testing.T) {t.Fatal("agent did not reach the HTTP upgrade")}cancel()- select {- case err := <-done:- require.NoError(t, err)- case <-time.After(2 * time.Second):- t.Error("agent did not cancel its pending handshake")- }+ requireAgentStopped(t, done, "agent did not cancel its pending handshake")}diff --git a/agent/server_test.go b/agent/server_test.goindex eef45cc3e..09b8cff68 100644--- a/agent/server_test.go+++ b/agent/server_test.go@@ -49,7 +49,7 @@ func TestStartServer(t *testing.T) {badSigner, err := gossh.NewSignerFromKey(badPrivKey)require.NoError(t, err)- socketFile := filepath.Join(t.TempDir(), "beszel-test.sock")+ socketFile := testSocketPath(t)tests := []struct {name string@@ -994,6 +994,17 @@ func TestGetHubVersionConcurrent(t *testing.T) {/////////////// Session Protocol Tests ///////////////////////////////////////////////////////////////////////////////////////////+// testSocketPath returns a short path for a unix socket. t.TempDir() embeds the+// test name, and on macOS it sits under /var/folders/…, so the full socket path+// can pass the 104-byte sun_path limit and the listener fails to bind.+func testSocketPath(t *testing.T) string {+ t.Helper()+ dir, err := os.MkdirTemp("", "bz")+ require.NoError(t, err)+ t.Cleanup(func() { _ = os.RemoveAll(dir) })+ return filepath.Join(dir, "s.sock")+}+// dialTestAgentClient starts an agent SSH server on a private unix socket and// connects a real SSH client whose ClientVersion mimics a hub of the given// version, so session-protocol behavior can be exercised end to end.@@ -1007,7 +1018,7 @@ func dialTestAgentClient(t *testing.T, clientVersion string) *gossh.Client {sshPubKey, err := gossh.NewPublicKey(pubKey)require.NoError(t, err)- socketFile := filepath.Join(t.TempDir(), "beszel-test.sock")+ socketFile := testSocketPath(t)agent, err := NewAgent("")require.NoError(t, err)@@ -1027,8 +1038,16 @@ func dialTestAgentClient(t *testing.T, clientVersion string) *gossh.Client {}})- // Wait for the listener instead of a fixed sleep.+ // Wait for the listener instead of a fixed sleep. A bind failure arrives on+ // errChan, so report it rather than waiting out the timeout with no reason.+ var startErr errorrequire.Eventually(t, func() bool {+ select {+ case err := <-errChan:+ startErr = err+ return true+ default:+ }conn, err := net.Dial("unix", socketFile)if err != nil {return false@@ -1036,6 +1055,7 @@ func dialTestAgentClient(t *testing.T, clientVersion string) *gossh.Client {_ = conn.Close()return true}, 5*time.Second, 25*time.Millisecond)+ require.NoError(t, startErr, "SSH server failed to start")client, err := gossh.Dial("unix", socketFile, &gossh.ClientConfig{User: "u",diff --git a/internal/site/src/lib/stores.ts b/internal/site/src/lib/stores.tsindex fc0d2905a..913d1b85e 100644--- a/internal/site/src/lib/stores.ts+++ b/internal/site/src/lib/stores.ts@@ -45,12 +45,12 @@ export const $maxValues = atom(false)const logTimestampsKey = "besz-log-ts"/** Whether to show timestamps in Docker and systemd logs */-export const $showLogTimestamps = atom<boolean>(JSON.parse(localStorage.getItem(logTimestampsKey) ?? "true"))+export const $showLogTimestamps = atom<boolean>(JSON.parse(globalThis.localStorage?.getItem(logTimestampsKey) ?? "true"))export function toggleLogTimestamps() {const next = !$showLogTimestamps.get()$showLogTimestamps.set(next)- localStorage.setItem(logTimestampsKey, JSON.stringify(next))+ globalThis.localStorage?.setItem(logTimestampsKey, JSON.stringify(next))}// export const UserSettingsSchema = v.object({