mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
Refactor sensor configuration handling in tests and implementation
- Add skipCollection propery - Ensure that sensors are initialized as an empty map
This commit is contained in:
@@ -18,21 +18,26 @@ type SensorConfig struct {
|
|||||||
primarySensor string
|
primarySensor string
|
||||||
isBlacklist bool
|
isBlacklist bool
|
||||||
hasWildcards bool
|
hasWildcards bool
|
||||||
|
skipCollection bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Agent) newSensorConfig() *SensorConfig {
|
func (a *Agent) newSensorConfig() *SensorConfig {
|
||||||
primarySensor, _ := GetEnv("PRIMARY_SENSOR")
|
primarySensor, _ := GetEnv("PRIMARY_SENSOR")
|
||||||
sysSensors, _ := GetEnv("SYS_SENSORS")
|
sysSensors, _ := GetEnv("SYS_SENSORS")
|
||||||
sensors, _ := GetEnv("SENSORS")
|
sensorsEnvVal, sensorsSet := GetEnv("SENSORS")
|
||||||
|
skipCollection := sensorsSet && sensorsEnvVal == ""
|
||||||
|
|
||||||
return a.newSensorConfigWithEnv(primarySensor, sysSensors, sensors)
|
return a.newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal, skipCollection)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newSensorConfigWithEnv creates a SensorConfig with the provided environment variables
|
// newSensorConfigWithEnv creates a SensorConfig with the provided environment variables
|
||||||
func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensors string) *SensorConfig {
|
// sensorsSet indicates if the SENSORS environment variable was explicitly set (even to empty string)
|
||||||
|
func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal string, skipCollection bool) *SensorConfig {
|
||||||
config := &SensorConfig{
|
config := &SensorConfig{
|
||||||
context: context.Background(),
|
context: context.Background(),
|
||||||
primarySensor: primarySensor,
|
primarySensor: primarySensor,
|
||||||
|
skipCollection: skipCollection,
|
||||||
|
sensors: make(map[string]struct{}),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sensors context (allows overriding sys location for sensors)
|
// Set sensors context (allows overriding sys location for sensors)
|
||||||
@@ -43,16 +48,13 @@ func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensors string
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sensors whitelist
|
|
||||||
if sensors != "" {
|
|
||||||
// handle blacklist
|
// handle blacklist
|
||||||
if strings.HasPrefix(sensors, "-") {
|
if strings.HasPrefix(sensorsEnvVal, "-") {
|
||||||
config.isBlacklist = true
|
config.isBlacklist = true
|
||||||
sensors = sensors[1:]
|
sensorsEnvVal = sensorsEnvVal[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
config.sensors = make(map[string]struct{})
|
for sensor := range strings.SplitSeq(sensorsEnvVal, ",") {
|
||||||
for sensor := range strings.SplitSeq(sensors, ",") {
|
|
||||||
sensor = strings.TrimSpace(sensor)
|
sensor = strings.TrimSpace(sensor)
|
||||||
if sensor != "" {
|
if sensor != "" {
|
||||||
config.sensors[sensor] = struct{}{}
|
config.sensors[sensor] = struct{}{}
|
||||||
@@ -61,7 +63,6 @@ func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensors string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
@@ -69,7 +70,7 @@ func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensors string
|
|||||||
// updateTemperatures updates the agent with the latest sensor temperatures
|
// updateTemperatures updates the agent with the latest sensor temperatures
|
||||||
func (a *Agent) updateTemperatures(systemStats *system.Stats) {
|
func (a *Agent) updateTemperatures(systemStats *system.Stats) {
|
||||||
// skip if sensors whitelist is set to empty string
|
// skip if sensors whitelist is set to empty string
|
||||||
if a.sensorConfig.sensors != nil && len(a.sensorConfig.sensors) == 0 {
|
if a.sensorConfig.skipCollection {
|
||||||
slog.Debug("Skipping temperature collection")
|
slog.Debug("Skipping temperature collection")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -113,8 +114,8 @@ func (a *Agent) updateTemperatures(systemStats *system.Stats) {
|
|||||||
|
|
||||||
// isValidSensor checks if a sensor is valid based on the sensor name and the sensor config
|
// isValidSensor checks if a sensor is valid based on the sensor name and the sensor config
|
||||||
func isValidSensor(sensorName string, config *SensorConfig) bool {
|
func isValidSensor(sensorName string, config *SensorConfig) bool {
|
||||||
// If no sensors configuration, everything is valid
|
// if no sensors configured, everything is valid
|
||||||
if config.sensors == nil {
|
if len(config.sensors) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +124,7 @@ func isValidSensor(sensorName string, config *SensorConfig) bool {
|
|||||||
return !config.isBlacklist
|
return !config.isBlacklist
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no wildcards, return false if blacklist, true if whitelist
|
// If no wildcards, return true if blacklist, false if whitelist
|
||||||
if !config.hasWildcards {
|
if !config.hasWildcards {
|
||||||
return config.isBlacklist
|
return config.isBlacklist
|
||||||
}
|
}
|
||||||
|
@@ -97,10 +97,13 @@ func TestIsValidSensor(t *testing.T) {
|
|||||||
expectedValid: true,
|
expectedValid: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Nil sensor config",
|
name: "No sensors configured",
|
||||||
sensorName: "any_temp",
|
sensorName: "any_temp",
|
||||||
config: &SensorConfig{
|
config: &SensorConfig{
|
||||||
sensors: nil,
|
sensors: map[string]struct{}{},
|
||||||
|
isBlacklist: false,
|
||||||
|
hasWildcards: false,
|
||||||
|
skipCollection: false,
|
||||||
},
|
},
|
||||||
expectedValid: true,
|
expectedValid: true,
|
||||||
},
|
},
|
||||||
@@ -162,6 +165,7 @@ func TestNewSensorConfigWithEnv(t *testing.T) {
|
|||||||
primarySensor string
|
primarySensor string
|
||||||
sysSensors string
|
sysSensors string
|
||||||
sensors string
|
sensors string
|
||||||
|
skipCollection bool
|
||||||
expectedConfig *SensorConfig
|
expectedConfig *SensorConfig
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -172,20 +176,36 @@ func TestNewSensorConfigWithEnv(t *testing.T) {
|
|||||||
expectedConfig: &SensorConfig{
|
expectedConfig: &SensorConfig{
|
||||||
context: context.Background(),
|
context: context.Background(),
|
||||||
primarySensor: "",
|
primarySensor: "",
|
||||||
sensors: nil,
|
sensors: map[string]struct{}{},
|
||||||
isBlacklist: false,
|
isBlacklist: false,
|
||||||
hasWildcards: false,
|
hasWildcards: false,
|
||||||
|
skipCollection: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Primary sensor only",
|
name: "Explicitly set to empty string",
|
||||||
|
primarySensor: "",
|
||||||
|
sysSensors: "",
|
||||||
|
sensors: "",
|
||||||
|
skipCollection: true,
|
||||||
|
expectedConfig: &SensorConfig{
|
||||||
|
context: context.Background(),
|
||||||
|
primarySensor: "",
|
||||||
|
sensors: map[string]struct{}{},
|
||||||
|
isBlacklist: false,
|
||||||
|
hasWildcards: false,
|
||||||
|
skipCollection: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Primary sensor only - should create sensor map",
|
||||||
primarySensor: "cpu_temp",
|
primarySensor: "cpu_temp",
|
||||||
sysSensors: "",
|
sysSensors: "",
|
||||||
sensors: "",
|
sensors: "",
|
||||||
expectedConfig: &SensorConfig{
|
expectedConfig: &SensorConfig{
|
||||||
context: context.Background(),
|
context: context.Background(),
|
||||||
primarySensor: "cpu_temp",
|
primarySensor: "cpu_temp",
|
||||||
sensors: nil,
|
sensors: map[string]struct{}{},
|
||||||
isBlacklist: false,
|
isBlacklist: false,
|
||||||
hasWildcards: false,
|
hasWildcards: false,
|
||||||
},
|
},
|
||||||
@@ -238,6 +258,22 @@ func TestNewSensorConfigWithEnv(t *testing.T) {
|
|||||||
hasWildcards: true,
|
hasWildcards: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Sensors with whitespace",
|
||||||
|
primarySensor: "cpu_temp",
|
||||||
|
sysSensors: "",
|
||||||
|
sensors: "cpu_*, gpu_temp",
|
||||||
|
expectedConfig: &SensorConfig{
|
||||||
|
context: context.Background(),
|
||||||
|
primarySensor: "cpu_temp",
|
||||||
|
sensors: map[string]struct{}{
|
||||||
|
"cpu_*": {},
|
||||||
|
"gpu_temp": {},
|
||||||
|
},
|
||||||
|
isBlacklist: false,
|
||||||
|
hasWildcards: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "With SYS_SENSORS path",
|
name: "With SYS_SENSORS path",
|
||||||
primarySensor: "cpu_temp",
|
primarySensor: "cpu_temp",
|
||||||
@@ -256,7 +292,7 @@ func TestNewSensorConfigWithEnv(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
result := agent.newSensorConfigWithEnv(tt.primarySensor, tt.sysSensors, tt.sensors)
|
result := agent.newSensorConfigWithEnv(tt.primarySensor, tt.sysSensors, tt.sensors, tt.skipCollection)
|
||||||
|
|
||||||
// Check primary sensor
|
// Check primary sensor
|
||||||
assert.Equal(t, tt.expectedConfig.primarySensor, result.primarySensor)
|
assert.Equal(t, tt.expectedConfig.primarySensor, result.primarySensor)
|
||||||
|
Reference in New Issue
Block a user