File: api_spec.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (109 lines) | stat: -rw-r--r-- 4,166 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'spec_helper'
require 'puppet_spec/handler'
require 'puppet/network/http'
require 'puppet/version'

describe Puppet::Network::HTTP::API do
  def respond(text)
    lambda { |req, res| res.respond_with(200, "text/plain", text) }
  end

  describe "#not_found" do
    let(:response) { Puppet::Network::HTTP::MemoryResponse.new }

    let(:routes) {
      Puppet::Network::HTTP::Route.path(Regexp.new("foo")).
      any.
      chain(Puppet::Network::HTTP::Route.path(%r{^/bar$}).get(respond("bar")),
            Puppet::Network::HTTP::API.not_found)
    }

    it "mounts the chained routes" do
      request = Puppet::Network::HTTP::Request.from_hash(:path => "foo/bar")
      routes.process(request, response)

      expect(response.code).to eq(200)
      expect(response.body).to eq("bar")
    end

    it "responds to unknown paths with a 404" do
      request = Puppet::Network::HTTP::Request.from_hash(:path => "foo/unknown")

      expect do
        routes.process(request, response)
      end.to raise_error(Puppet::Network::HTTP::Error::HTTPNotFoundError)
    end
  end

  describe "Puppet API" do
    let(:handler) { PuppetSpec::Handler.new(Puppet::Network::HTTP::API.server_routes,
                                            Puppet::Network::HTTP::API.not_found_upgrade) }

    let(:server_prefix) { Puppet::Network::HTTP::SERVER_URL_PREFIX }

    it "raises a not-found error for non-CA or server routes and suggests an upgrade" do
      req = Puppet::Network::HTTP::Request.from_hash(:path => "/unknown")
      res = {}
      handler.process(req, res)
      expect(res[:status]).to eq(404)
      expect(res[:body]).to include("Puppet version: #{Puppet.version}")
    end

    describe "when processing Puppet 3 routes" do
      it "gives an upgrade message for server routes" do
        req = Puppet::Network::HTTP::Request.from_hash(:path => "/production/node/foo")
        res = {}
        handler.process(req, res)
        expect(res[:status]).to eq(404)
        expect(res[:body]).to include("Puppet version: #{Puppet.version}")
        expect(res[:body]).to include("Supported /puppet API versions: #{Puppet::Network::HTTP::SERVER_URL_VERSIONS}")
      end

      it "gives an upgrade message for CA routes" do
        req = Puppet::Network::HTTP::Request.from_hash(:path => "/production/certificate/foo")
        res = {}
        handler.process(req, res)
        expect(res[:status]).to eq(404)
        expect(res[:body]).to include("Puppet version: #{Puppet.version}")
        expect(res[:body]).to include("Supported /puppet API versions: #{Puppet::Network::HTTP::SERVER_URL_VERSIONS}")
      end
    end

    describe "when processing server routes" do
      # simulate puppetserver registering its authconfigloader class
      around :each do |example|
        Puppet::Network::Authorization.authconfigloader_class = Object
        begin
          example.run
        ensure
          Puppet::Network::Authorization.authconfigloader_class = nil
        end
      end

      it "responds to v3 indirector requests" do
        req = Puppet::Network::HTTP::Request.from_hash(:path => "#{server_prefix}/v3/node/foo",
                                                       :params => {:environment => "production"},
                                                       :headers => {'accept' => "application/json"})
        res = {}
        handler.process(req, res)
        expect(res[:status]).to eq(200)
      end

      it "responds to v3 environments requests" do
        req = Puppet::Network::HTTP::Request.from_hash(:path => "#{server_prefix}/v3/environments")
        res = {}
        handler.process(req, res)
        expect(res[:status]).to eq(200)
      end

      it "responds with a not found error to non-v3 requests and does not suggest an upgrade" do
        req = Puppet::Network::HTTP::Request.from_hash(:path => "#{server_prefix}/unknown")
        res = {}
        handler.process(req, res)
        expect(res[:status]).to eq(404)
        expect(res[:body]).to include("No route for GET #{server_prefix}/unknown")
        expect(res[:body]).not_to include("Puppet version: #{Puppet.version}")
      end
    end
  end
end