Fixing and porting Transaction Report tests

There are still a few unported tests, but it's at least
better now.

Signed-off-by: Luke Kanies <luke@puppetlabs.com>
This commit is contained in:
Luke Kanies 2010-04-12 14:56:09 -07:00
Родитель 13d141acce
Коммит 94fddbc8f7
5 изменённых файлов: 117 добавлений и 107 удалений

Просмотреть файл

@ -38,7 +38,7 @@ Puppet::Reports.register_report(:tagmail) do
# Find all matching messages.
def match(taglists)
reports = []
matching_logs = []
taglists.each do |emails, pos, neg|
# First find all of the messages matched by our positive tags
messages = nil
@ -63,11 +63,11 @@ Puppet::Reports.register_report(:tagmail) do
Puppet.info "No messages to report to %s" % emails.join(",")
next
else
reports << [emails, messages.collect { |m| m.to_report }.join("\n")]
matching_logs << [emails, messages.collect { |m| m.to_report }.join("\n")]
end
end
return reports
return matching_logs
end
# Load the config file

94
spec/unit/reports/tagmail.rb Executable file
Просмотреть файл

@ -0,0 +1,94 @@
#!/usr/bin/env ruby
Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
require 'puppet/reports'
require 'puppettest'
tagmail = Puppet::Reports.report(:tagmail)
describe tagmail do
extend PuppetTest
before do
@processor = Puppet::Transaction::Report.new
@processor.extend(Puppet::Reports.report(:tagmail))
end
passers = File.join(datadir, "reports", "tagmail_passers.conf")
File.readlines(passers).each do |line|
it "should be able to parse '#{line.inspect}'" do
@processor.parse(line)
end
end
failers = File.join(datadir, "reports", "tagmail_failers.conf")
File.readlines(failers).each do |line|
it "should not be able to parse '#{line.inspect}'" do
lambda { @processor.parse(line) }.should raise_error(ArgumentError)
end
end
{
"tag: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, []],
"tag, other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag other}, []],
"tag-other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag-other}, []],
"tag, !other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, %w{other}],
"tag, !other, one, !two: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag one}, %w{other two}],
"tag: abuse@domain.com, other@domain.com" => [%w{abuse@domain.com other@domain.com}, %w{tag}, []]
}.each do |line, results|
it "should parse '#{line}' as #{results.inspect}" do
@processor.parse(line).shift.should == results
end
end
describe "when matching logs" do
before do
@processor << Puppet::Util::Log.new(:level => :notice, :message => "first", :tags => %w{one})
@processor << Puppet::Util::Log.new(:level => :notice, :message => "second", :tags => %w{one two})
@processor << Puppet::Util::Log.new(:level => :notice, :message => "third", :tags => %w{one two three})
end
def match(pos = [], neg = [])
pos = Array(pos)
neg = Array(neg)
result = @processor.match([[%w{abuse@domain.com}, pos, neg]])
actual_result = result.shift
if actual_result
actual_result[1]
else
nil
end
end
it "should match all messages when provided the 'all' tag as a positive matcher" do
results = match("all")
%w{first second third}.each do |str|
results.should be_include(str)
end
end
it "should remove messages that match a negated tag" do
match("all", "three").should_not be_include("third")
end
it "should find any messages tagged with a provided tag" do
results = match("two")
results.should be_include("second")
results.should be_include("third")
results.should_not be_include("first")
end
it "should allow negation of specific tags from a specific tag list" do
results = match("two", "three")
results.should be_include("second")
results.should_not be_include("third")
end
it "should allow a tag to negate all matches" do
results = match([], "one")
results.should be_nil
end
end
end

Просмотреть файл

@ -209,5 +209,22 @@ describe Puppet::Transaction::Report do
end
end
end
end
describe "when producing a summary" do
before do
resource = Puppet::Type.type(:notify).new(:name => "testing")
catalog = Puppet::Resource::Catalog.new
catalog.add_resource resource
trans = catalog.apply
@report = trans.report
@report.calculate_metrics
end
%w{Changes Total Resources}.each do |main|
it "should include information on #{main} in the summary" do
@report.summary.should be_include(main)
end
end
end
end

