fix(inputs.opcua): return an error with mismatched types (#11539)
This commit is contained in:
parent
af43d0183c
commit
13b0ed0e6e
|
|
@ -312,7 +312,11 @@ func (o *OpcUA) validateOPCTags() error {
|
||||||
|
|
||||||
//search identifier type
|
//search identifier type
|
||||||
switch node.tag.IdentifierType {
|
switch node.tag.IdentifierType {
|
||||||
case "s", "i", "g", "b":
|
case "i":
|
||||||
|
if _, err := strconv.Atoi(node.tag.Identifier); err != nil {
|
||||||
|
return fmt.Errorf("identifier type '%s' does not match the type of identifier '%s'", node.tag.IdentifierType, node.tag.Identifier)
|
||||||
|
}
|
||||||
|
case "s", "g", "b":
|
||||||
// Valid identifier type - do nothing.
|
// Valid identifier type - do nothing.
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid identifier type '%s' in '%s'", node.tag.IdentifierType, node.tag.FieldName)
|
return fmt.Errorf("invalid identifier type '%s' in '%s'", node.tag.IdentifierType, node.tag.FieldName)
|
||||||
|
|
|
||||||
|
|
@ -208,6 +208,42 @@ additional_valid_status_codes = ["0xC0"]
|
||||||
require.Equal(t, o.Workarounds.AdditionalValidStatusCodes[0], "0xC0")
|
require.Equal(t, o.Workarounds.AdditionalValidStatusCodes[0], "0xC0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConfigWithMismatchedTypes(t *testing.T) {
|
||||||
|
toml := `
|
||||||
|
[[inputs.opcua]]
|
||||||
|
name = "localhost"
|
||||||
|
endpoint = "opc.tcp://localhost:4840"
|
||||||
|
connect_timeout = "10s"
|
||||||
|
request_timeout = "5s"
|
||||||
|
security_policy = "auto"
|
||||||
|
security_mode = "auto"
|
||||||
|
certificate = "/etc/telegraf/cert.pem"
|
||||||
|
private_key = "/etc/telegraf/key.pem"
|
||||||
|
auth_method = "Anonymous"
|
||||||
|
username = ""
|
||||||
|
password = ""
|
||||||
|
nodes = [
|
||||||
|
{name="name", namespace="1", identifier_type="s", identifier="one"},
|
||||||
|
{name="name2", namespace="2", identifier_type="i", identifier="two"},
|
||||||
|
]
|
||||||
|
`
|
||||||
|
|
||||||
|
c := config.NewConfig()
|
||||||
|
err := c.LoadConfigData([]byte(toml))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Len(t, c.Inputs, 1)
|
||||||
|
|
||||||
|
o, ok := c.Inputs[0].Input.(*OpcUA)
|
||||||
|
require.True(t, ok)
|
||||||
|
|
||||||
|
require.Len(t, o.RootNodes, 2)
|
||||||
|
require.Equal(t, o.RootNodes[0].FieldName, "name")
|
||||||
|
require.Equal(t, o.RootNodes[1].FieldName, "name2")
|
||||||
|
|
||||||
|
require.Error(t, o.InitNodes())
|
||||||
|
}
|
||||||
|
|
||||||
func TestTagsSliceToMap(t *testing.T) {
|
func TestTagsSliceToMap(t *testing.T) {
|
||||||
m, err := tagsSliceToMap([][]string{{"foo", "bar"}, {"baz", "bat"}})
|
m, err := tagsSliceToMap([][]string{{"foo", "bar"}, {"baz", "bat"}})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue