Skip to content

Commit

Permalink
Correctly catch LDAP exception when failing to connect to server (#2574)
Browse files Browse the repository at this point in the history
  • Loading branch information
zippolyte authored Nov 12, 2018
1 parent 451a411 commit 2fd01be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions openldap/datadog_checks/openldap/openldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def check(self, instance):
res = conn.bind()
if not res:
raise ldap3.core.exceptions.LDAPBindError("Error binding to server: {}".format(conn.result))
except ldap3.core.exceptions.LDAPBindError:
self.log.exception("Could not connect to server at %s: %s", url, conn.result)
except ldap3.core.exceptions.LDAPExceptionError as e:
self.log.exception("Could not connect to server at %s: %s", url, e)
self.service_check("{}.can_connect".format(METRIC_PREFIX), self.CRITICAL, tags=tags)
raise

Expand Down
12 changes: 11 additions & 1 deletion openldap/tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,24 @@ def test_check(aggregator, check, openldap_server, instance):
def test_check_ssl(aggregator, check, openldap_server, instance_ssl):
tags = ["url:{}".format(instance_ssl["url"]), "test:integration"]
# Should fail certificate verification
with pytest.raises(ldap3.core.exceptions.LDAPSocketOpenError):
with pytest.raises(ldap3.core.exceptions.LDAPExceptionError):
check.check(instance_ssl)
aggregator.assert_service_check("openldap.can_connect", check.CRITICAL, tags=tags)
instance_ssl["ssl_verify"] = False
# Should work now
check.check(instance_ssl)
aggregator.assert_service_check("openldap.can_connect", check.OK, tags=tags)


def test_check_connection_failure(aggregator, check, openldap_server, instance):
instance["url"] = "bad_url"
tags = ["url:{}".format(instance["url"]), "test:integration"]
# Should fail certificate verification
with pytest.raises(ldap3.core.exceptions.LDAPExceptionError):
check.check(instance)
aggregator.assert_service_check("openldap.can_connect", check.CRITICAL, tags=tags)


@pytest.mark.skipif(not Platform.is_linux(), reason='Windows sockets are not file handles')
def test_check_socket(aggregator, check, openldap_server, instance):
instance["url"] = "ldapi://{}".format(openldap_server)
Expand Down

0 comments on commit 2fd01be

Please sign in to comment.