2013-02-23 01:20:04 +01:00
|
|
|
require 'spec_helper'
|
|
|
|
require 'puppet/util/puppetdb_validator'
|
|
|
|
|
|
|
|
describe 'Puppet::Util::PuppetdbValidator' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
response_ok = stub()
|
|
|
|
response_ok.stubs(:kind_of?).with(Net::HTTPSuccess).returns(true)
|
|
|
|
response_not_found = stub()
|
|
|
|
response_not_found.stubs(:kind_of?).with(Net::HTTPSuccess).returns(false)
|
|
|
|
response_not_found.stubs(:code).returns(404)
|
|
|
|
response_not_found.stubs(:msg).returns('Not found')
|
|
|
|
|
|
|
|
conn_ok = stub()
|
2015-05-15 23:10:08 +02:00
|
|
|
conn_ok.stubs(:get).with('/pdb/meta/v1/version', {"Accept" => "application/json"}).returns(response_ok)
|
2013-06-06 20:20:57 +02:00
|
|
|
conn_ok.stubs(:read_timeout=).with(2)
|
|
|
|
conn_ok.stubs(:open_timeout=).with(2)
|
2013-02-23 01:20:04 +01:00
|
|
|
|
|
|
|
conn_not_found = stub()
|
2015-05-15 23:10:08 +02:00
|
|
|
conn_not_found.stubs(:get).with('/pdb/meta/v1/version', {"Accept" => "application/json"}).returns(response_not_found)
|
2013-02-23 01:20:04 +01:00
|
|
|
|
|
|
|
Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host')
|
|
|
|
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused')
|
2014-10-07 16:06:54 +02:00
|
|
|
Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok)
|
2013-02-23 01:20:04 +01:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns true if connection succeeds' do
|
|
|
|
validator = Puppet::Util::PuppetdbValidator.new('mypuppetdb.com', 8081)
|
2015-04-21 15:10:41 +02:00
|
|
|
expect(validator.attempt_connection).to be true
|
2013-02-23 01:20:04 +01:00
|
|
|
end
|
|
|
|
|
2013-06-06 20:20:57 +02:00
|
|
|
it 'should still validate without ssl' do
|
|
|
|
Puppet[:configtimeout] = 2
|
|
|
|
validator = Puppet::Util::PuppetdbValidator.new('mypuppetdb.com', 8080, false)
|
2015-04-21 15:10:41 +02:00
|
|
|
expect(validator.attempt_connection).to be true
|
2013-06-06 20:20:57 +02:00
|
|
|
end
|
|
|
|
|
2013-02-23 01:20:04 +01:00
|
|
|
it 'returns false and issues an appropriate notice if connection is refused' do
|
|
|
|
puppetdb_server = 'mypuppetdb.com'
|
|
|
|
puppetdb_port = 8080
|
|
|
|
validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
|
2014-12-02 13:43:30 +01:00
|
|
|
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
|
2015-04-21 15:10:41 +02:00
|
|
|
expect(validator.attempt_connection).to be false
|
2013-02-23 01:20:04 +01:00
|
|
|
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)
|
2014-12-02 13:43:30 +01:00
|
|
|
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
|
2015-04-21 15:10:41 +02:00
|
|
|
expect(validator.attempt_connection).to be false
|
2013-02-23 01:20:04 +01:00
|
|
|
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)
|
2014-12-02 13:43:30 +01:00
|
|
|
Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
|
2015-04-21 15:10:41 +02:00
|
|
|
expect(validator.attempt_connection).to be false
|
2013-02-23 01:20:04 +01:00
|
|
|
end
|
2013-06-06 20:20:57 +02:00
|
|
|
|
2013-02-23 01:20:04 +01:00
|
|
|
end
|