test: initialise and stop hub fixture workers before cleanup (#2463)

This commit is contained in:
user01010111
2026-09-29 12:42:36 +13:00
committed by GitHub
parent 130365f5d3
commit 8b38db1ba0
14 changed files with 292 additions and 110 deletions

View File

@@ -5,6 +5,7 @@ package systems
import (
"context"
"fmt"
"testing"
entities "github.com/henrygd/beszel/internal/entities/system"
"github.com/henrygd/beszel/internal/entities/systemd"
@@ -21,6 +22,11 @@ func backgroundSmartFetchEnabled() bool { return false }
// Background ZFS fetching follows the same policy as SMART fetching.
func backgroundZfsFetchEnabled() bool { return false }
// InitializeSSHConfigForTesting prepares SSH without registering hooks or starting systems.
func (sm *SystemManager) InitializeSSHConfigForTesting() error {
return sm.createSSHClientConfig()
}
// TESTING ONLY: GetSystemCount returns the number of systems in the store
func (sm *SystemManager) GetSystemCount() int {
return sm.systems.Length()
@@ -106,25 +112,32 @@ func (sm *SystemManager) SetSystemStatusInDB(systemID string, status string) boo
record.Set("status", status)
err = sm.hub.Save(record)
if err != nil {
return false
}
return true
return err == nil
}
// TESTING ONLY: RemoveAllSystems removes all systems from the store
// TESTING ONLY: RemoveAllSystems cancels and joins updaters before database cleanup.
// Callers must stop producers that can add systems before calling it.
func (sm *SystemManager) RemoveAllSystems() {
for _, system := range sm.systems.GetAll() {
sm.RemoveSystem(system.Id)
}
// Removed or replaced systems may still be finishing their last update.
sm.updaters.Wait()
sm.smartFetchMap.StopCleaner()
sm.zfsFetchMap.StopCleaner()
}
// ResetContextForTesting replaces the manager context for a new synctest bubble.
func (sm *SystemManager) ResetContextForTesting() {
func (sm *SystemManager) ResetContextForTesting(t testing.TB) {
t.Helper()
sm.cancel()
sm.updaters.Wait()
sm.ctx, sm.cancel = context.WithCancel(context.Background())
cancel := sm.cancel
t.Cleanup(func() {
cancel()
sm.updaters.Wait()
})
}
func (s *System) StopUpdater() {