Skip to content

Commit

Permalink
fix(inputs.opcua): return an error with mismatched types (influxdata#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MyaLongmire authored Jul 27, 2022
1 parent af43d01 commit 13b0ed0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion plugins/inputs/opcua/opcua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions plugins/inputs/opcua/opcua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 13b0ed0

Please sign in to comment.