Files
ITWorx Pulse release export bd774932d5
Public source validation / validate (push) Failing after 3m8s
Publish ITWorx Pulse source
2026-09-03 02:09:19 +02:00

62 lines
2.2 KiB
Go

package main
import (
"context"
"errors"
"testing"
"time"
"github.com/itworx/pulse/internal/agentsource"
"github.com/itworx/pulse/internal/agentstore"
"github.com/itworx/pulse/internal/datasource"
)
type sourceHealthReader map[agentstore.Capability]agentstore.Snapshot
func (reader sourceHealthReader) Latest(_ context.Context, capability agentstore.Capability) (agentstore.Snapshot, error) {
snapshot, ok := reader[capability]
if !ok {
return agentstore.Snapshot{}, agentstore.ErrNoSnapshot
}
return snapshot, nil
}
func TestReadAgentSourceHealthRequiresContainersForHolisticUnraidHealth(t *testing.T) {
now := time.Date(2026, 8, 21, 12, 0, 0, 0, time.UTC)
reader := sourceHealthReader{}
for _, capability := range requiredUnraidCapabilities {
reader[capability] = agentstore.Snapshot{Capability: capability, ObservedAt: now.Add(-time.Second), ReceivedAt: now}
}
stale := reader[agentstore.CapabilityContainers]
stale.ObservedAt = now.Add(-time.Hour)
reader[agentstore.CapabilityContainers] = stale
unraidHealth, storageHealth, err := readAgentSourceHealth(context.Background(), reader, now)
if err != nil {
t.Fatal(err)
}
if unraidHealth.State != datasource.HealthUnknown || unraidHealth.ReasonCode != agentsource.ReasonStale {
t.Fatalf("partially stale Unraid health = %#v", unraidHealth)
}
if storageHealth.State != datasource.HealthHealthy {
t.Fatalf("fresh storage health = %#v", storageHealth)
}
fresh := reader[agentstore.CapabilityContainers]
fresh.ObservedAt = now.Add(-time.Second)
reader[agentstore.CapabilityContainers] = fresh
unraidHealth, storageHealth, err = readAgentSourceHealth(context.Background(), reader, now)
if err != nil || unraidHealth.State != datasource.HealthHealthy || storageHealth.State != datasource.HealthHealthy {
t.Fatalf("fully fresh health unraid=%#v storage=%#v err=%v", unraidHealth, storageHealth, err)
}
}
func TestReadAgentSourceHealthPropagatesCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, _, err := readAgentSourceHealth(ctx, sourceHealthReader{}, time.Now().UTC())
if !errors.Is(err, context.Canceled) {
t.Fatalf("cancellation error = %v", err)
}
}