2010-10-10 01:55:50 +00:00
|
|
|
## This class consists of helper methods for regexing logs
|
|
|
|
##
|
|
|
|
## TODO - clean up the style. looks like it was written in the early 90s
|
|
|
|
##
|
2010-10-26 06:05:24 +00:00
|
|
|
## $Id$
|
2010-10-10 01:55:50 +00:00
|
|
|
|
|
|
|
class Regexr
|
|
|
|
|
2010-10-26 06:05:24 +00:00
|
|
|
def initialize(verbose=false, case_insensitive=true)
|
2010-10-11 01:57:33 +00:00
|
|
|
@verbose = verbose
|
2010-10-26 06:05:24 +00:00
|
|
|
@case_insensitive = case_insensitive
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check for the beginning and end lines. Handy when you need to ensure a log has started & completed
|
|
|
|
def verify_start_and_end(data,the_start,the_end)
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
regex_start = Regexp.new(the_start, @case_insensitive)
|
2010-10-26 06:05:24 +00:00
|
|
|
regex_end = Regexp.new(the_end, @case_insensitive)
|
2010-10-11 01:57:33 +00:00
|
|
|
if regex_start =~ data_lines.first
|
2010-10-26 06:05:24 +00:00
|
|
|
return regex_end =~ data_lines.last
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Scan for any number of success lines. In order to pass, all successes must match.
|
2011-01-29 21:40:53 +00:00
|
|
|
def find_strings_that_dont_exist_in_data(data,regexes=[])
|
2010-10-10 01:55:50 +00:00
|
|
|
data_lines = data.split("\n")
|
2011-01-31 17:48:03 +00:00
|
|
|
|
|
|
|
return nil unless regexes ## count as a pass
|
|
|
|
|
2010-10-10 01:55:50 +00:00
|
|
|
if regexes
|
|
|
|
target_successes = regexes.size
|
2010-10-11 01:57:33 +00:00
|
|
|
success_count = 0
|
2010-10-10 01:55:50 +00:00
|
|
|
regexes.each { |condition|
|
2011-01-29 21:40:53 +00:00
|
|
|
|
|
|
|
## assume we haven't got it
|
2011-01-31 17:48:03 +00:00
|
|
|
found = false
|
2011-01-29 21:40:53 +00:00
|
|
|
|
2010-10-10 01:55:50 +00:00
|
|
|
re = Regexp.new(condition, @case_insensitive)
|
2011-01-29 21:40:53 +00:00
|
|
|
|
|
|
|
## for each of our data lines
|
2010-10-10 01:55:50 +00:00
|
|
|
data_lines.each {|line|
|
2011-01-29 21:40:53 +00:00
|
|
|
|
|
|
|
## if it's a match
|
2010-10-10 01:55:50 +00:00
|
|
|
if line =~ re
|
2011-01-31 17:48:03 +00:00
|
|
|
found = true
|
|
|
|
break ## success!
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
if !found
|
2011-01-29 21:40:53 +00:00
|
|
|
return condition ## return this string, it wasn't found.
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2011-01-29 21:40:53 +00:00
|
|
|
|
|
|
|
nil ## got all successes, woot!
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Scan for failures -- if any single failure matches, the test returns true.
|
2011-01-29 21:40:53 +00:00
|
|
|
def find_strings_that_exist_in_data_except(data,regexes=[],exceptions=[])
|
2010-10-10 01:55:50 +00:00
|
|
|
data_lines = data.split("\n")
|
2011-01-31 17:48:03 +00:00
|
|
|
|
|
|
|
return nil unless regexes ## count as a pass
|
2011-01-29 21:40:53 +00:00
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
regexes.each { |condition|
|
2010-10-10 01:55:50 +00:00
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
## for each failure condition that we've been passed
|
|
|
|
re = Regexp.new(condition, @case_insensitive)
|
2010-10-10 01:55:50 +00:00
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
## assume we're okay
|
|
|
|
found = false
|
2010-10-10 01:55:50 +00:00
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
data_lines.each { |line|
|
|
|
|
if re =~ line
|
|
|
|
found = true # oh, we found a match
|
2011-01-29 21:40:53 +00:00
|
|
|
|
2011-01-31 17:48:03 +00:00
|
|
|
# but let's check the exceptions
|
|
|
|
exceptions.map { |exception|
|
|
|
|
reg_exception = Regexp.new(exception, @case_insensitive)
|
|
|
|
|
|
|
|
# If the exception matches here, we'll spare it
|
|
|
|
if reg_exception =~ line
|
|
|
|
found = false
|
|
|
|
break
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
2011-01-31 17:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# If we didn't find an exception, we have to fail it. do not pass go.
|
|
|
|
return condition if found
|
|
|
|
end
|
2010-10-10 01:55:50 +00:00
|
|
|
}
|
2011-01-31 17:48:03 +00:00
|
|
|
}
|
2011-01-29 21:40:53 +00:00
|
|
|
|
|
|
|
nil ## no failures found!
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
end
|