feat(inputs.modbus): Error out on requests with no fields defined. (#11469)
This commit is contained in:
parent
acc8008fb2
commit
6c7b3b3032
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue