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 line. Handy when you need to ensure a log has started
|
|
|
|
def verify_start(data,the_start)
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
regex_start = Regexp.new(the_start, @case_insensitive)
|
2010-10-26 06:05:24 +00:00
|
|
|
if @verbose
|
2010-11-12 03:13:05 +00:00
|
|
|
puts "Testing: " + the_start + " =~ " + data_lines.first
|
2010-10-26 06:05:24 +00:00
|
|
|
end
|
2010-10-11 01:57:33 +00:00
|
|
|
return regex_start =~ data_lines.first
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Check for end line. Handy when you need to ensure a log has completed.
|
|
|
|
def verify_end(data,the_end)
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
regex_end = Regexp.new(the_end, @case_insensitive)
|
2010-10-26 06:05:24 +00:00
|
|
|
if @verbose
|
2010-11-12 03:13:05 +00:00
|
|
|
puts "Testing: " + the_end + " =~ " + data_lines.last
|
2010-10-26 06:05:24 +00:00
|
|
|
end
|
2010-10-11 01:57:33 +00:00
|
|
|
return regex_end =~ data_lines.last
|
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-10 01:55:50 +00:00
|
|
|
|
2010-10-26 06:05:24 +00:00
|
|
|
if @verbose
|
2010-11-12 03:13:05 +00:00
|
|
|
puts "Testing: " + the_start + " =~ " + data_lines.first
|
|
|
|
puts "Testing: " + the_end + " =~ " + data_lines.last
|
2010-10-26 06:05:24 +00:00
|
|
|
end
|
2010-10-10 01:55:50 +00:00
|
|
|
|
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
|
|
|
|
|
2010-10-11 01:57:33 +00:00
|
|
|
# Scan for a success line. In order to pass, the string must exist in the data
|
|
|
|
def ensure_exists_in_data(data,regex_string)
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
re = Regexp.new(regex_string, @case_insensitive)
|
|
|
|
|
|
|
|
data_lines.each {|line|
|
|
|
|
if line =~ re
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Scan for a fail line. In order to pass, the string must not exist in the data
|
|
|
|
def ensure_doesnt_exist_in_data(data,regex_string)
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
re = Regexp.new(regex_string, @case_insensitive)
|
|
|
|
|
|
|
|
data_lines.each {|line|
|
|
|
|
if line =~ re
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
# Scan for a fail line. In order to pass, the string must not exist in the data
|
2010-10-26 06:05:24 +00:00
|
|
|
def ensure_doesnt_exist_in_data_unless(data, regex_string, exceptions)
|
2010-10-11 01:57:33 +00:00
|
|
|
data_lines = data.split("\n")
|
|
|
|
data_lines.each {|line|
|
2010-10-26 06:05:24 +00:00
|
|
|
if line =~ Regexp.new(regex_string, @case_insensitive)
|
2010-10-11 01:57:33 +00:00
|
|
|
exceptions.each { |exception|
|
2010-10-26 06:05:24 +00:00
|
|
|
if line =~ Regexp.new(exception, @case_insensitive)
|
2010-10-11 01:57:33 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2010-10-10 01:55:50 +00:00
|
|
|
# Scan for any number of success lines. In order to pass, all successes must match.
|
|
|
|
def ensure_all_exist_in_data(data,regexes=[])
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
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|
|
|
|
|
matched = false
|
|
|
|
re = Regexp.new(condition, @case_insensitive)
|
|
|
|
data_lines.each {|line|
|
|
|
|
if line =~ re
|
2010-12-28 19:50:44 +00:00
|
|
|
if @verbose
|
|
|
|
puts "DEBUG: matched success: " + line
|
|
|
|
end
|
2010-10-11 01:57:33 +00:00
|
|
|
success_count += 1
|
2010-10-10 01:55:50 +00:00
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# A way to tell if a match was never found.
|
|
|
|
if !matched
|
|
|
|
if @verbose
|
|
|
|
puts "DEBUG: Didn't see success condition '#{condition}'"
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2010-10-11 01:57:33 +00:00
|
|
|
if target_successes == success_count
|
2010-12-28 19:50:44 +00:00
|
|
|
if @verbose
|
|
|
|
puts "DEBUG: Woot, got all successes"
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
if @verbose
|
2010-12-28 19:50:44 +00:00
|
|
|
puts "DEBUG: Didn't get enough successes, failing. (" + count + "/" + target_successes + ")"
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
2010-12-28 19:50:44 +00:00
|
|
|
return false
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
return true # No successes are defined, so count this as a pass (true).
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Scan for failures -- if any single failure matches, the test returns true.
|
|
|
|
def ensure_none_exist_in_data(data,regexes=[],exceptions=[])
|
|
|
|
data_lines = data.split("\n")
|
|
|
|
if regexes
|
|
|
|
regexes.each { |condition|
|
|
|
|
## for each failure condition that we've been passed
|
|
|
|
re = Regexp.new(condition, @case_insensitive)
|
|
|
|
|
|
|
|
## assume we're okay
|
|
|
|
okay = true
|
|
|
|
|
|
|
|
data_lines.each { |line|
|
|
|
|
|
2010-10-11 01:57:33 +00:00
|
|
|
if re =~ line
|
2010-10-10 01:55:50 +00:00
|
|
|
okay = false # oh, we found a match
|
|
|
|
|
|
|
|
if @verbose
|
2010-12-28 19:50:44 +00:00
|
|
|
puts "DEBUG: Matched failure: " + line
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
2010-10-11 01:57:33 +00:00
|
|
|
if reg_exception =~ line
|
2010-10-10 01:55:50 +00:00
|
|
|
if @verbose
|
2010-12-28 19:50:44 +00:00
|
|
|
puts "DEBUG: but \'" + line + "\' is an exception, we can ignore it."
|
2010-10-10 01:55:50 +00:00
|
|
|
end
|
|
|
|
okay = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# If we didn't find an exception, we have to fail it. do not pass go.
|
|
|
|
if !okay
|
|
|
|
if @verbose
|
|
|
|
puts "DEBUG: Saw failure condition '#{condition}' in #{line}; regex matched: #{re.inspect}"
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false # we gots no failures, so count this as a pass.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|