From: Matt Robinson <[email protected]>

There's now a diff method on reports that allows you diff between
another report and returns the properties that have different previous
values.  This is really only useful for our new inspect reports for
auditing.

Paired-with: Jesse Wolfe

Signed-off-by: Jesse Wolfe <[email protected]>
---
Local-branch: ticket/next/5174
 app/models/report.rb       |   21 ++++++++
 spec/models/report_spec.rb |  122 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/app/models/report.rb b/app/models/report.rb
index bc99993..39c95b9 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -50,8 +50,29 @@ class Report < ActiveRecord::Base
     end
   end
 
+  def diff(comparison_report)
+    diff_stuff = {}
+    comparison_report.report.resource_statuses.each do |name, value|
+      my_properties = events_to_hash( 
self.report.resource_statuses[name].events )
+      their_properties = events_to_hash( value.events )
+      my_properties.keys.each do |property|
+        if my_properties[property] != their_properties[property]
+          diff_stuff[ [name, property.to_sym] ] = [ my_properties[property], 
their_properties[property] ]
+        end
+      end
+    end
+    diff_stuff
+  end
+
   private
 
+  def events_to_hash(events)
+    events.inject({}) do |hash, event|
+      hash[event.property] = event.previous_value
+      hash
+    end
+  end
+
   def ensure_valid_format
     begin
       report
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index f06570c..25c7bbc 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -163,4 +163,126 @@ describe Report do
       @node.status.should == 'unchanged'
     end
   end
+
+  describe "when diffing inspection reports" do
+    before :each do
+      @report_yaml = <<-'HEREDOC'
+--- !ruby/object:Puppet::Transaction::Report
+  host: mattmac.puppetlabs.lan
+  kind: inspect
+  logs: []
+  metrics: {}
+  resource_statuses: 
+    "File[/tmp/foo]": !ruby/object:Puppet::Resource::Status
+      events: 
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: &id001 
/Users/matthewrobinson/work/puppet/test_data/genreportm/manifests/site.pp
+          line: 5
+          message: inspected value is :file
+          previous_value: !ruby/sym file
+          property: ensure
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - &id002 file
+            - &id003 class
+          time: 2010-12-03 12:18:40.039434 -08:00
+          version: 1291407517
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: *id001
+          line: 5
+          message: "inspected value is \"{md5}foo\""
+          previous_value: "{md5}foo"
+          property: content
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - *id002
+            - *id003
+          time: 2010-12-03 12:08:59.061376 -08:00
+          version: 1291406846
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: *id001
+          line: 5
+          message: inspected value is nil
+          property: target
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - *id002
+            - *id003
+          time: 2010-12-03 12:08:59.061413 -08:00
+          version: 1291406846
+HEREDOC
+      @report_yaml2 = <<-'HEREDOC'
+--- !ruby/object:Puppet::Transaction::Report
+  host: mattmac.puppetlabs.lan
+  kind: inspect
+  logs: []
+  metrics: {}
+  resource_statuses: 
+    "File[/tmp/foo]": !ruby/object:Puppet::Resource::Status
+      events: 
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: &id001 
/Users/matthewrobinson/work/puppet/test_data/genreportm/manifests/site.pp
+          line: 5
+          message: inspected value is :directory
+          previous_value: !ruby/sym directory
+          property: ensure
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - &id002 file
+            - &id003 class
+          time: 2010-12-03 12:18:40.039434 -08:00
+          version: 1291407517
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: *id001
+          line: 5
+          message: "inspected value is \"{md5}bar\""
+          previous_value: "{md5}bar"
+          property: content
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - *id002
+            - *id003
+          time: 2010-12-03 12:08:59.061376 -08:00
+          version: 1291406846
+        - !ruby/object:Puppet::Transaction::Event
+          default_log_level: !ruby/sym notice
+          file: *id001
+          line: 5
+          message: inspected value is nil
+          property: target
+          resource: "File[/tmp/foo]"
+          status: audit
+          tags: 
+            - *id002
+            - *id003
+          time: 2010-12-03 12:08:59.061413 -08:00
+          version: 1291406846
+HEREDOC
+    end
+
+    it "should produce an empty diff for the same report twice" do
+      report1 = Report.create(:report => @report_yaml)
+      report2 = Report.create(:report => @report_yaml)
+      report1.diff(report2).should == {}
+    end
+
+    it "should show diff for the different reports" do
+      report1 = Report.create(:report => @report_yaml)
+      report2 = Report.create(:report => @report_yaml2)
+      report1.diff(report2).should == {
+        ['File[/tmp/foo]', :ensure] => [:file, :directory],
+        ['File[/tmp/foo]', :content] => ["{md5}foo", "{md5}bar"]
+      }
+    end
+  end
 end
-- 
1.7.0.4

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to