Browse Source

[ci] Ignore LDAP tests warnings to avoid GH false positive checks (#1984)

Tried moving warning suppression to test function

    with warnings.catch_warnings():  # Hide GH false positive check from LDAPError exception.
      warnings.simplefilter("ignore")

And:

PYTHONWARNINGS=always,ignore:::useradmin.test_ldap

Log trace:

2021-03-17T01:57:26.2822415Z   raise PopupException(smart_str(_('There was an error when communicating with LDAP: %s')) % str(e))
2021-03-17T01:57:26.2827941Z [16/Mar/2021 18:57:26 -0700] exceptions_renderable ERROR    Potential trace: [<FrameSummary file /home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py, line 601 in add_ldap_users>, <FrameSummary file /home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py, line 802 in import_ldap_users>, <FrameSummary file /home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py, line 945 in _import_ldap_users>, <FrameSummary file /home/runner/work/hue/hue/apps/useradmin/src/useradmin/test_ldap.py, line 797 in find_users>]
2021-03-17T01:57:26.2831079Z /home/runner/work/hue/hue/desktop/core/src/desktop/lib/i18n.py:62: RemovedInDjango40Warning: smart_text() is deprecated in favor of smart_str().
2021-03-17T01:57:26.2833451Z   return django.utils.encoding.smart_text(
2021-03-17T01:57:26.2834986Z /home/runner/work/hue/hue/desktop/core/src/desktop/lib/i18n.py:62: RemovedInDjango40Warning: smart_text() is deprecated in favor of smart_str().
2021-03-17T01:57:26.2836205Z   return django.utils.encoding.smart_text(
2021-03-17T01:57:26.2837847Z [16/Mar/2021 18:57:26 -0700] middleware   INFO     Processing exception: There was an error when communicating with LDAP: No such object: Traceback (most recent call last):
2021-03-17T01:57:26.2840575Z   File "/home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py", line 601, in add_ldap_users
2021-03-17T01:57:26.2841730Z     users = import_ldap_users(connection, username_pattern, False, import_by_dn, failed_users=failed_ldap_users)
2021-03-17T01:57:26.2842910Z   File "/home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py", line 802, in import_ldap_users
2021-03-17T01:57:26.2843714Z     return _import_ldap_users(
2021-03-17T01:57:26.2844545Z   File "/home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py", line 945, in _import_ldap_users
2021-03-17T01:57:26.2845559Z     user_info = connection.find_users(username_pattern, find_by_dn=import_by_dn)
2021-03-17T01:57:26.2846599Z   File "/home/runner/work/hue/hue/apps/useradmin/src/useradmin/test_ldap.py", line 797, in find_users
2021-03-17T01:57:26.2857228Z ##[error]    raise ldap.LDAPError('No such object')
2021-03-17T01:57:26.2868570Z ldap.LDAPError: No such object
2021-03-17T01:57:26.2868991Z
2021-03-17T01:57:26.2869623Z During handling of the above exception, another exception occurred:
2021-03-17T01:57:26.2870171Z
2021-03-17T01:57:26.2870600Z Traceback (most recent call last):
2021-03-17T01:57:26.2872129Z   File "/home/runner/work/hue/hue/build/env/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
2021-03-17T01:57:26.2873243Z     response = wrapped_callback(request, *callback_args, **callback_kwargs)
2021-03-17T01:57:26.2874132Z   File "/usr/lib/python3.8/contextlib.py", line 75, in inner
2021-03-17T01:57:26.2874778Z     return func(*args, **kwds)
2021-03-17T01:57:26.2875576Z   File "/home/runner/work/hue/hue/apps/useradmin/src/useradmin/views.py", line 604, in add_ldap_users
2021-03-17T01:57:26.2877056Z     raise PopupException(smart_str(_('There was an error when communicating with LDAP: %s')) % str(e))
2021-03-17T01:57:26.2878462Z desktop.lib.exceptions_renderable.PopupException: There was an error when communicating with LDAP: No such object
Romain Rigaux 4 years ago
parent
commit
793a1ebc42

+ 14 - 10
apps/useradmin/src/useradmin/test_ldap.py

@@ -40,6 +40,11 @@ from useradmin.views import sync_ldap_users, sync_ldap_groups, import_ldap_users
     add_ldap_users, add_ldap_groups, sync_ldap_users_groups
 from useradmin.tests import BaseUserAdminTests, LdapTestConnection, reset_all_groups, reset_all_users, create_long_username
 
+if sys.version_info[0] > 2:
+  from unittest.mock import patch, Mock, MagicMock
+else:
+  from mock import patch, Mock, MagicMock
+
 
 def get_multi_ldap_config():
   return {'multi_ldap_conf': {
@@ -792,10 +797,7 @@ class TestUserAdminLdap(BaseUserAdminTests):
 
   def test_ldap_exception_handling(self):
     # Set up LDAP tests to use a LdapTestConnection instead of an actual LDAP connection
-    class LdapTestConnectionError(LdapTestConnection):
-      def find_users(self, user, find_by_dn=False):
-        raise ldap.LDAPError('No such object')
-    ldap_access.CACHED_LDAP_CONN = LdapTestConnectionError()
+    ldap_access.CACHED_LDAP_CONN = LdapTestConnection()
 
     c = make_logged_in_client('test', is_superuser=True)
 
@@ -806,12 +808,14 @@ class TestUserAdminLdap(BaseUserAdminTests):
     reset.append(desktop.conf.LDAP.LDAP_SERVERS.set_for_testing(get_multi_ldap_config()))
 
     try:
-      response = c.post(
-        reverse('useradmin:useradmin.views.add_ldap_users'),
-        dict(server='multi_ldap_conf', username_pattern='moe', password1='test', password2='test'),
-        follow=True
-      )
-      assert_true(b'There was an error when communicating with LDAP' in response.content, response)
+      with patch('useradmin.test_ldap.LdapTestConnection.find_users') as find_users:
+        find_users.side_effect = ldap.LDAPError('No such object')
+        response = c.post(
+          reverse('useradmin:useradmin.views.add_ldap_users'),
+          dict(server='multi_ldap_conf', username_pattern='moe', password1='test', password2='test'),
+          follow=True
+        )
+        assert_true(b'There was an error when communicating with LDAP' in response.content, response)
     finally:
       for finish in reset:
         finish()

+ 13 - 8
apps/useradmin/src/useradmin/test_ldap_deprecated.py

@@ -18,6 +18,7 @@
 
 from __future__ import absolute_import
 import ldap
+import sys
 
 from django.conf import settings
 from django.urls import reverse
@@ -37,6 +38,11 @@ from useradmin.views import sync_ldap_users, sync_ldap_groups, import_ldap_users
 from useradmin import ldap_access
 from useradmin.tests import BaseUserAdminTests, LdapTestConnection, reset_all_groups, reset_all_users
 
+if sys.version_info[0] > 2:
+  from unittest.mock import patch, Mock, MagicMock
+else:
+  from mock import patch, Mock, MagicMock
+
 
 class TestUserAdminLdapDeprecated(BaseUserAdminTests):
   def test_useradmin_ldap_user_group_membership_sync(self):
@@ -618,17 +624,16 @@ class TestUserAdminLdapDeprecated(BaseUserAdminTests):
 
   def test_ldap_exception_handling(self):
     # Set up LDAP tests to use a LdapTestConnection instead of an actual LDAP connection
-    class LdapTestConnectionError(LdapTestConnection):
-      def find_users(self, user, find_by_dn=False):
-        raise ldap.LDAPError('No such object')
-    ldap_access.CACHED_LDAP_CONN = LdapTestConnectionError()
+    ldap_access.CACHED_LDAP_CONN = LdapTestConnection()
 
     c = make_logged_in_client('test', is_superuser=True)
 
-    response = c.post(
-      reverse('useradmin:useradmin.views.add_ldap_users'), dict(username_pattern='moe', password1='test', password2='test'), follow=True
-    )
-    assert_true(b'There was an error when communicating with LDAP' in response.content, response)
+    with patch('useradmin.test_ldap_deprecated.LdapTestConnection.find_users') as find_users:
+      find_users.side_effect = ldap.LDAPError('No such object')
+      response = c.post(
+        reverse('useradmin:useradmin.views.add_ldap_users'), dict(username_pattern='moe', password1='test', password2='test'), follow=True
+      )
+      assert_true(b'There was an error when communicating with LDAP' in response.content, response)
 
 class TestUserAdminLdapDeprecatedWithHadoop(BaseUserAdminTests):
   requires_hadoop = True