pairing with luke, nagios_command provider skeleton
[vinpup.git] / test / other / report.rb
blob9894e2a8d64ca146ded601a0b5abcc524e12345b
1 #!/usr/bin/env ruby
3 require File.dirname(__FILE__) + '/../lib/puppettest'
5 require 'puppet'
6 require 'puppet/reports'
7 require 'puppet/transaction/report'
8 require 'puppettest'
9 require 'puppettest/reporttesting'
11 class TestReports < Test::Unit::TestCase
12         include PuppetTest
13         include PuppetTest::Reporttesting
15     def mkreport
16         # First do some work
17         objects = []
18         6.times do |i|
19             file = tempfile()
21             # Make every third file
22             File.open(file, "w") { |f| f.puts "" } if i % 3 == 0
24             objects << Puppet::Type.newfile(
25                 :path => file,
26                 :ensure => "file"
27             )
28         end
30         config = mk_catalog(*objects)
31         # So the report works out.
32         config.retrieval_duration = 0.001
33         trans = config.apply
35         return trans.generate_report
36     end
38     # Make sure we can use reports as log destinations.
39     def test_reports_as_log_destinations
40         report = fakereport
42         assert_nothing_raised {
43             Puppet::Util::Log.newdestination(report)
44         }
46         # Now make a file for testing logging
47         file = Puppet::Type.newfile(:path => tempfile(), :ensure => "file")
48         file.finish
50         log = nil
51         assert_nothing_raised {
52             log = file.log "This is a message, yo"
53         }
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)
63         }
65         log = file.log "This is another message, yo"
67         assert(! report.logs.include?(log), "Report got log message after close")
68     end
70     def test_newmetric
71         report = nil
72         assert_nothing_raised {
73             report = Puppet::Transaction::Report.new
74         }
76         assert_nothing_raised {
77             report.newmetric(:mymetric,
78                 :total => 12,
79                 :done => 6
80             )
81         }
82     end
84     def test_store_report
85         # Create a bunch of log messages in an array.
86         report = Puppet::Transaction::Report.new
87         
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)
92         3.times { |i|
93             log = Puppet.warning("Report test message %s" % i)
94             log.tags = %w{a list of tags}
95             log.tags << "tag%s" % i
97             report.newlog(log)
98         }
100         assert_nothing_raised do
101             report.extend(Puppet::Reports.report(:store))
102         end
104         yaml = YAML.dump(report)
106         file = nil
107         assert_nothing_raised {
108             file = report.process
109         }
111         assert(FileTest.exists?(file), "report file did not get created")
112         assert_equal(yaml, File.read(file), "File did not get written")
113     end
115     if Puppet.features.rrd?
116     def test_rrdgraph_report
117         Puppet.settings.use(:main, :metrics)
118         report = mkreport
120         assert(! report.metrics.empty?, "Did not receive any metrics")
122         assert_nothing_raised do
123             report.extend(Puppet::Reports.report(:rrdgraph))
124         end
126         assert_nothing_raised {
127             report.process
128         }
130         hostdir = nil
131         assert_nothing_raised do
132             hostdir = report.hostdir
133         end
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)
149         end
151     end
152     else
153     $stderr.puts "Install RRD for metric reporting tests"
154     end
155     
156     def test_tagmail_parsing
157         report = Object.new
158         report.extend(Puppet::Reports.report(:tagmail))
159         
160         passers = File.join(datadir, "reports", "tagmail_passers.conf")
161         assert(FileTest.exists?(passers), "no passers file %s" % passers)
162         
163         File.readlines(passers).each do |line|
164             assert_nothing_raised("Could not parse %s" % line.inspect) do
165                 report.parse(line)
166             end
167         end
168         
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)
172         
173         File.readlines(failers).each do |line|
174             assert_raise(ArgumentError, "Parsed %s" % line.inspect) do
175                 report.parse(line)
176             end
177         end
178     end
179     
180     def test_tagmail_parsing_results
181         report = Object.new
182         report.extend(Puppet::Reports.report(:tagmail))
183         # Now test a few specific lines to make sure we get the results we want
184         {
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}, []]
191             
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])
195             end
196         end
197     end
198     
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)
204             
205             report << log
206         end
207         
208         list = report.logs.collect { |l| l.to_report }
209         
210         report.extend(Puppet::Reports.report(:tagmail))
211         
212         {
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|
220             results = nil
221             assert_nothing_raised("Could not match with %s" % args.inspect) do
222                 results = report.match([args])
223             end
224             
225             if expected
226                 assert_equal([args[0], expected.join("\n")], results[0], "did get correct results for %s" % args.inspect)
227             else
228                 assert_nil(results[0], "got a report for %s" % args.inspect)
229             end
230         end
231     end
233     def test_summary
234         report = mkreport
236         summary = nil
237         assert_nothing_raised("Could not create report summary") do
238             summary = report.summary
239         end
241         %w{Changes Total Resources}.each do |main|
242             assert(summary.include?(main), "Summary did not include info for %s" % main)
243         end
244     end