mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-08-25 08:52:06 +00:00
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.
121 lines
3.7 KiB
Go
121 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/truenas"
|
|
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/vmware"
|
|
)
|
|
|
|
func TestRouterMockMode_SeedsTrueNASAndVMwareSupplementalResources(t *testing.T) {
|
|
setMockModeForTest(t, true)
|
|
|
|
cfg := &config.Config{DataPath: t.TempDir()}
|
|
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
|
|
t.Cleanup(func() {
|
|
router.shutdownBackgroundWorkers()
|
|
})
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
source string
|
|
want unified.DataSource
|
|
}{
|
|
{name: "truenas", source: "truenas", want: unified.SourceTrueNAS},
|
|
{name: "vmware", source: "vmware-vsphere", want: unified.SourceVMware},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/api/resources?source="+tc.source, nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
router.resourceHandlers.HandleListResources(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
var resp ResourcesResponse
|
|
if err := json.NewDecoder(rec.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode resources response: %v", err)
|
|
}
|
|
if len(resp.Data) == 0 {
|
|
t.Fatalf("expected mock %s resources, got none", tc.source)
|
|
}
|
|
foundSource := false
|
|
for _, resource := range resp.Data {
|
|
for _, source := range resource.Sources {
|
|
if source == tc.want {
|
|
foundSource = true
|
|
break
|
|
}
|
|
}
|
|
if foundSource {
|
|
break
|
|
}
|
|
}
|
|
if !foundSource {
|
|
t.Fatalf("expected at least one resource with source %q, got %#v", tc.want, resp.Data)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRouterMockMode_SeedsVMwareSupplementalActivity(t *testing.T) {
|
|
setMockModeForTest(t, true)
|
|
|
|
adapter := mockSupplementalRecordsAdapter{source: unified.SourceVMware}
|
|
changes := adapter.SupplementalChanges(nil, "default")
|
|
if len(changes) == 0 {
|
|
t.Fatal("expected mock VMware supplemental activity changes")
|
|
}
|
|
if changes[0].Kind != unified.ChangeActivity || changes[0].SourceAdapter != unified.AdapterVMware {
|
|
t.Fatalf("unexpected mock VMware activity change: %#v", changes[0])
|
|
}
|
|
if changes[0].Metadata[unified.MetadataActivityType] == "" {
|
|
t.Fatalf("expected VMware activity metadata, got %#v", changes[0].Metadata)
|
|
}
|
|
if got := adapter.SupplementalChanges(nil, "org-b"); len(got) != 0 {
|
|
t.Fatalf("expected tenant-scoped mock activity to be empty for non-default org, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestRouterMockMode_RestoresPlatformFeatureFlagsAfterDisable(t *testing.T) {
|
|
t.Setenv(truenas.FeatureTrueNAS, "false")
|
|
t.Setenv(vmware.FeatureVMware, "false")
|
|
|
|
previousTrueNAS := truenas.IsFeatureEnabled()
|
|
previousVMware := vmware.IsFeatureEnabled()
|
|
truenas.ResetFeatureEnabledFromEnv()
|
|
vmware.ResetFeatureEnabledFromEnv()
|
|
t.Cleanup(func() {
|
|
truenas.SetFeatureEnabled(previousTrueNAS)
|
|
vmware.SetFeatureEnabled(previousVMware)
|
|
})
|
|
|
|
cfg := &config.Config{DataPath: t.TempDir()}
|
|
router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
|
|
t.Cleanup(func() {
|
|
router.shutdownBackgroundWorkers()
|
|
})
|
|
|
|
router.syncPlatformSupplementalProviders(true)
|
|
if !truenas.IsFeatureEnabled() {
|
|
t.Fatal("expected mock mode to force-enable TrueNAS feature flag")
|
|
}
|
|
if !vmware.IsFeatureEnabled() {
|
|
t.Fatal("expected mock mode to force-enable VMware feature flag")
|
|
}
|
|
|
|
router.syncPlatformSupplementalProviders(false)
|
|
if truenas.IsFeatureEnabled() {
|
|
t.Fatal("expected disabling mock mode to restore TrueNAS feature flag from env")
|
|
}
|
|
if vmware.IsFeatureEnabled() {
|
|
t.Fatal("expected disabling mock mode to restore VMware feature flag from env")
|
|
}
|
|
}
|