diff --git a/internal/configuration/validation.go b/internal/configuration/validation.go index 8ee1d65..9aefbf5 100644 --- a/internal/configuration/validation.go +++ b/internal/configuration/validation.go @@ -55,7 +55,13 @@ func containsCmdSensors() bool { } func validateSensors(config *Configuration) error { + sensorIds := []string{} + for _, sensorConfig := range config.Sensors { + if slices.Contains(sensorIds, sensorConfig.ID) { + return errors.New(fmt.Sprintf("Duplicate sensor id detected: %s", sensorConfig.ID)) + } + sensorIds = append(sensorIds, sensorConfig.ID) subConfigs := 0 if sensorConfig.HwMon != nil { @@ -107,8 +113,14 @@ func isSensorConfigInUse(config SensorConfig, curves []CurveConfig) bool { func validateCurves(config *Configuration) error { graph := make(map[interface{}][]interface{}) + curveIds := []string{} for _, curveConfig := range config.Curves { + if slices.Contains(curveIds, curveConfig.ID) { + return errors.New(fmt.Sprintf("Duplicate curve id detected: %s", curveConfig.ID)) + } + curveIds = append(curveIds, curveConfig.ID) + subConfigs := 0 if curveConfig.Linear != nil { subConfigs++ @@ -219,7 +231,14 @@ func isCurveConfigInUse(config CurveConfig, curves []CurveConfig, fans []FanConf } func validateFans(config *Configuration) error { + fanIds := []string{} + for _, fanConfig := range config.Fans { + if slices.Contains(fanIds, fanConfig.ID) { + return errors.New(fmt.Sprintf("Duplicate fan id detected: %s", fanConfig.ID)) + } + fanIds = append(fanIds, fanConfig.ID) + subConfigs := 0 if fanConfig.HwMon != nil { subConfigs++ diff --git a/internal/configuration/validation_test.go b/internal/configuration/validation_test.go index e6b70a8..5c9d9cd 100644 --- a/internal/configuration/validation_test.go +++ b/internal/configuration/validation_test.go @@ -1,10 +1,61 @@ package configuration import ( + "fmt" "github.com/stretchr/testify/assert" "testing" ) +func TestValidateDuplicateFanId(t *testing.T) { + // GIVEN + fanId := "fan" + config := Configuration{ + Fans: []FanConfig{ + { + ID: fanId, + Curve: "curve", + HwMon: nil, + File: &FileFanConfig{ + Path: "abc", + }, + }, + { + ID: fanId, + Curve: "curve", + HwMon: nil, + File: &FileFanConfig{ + Path: "abc", + }, + }, + }, + Curves: []CurveConfig{ + { + ID: "curve", + Linear: &LinearCurveConfig{ + Sensor: "sensor", + Min: 0, + Max: 100, + }, + Function: nil, + }, + }, + Sensors: []SensorConfig{ + { + ID: "sensor", + File: &FileSensorConfig{ + Path: "", + }, + }, + }, + } + + // WHEN + err := validateConfig(&config, "") + + // THEN + assert.EqualError(t, err, fmt.Sprintf("Duplicate fan id detected: %s", fanId)) +} + func TestValidateFanSubConfigIsMissing(t *testing.T) { // GIVEN config := Configuration{ @@ -209,6 +260,46 @@ func TestValidateCurveDependencyWithIdIsNotDefined(t *testing.T) { assert.EqualError(t, err, "Curve curve1: no curve definition with id 'curve2' found") } +func TestValidateDuplicateCurveId(t *testing.T) { + // GIVEN + curveId := "curve" + config := Configuration{ + Curves: []CurveConfig{ + { + ID: curveId, + Linear: &LinearCurveConfig{ + Sensor: "sensor", + Min: 0, + Max: 100, + }, + }, + { + ID: curveId, + Linear: &LinearCurveConfig{ + Sensor: "sensor", + Min: 0, + Max: 100, + }, + }, + }, + Sensors: []SensorConfig{ + { + ID: "sensor", + File: &FileSensorConfig{ + // TODO: path empty validation + Path: "", + }, + }, + }, + } + + // WHEN + err := validateConfig(&config, "") + + // THEN + assert.EqualError(t, err, fmt.Sprintf("Duplicate curve id detected: %s", curveId)) +} + func TestValidateCurve(t *testing.T) { // GIVEN config := Configuration{ @@ -299,3 +390,30 @@ func TestValidateSensor(t *testing.T) { // THEN assert.NoError(t, err) } + +func TestValidateDuplicateSensorId(t *testing.T) { + // GIVEN + sensorId := "sensor" + config := Configuration{ + Sensors: []SensorConfig{ + { + ID: sensorId, + File: &FileSensorConfig{ + Path: "", + }, + }, + { + ID: sensorId, + File: &FileSensorConfig{ + Path: "", + }, + }, + }, + } + + // WHEN + err := validateConfig(&config, "") + + // THEN + assert.EqualError(t, err, fmt.Sprintf("Duplicate sensor id detected: %s", sensorId)) +}