diff options
-rw-r--r-- | lib/puppet/util/ldap/connection.rb | 13 | ||||
-rwxr-xr-x | spec/unit/util/ldap/connection.rb | 42 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/puppet/util/ldap/connection.rb b/lib/puppet/util/ldap/connection.rb index abcc07ecb..861539872 100644 --- a/lib/puppet/util/ldap/connection.rb +++ b/lib/puppet/util/ldap/connection.rb @@ -8,6 +8,18 @@ class Puppet::Util::Ldap::Connection attr_reader :connection + # Return a default connection, using our default settings. + def self.instance + ssl = if Puppet[:ldaptls] + :tls + elsif Puppet[:ldapssl] + true + else + false + end + new(Puppet[:ldapserver], Puppet[:ldapport], :ssl => ssl) + end + def close connection.unbind if connection.bound? end @@ -51,6 +63,7 @@ class Puppet::Util::Ldap::Connection @connection.set_option(LDAP::LDAP_OPT_REFERRALS, LDAP::LDAP_OPT_ON) @connection.simple_bind(user, password) rescue => detail + puts detail.class raise Puppet::Error, "Could not connect to LDAP: %s" % detail end end diff --git a/spec/unit/util/ldap/connection.rb b/spec/unit/util/ldap/connection.rb index 212f3ca54..9392466c7 100755 --- a/spec/unit/util/ldap/connection.rb +++ b/spec/unit/util/ldap/connection.rb @@ -111,4 +111,46 @@ describe Puppet::Util::Ldap::Connection do @connection.close end end + + it "should have a class-level method for creating a default connection" do + Puppet::Util::Ldap::Connection.should respond_to(:instance) + end + + describe "when creating a default connection" do + before do + Puppet.settings.stubs(:value).returns "whatever" + end + + it "should use the :ldapserver setting to determine the host" do + Puppet.settings.expects(:value).with(:ldapserver).returns "myserv" + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| host == "myserv" } + Puppet::Util::Ldap::Connection.instance + end + + it "should use the :ldapport setting to determine the port" do + Puppet.settings.expects(:value).with(:ldapport).returns "456" + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| port == "456" } + Puppet::Util::Ldap::Connection.instance + end + + it "should set ssl to :tls if tls is enabled" do + Puppet.settings.expects(:value).with(:ldaptls).returns true + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:ssl] == :tls } + Puppet::Util::Ldap::Connection.instance + end + + it "should set ssl to 'true' if ssl is enabled and tls is not" do + Puppet.settings.expects(:value).with(:ldaptls).returns false + Puppet.settings.expects(:value).with(:ldapssl).returns true + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:ssl] == true } + Puppet::Util::Ldap::Connection.instance + end + + it "should set ssl to false if neither ssl nor tls are enabled" do + Puppet.settings.expects(:value).with(:ldaptls).returns false + Puppet.settings.expects(:value).with(:ldapssl).returns false + Puppet::Util::Ldap::Connection.expects(:new).with { |host, port, options| options[:ssl] == false } + Puppet::Util::Ldap::Connection.instance + end + end end |