package monitoring import ( "context" "testing" "time" "github.com/rcourtman/pulse-go-rewrite/internal/models" "github.com/rcourtman/pulse-go-rewrite/pkg/proxmox" "github.com/rs/zerolog" "github.com/stretchr/testify/require" ) func TestDockerCommandDiagnosticsDoNotBlockProxmoxPoll(t *testing.T) { const hostID = "fixture-docker" for _, tc := range []struct { name, message, wantStatus string prepare func(*testing.T, *Monitor) func() }{ {"queue-stop", "Queued docker host stop command", DockerCommandStatusQueued, func(t *testing.T, m *Monitor) func() { return func() { if _, err := m.QueueDockerHostStop(hostID); err != nil { t.Error(err) } } }}, {"queue-update", "Queued docker container update command", DockerCommandStatusQueued, func(t *testing.T, m *Monitor) func() { return func() { if _, err := m.QueueDockerContainerUpdateCommand(hostID, "fixture-container", "fixture container"); err != nil { t.Error(err) } } }}, {"queue-update-all", "Queued docker update all command", DockerCommandStatusQueued, func(t *testing.T, m *Monitor) func() { return func() { if _, err := m.QueueDockerUpdateAllCommand(hostID); err != nil { t.Error(err) } } }}, {"queue-check", "Queued docker check updates command", DockerCommandStatusQueued, func(t *testing.T, m *Monitor) func() { return func() { if _, err := m.QueueDockerCheckUpdatesCommand(hostID); err != nil { t.Error(err) } } }}, {"expire", "Docker command expired prior to dispatch", DockerCommandStatusExpired, func(t *testing.T, m *Monitor) func() { command, err := m.QueueDockerHostStop(hostID) require.NoError(t, err) past := time.Now().Add(-time.Minute) m.dockerCommands[hostID].status.ExpiresAt = &past return func() { if payload, status := m.FetchDockerCommandForHost(hostID); payload != nil || status != nil { t.Errorf("expired command %q offered again: %v, %+v", command.ID, payload, status) } } }}, } { t.Run(tc.name, func(t *testing.T) { m := newScheduleFixture(t.Context(), false, func(context.Context) ([]proxmox.Node, error) { return []proxmox.Node{{Node: scheduleFixtureNode, Status: "online", CPU: 0.42, Uptime: 36000}}, nil }) m.state.UpsertDockerHost(models.DockerHost{ ID: hostID, Hostname: hostID, Status: "online", Containers: []models.DockerContainer{{ ID: "fixture-container", Name: "fixture container", UpdateStatus: &models.DockerContainerUpdateStatus{UpdateAvailable: true}, }}, }) runCommand := tc.prepare(t, m) writer := heldDockerInventoryDiagnostic{ message: tc.message, entered: make(chan struct{}, 1), release: make(chan struct{}), } restore := setPackageTestLogOutput(t, writer, zerolog.InfoLevel) defer restore() var pollDone chan struct{} commandDone := make(chan struct{}) go func() { defer close(commandDone); runCommand() }() defer func() { close(writer.release) <-commandDone if pollDone != nil { <-pollDone } }() select { case <-writer.entered: case <-time.After(5 * time.Second): t.Fatal("command did not reach its existing diagnostic") } host := findDockerHost(t, m, hostID) require.NotNil(t, host.Command) require.Equal(t, tc.wantStatus, host.Command.Status) require.Equal(t, tc.name == "queue-stop", host.PendingUninstall) pollDone = make(chan struct{}) go func() { defer close(pollDone) // Run the same planner and worker execution used by the loop. m.planScheduledPolls(t.Context(), time.Now()) ctx, cancel := context.WithTimeout(t.Context(), time.Second) defer cancel() if task, ok := m.taskQueue.WaitNext(ctx); ok { m.executeAndRescheduleTask(t.Context(), task, nil, 0) } }() select { case <-pollDone: case <-time.After(3 * time.Second): t.Error("Proxmox polling stalled behind the Docker command diagnostic") return } nodes := m.state.GetSnapshot().Nodes require.Len(t, nodes, 1) require.Equal(t, scheduleFixtureNode, nodes[0].Name) require.InDelta(t, 0.42, nodes[0].CPU, 0.001) next, ok := m.taskQueue.Get(InstanceTypePVE, scheduleFixtureNode) require.True(t, ok) require.True(t, next.NextRun.After(time.Now()), "poll did not schedule its ordinary successor") // Terminal state and removal from both active indexes must already // be committed before the diagnostic writes, not postponed with it. if tc.wantStatus != DockerCommandStatusQueued { _, active := m.dockerCommands[hostID] _, indexed := m.dockerCommandIndex[host.Command.ID] require.False(t, active) require.False(t, indexed) } }) } }