logstash-filter-ruby
logstash-filter-ruby copied to clipboard
TypeError: no implicit conversion of Array into String when checking size of Array in the test expect block
Logstash information:
Please include the following information:
- Logstash version (e.g.
bin/logstash --version) 8.9.1 - Logstash installation source (e.g. built from source, with a package manager: DEB/RPM, expanded from tar or zip archive, docker) RPM
- How is Logstash being run (e.g. as a service/service manager: systemd, upstart, etc. Via command line, docker/kubernetes) service most of the time, command line for running ruby tests
- How was the Logstash Plugin installed sudo yum install logstash
JVM (e.g. java -version): Open JDK 11.0.19
OS version (uname -a if on a Unix-like system): Red Hat Linux 7.x
Description of the problem including expected versus actual behavior: Cannot check size/length of array in the expect block of the testing framework. Error is {"exception"=>"#<TypeError: no implicit conversion of Array into String>"
Steps to reproduce:
Please include a minimal but complete recreation of the problem, including (e.g.) pipeline definition(s), settings, locale, etc. The easier you make for us to reproduce it, the more likely that somebody will take the time to look at it.
- Create a .rb file with the filter and test methods
- Filter should instantiate JSON array and set it in the event - eg. event.set("myarray", the_array)
- Implement in_event block in the test with example JSON that includes an array.
- Implement expect block to check size of the array which was set in the filter event. None of the usual methods work: length, size, count
- Execute test on command line. eg. logstash -e "the file" -t
- Above error is reported.
Sample code):
def filter(event)
taskArray = Array.new
errorElements = event.get("[error]")
if errorElements.is_a? Array
errorElements.each_index { |x|
taskArray.push(errorElements[x]["task"])
}
end
event.set("error_task_array", taskArray)
return [event]
end
test "get task array" do
in_event {
{
"name":"job1",
"error":[
{
"task":"75fc",
"message":{
"ietf-restconf:errors":{
"error":[
{
"error-type":"application",
"error-tag":"malformed-message",
"error-path":"/pathto/problem",
"error-message":"missing element: name in thepath"
}
]
}
},
"timestamp":1.695060733555E+12
},
{
"task":"job",
"message":"Job has no available transitions. 6649, cb04, f5dd could have led to the workflow end task, but did not. These tasks performed in a way that the end of the workflow could not be reached.",
"timestamp":1.69506073357E+12
}
]
}
}
expect("extracts the task array") do |events|
taskArray = events[0].get(["error_task_array"])
taskArray.count == 2
end # expect
end # test