Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/#159 metrics are not detected #160

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/configuration/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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++
Expand Down
118 changes: 118 additions & 0 deletions internal/configuration/validation_test.go
Original file line number Diff line number Diff line change
@@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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))
}