feat(inputs.modbus): Error out on requests with no fields defined. (#11469)

This commit is contained in:
Sven Rebhan 2022-07-15 12:01:53 +02:00 committed by GitHub
parent acc8008fb2
commit 6c7b3b3032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package modbus
import (
_ "embed"
"errors"
"fmt"
"hash/maphash"
)
@ -65,6 +66,12 @@ func (c *ConfigurationPerRequest) Check() error {
def.Measurement = "modbus"
}
// Reject any configuration without fields as it
// makes no sense to not define anything but a request.
if len(def.Fields) == 0 {
return errors.New("found request section without fields")
}
// Check the fields
for fidx, f := range def.Fields {
// Check the input type for all fields except the bit-field ones.

View File

@ -1924,3 +1924,21 @@ func TestRequestsStartingWithOmits(t *testing.T) {
acc.Wait(len(expected))
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}
func TestRequestsEmptyFields(t *testing.T) {
modbus := Modbus{
Name: "Test",
Controller: "tcp://localhost:1502",
ConfigurationType: "request",
Log: testutil.Logger{},
}
modbus.Requests = []requestDefinition{
{
SlaveID: 1,
ByteOrder: "ABCD",
RegisterType: "holding",
},
}
err := modbus.Init()
require.EqualError(t, err, `configuraton invalid: found request section without fields`)
}