Просмотреть файл

@ -7,7 +7,7 @@ module PuppetTest::Reporttesting
# We have to use warning so that the logs always happen
log = Puppet.warning("Report test message %s" % i)
report.newlog(log)
report << log
}
return report

Просмотреть файл

@ -66,20 +66,6 @@ class TestReports < Test::Unit::TestCase
assert(! report.logs.include?(log), "Report got log message after close")
end
def test_newmetric
report = nil
assert_nothing_raised {
report = Puppet::Transaction::Report.new
}
assert_nothing_raised {
report.newmetric(:mymetric,
:total => 12,
:done => 6
)
}
end
def test_store_report
# Create a bunch of log messages in an array.
report = Puppet::Transaction::Report.new
@ -91,7 +77,7 @@ class TestReports < Test::Unit::TestCase
3.times { |i|
log = Puppet.warning("Report test message %s" % i)
report.newlog(log)
report << log
}
assert_nothing_raised do
@ -146,92 +132,5 @@ class TestReports < Test::Unit::TestCase
else
$stderr.puts "Install RRD for metric reporting tests"
end
def test_tagmail_parsing
report = Object.new
report.extend(Puppet::Reports.report(:tagmail))
passers = File.join(datadir, "reports", "tagmail_passers.conf")
assert(FileTest.exists?(passers), "no passers file %s" % passers)
File.readlines(passers).each do |line|
assert_nothing_raised("Could not parse %s" % line.inspect) do
report.parse(line)
end
end
# Now make sure the failers fail
failers = File.join(datadir, "reports", "tagmail_failers.conf")
assert(FileTest.exists?(failers), "no failers file %s" % failers)
File.readlines(failers).each do |line|
assert_raise(ArgumentError, "Parsed %s" % line.inspect) do
report.parse(line)
end
end
end
def test_tagmail_parsing_results
report = Object.new
report.extend(Puppet::Reports.report(:tagmail))
# Now test a few specific lines to make sure we get the results we want
{
"tag: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, []],
"tag, other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag other}, []],
"tag-other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag-other}, []],
"tag, !other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, %w{other}],
"tag, !other, one, !two: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag one}, %w{other two}],
"tag: abuse@domain.com, other@domain.com" => [%w{abuse@domain.com other@domain.com}, %w{tag}, []]
}.each do |line, results|
assert_nothing_raised("Failed to parse %s" % line.inspect) do
assert_equal(results, report.parse(line).shift, "line %s returned incorrect results %s" % [line.inspect, results.inspect])
end
end
end
def test_tagmail_matching
report = Puppet::Transaction::Report.new
Puppet::Util::Log.close
[%w{one}, %w{one two}, %w{one two three}, %w{one two three four}].each do |tags|
log = Puppet::Util::Log.new(:level => :notice, :message => tags.join(" "), :tags => tags)
report << log
end
list = report.logs.collect { |l| l.to_report }
report.extend(Puppet::Reports.report(:tagmail))
{
[%w{abuse@domain.com}, %w{all}, []] => list,
[%w{abuse@domain.com}, %w{all}, %w{three}] => list[0..1],
[%w{abuse@domain.com}, %w{one}, []] => list,
[%w{abuse@domain.com}, %w{two}, []] => list[1..3],
[%w{abuse@domain.com}, %w{two}, %w{three}] => list[1..1],
[%w{abuse@domain.com}, %w{}, %w{one}] => nil
}.each do |args, expected|
results = nil
assert_nothing_raised("Could not match with %s" % args.inspect) do
results = report.match([args])
end
if expected
assert_equal([args[0], expected.join("\n")], results[0], "did get correct results for %s" % args.inspect)
else
assert_nil(results[0], "got a report for %s" % args.inspect)
end
end
end
def test_summary
report = mkreport
summary = report.summary
%w{Changes Total Resources}.each do |main|
assert(summary.include?(main), "Summary did not include info for %s" % main)
end
end
end