ac7d03
From e0f1082c7664235a298bbb1d574549917a00e8a0 Mon Sep 17 00:00:00 2001
ac7d03
From: Martin Babinsky <mbabinsk@redhat.com>
ac7d03
Date: Thu, 18 May 2017 16:20:13 +0200
ac7d03
Subject: [PATCH] test_serverroles: Get rid of MockLDAP and use ldap2 instead
ac7d03
ac7d03
The test fixture haphazardly intermixed MockLDAP and ldap2 calls in
ac7d03
setup and teardown code, greatly hampering extension of the code and
ac7d03
also porting efforts to Python 3. Get rid of MockLDAP and use ldap2 for
ac7d03
all LDAP operations.
ac7d03
ac7d03
https://pagure.io/freeipa/issue/6937
ac7d03
ac7d03
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
ac7d03
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
ac7d03
---
ac7d03
 ipatests/test_ipaserver/test_serverroles.py | 109 +++++++++++++---------------
ac7d03
 1 file changed, 51 insertions(+), 58 deletions(-)
ac7d03
ac7d03
diff --git a/ipatests/test_ipaserver/test_serverroles.py b/ipatests/test_ipaserver/test_serverroles.py
ac7d03
index b373a4d32f60e5ef48bcf07ac29162516113e8a8..985c750b64f109e0a83686f31ddb3b8d4171072d 100644
ac7d03
--- a/ipatests/test_ipaserver/test_serverroles.py
ac7d03
+++ b/ipatests/test_ipaserver/test_serverroles.py
ac7d03
@@ -14,40 +14,39 @@ import pytest
ac7d03
 from ipaplatform.paths import paths
ac7d03
 from ipalib import api, create_api, errors
ac7d03
 from ipapython.dn import DN
ac7d03
-from ipatests.util import MockLDAP
ac7d03
 
ac7d03
 
ac7d03
-def _make_service_entry_mods(enabled=True, other_config=None):
ac7d03
+def _make_service_entry(ldap_backend, dn, enabled=True, other_config=None):
ac7d03
     mods = {
ac7d03
-        b'objectClass': [b'top', b'nsContainer', b'ipaConfigObject'],
ac7d03
+        'objectClass': ['top', 'nsContainer', 'ipaConfigObject'],
ac7d03
     }
ac7d03
     if enabled:
ac7d03
-        mods.update({b'ipaConfigString': [b'enabledService']})
ac7d03
+        mods.update({'ipaConfigString': ['enabledService']})
ac7d03
 
ac7d03
     if other_config is not None:
ac7d03
-        mods.setdefault(b'ipaConfigString', [])
ac7d03
-        mods[b'ipaConfigString'].extend(other_config)
ac7d03
+        mods.setdefault('ipaConfigString', [])
ac7d03
+        mods['ipaConfigString'].extend(other_config)
ac7d03
 
ac7d03
-    return mods
ac7d03
+    return ldap_backend.make_entry(dn, **mods)
ac7d03
 
ac7d03
 
