The nodes controller now has the ability to respond to requests for CSV data,
by serializing its collection of nodes to CSV, similarly to YAML requests.
This is a necessary step to make CSV output actually available to users.

Paired-With: Ben Hengst
Paired-With: Daniel Pittman <[email protected]>
Paired-With: Jacob Helwig <[email protected]>
Signed-off-by: Nick Lewis <[email protected]>
---
Local-branch: ticket/next/7007
 app/controllers/nodes_controller.rb       |    1 +
 spec/controllers/nodes_controller_spec.rb |   76 ++++++++++++++++++++++++++++-
 2 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/app/controllers/nodes_controller.rb 
b/app/controllers/nodes_controller.rb
index 1ba0d1c..3e0f60d 100644
--- a/app/controllers/nodes_controller.rb
+++ b/app/controllers/nodes_controller.rb
@@ -123,6 +123,7 @@ class NodesController < InheritedResources::Base
 
       format.html { render :index }
       format.yaml { render :text => collection.to_yaml, :content_type => 
'application/x-yaml' }
+      format.csv  { render :text => collection.to_csv, :content_type => 
'text/csv' }
     end
   end
 end
diff --git a/spec/controllers/nodes_controller_spec.rb 
b/spec/controllers/nodes_controller_spec.rb
index c9029cf..f1d79f3 100644
--- a/spec/controllers/nodes_controller_spec.rb
+++ b/spec/controllers/nodes_controller_spec.rb
@@ -6,7 +6,8 @@ describe NodesController do
 
   describe "#index" do
     before :each do
-      @node = Node.generate!
+      @node = Factory(:compliant_node)
+      @resource = @node.last_apply_report.resource_statuses.first
     end
 
     context "as HTML" do
@@ -56,6 +57,79 @@ describe NodesController do
         end
       end
     end
+
+    context "as CSV" do
+      let :header do
+        CSV.generate_line [ 'name',            'status',            
'resource_count', 'pending_count',
+                            'failed_count',    'compliant_count',   
'resource_type',  'title',
+                            'evaluation_time', 'file',              'line',    
       'time',
+                            'change_count',    'out_of_sync_count', 'skipped', 
       'failed' ]
+      end
+
+      it "should make correct CSV" do
+        get :index, :format => "csv"
+
+        response.should be_success
+        response.body.split("\n").should =~ [
+          header,
+          
"#{@node.name},changed,1,0,0,1,#{@resource.resource_type},#{@resource.title},#{@resource.evaluation_time},#{@resource.file},#{@resource.line},#{@resource.time},#{@resource.change_count},#{@resource.out_of_sync_count},#{@resource.skipped},#{@resource.failed}"
+        ]
+
+      end
+
+      it "should handle unreported nodes" do
+        unreported_node = Node.generate!
+
+        get :index, :format => "csv"
+
+        response.should be_success
+        response.body.split("\n").should =~ [
+          header,
+          
"#{@node.name},changed,1,0,0,1,#{@resource.resource_type},#{@resource.title},#{@resource.evaluation_time},#{@resource.file},#{@resource.line},#{@resource.time},#{@resource.change_count},#{@resource.out_of_sync_count},#{@resource.skipped},#{@resource.failed}",
+          "#{unreported_node.name},,,,,,,,,,,,,,,"
+        ]
+      end
+
+      %w[foo,_-' bar/\\$^ <ba"z>>].each do |name|
+        it "should handle a node named #{name}" do
+          @node.name = name
+          @node.save
+          get :index, :format => "csv"
+
+          response.should be_success
+          CSV.parse(response.body).last.first.should == name
+        end
+      end
+
+      it "should include the node's resources" do
+        report = Report.generate!(:host => @node.name, :status => "failed", 
:time => Time.now)
+        res1 = report.resource_statuses.generate!( :resource_type     => 
"File",    :title        => "/etc/sudoers",
+                                                   :evaluation_time   => 
1.second,  :file         => "/etc/puppet/manifests/site.pp",
+                                                   :line              => 1,    
     :tags         => ["file", "default"],
+                                                   :time              => 
Time.now,  :change_count => 1,
+                                                   :out_of_sync_count => 1,    
     :skipped      => false,
+                                                   :failed            => false 
)
+
+        res2 = report.resource_statuses.generate!( :resource_type     => 
"File",    :title        => "/etc/hosts",
+                                                   :evaluation_time   => 
2.seconds, :file         => "/etc/puppet/manifests/site.pp",
+                                                   :line              => 5,    
     :tags         => ["file", "default"],
+                                                   :time              => 
Time.now,  :change_count => 2,
+                                                   :out_of_sync_count => 2,    
     :skipped      => false,
+                                                   :failed            => true )
+
+        res1.reload
+        res2.reload
+
+        get :index, :format => "csv"
+
+        response.should be_success
+        response.body.split("\n").should =~ [
+          header,
+          
%Q[#{@node.name},failed,2,0,1,1,File,/etc/sudoers,1.0,/etc/puppet/manifests/site.pp,1,#{res1.time},1,1,false,false],
+          
%Q[#{@node.name},failed,2,0,1,1,File,/etc/hosts,2.0,/etc/puppet/manifests/site.pp,5,#{res2.time},2,2,false,true]
+        ]
+      end
+    end
   end
 
   describe "#show" do
-- 
1.7.5.1

-- 
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