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
This commit is contained in:
rcourtman
2026-01-22 13:49:05 +00:00
parent ad4acf1222
commit 8412cc7ddb
5 changed files with 69 additions and 3 deletions
+44
View File
@@ -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
+2 -1
View File
@@ -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",
+6 -2
View File
@@ -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 {
+13
View File
@@ -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 {
+4
View File
@@ -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