diff --git a/lib/puppet/reports/store.rb b/lib/puppet/reports/store.rb index 30f24591c..99a9fc177 100644 --- a/lib/puppet/reports/store.rb +++ b/lib/puppet/reports/store.rb @@ -1,60 +1,42 @@ require 'puppet' Puppet::Reports.register_report(:store) do desc "Store the yaml report on disk. Each host sends its report as a YAML dump and this just stores the file on disk, in the `reportdir` directory. These files collect quickly -- one every half hour -- so it is a good idea to perform some maintenance on them if you use this report (it's the only default report)." - def mkclientdir(client, dir) - config = Puppet::Util::Settings.new - - config.setdefaults( - "reportclient-#{client}".to_sym, - "client-#{client}-dir" => { :default => dir, - :mode => 0750, - :desc => "Client dir for #{client}", - :owner => 'service', - :group => 'service' - }, - - :noop => [false, "Used by settings internally."] - ) - - config.use("reportclient-#{client}".to_sym) - end - def process # We don't want any tracking back in the fs. Unlikely, but there # you go. client = self.host.gsub("..",".") dir = File.join(Puppet[:reportdir], client) - mkclientdir(client, dir) unless FileTest.exists?(dir) + Dir.mkdir(dir, 0750) unless FileTest.exists?(dir) # Now store the report. now = Time.now.gmtime name = %w{year month day hour min}.collect do |method| # Make sure we're at least two digits everywhere "%02d" % now.send(method).to_s end.join("") + ".yaml" file = File.join(dir, name) begin File.open(file, "w", 0640) do |f| f.print to_yaml end rescue => detail puts detail.backtrace if Puppet[:trace] Puppet.warning "Could not write report for #{client} at #{file}: #{detail}" end # Only testing cares about the return value file end end diff --git a/spec/unit/reports/store_spec.rb b/spec/unit/reports/store_spec.rb new file mode 100644 index 000000000..1acb5badd --- /dev/null +++ b/spec/unit/reports/store_spec.rb @@ -0,0 +1,31 @@ +#!/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 'time' + +processor = Puppet::Reports.report(:store) + +describe processor do + describe "#process" do + include PuppetSpec::Files + before :each do + Puppet[:reportdir] = tmpdir('reports') + @report = YAML.load_file(File.join(PuppetSpec::FIXTURE_DIR, 'yaml/report2.6.x.yaml')).extend processor + end + + it "should create a report directory for the client if one doesn't exist" do + @report.process + + File.should be_directory(File.join(Puppet[:reportdir], @report.host)) + end + + it "should write the report to the file in YAML" do + Time.stubs(:now).returns(Time.parse("2011-01-06 12:00:00 UTC")) + @report.process + + File.read(File.join(Puppet[:reportdir], @report.host, "201101061200.yaml")).should == @report.to_yaml + end + end +end