|
3 | 3 |
|
4 | 4 | describe 'Puppet::Util::PuppetdbValidator' do
|
5 | 5 | before(:each) do
|
6 |
| - response_ok = stub |
7 |
| - response_ok.stubs(:is_a?).with(Net::HTTPSuccess).returns(true) |
8 |
| - response_not_found = stub |
9 |
| - response_not_found.stubs(:is_a?).with(Net::HTTPSuccess).returns(false) |
10 |
| - response_not_found.stubs(:code).returns(404) |
11 |
| - response_not_found.stubs(:msg).returns('Not found') |
12 |
| - |
13 |
| - conn_ok = stub |
14 |
| - conn_ok.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(response_ok) |
15 |
| - conn_ok.stubs(:read_timeout=).with(2) |
16 |
| - conn_ok.stubs(:open_timeout=).with(2) |
17 |
| - |
18 |
| - conn_not_found = stub |
19 |
| - conn_not_found.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(response_not_found) |
20 |
| - |
21 |
| - Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host') |
22 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused') |
23 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok) |
24 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8081, true).returns(conn_ok) |
25 |
| - Puppet::Network::HttpPool.stubs(:http_instance).with('wrongserver.com', 8081, true).returns(conn_not_found) |
| 6 | + nethttpok = Net::HTTPOK.new('1.1', 200, 'OK') |
| 7 | + notfound = Net::HTTPNotFound.new('1.1', 404, 'Not found') |
| 8 | + |
| 9 | + url = '/pdb/meta/v1/version' |
| 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) |
| 15 | + |
| 16 | + conn_not_found = stub |
| 17 | + conn_not_found.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(notfound) |
| 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) |
| 24 | + else |
| 25 | + http = stub |
| 26 | + Puppet::HTTP::Client.stubs(:new).returns(http) |
| 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' |
| 56 | + end |
26 | 57 | end
|
27 | 58 |
|
28 | 59 | it 'returns true if connection succeeds' do
|
|
0 commit comments