3 require File.dirname(__FILE__) + '/../lib/puppettest'
6 require 'puppet/reports'
7 require 'puppet/transaction/report'
9 require 'puppettest/reporttesting'
11 class TestReports < Test::Unit::TestCase
13 include PuppetTest::Reporttesting
21 # Make every third file
22 File.open(file, "w") { |f| f.puts "" } if i % 3 == 0
24 objects << Puppet::Type.newfile(
30 config = mk_catalog(*objects)
31 # So the report works out.
32 config.retrieval_duration = 0.001
35 return trans.generate_report
38 # Make sure we can use reports as log destinations.
39 def test_reports_as_log_destinations
42 assert_nothing_raised {
43 Puppet::Util::Log.newdestination(report)
46 # Now make a file for testing logging
47 file = Puppet::Type.newfile(:path => tempfile(), :ensure => "file")
51 assert_nothing_raised {
52 log = file.log "This is a message, yo"
55 assert(report.logs.include?(log), "Report did not get log message")
57 log = Puppet.warning "This is a non-sourced message"
59 assert(! report.logs.include?(log), "Report got log message")
61 assert_nothing_raised {
62 Puppet::Util::Log.close(report)
65 log = file.log "This is another message, yo"
67 assert(! report.logs.include?(log), "Report got log message after close")
72 assert_nothing_raised {
73 report = Puppet::Transaction::Report.new
76 assert_nothing_raised {
77 report.newmetric(:mymetric,
85 # Create a bunch of log messages in an array.
86 report = Puppet::Transaction::Report.new
88 # We have to reuse reporting here because of something going on in the
89 # server/report.rb file
90 Puppet.settings.use(:main, :reporting)
93 log = Puppet.warning("Report test message %s" % i)
94 log.tags = %w{a list of tags}
95 log.tags << "tag%s" % i
100 assert_nothing_raised do
101 report.extend(Puppet::Reports.report(:store))
104 yaml = YAML.dump(report)
107 assert_nothing_raised {
108 file = report.process
111 assert(FileTest.exists?(file), "report file did not get created")
112 assert_equal(yaml, File.read(file), "File did not get written")
115 if Puppet.features.rrd?
116 def test_rrdgraph_report
117 Puppet.settings.use(:main, :metrics)
120 assert(! report.metrics.empty?, "Did not receive any metrics")
122 assert_nothing_raised do
123 report.extend(Puppet::Reports.report(:rrdgraph))
126 assert_nothing_raised {
131 assert_nothing_raised do
132 hostdir = report.hostdir
135 assert(hostdir, "Did not get hostdir back")
137 assert(FileTest.directory?(hostdir), "Host rrd dir did not get created")
138 index = File.join(hostdir, "index.html")
139 assert(FileTest.exists?(index), "index file was not created")
141 # Now make sure it creaets each of the rrd files
142 %w{changes resources time}.each do |type|
143 file = File.join(hostdir, "%s.rrd" % type)
144 assert(FileTest.exists?(file), "Did not create rrd file for %s" % type)
146 daily = file.sub ".rrd", "-daily.png"
147 assert(FileTest.exists?(daily),
148 "Did not make daily graph for %s" % type)
153 $stderr.puts "Install RRD for metric reporting tests"
156 def test_tagmail_parsing
158 report.extend(Puppet::Reports.report(:tagmail))
160 passers = File.join(datadir, "reports", "tagmail_passers.conf")
161 assert(FileTest.exists?(passers), "no passers file %s" % passers)
163 File.readlines(passers).each do |line|
164 assert_nothing_raised("Could not parse %s" % line.inspect) do
169 # Now make sure the failers fail
170 failers = File.join(datadir, "reports", "tagmail_failers.conf")
171 assert(FileTest.exists?(failers), "no failers file %s" % failers)
173 File.readlines(failers).each do |line|
174 assert_raise(ArgumentError, "Parsed %s" % line.inspect) do
180 def test_tagmail_parsing_results
182 report.extend(Puppet::Reports.report(:tagmail))
183 # Now test a few specific lines to make sure we get the results we want
185 "tag: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, []],
186 "tag, other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag other}, []],
187 "tag-other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag-other}, []],
188 "tag, !other: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag}, %w{other}],
189 "tag, !other, one, !two: abuse@domain.com" => [%w{abuse@domain.com}, %w{tag one}, %w{other two}],
190 "tag: abuse@domain.com, other@domain.com" => [%w{abuse@domain.com other@domain.com}, %w{tag}, []]
192 }.each do |line, results|
193 assert_nothing_raised("Failed to parse %s" % line.inspect) do
194 assert_equal(results, report.parse(line).shift, "line %s returned incorrect results %s" % [line.inspect, results.inspect])
199 def test_tagmail_matching
200 report = Puppet::Transaction::Report.new
201 Puppet::Util::Log.close
202 [%w{one}, %w{one two}, %w{one two three}, %w{one two three four}].each do |tags|
203 log = Puppet::Util::Log.new(:level => :notice, :message => tags.join(" "), :tags => tags)
208 list = report.logs.collect { |l| l.to_report }
210 report.extend(Puppet::Reports.report(:tagmail))
213 [%w{abuse@domain.com}, %w{all}, []] => list,
214 [%w{abuse@domain.com}, %w{all}, %w{three}] => list[0..1],
215 [%w{abuse@domain.com}, %w{one}, []] => list,
216 [%w{abuse@domain.com}, %w{two}, []] => list[1..3],
217 [%w{abuse@domain.com}, %w{two}, %w{three}] => list[1..1],
218 [%w{abuse@domain.com}, %w{}, %w{one}] => nil
219 }.each do |args, expected|
221 assert_nothing_raised("Could not match with %s" % args.inspect) do
222 results = report.match([args])
226 assert_equal([args[0], expected.join("\n")], results[0], "did get correct results for %s" % args.inspect)
228 assert_nil(results[0], "got a report for %s" % args.inspect)
237 assert_nothing_raised("Could not create report summary") do
238 summary = report.summary
241 %w{Changes Total Resources}.each do |main|
242 assert(summary.include?(main), "Summary did not include info for %s" % main)