~ubuntu-branches/ubuntu/quantal/puppet/quantal

« back to all changes in this revision

Viewing changes to spec/unit/reports/http_spec.rb

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-07-14 01:56:30 UTC
  • mfrom: (1.1.29) (3.1.43 sid)
  • Revision ID: package-import@ubuntu.com-20120714015630-ntj41rkvkq4zph4y
Tags: 2.7.18-1ubuntu1
* Resynchronise with Debian. (LP: #1023931) Remaining changes:
  - debian/puppetmaster-passenger.postinst: Make sure we error if puppet
    config print doesn't work
  - debian/puppetmaster-passenger.postinst: Ensure upgrades from
    <= 2.7.11-1 fixup passenger apache configuration.
* Dropped upstreamed patches:
  - debian/patches/CVE-2012-1906_CVE-2012-1986_to_CVE-2012-1989.patch
  - debian/patches/puppet-12844
  - debian/patches/2.7.17-Puppet-July-2012-CVE-fixes.patch
* Drop Build-Depends on ruby-rspec (in universe):
  - debian/control: remove ruby-rspec from Build-Depends
  - debian/patches/no-rspec.patch: make Rakefile work anyway if rspec
    isn't installed so we can use it in debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env rspec
2
2
require 'spec_helper'
3
 
 
4
3
require 'puppet/reports'
5
4
 
6
 
# FakeHTTP fakes the behavior of Net::HTTP#request and acts as a sensor for an
7
 
# otherwise difficult to trace method call.
8
 
#
9
 
class FakeHTTP
10
 
  REQUESTS = {}
11
 
  def self.request(req)
12
 
    REQUESTS[req.path] = req
13
 
  end
14
 
end
15
 
 
16
5
processor = Puppet::Reports.report(:http)
17
6
 
18
7
describe processor do
19
 
  before  { Net::HTTP.any_instance.stubs(:start).yields(FakeHTTP) }
20
8
  subject { Puppet::Transaction::Report.new("apply").extend(processor) }
21
9
 
22
 
  it { should respond_to(:process) }
23
 
 
24
10
  it "should use the reporturl setting's host and port" do
25
11
    uri = URI.parse(Puppet[:reporturl])
26
12
    Net::HTTP.expects(:new).with(uri.host, uri.port).returns(stub_everything('http'))
27
13
    subject.process
28
14
  end
29
15
 
30
 
  describe "request" do
31
 
    before { subject.process }
32
 
 
33
 
    describe "path" do
34
 
      it "should use the path specified by the 'reporturl' setting" do
35
 
        reports_request.path.should == URI.parse(Puppet[:reporturl]).path
36
 
      end
37
 
    end
38
 
 
39
 
    describe "body" do
40
 
      it "should be the report as YAML" do
41
 
        reports_request.body.should == subject.to_yaml
42
 
      end
43
 
    end
44
 
 
45
 
    describe "content type" do
46
 
      it "should be 'application/x-yaml'" do
47
 
        reports_request.content_type.should == "application/x-yaml"
48
 
      end
49
 
    end
 
16
  describe "when making a request" do
 
17
    let(:http) { mock "http" }
 
18
    let(:httpok) { Net::HTTPOK.new('1.1', 200, '') }
 
19
 
 
20
    before :each do
 
21
      Net::HTTP.any_instance.expects(:start).yields(http)
 
22
    end
 
23
 
 
24
    it "should use the path specified by the 'reporturl' setting" do
 
25
      http.expects(:request).with {|req|
 
26
        req.path.should == URI.parse(Puppet[:reporturl]).path
 
27
      }.returns(httpok)
 
28
 
 
29
      subject.process
 
30
    end
 
31
 
 
32
    it "should give the body as the report as YAML" do
 
33
      http.expects(:request).with {|req|
 
34
        req.body.should == subject.to_yaml
 
35
      }.returns(httpok)
 
36
 
 
37
      subject.process
 
38
    end
 
39
 
 
40
    it "should set content-type to 'application/x-yaml'" do
 
41
      http.expects(:request).with {|req|
 
42
        req.content_type.should == "application/x-yaml"
 
43
      }.returns(httpok)
 
44
 
 
45
      subject.process
 
46
    end
 
47
 
 
48
    Net::HTTPResponse::CODE_TO_OBJ.each do |code, klass|
 
49
      if code.to_i >= 200 and code.to_i < 300
 
50
        it "should succeed on http code #{code}" do
 
51
          response = klass.new('1.1', code, '')
 
52
          http.expects(:request).returns(response)
 
53
 
 
54
          Puppet.expects(:err).never
 
55
          subject.process
 
56
        end
 
57
      end
 
58
 
 
59
      if code.to_i >= 300
 
60
        it "should log error on http code #{code}" do
 
61
          response = klass.new('1.1', code, '')
 
62
          http.expects(:request).returns(response)
 
63
 
 
64
          Puppet.expects(:err)
 
65
          subject.process
 
66
        end
 
67
      end
 
68
    end
 
69
 
50
70
  end
51
 
 
52
 
  private
53
 
 
54
 
  def reports_request; FakeHTTP::REQUESTS[URI.parse(Puppet[:reporturl]).path] end
55
71
end