|
8 | 8 |
|
9 | 9 | url = '/pdb/meta/v1/version'
|
10 | 10 | if Puppet::PUPPETVERSION.to_f < 7
|
11 |
| - conn_ok = stub |
12 |
| - conn_ok.stubs(:get).with(url, 'Accept' => 'application/json').returns(nethttpok) |
13 |
| - conn_ok.stubs(:read_timeout=).with(2) |
14 |
| - conn_ok.stubs(:open_timeout=).with(2) |
| 11 | + conn_ok = double |
| 12 | + allow(conn_ok).to receive(:get).with(url, 'Accept' => 'application/json').and_return(nethttpok) |
| 13 | + allow(conn_ok).to receive(:read_timeout=).with(2) |
| 14 | + allow(conn_ok).to receive(:open_timeout=).with(2) |
15 | 15 |
|
16 |
| - conn_not_found = stub |
17 |
| - conn_not_found.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(notfound) |
| 16 | + conn_not_found = double |
| 17 | + allow(conn_not_found).to receive(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').and_return(notfound) |
18 | 18 |
|
19 |
| - Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host') |
20 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused') |
21 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok) |
22 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8081, true).returns(conn_ok) |
23 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('wrongserver.com', 8081, true).returns(conn_not_found) |
| 19 | + allow(Puppet::Network::HttpPool).to receive(:http_instance).and_raise('Unknown host') |
| 20 | + allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, true).and_raise('Connection refused') |
| 21 | + allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, false).and_return(conn_ok) |
| 22 | + allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8081, true).and_return(conn_ok) |
| 23 | + allow(Puppet::Network::HttpPool).to receive(:http_instance).with('wrongserver.com', 8081, true).and_return(conn_not_found) |
24 | 24 | else
|
25 |
| - http = stub |
26 |
| - Puppet::HTTP::Client.stubs(:new).returns(http) |
| 25 | + http = double |
| 26 | + allow(Puppet::HTTP::Client).to receive(:new).and_return(http) |
27 | 27 |
|
28 |
| - http.stubs(:get).with { |uri, _opts| |
29 |
| - uri.hostname == 'mypuppetdb.com' && |
30 |
| - uri.port == 8080 && |
31 |
| - uri.scheme == 'https' |
32 |
| - }.raises Puppet::HTTP::HTTPError, 'Connection refused' |
33 |
| - |
34 |
| - http.stubs(:get).with { |uri, _opts| |
35 |
| - uri.hostname == 'mypuppetdb.com' && |
36 |
| - uri.port == 8080 && |
37 |
| - uri.scheme == 'http' |
38 |
| - }.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)) |
39 |
| - |
40 |
| - http.stubs(:get).with { |uri, _opts| |
41 |
| - uri.hostname == 'mypuppetdb.com' && |
42 |
| - uri.port == 8081 && |
43 |
| - uri.scheme == 'https' |
44 |
| - }.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)) |
45 |
| - |
46 |
| - http.stubs(:get).with { |uri, _opts| |
47 |
| - uri.hostname == 'wrongserver.com' && |
48 |
| - uri.port == 8081 && |
49 |
| - uri.scheme == 'https' |
50 |
| - }.raises Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound) |
51 |
| - |
52 |
| - http.stubs(:get).with { |uri, _opts| |
53 |
| - uri.hostname == 'non-existing.com' && |
54 |
| - uri.scheme == 'https' |
55 |
| - }.raises Puppet::HTTP::HTTPError, 'Unknown host' |
| 28 | + allow(http).to receive(:get) do |uri, _opts| |
| 29 | + if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'https' |
| 30 | + raise(Puppet::HTTP::HTTPError, 'Connection refused') |
| 31 | + elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'http' |
| 32 | + Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok) |
| 33 | + elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8081 && uri.scheme == 'https' |
| 34 | + Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok) |
| 35 | + elsif uri.hostname == 'wrongserver.com' && uri.port == 8081 && uri.scheme == 'https' |
| 36 | + raise Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound) |
| 37 | + elsif uri.hostname == 'non-existing.com' && uri.scheme == 'https' |
| 38 | + raise Puppet::HTTP::HTTPError, 'Unknown host' |
| 39 | + end |
| 40 | + end |
56 | 41 | end
|
57 | 42 | end
|
58 | 43 |
|
|
70 | 55 | puppetdb_server = 'mypuppetdb.com'
|
71 | 56 | puppetdb_port = 8080
|
72 | 57 | validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
|
73 |
| - Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused") |
| 58 | + allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused") |
74 | 59 | expect(validator.attempt_connection).to be false
|
| 60 | + expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused") |
75 | 61 | end
|
76 | 62 |
|
77 | 63 | it 'returns false and issues an appropriate notice if connection succeeds but puppetdb is not available' do
|
78 | 64 | puppetdb_server = 'wrongserver.com'
|
79 | 65 | puppetdb_port = 8081
|
80 | 66 | validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
|
81 |
| - Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found") |
| 67 | + allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found") |
82 | 68 | expect(validator.attempt_connection).to be false
|
| 69 | + expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found") |
83 | 70 | end
|
84 | 71 |
|
85 | 72 | it 'returns false and issues an appropriate notice if host:port is unreachable or does not exist' do
|
86 | 73 | puppetdb_server = 'non-existing.com'
|
87 | 74 | puppetdb_port = nil
|
88 | 75 | validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
|
89 |
| - Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host") |
| 76 | + allow(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host") |
90 | 77 | expect(validator.attempt_connection).to be false
|
| 78 | + expect(Puppet).to have_received(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host") |
91 | 79 | end
|
92 | 80 | end
|
0 commit comments