puppetdb_validator_spec.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. require 'spec_helper'
  2. require 'puppet/util/puppetdb_validator'
  3. describe 'Puppet::Util::PuppetdbValidator' do
  4. before do
  5. response_ok = stub()
  6. response_ok.stubs(:kind_of?).with(Net::HTTPSuccess).returns(true)
  7. response_not_found = stub()
  8. response_not_found.stubs(:kind_of?).with(Net::HTTPSuccess).returns(false)
  9. response_not_found.stubs(:code).returns(404)
  10. response_not_found.stubs(:msg).returns('Not found')
  11. conn_ok = stub()
  12. conn_ok.stubs(:get).with('/pdb/meta/v1/version', {"Accept" => "application/json"}).returns(response_ok)
  13. conn_ok.stubs(:read_timeout=).with(2)
  14. conn_ok.stubs(:open_timeout=).with(2)
  15. conn_not_found = stub()
  16. conn_not_found.stubs(:get).with('/pdb/meta/v1/version', {"Accept" => "application/json"}).returns(response_not_found)
  17. Puppet::Network::HttpPool.stubs(:http_instance).raises('Unknown host')
  18. Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, true).raises('Connection refused')
  19. Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8080, false).returns(conn_ok)
  20. Puppet::Network::HttpPool.stubs(:http_instance).with('mypuppetdb.com', 8081, true).returns(conn_ok)
  21. Puppet::Network::HttpPool.stubs(:http_instance).with('wrongserver.com', 8081, true).returns(conn_not_found)
  22. end
  23. it 'returns true if connection succeeds' do
  24. validator = Puppet::Util::PuppetdbValidator.new('mypuppetdb.com', 8081)
  25. expect(validator.attempt_connection).to be true
  26. end
  27. it 'should still validate without ssl' do
  28. Puppet[:configtimeout] = 2
  29. validator = Puppet::Util::PuppetdbValidator.new('mypuppetdb.com', 8080, false)
  30. expect(validator.attempt_connection).to be true
  31. end
  32. it 'returns false and issues an appropriate notice if connection is refused' do
  33. puppetdb_server = 'mypuppetdb.com'
  34. puppetdb_port = 8080
  35. validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
  36. Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Connection refused")
  37. expect(validator.attempt_connection).to be false
  38. end
  39. it 'returns false and issues an appropriate notice if connection succeeds but puppetdb is not available' do
  40. puppetdb_server = 'wrongserver.com'
  41. puppetdb_port = 8081
  42. validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
  43. Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): [404] Not found")
  44. expect(validator.attempt_connection).to be false
  45. end
  46. it 'returns false and issues an appropriate notice if host:port is unreachable or does not exist' do
  47. puppetdb_server = 'non-existing.com'
  48. puppetdb_port = nil
  49. validator = Puppet::Util::PuppetdbValidator.new(puppetdb_server, puppetdb_port)
  50. Puppet.expects(:notice).with("Unable to connect to puppetdb server (https://#{puppetdb_server}:#{puppetdb_port}): Unknown host")
  51. expect(validator.attempt_connection).to be false
  52. end
  53. end