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"
|
"time"
|
||||||
|
|
||||||
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
||||||
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
||||||
"github.com/rcourtman/pulse-go-rewrite/pkg/pmg"
|
"github.com/rcourtman/pulse-go-rewrite/pkg/pmg"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -87,25 +86,24 @@ func TestPollPMGInstancePopulatesState(t *testing.T) {
|
|||||||
t.Fatalf("unexpected client error: %v", err)
|
t.Fatalf("unexpected client error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mon := &Monitor{
|
cfg := &config.Config{
|
||||||
config: &config.Config{
|
PMGInstances: []config.PMGInstance{
|
||||||
PMGInstances: []config.PMGInstance{
|
{
|
||||||
{
|
Name: "primary",
|
||||||
Name: "primary",
|
Host: server.URL,
|
||||||
Host: server.URL,
|
User: "api@pmg",
|
||||||
User: "api@pmg",
|
Password: "secret",
|
||||||
Password: "secret",
|
MonitorMailStats: true,
|
||||||
MonitorMailStats: true,
|
MonitorQueues: true,
|
||||||
MonitorQueues: true,
|
MonitorQuarantine: true,
|
||||||
MonitorQuarantine: true,
|
MonitorDomainStats: true,
|
||||||
MonitorDomainStats: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
state: models.NewState(),
|
}
|
||||||
authFailures: make(map[string]int),
|
|
||||||
lastAuthAttempt: make(map[string]time.Time),
|
mon, err := New(cfg)
|
||||||
pbsBackupPollers: make(map[string]bool),
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create monitor: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mon.pollPMGInstance(context.Background(), "primary", client)
|
mon.pollPMGInstance(context.Background(), "primary", client)
|
||||||
@@ -187,22 +185,21 @@ func TestPollPMGInstanceRecordsAuthFailures(t *testing.T) {
|
|||||||
t.Fatalf("unexpected error creating token client: %v", err)
|
t.Fatalf("unexpected error creating token client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mon := &Monitor{
|
cfg := &config.Config{
|
||||||
config: &config.Config{
|
PMGInstances: []config.PMGInstance{
|
||||||
PMGInstances: []config.PMGInstance{
|
{
|
||||||
{
|
Name: "failing",
|
||||||
Name: "failing",
|
Host: server.URL,
|
||||||
Host: server.URL,
|
User: "apitest@pmg",
|
||||||
User: "apitest@pmg",
|
TokenName: "apitoken",
|
||||||
TokenName: "apitoken",
|
TokenValue: "secret",
|
||||||
TokenValue: "secret",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
state: models.NewState(),
|
}
|
||||||
authFailures: make(map[string]int),
|
|
||||||
lastAuthAttempt: make(map[string]time.Time),
|
mon, err := New(cfg)
|
||||||
pbsBackupPollers: make(map[string]bool),
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create monitor: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mon.pollPMGInstance(context.Background(), "failing", client)
|
mon.pollPMGInstance(context.Background(), "failing", client)
|
||||||
|
|||||||
Reference in New Issue
Block a user