Files
pulse/internal/monitoring/mock_mode_config_test.go
T
rcourtman b707512e38 Clear all errcheck and gofmt violations so make lint gates on real findings
golangci-lint run ./... failed on ~190 pre-existing errcheck violations and
5 unformatted files, burying any new regression in noise. Fix all of them:

- Test files that hand-rolled mock-mode set/restore (vmware, truenas, and
  friends) now use the canonical setMockModeForTest/testutil.SetMockMode
  helper instead of drift copies that ignored SetEnabled errors.
- internal/mock and internal/monitoring tests get package-local
  mustSetEnabled/mustSetMockEnabled/mustSetMonitorMockMode helpers that
  fail the test on toggle errors.
- pkg/auth/sqlite_manager.go, pkg/metrics/store.go, pkg/server/server.go:
  rollbacks in defers use the explicit-discard idiom, migration renames and
  rollup commits log failures, the hosted reaper goroutine logs an error
  exit, shutdown mock-disable logs failures.
- Remaining test sites check errors with t.Fatalf/t.Errorf or explicitly
  discard best-effort calls (restore-chmods, handler-closure unmarshals)
  per existing repo style.
- gofmt: internal/api/maintenance_verification.go, internal/ai/demo.go and
  three findings test files.

Only dupl findings remain (44 pre-existing production-code duplication
pairs) — those need real refactors, not mechanical fixes.

Full test suites pass for every touched package.
2026-06-09 21:42:21 +01:00

102 lines
2.7 KiB
Go

package monitoring
import (
"testing"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
)
func TestKeepRealPollingInMockMode(t *testing.T) {
t.Run("default_false", func(t *testing.T) {
t.Setenv(mockKeepRealPollingEnv, "")
if keepRealPollingInMockMode() {
t.Fatal("expected default behavior to disable real polling in mock mode")
}
})
t.Run("explicit_false", func(t *testing.T) {
t.Setenv(mockKeepRealPollingEnv, "false")
if keepRealPollingInMockMode() {
t.Fatal("expected false override to disable real polling in mock mode")
}
})
t.Run("explicit_true", func(t *testing.T) {
t.Setenv(mockKeepRealPollingEnv, "true")
if !keepRealPollingInMockMode() {
t.Fatal("expected true override to keep real polling in mock mode")
}
})
}
func TestNew_MockModeClientInitializationRespectsKeepRealPollingSetting(t *testing.T) {
makeConfig := func(dataPath string) *config.Config {
return &config.Config{
DataPath: dataPath,
PVEInstances: []config.PVEInstance{
{
Name: "pve-test",
Host: "https://127.0.0.1:8006",
User: "root@pam",
Password: "secret",
},
},
}
}
withClientStub := func(t *testing.T, fn func(called *bool)) {
t.Helper()
orig := newProxmoxClientFunc
t.Cleanup(func() { newProxmoxClientFunc = orig })
called := false
newProxmoxClientFunc = func(cfg proxmox.ClientConfig) (PVEClientInterface, error) {
called = true
return &mockPVEClient{}, nil
}
fn(&called)
}
t.Run("disabled_by_default", func(t *testing.T) {
t.Setenv(mockKeepRealPollingEnv, "")
mustSetMockEnabled(t, true)
t.Cleanup(func() { mustSetMockEnabled(t, false) })
withClientStub(t, func(called *bool) {
monitor, err := New(makeConfig(t.TempDir()))
if err != nil {
t.Fatalf("New() returned error: %v", err)
}
t.Cleanup(func() { monitor.Stop() })
if *called {
t.Fatal("expected PVE client initialization to be skipped in mock mode by default")
}
if len(monitor.pveClients) != 0 {
t.Fatalf("expected 0 initialized PVE clients, got %d", len(monitor.pveClients))
}
})
})
t.Run("disabled_via_env", func(t *testing.T) {
t.Setenv(mockKeepRealPollingEnv, "false")
mustSetMockEnabled(t, true)
t.Cleanup(func() { mustSetMockEnabled(t, false) })
withClientStub(t, func(called *bool) {
monitor, err := New(makeConfig(t.TempDir()))
if err != nil {
t.Fatalf("New() returned error: %v", err)
}
t.Cleanup(func() { monitor.Stop() })
if *called {
t.Fatal("expected PVE client initialization to be skipped when override is false")
}
if len(monitor.pveClients) != 0 {
t.Fatalf("expected 0 initialized PVE clients, got %d", len(monitor.pveClients))
}
})
})
}