From 8412cc7ddbe6bcdcebfb051bbabb062676222324 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Thu, 22 Jan 2026 13:49:05 +0000 Subject: [PATCH] fix: env overrides and OS-aware test improvements - Add PBS/PMG polling interval environment variable overrides in config.go - Fix temp path expectation in detect_root_test.go using filepath.Join - Use EvalSymlinks for symlink target comparison in self_update_test.go - Add Linux-only skip for MAC fallback test in agent_new_test.go - Add OS-aware RAID/SMART assertions in agent_metrics_test.go --- internal/config/config.go | 44 ++++++++++++++++++++++++ internal/config/detect_root_test.go | 3 +- internal/dockeragent/self_update_test.go | 8 +++-- internal/hostagent/agent_metrics_test.go | 13 +++++++ internal/hostagent/agent_new_test.go | 4 +++ 5 files changed, 69 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 57a4f1509..5399eb7e7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -830,6 +830,50 @@ func Load() (*Config, error) { } } + if intervalStr := utils.GetenvTrim("PBS_POLLING_INTERVAL"); intervalStr != "" { + if dur, err := time.ParseDuration(intervalStr); err == nil { + if dur < 10*time.Second { + log.Warn().Dur("interval", dur).Msg("Ignoring PBS_POLLING_INTERVAL below 10s from environment") + } else { + cfg.PBSPollingInterval = dur + cfg.EnvOverrides["PBS_POLLING_INTERVAL"] = true + log.Info().Dur("interval", dur).Msg("Overriding PBS polling interval from environment") + } + } else if seconds, err := strconv.Atoi(intervalStr); err == nil { + if seconds < 10 { + log.Warn().Int("seconds", seconds).Msg("Ignoring PBS_POLLING_INTERVAL below 10s from environment") + } else { + cfg.PBSPollingInterval = time.Duration(seconds) * time.Second + cfg.EnvOverrides["PBS_POLLING_INTERVAL"] = true + log.Info().Int("seconds", seconds).Msg("Overriding PBS polling interval (seconds) from environment") + } + } else { + log.Warn().Str("value", intervalStr).Msg("Invalid PBS_POLLING_INTERVAL value, expected duration or seconds") + } + } + + if intervalStr := utils.GetenvTrim("PMG_POLLING_INTERVAL"); intervalStr != "" { + if dur, err := time.ParseDuration(intervalStr); err == nil { + if dur < 10*time.Second { + log.Warn().Dur("interval", dur).Msg("Ignoring PMG_POLLING_INTERVAL below 10s from environment") + } else { + cfg.PMGPollingInterval = dur + cfg.EnvOverrides["PMG_POLLING_INTERVAL"] = true + log.Info().Dur("interval", dur).Msg("Overriding PMG polling interval from environment") + } + } else if seconds, err := strconv.Atoi(intervalStr); err == nil { + if seconds < 10 { + log.Warn().Int("seconds", seconds).Msg("Ignoring PMG_POLLING_INTERVAL below 10s from environment") + } else { + cfg.PMGPollingInterval = time.Duration(seconds) * time.Second + cfg.EnvOverrides["PMG_POLLING_INTERVAL"] = true + log.Info().Int("seconds", seconds).Msg("Overriding PMG polling interval (seconds) from environment") + } + } else { + log.Warn().Str("value", intervalStr).Msg("Invalid PMG_POLLING_INTERVAL value, expected duration or seconds") + } + } + if enabledStr := utils.GetenvTrim("ENABLE_TEMPERATURE_MONITORING"); enabledStr != "" { if enabled, err := strconv.ParseBool(enabledStr); err == nil { cfg.TemperatureMonitoringEnabled = enabled diff --git a/internal/config/detect_root_test.go b/internal/config/detect_root_test.go index 06e365d4f..e2fb32662 100644 --- a/internal/config/detect_root_test.go +++ b/internal/config/detect_root_test.go @@ -2,6 +2,7 @@ package config import ( "os" + "path/filepath" "testing" ) @@ -93,7 +94,7 @@ func TestDetectAppRoot_Scenarios(t *testing.T) { name: "Executable in temp, getwd error", mockExec: os.TempDir() + "/go-build123/exe", mockGetwdErr: os.ErrPermission, - expectedResult: os.TempDir() + "/go-build123", // Falls back to exe dir + expectedResult: filepath.Join(os.TempDir(), "go-build123"), // Falls back to exe dir }, { name: "Executable error, getwd error", diff --git a/internal/dockeragent/self_update_test.go b/internal/dockeragent/self_update_test.go index 1f462ca50..4641c23b6 100644 --- a/internal/dockeragent/self_update_test.go +++ b/internal/dockeragent/self_update_test.go @@ -99,8 +99,12 @@ func TestResolveSymlink(t *testing.T) { if err != nil { t.Fatalf("unexpected error: %v", err) } - if got != target { - t.Fatalf("expected %q, got %q", target, got) + expected, err := filepath.EvalSymlinks(target) + if err != nil { + t.Fatalf("eval symlinks target: %v", err) + } + if got != expected { + t.Fatalf("expected %q, got %q", expected, got) } if _, err := resolveSymlink(filepath.Join(dir, "missing")); err == nil { diff --git a/internal/hostagent/agent_metrics_test.go b/internal/hostagent/agent_metrics_test.go index cf73e2862..b764c05a7 100644 --- a/internal/hostagent/agent_metrics_test.go +++ b/internal/hostagent/agent_metrics_test.go @@ -3,6 +3,7 @@ package hostagent import ( "context" "errors" + "runtime" "testing" "time" @@ -184,6 +185,12 @@ func TestBuildReport(t *testing.T) { t.Fatalf("buildReport failed: %v", err) } + if runtime.GOOS != "linux" { + if len(report.RAID) != 0 { + t.Errorf("Expected no RAID arrays on %s, got %d", runtime.GOOS, len(report.RAID)) + } + return + } if len(report.RAID) != 1 { t.Errorf("Expected 1 RAID array, got %d", len(report.RAID)) } else if report.RAID[0].Name != "md0" { @@ -270,6 +277,12 @@ func TestBuildReport(t *testing.T) { } // SMART data is attached to Sensors in the report + if runtime.GOOS != "linux" { + if len(report.Sensors.SMART) != 0 { + t.Errorf("Expected no SMART disks on %s, got %d", runtime.GOOS, len(report.Sensors.SMART)) + } + return + } if len(report.Sensors.SMART) != 1 { t.Errorf("Expected 1 SMART disk, got %d", len(report.Sensors.SMART)) } else { diff --git a/internal/hostagent/agent_new_test.go b/internal/hostagent/agent_new_test.go index c3fb2f68a..b0bd60184 100644 --- a/internal/hostagent/agent_new_test.go +++ b/internal/hostagent/agent_new_test.go @@ -172,6 +172,10 @@ func TestNew_FallsBackToHostnameWhenMachineIDAndMACEmpty(t *testing.T) { } func TestNew_FallsBackToMACWhenMachineIDEmpty(t *testing.T) { + if runtime.GOOS != "linux" { + t.Skip("MAC fallback is Linux-only") + } + originalHostInfo := hostInfoWithContext originalReadFile := readFile originalNetInterfaces := netInterfaces