Skip to content

Commit c8bb93d

Browse files
committed
(MODULES-10876) Use version-specific http client
In versions before 7.0.0 we are not guaranteed to have the new client. So we should continue to use the old client.
1 parent 998f4c5 commit c8bb93d

File tree

2 files changed

+93
-29
lines changed

2 files changed

+93
-29
lines changed

lib/puppet/util/puppetdb_validator.rb

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,50 @@
22

33
# Validator class, for testing that PuppetDB is alive
44
class Puppet::Util::PuppetdbValidator
5-
attr_reader :test_uri
5+
attr_reader :puppetdb_server
6+
attr_reader :puppetdb_port
7+
attr_reader :use_ssl
8+
attr_reader :test_path
69
attr_reader :test_headers
710

811
def initialize(puppetdb_server, puppetdb_port, use_ssl = true, test_path = '/pdb/meta/v1/version')
9-
@test_uri = URI("#{use_ssl ? 'https' : 'http'}://#{puppetdb_server}:#{puppetdb_port}#{test_path}")
12+
@puppetdb_server = puppetdb_server
13+
@puppetdb_port = puppetdb_port
14+
@use_ssl = use_ssl
15+
@test_path = test_path
1016
@test_headers = { 'Accept' => 'application/json' }
1117
end
1218

19+
def log_error(cause, code = nil)
20+
if code.nil?
21+
Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): #{cause}"
22+
else
23+
Puppet.notice "Unable to connect to puppetdb server (http#{use_ssl ? 's' : ''}://#{puppetdb_server}:#{puppetdb_port}): [#{code}] #{cause}"
24+
end
25+
end
26+
27+
def valid_connection_new_client?
28+
test_uri = URI("#{use_ssl ? 'https' : 'http'}://#{puppetdb_server}:#{puppetdb_port}#{test_path}")
29+
begin
30+
conn = Puppet.runtime[:http]
31+
_response = conn.get(test_uri, headers: test_headers)
32+
true
33+
rescue Puppet::HTTP::ResponseError => e
34+
log_error e.message, e.response.code
35+
false
36+
end
37+
end
38+
39+
def valid_connection_old_client?
40+
conn = Puppet::Network::HttpPool.http_instance(puppetdb_server, puppetdb_port, use_ssl)
41+
response = conn.get(test_path, test_headers)
42+
unless response.is_a?(Net::HTTPSuccess)
43+
log_error(response.msg, response.code)
44+
return false
45+
end
46+
true
47+
end
48+
1349
# Utility method; attempts to make an http/https connection to the puppetdb server.
1450
# This is abstracted out into a method so that it can be called multiple times
1551
# for retry attempts.
@@ -19,17 +55,14 @@ def attempt_connection
1955
# All that we care about is that we are able to connect successfully via
2056
# http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
2157
# on the puppetdb server.
22-
conn = Puppet.runtime[:http]
2358

24-
response = conn.get(test_uri, headers: test_headers)
25-
if response.success?
26-
return true
59+
if Gem::Version.new(Puppet.version) >= Gem::Version.new('7.0.0')
60+
valid_connection_new_client?
2761
else
28-
Puppet.notice "Unable to connect to puppetdb server (#{test_uri}): [#{response.code}] #{response.reason}"
29-
return false
62+
valid_connection_old_client?
3063
end
3164
rescue StandardError => e
32-
Puppet.notice "Unable to connect to puppetdb server (#{test_uri}): #{e.message}"
65+
log_error(e.message)
3366
return false
3467
end
3568
end

spec/unit/util/puppetdb_validator_spec.rb

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,57 @@
33

44
describe 'Puppet::Util::PuppetdbValidator' do
55
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
2657
end
2758

2859
it 'returns true if connection succeeds' do

0 commit comments

Comments
 (0)