Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Modernize/Fix CI
  • Loading branch information
smortex committed Oct 8, 2023
commit 3e25635301c2ae9b90839c14ca42e218f8ffe638
69 changes: 26 additions & 43 deletions spec/unit/util/puppetdb_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,34 @@

url = '/pdb/meta/v1/version'
if Puppet::PUPPETVERSION.to_f < 7
conn_ok = stub
conn_ok.stubs(:get).with(url, 'Accept' => 'application/json').returns(nethttpok)
conn_ok.stubs(:read_timeout=).with(2)
conn_ok.stubs(:open_timeout=).with(2)
conn_ok = double
allow(conn_ok).to receive(:get).with(url, 'Accept' => 'application/json').and_return(nethttpok)
allow(conn_ok).to receive(:read_timeout=).with(2)
allow(conn_ok).to receive(:open_timeout=).with(2)

conn_not_found = stub
conn_not_found.stubs(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').returns(notfound)
conn_not_found = double
allow(conn_not_found).to receive(:get).with('/pdb/meta/v1/version', 'Accept' => 'application/json').and_return(notfound)

Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host')
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused')
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok)
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8081, true).returns(conn_ok)
Puppet::Network::HttpPool.stubs(:http_instance).with('wrongserver.com', 8081, true).returns(conn_not_found)
allow(Puppet::Network::HttpPool).to receive(:http_instance).and_raise('Unknown host')
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, true).and_raise('Connection refused')
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8080, false).and_return(conn_ok)
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('mypuppetdb.com', 8081, true).and_return(conn_ok)
allow(Puppet::Network::HttpPool).to receive(:http_instance).with('wrongserver.com', 8081, true).and_return(conn_not_found)
else
http = stub
Puppet::HTTP::Client.stubs(:new).returns(http)
http = double
allow(Puppet::HTTP::Client).to receive(:new).and_return(http)

http.stubs(:get).with { |uri, _opts|
uri.hostname == 'mypuppetdb.com' &&
uri.port == 8080 &&
uri.scheme == 'https'
}.raises Puppet::HTTP::HTTPError, 'Connection refused'
allow(http).to receive(:get) do |uri, _opts|
raise(Puppet::HTTP::HTTPError, 'Connection refused') if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'https'
raise Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound) if uri.hostname == 'wrongserver.com' && uri.port == 8081 && uri.scheme == 'https'
raise Puppet::HTTP::HTTPError, 'Unknown host' if uri.hostname == 'non-existing.com' && uri.scheme == 'https'

http.stubs(:get).with { |uri, _opts|
uri.hostname == 'mypuppetdb.com' &&
uri.port == 8080 &&
uri.scheme == 'http'
}.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok))

http.stubs(:get).with { |uri, _opts|
uri.hostname == 'mypuppetdb.com' &&
uri.port == 8081 &&
uri.scheme == 'https'
}.returns(Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok))

http.stubs(:get).with { |uri, _opts|
uri.hostname == 'wrongserver.com' &&
uri.port == 8081 &&
uri.scheme == 'https'
}.raises Puppet::HTTP::ResponseError, Puppet::HTTP::ResponseNetHTTP.new(url, notfound)

http.stubs(:get).with { |uri, _opts|
uri.hostname == 'non-existing.com' &&
uri.scheme == 'https'
}.raises Puppet::HTTP::HTTPError, 'Unknown host'
if uri.hostname == 'mypuppetdb.com' && uri.port == 8080 && uri.scheme == 'http'
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
elsif uri.hostname == 'mypuppetdb.com' && uri.port == 8081 && uri.scheme == 'https'
Puppet::HTTP::ResponseNetHTTP.new(url, nethttpok)
end
end
end
end

Expand All @@ -70,23 +53,23 @@
puppetdb_server = 'mypuppetdb.com'
puppetdb_port = 8080
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
expect(validator.attempt_connection).to be false
end

it 'returns false and issues an appropriate notice if connection succeeds but puppetdb is not available' do
puppetdb_server = 'wrongserver.com'
puppetdb_port = 8081
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
expect(validator.attempt_connection).to be false
end

it 'returns false and issues an appropriate notice if host:port is unreachable or does not exist' do
puppetdb_server = 'non-existing.com'
puppetdb_port = nil
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
expect(Puppet).to receive(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
expect(validator.attempt_connection).to be false
end
end