File: puppetserver_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (112 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download | duplicates (2)
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
110
111
112
require 'spec_helper'
require 'puppet/http'

describe Puppet::HTTP::Service::Puppetserver do
  let(:ssl_context) { Puppet::SSL::SSLContext.new }
  let(:client) { Puppet::HTTP::Client.new(ssl_context: ssl_context) }
  let(:subject) { client.create_session.route_to(:puppetserver) }

  before :each do
    Puppet[:server] = 'puppetserver.example.com'
  end

  context 'when making requests' do
    it 'includes default HTTP headers' do
      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/server").with do |request|
        expect(request.headers).to include({'X-Puppet-Version' => /./, 'User-Agent' => /./})
        expect(request.headers).to_not include('X-Puppet-Profiling')
      end.to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      subject.get_simple_status
    end

    it 'includes extra headers' do
      Puppet[:http_extra_headers] = 'region:us-west'

      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/server")
        .with(headers: {'Region' => 'us-west'})
        .to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      subject.get_simple_status
    end
  end

  context 'when routing to the puppetserver service' do
    it 'defaults the server and port based on settings' do
      Puppet[:server] = 'compiler2.example.com'
      Puppet[:serverport] = 8141

      stub_request(:get, "https://compiler2.example.com:8141/status/v1/simple/server")
        .to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      subject.get_simple_status
    end
  end

  context 'when getting puppetserver status' do
    let(:url) { "https://puppetserver.example.com:8140/status/v1/simple/server" }

    it 'returns the request response and status' do
      stub_request(:get, url)
        .to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      resp, status = subject.get_simple_status
      expect(resp).to be_a(Puppet::HTTP::Response)
      expect(status).to eq('running')
    end

    it 'raises a response error if unsuccessful' do
      stub_request(:get, url).to_return(status: [500, 'Internal Server Error'])

      expect {
        subject.get_simple_status
      }.to raise_error do |err|
        expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
        expect(err.message).to eq("Internal Server Error")
        expect(err.response.code).to eq(500)
      end
    end

    it 'accepts an ssl context' do
      stub_request(:get, url)
        .to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      other_ctx = Puppet::SSL::SSLContext.new
      expect(client).to receive(:connect).with(URI(url), options: {ssl_context: other_ctx}).and_call_original

      session = client.create_session
      service = Puppet::HTTP::Service.create_service(client, session, :puppetserver, 'puppetserver.example.com', 8140)
      service.get_simple_status(ssl_context: other_ctx)
    end
  end

  context 'when /status/v1/simple/server returns not found' do
    it 'calls /status/v1/simple/master' do
      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/server")
        .to_return(status: [404, 'not found: server'])

      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/master")
        .to_return(body: "running", headers: {'Content-Type' => 'text/plain;charset=utf-8'})

      resp, status = subject.get_simple_status
      expect(resp).to be_a(Puppet::HTTP::Response)
      expect(status).to eq('running')
    end

    it 'raises a response error if fallback is unsuccessful' do
      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/server")
        .to_return(status: [404, 'not found: server'])

      stub_request(:get, "https://puppetserver.example.com:8140/status/v1/simple/master")
        .to_return(status: [404, 'not found: master'])

      expect {
        subject.get_simple_status
      }.to raise_error do |err|
        expect(err).to be_an_instance_of(Puppet::HTTP::ResponseError)
        expect(err.message).to eq('not found: master')
        expect(err.response.code).to eq(404)
      end
    end
  end
end