ac7d03
-def _make_master_entry_mods(ca=False):
ac7d03
+def _make_master_entry(ldap_backend, dn, ca=False):
ac7d03
     mods = {
ac7d03
-        b'objectClass': [
ac7d03
-            b'top',
ac7d03
-            b'nsContainer',
ac7d03
-            b'ipaReplTopoManagedServer',
ac7d03
-            b'ipaSupportedDomainLevelConfig',
ac7d03
-            b'ipaConfigObject',
ac7d03
+        'objectClass': [
ac7d03
+            'top',
ac7d03
+            'nsContainer',
ac7d03
+            'ipaReplTopoManagedServer',
ac7d03
+            'ipaSupportedDomainLevelConfig',
ac7d03
+            'ipaConfigObject',
ac7d03
         ],
ac7d03
-        b'ipaMaxDomainLevel': [b'1'],
ac7d03
-        b'ipaMinDomainLevel': [b'0'],
ac7d03
-        b'ipaReplTopoManagedsuffix': [str(api.env.basedn)]
ac7d03
+        'ipaMaxDomainLevel': ['1'],
ac7d03
+        'ipaMinDomainLevel': ['0'],
ac7d03
+        'ipaReplTopoManagedsuffix': [str(api.env.basedn)]
ac7d03
     }
ac7d03
     if ca:
ac7d03
-        mods[b'ipaReplTopoManagedsuffix'].append(b'o=ipaca')
ac7d03
+        mods['ipaReplTopoManagedsuffix'].append('o=ipaca')
ac7d03
 
ac7d03
-    return mods
ac7d03
+    return ldap_backend.make_entry(dn, **mods)
ac7d03
 
ac7d03
 _adtrust_agents = DN(
ac7d03
     ('cn', 'adtrust agents'),
ac7d03
@@ -235,7 +234,7 @@ class MockMasterTopology(object):
ac7d03
             ('cn', self.api.env.host), self.api.env.container_masters,
ac7d03
             self.api.env.basedn)
ac7d03
 
ac7d03
-        self.ldap = MockLDAP()
ac7d03
+        self.ldap = self.api.Backend.ldap2
ac7d03
 
ac7d03
         self.existing_masters = {
ac7d03
             m['cn'][0] for m in self.api.Command.server_find(
ac7d03
@@ -302,8 +301,9 @@ class MockMasterTopology(object):
ac7d03
             svc_mods = svc_desc[name]
ac7d03
 
ac7d03
             self.ldap.add_entry(
ac7d03
-                str(svc_dn),
ac7d03
-                _make_service_entry_mods(
ac7d03
+                _make_service_entry(
ac7d03
+                    self.ldap,
ac7d03
+                    svc_dn,
ac7d03
                     enabled=svc_mods['enabled'],
ac7d03
                     other_config=svc_mods.get('config', None)))
ac7d03
 
ac7d03
@@ -311,16 +311,16 @@ class MockMasterTopology(object):
ac7d03
 
ac7d03
     def _remove_svc_master_entries(self, master_dn):
ac7d03
         try:
ac7d03
-            entries = self.ldap.connection.search_s(
ac7d03
-                str(master_dn), ldap.SCOPE_SUBTREE
ac7d03
+            entries = self.ldap.get_entries(
ac7d03
+                master_dn, ldap.SCOPE_SUBTREE
ac7d03
             )
ac7d03
-        except ldap.NO_SUCH_OBJECT:
ac7d03
+        except errors.NotFound:
ac7d03
             return
ac7d03
 
ac7d03
         if entries:
ac7d03
-            entries.sort(key=lambda x: len(x[0]), reverse=True)
ac7d03
-            for entry_dn, _attrs in entries:
ac7d03
-                self.ldap.del_entry(str(entry_dn))
ac7d03
+            entries.sort(key=lambda x: len(x.dn), reverse=True)
ac7d03
+            for entry in entries:
ac7d03
+                self.ldap.delete_entry(entry)
ac7d03
 
ac7d03
     def _add_ipamaster_services(self, master_dn):
ac7d03
         """
ac7d03
@@ -329,19 +329,14 @@ class MockMasterTopology(object):
ac7d03
         for svc_name in self.ipamaster_services:
ac7d03
             svc_dn = self.get_service_dn(svc_name, master_dn)
ac7d03
             try:
ac7d03
-                self.api.Backend.ldap2.get_entry(svc_dn)
ac7d03
+                self.ldap.get_entry(svc_dn)
ac7d03
             except errors.NotFound:
ac7d03
-                self.ldap.add_entry(
ac7d03
-                    str(svc_dn), _make_service_entry_mods())
ac7d03
+                self.ldap.add_entry(_make_service_entry(self.ldap, svc_dn))
ac7d03
 
ac7d03
     def _add_members(self, dn, fqdn, member_attrs):
ac7d03
-        _entry, attrs = self.ldap.connection.search_s(
ac7d03
-            str(dn), ldap.SCOPE_SUBTREE)[0]
ac7d03
-        mods = []
ac7d03
-        value = attrs.get('member', [])
ac7d03
-        mod_op = ldap.MOD_REPLACE
ac7d03
-        if not value:
ac7d03
-            mod_op = ldap.MOD_ADD
ac7d03
+        entry_attrs = self.ldap.get_entry(dn)
ac7d03
+
ac7d03
+        value = entry_attrs.get('member', [])
ac7d03
 
ac7d03
         for a in member_attrs:
ac7d03
 
ac7d03
@@ -352,20 +347,18 @@ class MockMasterTopology(object):
ac7d03
                 result = self._add_service_entry(a, fqdn)['result']
ac7d03
                 value.append(str(result['dn']))
ac7d03
 
ac7d03
-        mods.append(
ac7d03
-            (mod_op, 'member', value)
ac7d03
-        )
ac7d03
-
ac7d03
-        self.ldap.connection.modify_s(str(dn), mods)
ac7d03
+        entry_attrs['member'] = value
ac7d03
+        self.ldap.update_entry(entry_attrs)
ac7d03
 
ac7d03
     def _remove_members(self, dn, fqdn, member_attrs):
ac7d03
-        _entry, attrs = self.ldap.connection.search_s(
ac7d03
-            str(dn), ldap.SCOPE_SUBTREE)[0]
ac7d03
-        mods = []
ac7d03
+        entry_attrs = self.ldap.get_entry(dn)
ac7d03
+
ac7d03
+        value = set(entry_attrs.get('member', []))
ac7d03
+
ac7d03
+        if not value:
ac7d03
+            return
ac7d03
+
ac7d03
         for a in member_attrs:
ac7d03
-            value = set(attrs.get('member', []))
ac7d03
-            if not value:
ac7d03
-                continue
ac7d03
 
ac7d03
             if a == 'host':
ac7d03
                 try:
ac7d03
@@ -382,13 +375,11 @@ class MockMasterTopology(object):
ac7d03
                     pass
ac7d03
                 self._del_service_entry(a, fqdn)
ac7d03
 
ac7d03
-        mods.append(
ac7d03
-            (ldap.MOD_REPLACE, 'member', list(value))
ac7d03
-        )
ac7d03
+        entry_attrs['member'] = list(value)
ac7d03
 
ac7d03
         try:
ac7d03
-            self.ldap.connection.modify_s(str(dn), mods)
ac7d03
-        except (ldap.NO_SUCH_OBJECT, ldap.NO_SUCH_ATTRIBUTE):
ac7d03
+            self.ldap.update_entry(entry_attrs)
ac7d03
+        except (errors.NotFound, errors.EmptyModlist):
ac7d03
             pass
ac7d03
 
ac7d03
     def _remove_test_host_attrs(self):
ac7d03
@@ -397,7 +388,7 @@ class MockMasterTopology(object):
ac7d03
         for attr_name in (
ac7d03
                 'caRenewalMaster', 'dnssecKeyMaster', 'pkinitEnabled'):
ac7d03
             try:
ac7d03
-                svc_entry = self.api.Backend.ldap2.find_entry_by_attr(
ac7d03
+                svc_entry = self.ldap.find_entry_by_attr(
ac7d03
                     'ipaConfigString', attr_name, 'ipaConfigObject',
ac7d03
                     base_dn=self.test_master_dn)
ac7d03
             except errors.NotFound:
ac7d03
@@ -407,7 +398,7 @@ class MockMasterTopology(object):
ac7d03
                     (svc_entry.dn, list(svc_entry.get('ipaConfigString', [])))
ac7d03
                 )
ac7d03
                 svc_entry[u'ipaConfigString'].remove(attr_name)
ac7d03
-                self.api.Backend.ldap2.update_entry(svc_entry)
ac7d03
+                self.ldap.update_entry(svc_entry)
ac7d03
 
ac7d03
         return original_dns_configs
ac7d03
 
ac7d03
@@ -416,7 +407,7 @@ class MockMasterTopology(object):
ac7d03
             try:
ac7d03
                 svc_entry = self.api.Backend.ldap2.get_entry(dn)
ac7d03
                 svc_entry['ipaConfigString'] = config
ac7d03
-                self.api.Backend.ldap2.update_entry(svc_entry)
ac7d03
+                self.ldap.update_entry(svc_entry)
ac7d03
             except (errors.NotFound, errors.EmptyModlist):
ac7d03
                 continue
ac7d03
 
ac7d03
@@ -427,7 +418,9 @@ class MockMasterTopology(object):
ac7d03
 
ac7d03
             # create master
ac7d03
             self.ldap.add_entry(
ac7d03
-                str(master_data.dn), _make_master_entry_mods(
ac7d03
+                _make_master_entry(
ac7d03
+                    self.ldap,
ac7d03
+                    master_data.dn,
ac7d03
                     ca='CA' in master_data.services))
ac7d03
 
ac7d03
             # now add service entries
ac7d03
-- 
ac7d03
2.9.4
ac7d03