Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

4.6 KB
Raw Blame
//go:build testing

package systems

import (
	"context"
	"fmt"
	"testing"

	"ganigeorgiev.com/pocketbase/pocketbase/core"
	entities "github.com/henrygd/beszel/internal/entities/system"
	"github.com/henrygd/beszel/internal/entities/systemd"
)

// The hub integration tests create/replace systems and cleanup the test apps quickly.
// Background SMART fetching can outlive teardown and crash in PocketBase internals (nil DB).
//
// We keep the explicit SMART refresh endpoint / method available, but disable
// the automatic background fetch during tests.
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()
}

// TESTING ONLY: HasSystem checks if a system with the given ID exists in the store
func (sm *SystemManager) HasSystem(systemID string) bool {
	return sm.systems.Has(systemID)
}

// TESTING ONLY: GetSystemStatusFromStore returns the status of a system with the given ID
// Returns an empty string if the system doesn't exist
func (sm *SystemManager) GetSystemStatusFromStore(systemID string) string {
	sys, ok := sm.systems.GetOk(systemID)
	if !ok {
		return ""
	}
	return sys.GetStatus()
}

// TESTING ONLY: GetSystemContextFromStore returns the context and cancel function for a system
func (sm *SystemManager) GetSystemContextFromStore(systemID string) (context.Context, context.CancelFunc, error) {
	sys, ok := sm.systems.GetOk(systemID)
	if !ok {
		return nil, nil, fmt.Errorf("no system")
	}
	return sys.ctx, sys.cancel, nil
}

// TESTING ONLY: GetSystemFromStore returns a store from the system
func (sm *SystemManager) GetSystemFromStore(systemID string) (*System, error) {
	sys, ok := sm.systems.GetOk(systemID)
	if !ok {
		return nil, fmt.Errorf("no system")
	}
	return sys, nil
}

// TESTING ONLY: GetAllSystemIDs returns a slice of all system IDs in the store
func (sm *SystemManager) GetAllSystemIDs() []string {
	data := sm.systems.GetAll()
	ids := make([]string, 0, len(data))
	for id := range data {
		ids = append(ids, id)
	}
	return ids
}

// TESTING ONLY: GetSystemData returns the combined data for a system with the given ID
// Returns nil if the system doesn't exist
// This method is intended for testing
func (sm *SystemManager) GetSystemData(systemID string) *entities.CombinedData {
	sys, ok := sm.systems.GetOk(systemID)
	if !ok {
		return nil
	}
	return sys.data
}

// TESTING ONLY: GetSystemHostPort returns the host and port for a system with the given ID
// Returns empty strings if the system doesn't exist
func (sm *SystemManager) GetSystemHostPort(systemID string) (string, string) {
	sys, ok := sm.systems.GetOk(systemID)
	if !ok {
		return "", ""
	}
	return sys.Host, sys.Port
}

// TESTING ONLY: SetSystemStatusInDB sets the status of a system directly and updates the database record
// This is intended for testing
// Returns false if the system doesn't exist
func (sm *SystemManager) SetSystemStatusInDB(systemID string, status string) bool {
	if !sm.HasSystem(systemID) {
		return false
	}

	// Update the database record
	record, err := sm.hub.FindRecordById("systems", systemID)
	if err != nil {
		return false
	}

	record.Set("status", status)
	err = sm.hub.Save(record)
	return err == nil
}

// 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(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() {
	s.cancel()
}

func (s *System) CreateRecords(data *entities.CombinedData) (*core.Record, error) {
	s.data = data
	return s.createRecords(data)
}

func CreateSystemdStatsRecords(app core.App, data []*systemd.Service, systemId string) error {
	return createSystemdStatsRecords(app, data, systemId)
}