Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

test(api): own router worker cleanup in early API fixtures

pulse-triage[bot]fba39a8d4ce1
diff --git a/internal/api/admin_bypass_router_test.go b/internal/api/admin_bypass_router_test.go
index 8c6cfc55ba..2e4bc76cbb 100644
--- a/internal/api/admin_bypass_router_test.go
+++ b/internal/api/admin_bypass_router_test.go
@@ -18,9 +18,7 @@ func TestAdminBypassPreservesExplicitTokenAuthority(t *testing.T) {
record.OrgID = "default"
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "test")
- t.Cleanup(router.shutdownBackgroundWorkers)
- t.Cleanup(router.ShutdownResourceStores)
- t.Cleanup(router.ShutdownRBAC)
+ cleanupTestRouter(t, router)
router.mux.HandleFunc("/api/qualification-runner-token", RequireAuth(cfg, RequireScope(config.ScopeAgentExec, func(w http.ResponseWriter, req *http.Request) {
actual := getAPITokenRecordFromRequest(req)
if actual == nil || actual.ID != record.ID || isAdminBypassRequest(req.Context()) {
@@ -65,6 +63,7 @@ func TestAdminBypassDoesNotAllowAdminEndpointsByDefault(t *testing.T) {
record := newTokenRecord(t, "admin-bypass-test-token-123.12345678", nil, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/system/settings", nil)
rec := httptest.NewRecorder()
@@ -84,6 +83,7 @@ func TestAdminBypassAllowsAdminEndpointInDevMode(t *testing.T) {
record := newTokenRecord(t, "admin-bypass-dev-token-123.12345678", nil, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/system/settings", nil)
rec := httptest.NewRecorder()
@@ -103,6 +103,7 @@ func TestAdminBypassRequiresExplicitFlag(t *testing.T) {
record := newTokenRecord(t, "admin-bypass-flag-token-123.12345678", nil, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/system/settings", nil)
rec := httptest.NewRecorder()
@@ -122,6 +123,7 @@ func TestAdminBypassDeclinedOutsideDevMode(t *testing.T) {
record := newTokenRecord(t, "admin-bypass-prod-token-123.12345678", nil, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/system/settings", nil)
rec := httptest.NewRecorder()
diff --git a/internal/api/agent_profiles_security_test.go b/internal/api/agent_profiles_security_test.go
index 907544b070..f86218ef0a 100644
--- a/internal/api/agent_profiles_security_test.go
+++ b/internal/api/agent_profiles_security_test.go
@@ -14,6 +14,7 @@ func TestAgentProfilesRequireSettingsWriteScope(t *testing.T) {
record := newTokenRecord(t, rawToken, []string{config.ScopeSettingsRead}, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/admin/profiles/", nil)
req.Header.Set("X-API-Token", rawToken)
diff --git a/internal/api/agent_substrate_e2e_test.go b/internal/api/agent_substrate_e2e_test.go
index 56e9a78da4..c514545277 100644
--- a/internal/api/agent_substrate_e2e_test.go
+++ b/internal/api/agent_substrate_e2e_test.go
@@ -45,6 +45,7 @@ func TestAgentSubstrate_DiscoveryToTriageToDepthFlowsThroughHTTPBoundary(t *test
record := newTokenRecord(t, rawToken, []string{config.ScopeMonitoringRead}, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
// --- 1. Discovery: fetch the capabilities manifest. ---
// Intentionally unauthenticated — the manifest itself is public
@@ -265,8 +266,9 @@ func TestAgentSubstrate_NodeProvisioningCapabilitiesRouteThroughHTTPBoundary(t *
if err != nil {
t.Fatalf("new monitor: %v", err)
}
- defer monitor.Stop()
+ t.Cleanup(monitor.Stop)
router := NewRouter(cfg, monitor, nil, nil, func() error { return nil }, "1.0.0")
+ cleanupTestRouter(t, router)
req := httptest.NewRequest(http.MethodGet, "/api/agent/capabilities", nil)
rec := httptest.NewRecorder()
@@ -388,6 +390,7 @@ func TestAgentSubstrate_OperatorStateWriteRoundTripsThroughHTTPBoundary(t *testi
[]string{config.ScopeMonitoringRead, config.ScopeMonitoringWrite}, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
// Pull the manifest first to confirm what we're about to
// exercise matches what the discovery contract declares. This
@@ -607,6 +610,7 @@ func TestAgentSubstrate_ActionEndpointsEmitAgentStableEnvelope(t *testing.T) {
[]string{config.ScopeMonitoringRead, config.ScopeAIExecute}, nil)
cfg := newTestConfigWithTokens(t, record)
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+ cleanupTestRouter(t, router)
// First verify the manifest declares the three action
// capabilities, since the substrate's promise is that the
diff --git a/internal/api/router_fixture_cleanup_test.go b/internal/api/router_fixture_cleanup_test.go
new file mode 100644
index 0000000000..af55220c70
--- /dev/null
+++ b/internal/api/router_fixture_cleanup_test.go
@@ -0,0 +1,49 @@
+package api
+
+import (
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// cleanupTestRouter gives the fixture ownership of constructor-started workers.
+// Register immediately after construction, before request assertions can fail.
+func cleanupTestRouter(t *testing.T, router *Router) {
+ t.Helper()
+ t.Cleanup(func() {
+ if router.agentExecServer != nil {
+ router.agentExecServer.Shutdown()
+ }
+ router.shutdownBackgroundWorkers()
+ router.ShutdownResourceStores()
+ router.ShutdownRBAC()
+ })
+}
+
+func TestRouterFixtureCleanupCancelsLifecycle(t *testing.T) {
+ var router *Router
+ workerDone := make(chan struct{})
+ t.Run("fixture", func(t *testing.T) {
+ cfg := &config.Config{DataPath: t.TempDir()}
+ router = NewRouter(cfg, nil, nil, nil, nil, "test")
+ cleanupTestRouter(t, router)
+ router.startLifecycleWorker(func() {
+ <-router.lifecycleCtx.Done()
+ close(workerDone)
+ })
+ })
+ // Also clean up after a regression so the proof itself cannot leak.
+ defer router.shutdownBackgroundWorkers()
+ defer router.ShutdownResourceStores()
+ defer router.ShutdownRBAC()
+ select {
+ case <-router.lifecycleCtx.Done():
+ default:
+ t.Fatal("router lifecycle remains active after fixture cleanup")
+ }
+ select {
+ case <-workerDone:
+ default:
+ t.Fatal("router lifecycle worker was not joined by fixture cleanup")
+ }
+}