fix(inputs.opcua): return an error with mismatched types (#11539)

This commit is contained in:
Mya 2022-07-27 13:14:01 -06:00 committed by GitHub
parent af43d0183c
commit 13b0ed0e6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -312,7 +312,11 @@ func (o *OpcUA) validateOPCTags() error {
//search identifier type
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.
default:
return fmt.Errorf("invalid identifier type '%s' in '%s'", node.tag.IdentifierType, node.tag.FieldName)

View File

@ -208,6 +208,42 @@ additional_valid_status_codes = ["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) {
m, err := tagsSliceToMap([][]string{{"foo", "bar"}, {"baz", "bat"}})
require.NoError(t, err)