mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-02-18 00:17:39 +01:00
fix: use proper Monitor constructor in PMG tests to initialize all maps
Fixes panic: assignment to entry in nil map in PMG polling tests. **Problem:** Tests were manually creating Monitor structs without initializing internal maps like pollStatusMap, causing nil map panics when recordTaskResult() tried to update task status. **Root Cause:** - TestPollPMGInstancePopulatesState (line 90) - TestPollPMGInstanceRecordsAuthFailures (line 189) Both created Monitor with only partial field initialization, missing: - pollStatusMap - dlqInsightMap - instanceInfoCache - Other internal state maps **Solution:** Changed both tests to use New() constructor which properly initializes all maps and internal state (monitor.go:1541). This ensures tests match production initialization and will automatically pick up any future map additions. **Tests:** ✅ TestPollPMGInstancePopulatesState - now passes ✅ TestPollPMGInstanceRecordsAuthFailures - now passes ✅ All monitoring tests pass (0.125s) Follows best practice: use constructors instead of manual struct creation to maintain initialization invariants.
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
||||
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
||||
"github.com/rcourtman/pulse-go-rewrite/pkg/pmg"
|
||||
)
|
||||
|
||||
@@ -87,25 +86,24 @@ func TestPollPMGInstancePopulatesState(t *testing.T) {
|
||||
t.Fatalf("unexpected client error: %v", err)
|
||||
}
|
||||
|
||||
mon := &Monitor{
|
||||
config: &config.Config{
|
||||
PMGInstances: []config.PMGInstance{
|
||||
{
|
||||
Name: "primary",
|
||||
Host: server.URL,
|
||||
User: "api@pmg",
|
||||
Password: "secret",
|
||||
MonitorMailStats: true,
|
||||
MonitorQueues: true,
|
||||
MonitorQuarantine: true,
|
||||
MonitorDomainStats: true,
|
||||
},
|
||||
cfg := &config.Config{
|
||||
PMGInstances: []config.PMGInstance{
|
||||
{
|
||||
Name: "primary",
|
||||
Host: server.URL,
|
||||
User: "api@pmg",
|
||||
Password: "secret",
|
||||
MonitorMailStats: true,
|
||||
MonitorQueues: true,
|
||||
MonitorQuarantine: true,
|
||||
MonitorDomainStats: true,
|
||||
},
|
||||
},
|
||||
state: models.NewState(),
|
||||
authFailures: make(map[string]int),
|
||||
lastAuthAttempt: make(map[string]time.Time),
|
||||
pbsBackupPollers: make(map[string]bool),
|
||||
}
|
||||
|
||||
mon, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create monitor: %v", err)
|
||||
}
|
||||
|
||||
mon.pollPMGInstance(context.Background(), "primary", client)
|
||||
@@ -187,22 +185,21 @@ func TestPollPMGInstanceRecordsAuthFailures(t *testing.T) {
|
||||
t.Fatalf("unexpected error creating token client: %v", err)
|
||||
}
|
||||
|
||||
mon := &Monitor{
|
||||
config: &config.Config{
|
||||
PMGInstances: []config.PMGInstance{
|
||||
{
|
||||
Name: "failing",
|
||||
Host: server.URL,
|
||||
User: "apitest@pmg",
|
||||
TokenName: "apitoken",
|
||||
TokenValue: "secret",
|
||||
},
|
||||
cfg := &config.Config{
|
||||
PMGInstances: []config.PMGInstance{
|
||||
{
|
||||
Name: "failing",
|
||||
Host: server.URL,
|
||||
User: "apitest@pmg",
|
||||
TokenName: "apitoken",
|
||||
TokenValue: "secret",
|
||||
},
|
||||
},
|
||||
state: models.NewState(),
|
||||
authFailures: make(map[string]int),
|
||||
lastAuthAttempt: make(map[string]time.Time),
|
||||
pbsBackupPollers: make(map[string]bool),
|
||||
}
|
||||
|
||||
mon, err := New(cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create monitor: %v", err)
|
||||
}
|
||||
|
||||
mon.pollPMGInstance(context.Background(), "failing", client)
|
||||
|
||||
Reference in New Issue
Block a user