mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-10-15 08:33:03 +00:00
b707512e38
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.
148 lines
4.0 KiB
Go
148 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/server"
|
|
)
|
|
|
|
func TestShouldAutoImport(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
|
|
// No env vars => false.
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
if server.ShouldAutoImport() {
|
|
t.Fatalf("expected false when no env vars set")
|
|
}
|
|
|
|
// Env var set => true.
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "anything")
|
|
if !server.ShouldAutoImport() {
|
|
t.Fatalf("expected true when init config env var set")
|
|
}
|
|
|
|
// Existing config should disable auto-import.
|
|
if err := os.WriteFile(filepath.Join(dir, "nodes.enc"), []byte("exists"), 0o600); err != nil {
|
|
t.Fatalf("write nodes.enc: %v", err)
|
|
}
|
|
if server.ShouldAutoImport() {
|
|
t.Fatalf("expected false when nodes.enc exists")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_ValidPayload(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
|
|
pass := "test-passphrase"
|
|
cp := config.NewConfigPersistence(dir)
|
|
exported, err := cp.ExportConfig(pass)
|
|
if err != nil {
|
|
t.Fatalf("ExportConfig: %v", err)
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", pass)
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", exported)
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
|
|
if err := server.PerformAutoImport(); err != nil {
|
|
t.Fatalf("performAutoImport: %v", err)
|
|
}
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "nodes.enc")); err != nil {
|
|
t.Fatalf("expected nodes.enc to exist: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_MissingPassphrase(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "data")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
|
|
if err := server.PerformAutoImport(); err == nil {
|
|
t.Fatalf("expected error")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_MissingData(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "pass")
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", "")
|
|
|
|
if err := server.PerformAutoImport(); err == nil {
|
|
t.Fatalf("expected error")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_File(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
|
|
pass := "test-passphrase"
|
|
cp := config.NewConfigPersistence(dir)
|
|
exported, err := cp.ExportConfig(pass)
|
|
if err != nil {
|
|
t.Fatalf("ExportConfig: %v", err)
|
|
}
|
|
|
|
importFile := filepath.Join(dir, "import.enc")
|
|
if err := os.WriteFile(importFile, []byte(exported), 0600); err != nil {
|
|
t.Fatalf("WriteFile: %v", err)
|
|
}
|
|
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", pass)
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", importFile)
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", "")
|
|
|
|
if err := server.PerformAutoImport(); err != nil {
|
|
t.Fatalf("performAutoImport with file: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_FileReadError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "pass")
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", filepath.Join(dir, "nonexistent"))
|
|
|
|
if err := server.PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error reading nonexistent file")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_NormalizeError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "pass")
|
|
t.Setenv("PULSE_INIT_CONFIG_DATA", " ") // Will trigger "payload is empty" error
|
|
|
|
if err := server.PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error from normalizeImportPayload")
|
|
}
|
|
}
|
|
|
|
func TestPerformAutoImport_FileNormalizeError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
t.Setenv("PULSE_DATA_DIR", dir)
|
|
t.Setenv("PULSE_INIT_CONFIG_PASSPHRASE", "pass")
|
|
|
|
importFile := filepath.Join(dir, "empty.enc")
|
|
if err := os.WriteFile(importFile, []byte(" "), 0600); err != nil {
|
|
t.Fatalf("write import file: %v", err)
|
|
}
|
|
t.Setenv("PULSE_INIT_CONFIG_FILE", importFile)
|
|
|
|
if err := server.PerformAutoImport(); err == nil {
|
|
t.Fatal("expected error from normalizeImportPayload for file")
|
|
}
|
|
}
|