Please review pull request #561: Ticket/2.7.x/10722 error on bad http response opened by (pcarlisle)

Description:

Log error when report processing gets a non-2xx http response

incorporates https://github.com/puppetlabs/puppet/pull/205

  • Opened: Mon Mar 05 20:28:05 UTC 2012
  • Based on: puppetlabs:2.7.x (3c422310c05e884c65b24c9af76e56a8d625d6c7)
  • Requested merge: pcarlisle:ticket/2.7.x/10722-error-on-bad-http-response (a2208d037aca65cb211a653fb15a14af35fa94ff)

Diff follows:

diff --git a/lib/puppet/reports/http.rb b/lib/puppet/reports/http.rb
index 7ac54df..c01e879 100644
--- a/lib/puppet/reports/http.rb
+++ b/lib/puppet/reports/http.rb
@@ -16,7 +16,10 @@ def process
     req.body = self.to_yaml
     req.content_type = "application/x-yaml"
     Net::HTTP.new(url.host, url.port).start {|http|
-      http.request(req)
+      response = http.request(req)
+      unless response.kind_of?(Net::HTTPSuccess)
+        Puppet.err "Unable to submit report to #{Puppet[:reporturl].to_s} [#{response.code}] #{response.msg}"
+      end
     }
   end
 end
diff --git a/spec/unit/reports/http_spec.rb b/spec/unit/reports/http_spec.rb
index d7c37bf..5a6b5cb 100755
--- a/spec/unit/reports/http_spec.rb
+++ b/spec/unit/reports/http_spec.rb
@@ -1,55 +1,71 @@
 #!/usr/bin/env rspec
 require 'spec_helper'
-
 require 'puppet/reports'
 
-# FakeHTTP fakes the behavior of Net::HTTP#request and acts as a sensor for an
-# otherwise difficult to trace method call.
-#
-class FakeHTTP
-  REQUESTS = {}
-  def self.request(req)
-    REQUESTS[req.path] = req
-  end
-end
-
 processor = Puppet::Reports.report(:http)
 
 describe processor do
-  before  { Net::HTTP.any_instance.stubs(:start).yields(FakeHTTP) }
   subject { Puppet::Transaction::Report.new("apply").extend(processor) }
 
-  it { should respond_to(:process) }
-
   it "should use the reporturl setting's host and port" do
     uri = URI.parse(Puppet[:reporturl])
     Net::HTTP.expects(:new).with(uri.host, uri.port).returns(stub_everything('http'))
     subject.process
   end
 
-  describe "request" do
-    before { subject.process }
+  describe "when making a request" do
+    let(:http) { mock "http" }
+    let(:httpok) { Net::HTTPOK.new('1.1', 200, '') }
 
-    describe "path" do
-      it "should use the path specified by the 'reporturl' setting" do
-        reports_request.path.should == URI.parse(Puppet[:reporturl]).path
-      end
+    before :each do
+      Net::HTTP.any_instance.expects(:start).yields(http)
     end
 
-    describe "body" do
-      it "should be the report as YAML" do
-        reports_request.body.should == subject.to_yaml
-      end
+    it "should use the path specified by the 'reporturl' setting" do
+      http.expects(:request).with {|req|
+        req.path.should == URI.parse(Puppet[:reporturl]).path
+      }.returns(httpok)
+
+      subject.process
     end
 
-    describe "content type" do
-      it "should be 'application/x-yaml'" do
-        reports_request.content_type.should == "application/x-yaml"
-      end
+    it "should give the body as the report as YAML" do
+      http.expects(:request).with {|req|
+        req.body.should == subject.to_yaml
+      }.returns(httpok)
+
+      subject.process
     end
-  end
 
-  private
+    it "should set content-type to 'application/x-yaml'" do
+      http.expects(:request).with {|req|
+        req.content_type.should == "application/x-yaml"
+      }.returns(httpok)
 
-  def reports_request; FakeHTTP::REQUESTS[URI.parse(Puppet[:reporturl]).path] end
+      subject.process
+    end
+
+    Net::HTTPResponse::CODE_TO_OBJ.each do |code, klass|
+      if code.to_i >= 200 and code.to_i < 300
+        it "should succeed on http code #{code}" do
+          response = klass.new('1.1', code, '')
+          http.expects(:request).returns(response)
+
+          Puppet.expects(:err).never
+          subject.process
+        end
+      end
+
+      if code.to_i >= 300
+        it "should log error on http code #{code}" do
+          response = klass.new('1.1', code, '')
+          http.expects(:request).returns(response)
+
+          Puppet.expects(:err)
+          subject.process
+        end
+      end
+    end
+
+  end
 end

    

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