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:
rcourtman
2025-10-20 15:22:23 +00:00
parent 02b590966d
commit 656ae0d254

View File

@@ -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,8 +86,7 @@ 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",
@@ -101,11 +99,11 @@ func TestPollPMGInstancePopulatesState(t *testing.T) {
MonitorDomainStats: true, MonitorDomainStats: true,
}, },
}, },
}, }
state: models.NewState(),
authFailures: make(map[string]int), mon, err := New(cfg)
lastAuthAttempt: make(map[string]time.Time), if err != nil {
pbsBackupPollers: make(map[string]bool), t.Fatalf("failed to create monitor: %v", err)
} }
mon.pollPMGInstance(context.Background(), "primary", client) mon.pollPMGInstance(context.Background(), "primary", client)
@@ -187,8 +185,7 @@ 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",
@@ -198,11 +195,11 @@ func TestPollPMGInstanceRecordsAuthFailures(t *testing.T) {
TokenValue: "secret", TokenValue: "secret",
}, },
}, },
}, }
state: models.NewState(),
authFailures: make(map[string]int), mon, err := New(cfg)
lastAuthAttempt: make(map[string]time.Time), if err != nil {
pbsBackupPollers: make(map[string]bool), t.Fatalf("failed to create monitor: %v", err)
} }
mon.pollPMGInstance(context.Background(), "failing", client) mon.pollPMGInstance(context.Background(), "failing", client)