From 4d996f6054a7f4a6ac11f6fcf6109c4d36a841bd Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Fri, 4 Sep 2015 15:46:50 -0400
Subject: [PATCH] Ticket 48204 - update lib389 test scripts for python 3

Description:  Added support for python3, and also addressed certain failing
              test scripts.

https://fedorahosted.org/389/ticket/48204

Reviewed by: ?
---
 .../attr_uniqueness_plugin/attr_uniqueness_test.py |   5 +-
 dirsrvtests/suites/basic/basic_test.py             |   5 +-
 dirsrvtests/tickets/ticket47653MMR_test.py         |   3 +-
 dirsrvtests/tickets/ticket47669_test.py            | 121 +++++++++++----------
 dirsrvtests/tickets/ticket47714_test.py            |   8 +-
 dirsrvtests/tickets/ticket48226_test.py            |  56 +++++-----
 6 files changed, 101 insertions(+), 97 deletions(-)

diff --git a/dirsrvtests/suites/attr_uniqueness_plugin/attr_uniqueness_test.py b/dirsrvtests/suites/attr_uniqueness_plugin/attr_uniqueness_test.py
index c26a0a2..06e7425 100644
--- a/dirsrvtests/suites/attr_uniqueness_plugin/attr_uniqueness_test.py
+++ b/dirsrvtests/suites/attr_uniqueness_plugin/attr_uniqueness_test.py
@@ -21,7 +21,8 @@ from lib389.utils import *
 
 logging.getLogger(__name__).setLevel(logging.DEBUG)
 log = logging.getLogger(__name__)
-
+USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
+USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
 installation1_prefix = None
 
 
@@ -67,6 +68,8 @@ def test_attr_uniqueness_init(topology):
         ldap.fatal('Failed to enable dynamic plugin!' + e.message['desc'])
         assert False
 
+    topology.standalone.plugins.enable(name=PLUGIN_ATTR_UNIQUENESS)
+
 
 def test_attr_uniqueness(topology):
     log.info('Running test_attr_uniqueness...')
diff --git a/dirsrvtests/suites/basic/basic_test.py b/dirsrvtests/suites/basic/basic_test.py
index c6ac74b..e75d770 100644
--- a/dirsrvtests/suites/basic/basic_test.py
+++ b/dirsrvtests/suites/basic/basic_test.py
@@ -622,15 +622,16 @@ def test_basic_systemctl(topology, import_example_ldif):
         log.fatal('test_basic_systemctl: The server incorrectly started')
         assert False
     log.info('Server failed to start as expected')
+    time.sleep(5)
 
     #
     # Fix the dse.ldif, and make sure the server starts up,
     # and systemctl correctly identifies the successful start
     #
     shutil.copy(tmp_dir + 'dse.ldif', config_dir)
-    log.info('Starting the server...')
+    log.info('Starting the server with good dse.ldif...')
     rc = os.system(start_ds)
-    time.sleep(10)
+    time.sleep(5)
     log.info('Check the status...')
     if rc != 0 or os.system(is_running) != 0:
         log.fatal('test_basic_systemctl: Failed to start the server')
diff --git a/dirsrvtests/tickets/ticket47653MMR_test.py b/dirsrvtests/tickets/ticket47653MMR_test.py
index 0c6da02..f951e55 100644
--- a/dirsrvtests/tickets/ticket47653MMR_test.py
+++ b/dirsrvtests/tickets/ticket47653MMR_test.py
@@ -104,10 +104,9 @@ def topology(request):
     master2.allocate(args_master)
 
     # Get the status of the instance and restart it if it exists
-    instance_master1   = master1.exists()
+    instance_master1 = master1.exists()
     instance_master2 = master2.exists()
 
-
     # Remove all the instances
     if instance_master1:
         master1.delete()
diff --git a/dirsrvtests/tickets/ticket47669_test.py b/dirsrvtests/tickets/ticket47669_test.py
index 5458fba..2ef1f3e 100644
--- a/dirsrvtests/tickets/ticket47669_test.py
+++ b/dirsrvtests/tickets/ticket47669_test.py
@@ -79,7 +79,7 @@ def topology(request):
     return TopologyStandalone(standalone)
 
 
-def test_ticket47669_init(topo):
+def test_ticket47669_init(topology):
     """
     Add cn=changelog5,cn=config
     Enable cn=Retro Changelog Plugin,cn=plugins,cn=config
@@ -87,12 +87,12 @@ def test_ticket47669_init(topo):
     log.info('Testing Ticket 47669 - Test duration syntax in the changelogs')
 
     # bind as directory manager
-    topo.standalone.log.info("Bind as %s" % DN_DM)
-    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
+    topology.standalone.log.info("Bind as %s" % DN_DM)
+    topology.standalone.simple_bind_s(DN_DM, PASSWORD)
 
     try:
-        changelogdir = "%s/changelog" % topo.standalone.dbdir
-        topo.standalone.add_s(Entry((CHANGELOG,
+        changelogdir = "%s/changelog" % topology.standalone.dbdir
+        topology.standalone.add_s(Entry((CHANGELOG,
                                      {'objectclass': 'top extensibleObject'.split(),
                                       'nsslapd-changelogdir': changelogdir})))
     except ldap.LDAPError as e:
@@ -100,23 +100,23 @@ def test_ticket47669_init(topo):
         assert False
 
     try:
-        topo.standalone.modify_s(RETROCHANGELOG, [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', 'on')])
+        topology.standalone.modify_s(RETROCHANGELOG, [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', 'on')])
     except ldap.LDAPError as e:
         log.error('Failed to enable ' + RETROCHANGELOG + ': error ' + e.message['desc'])
         assert False
 
     # restart the server
-    topo.standalone.restart(timeout=10)
+    topology.standalone.restart(timeout=10)
 
 
-def add_and_check(topo, plugin, attr, val, isvalid):
+def add_and_check(topology, plugin, attr, val, isvalid):
     """
     Helper function to add/replace attr: val and check the added value
     """
     if isvalid:
         log.info('Test %s: %s -- valid' % (attr, val))
         try:
-            topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
+            topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
         except ldap.LDAPError as e:
             log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
             assert False
@@ -124,17 +124,18 @@ def add_and_check(topo, plugin, attr, val, isvalid):
         log.info('Test %s: %s -- invalid' % (attr, val))
         if plugin == CHANGELOG:
             try:
-                topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
+                topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
             except ldap.LDAPError as e:
-                log.error('Expectedly failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
+                log.error('Expectedly failed to add ' + attr + ': ' + val +
+                          ' to ' + plugin + ': error ' + e.message['desc'])
         else:
             try:
-                topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
+                topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
             except ldap.LDAPError as e:
                 log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
 
     try:
-        entries = topo.standalone.search_s(plugin, ldap.SCOPE_BASE, FILTER, [attr])
+        entries = topology.standalone.search_s(plugin, ldap.SCOPE_BASE, FILTER, [attr])
         if isvalid:
             if not entries[0].hasValue(attr, val):
                 log.fatal('%s does not have expected (%s: %s)' % (plugin, attr, val))
@@ -153,86 +154,86 @@ def add_and_check(topo, plugin, attr, val, isvalid):
         assert False
 
 
-def test_ticket47669_changelog_maxage(topo):
+def test_ticket47669_changelog_maxage(topology):
     """
     Test nsslapd-changelogmaxage in cn=changelog5,cn=config
     """
     log.info('1. Test nsslapd-changelogmaxage in cn=changelog5,cn=config')
 
     # bind as directory manager
-    topo.standalone.log.info("Bind as %s" % DN_DM)
-    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
+    topology.standalone.log.info("Bind as %s" % DN_DM)
+    topology.standalone.simple_bind_s(DN_DM, PASSWORD)
 
-    add_and_check(topo, CHANGELOG, MAXAGE, '12345', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '10s', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '30M', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '12h', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '2D', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '4w', True)
-    add_and_check(topo, CHANGELOG, MAXAGE, '-123', False)
-    add_and_check(topo, CHANGELOG, MAXAGE, 'xyz', False)
+    add_and_check(topology, CHANGELOG, MAXAGE, '12345', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '10s', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '30M', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '12h', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '2D', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '4w', True)
+    add_and_check(topology, CHANGELOG, MAXAGE, '-123', False)
+    add_and_check(topology, CHANGELOG, MAXAGE, 'xyz', False)
 
 
-def test_ticket47669_changelog_triminterval(topo):
+def test_ticket47669_changelog_triminterval(topology):
     """
     Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config
     """
     log.info('2. Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config')
 
     # bind as directory manager
-    topo.standalone.log.info("Bind as %s" % DN_DM)
-    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
+    topology.standalone.log.info("Bind as %s" % DN_DM)
+    topology.standalone.simple_bind_s(DN_DM, PASSWORD)
 
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '12345', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '10s', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '30M', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '12h', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '2D', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '4w', True)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, '-123', False)
-    add_and_check(topo, CHANGELOG, TRIMINTERVAL, 'xyz', False)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '12345', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '10s', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '30M', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '12h', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '2D', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '4w', True)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, '-123', False)
+    add_and_check(topology, CHANGELOG, TRIMINTERVAL, 'xyz', False)
 
 
-def test_ticket47669_changelog_compactdbinterval(topo):
+def test_ticket47669_changelog_compactdbinterval(topology):
     """
     Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config
     """
     log.info('3. Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config')
 
     # bind as directory manager
-    topo.standalone.log.info("Bind as %s" % DN_DM)
-    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
+    topology.standalone.log.info("Bind as %s" % DN_DM)
+    topology.standalone.simple_bind_s(DN_DM, PASSWORD)
 
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '12345', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '10s', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '30M', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '12h', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '2D', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '4w', True)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '-123', False)
-    add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, 'xyz', False)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '12345', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '10s', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '30M', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '12h', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '2D', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '4w', True)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '-123', False)
+    add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, 'xyz', False)
 
 
-def test_ticket47669_retrochangelog_maxage(topo):
+def test_ticket47669_retrochangelog_maxage(topology):
     """
     Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config
     """
     log.info('4. Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config')
 
     # bind as directory manager
-    topo.standalone.log.info("Bind as %s" % DN_DM)
-    topo.standalone.simple_bind_s(DN_DM, PASSWORD)
-
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '12345', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '10s', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '30M', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '12h', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '2D', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '4w', True)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, '-123', False)
-    add_and_check(topo, RETROCHANGELOG, MAXAGE, 'xyz', False)
-
-    topo.standalone.log.info("ticket47669 was successfully verified.")
+    topology.standalone.log.info("Bind as %s" % DN_DM)
+    topology.standalone.simple_bind_s(DN_DM, PASSWORD)
+
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '12345', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '10s', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '30M', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '12h', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '2D', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '4w', True)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, '-123', False)
+    add_and_check(topology, RETROCHANGELOG, MAXAGE, 'xyz', False)
+
+    topology.standalone.log.info("ticket47669 was successfully verified.")
 
 
 def test_ticket47669_final(topology):
diff --git a/dirsrvtests/tickets/ticket47714_test.py b/dirsrvtests/tickets/ticket47714_test.py
index 400c243..268ddef 100644
--- a/dirsrvtests/tickets/ticket47714_test.py
+++ b/dirsrvtests/tickets/ticket47714_test.py
@@ -209,22 +209,22 @@ def test_ticket47714_run_1(topology):
     except ldap.CONSTRAINT_VIOLATION as e:
         log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
 
+    time.sleep(1)
+
     topology.standalone.simple_bind_s(DN_DM, PASSWORD)
     entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
-
     lastLoginTime0 = entry[0].lastLoginTime
 
-    time.sleep(2)
-
     log.info("\n######################### Bind as %s again ######################\n" % TEST_USER_DN)
     try:
         topology.standalone.simple_bind_s(TEST_USER_DN, TEST_USER_PW)
     except ldap.CONSTRAINT_VIOLATION as e:
         log.error('CONSTRAINT VIOLATION ' + e.message['desc'])
 
+    time.sleep(1)
+
     topology.standalone.simple_bind_s(DN_DM, PASSWORD)
     entry = topology.standalone.search_s(TEST_USER_DN, ldap.SCOPE_BASE, SEARCHFILTER, ['lastLoginTime'])
-
     lastLoginTime1 = entry[0].lastLoginTime
 
     log.info("First lastLoginTime: %s, Second lastLoginTime: %s" % (lastLoginTime0, lastLoginTime1))
diff --git a/dirsrvtests/tickets/ticket48226_test.py b/dirsrvtests/tickets/ticket48226_test.py
index 87814e7..acc1030 100644
--- a/dirsrvtests/tickets/ticket48226_test.py
+++ b/dirsrvtests/tickets/ticket48226_test.py
@@ -3,7 +3,7 @@
 # All rights reserved.
 #
 # License: GPL (version 3 or any later version).
-# See LICENSE for details. 
+# See LICENSE for details.
 # --- END COPYRIGHT BLOCK ---
 #
 import os
@@ -122,7 +122,8 @@ def topology(request):
 
     return TopologyReplication(master1, master2)
 
-def test_ticket11111_set_purgedelay(topology):
+
+def test_ticket48226_set_purgedelay(topology):
     args = {REPLICA_PURGE_DELAY: '5',
             REPLICA_PURGE_INTERVAL: '5'}
     try:
@@ -139,21 +140,20 @@ def test_ticket11111_set_purgedelay(topology):
     topology.master2.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-auditlog-logging-enabled', 'on')])
     topology.master1.restart(10)
     topology.master2.restart(10)
-    
 
-def test_ticket11111_1(topology):
+
+def test_ticket48226_1(topology):
     name = 'test_entry'
     dn = "cn=%s,%s" % (name, SUFFIX)
-    
-    topology.master1.add_s(Entry((dn , {
-                                            'objectclass': "top person".split(),
-                                            'sn': name,
-                                            'cn': name})))
-                                            
+
+    topology.master1.add_s(Entry((dn, {'objectclass': "top person".split(),
+                                        'sn': name,
+                                        'cn': name})))
+
     # First do an update that is replicated
     mods = [(ldap.MOD_ADD, 'description', '5')]
     topology.master1.modify_s(dn, mods)
-    
+
     nbtry = 0
     while (nbtry <= 10):
         try:
@@ -165,17 +165,17 @@ def test_ticket11111_1(topology):
         nbtry = nbtry + 1
         time.sleep(1)
     assert nbtry <= 10
-    
+
     # Stop M2 so that it will not receive the next update
     topology.master2.stop(10)
-    
+
     # ADD a new value that is not replicated
     mods = [(ldap.MOD_DELETE, 'description', '5')]
     topology.master1.modify_s(dn, mods)
-    
+
     # Stop M1 so that it will keep del '5' that is unknown from master2
     topology.master1.stop(10)
-    
+
     # Get the sbin directory so we know where to replace 'ns-slapd'
     sbin_dir = get_sbin_dir(prefix=topology.master2.prefix)
 
@@ -184,18 +184,18 @@ def test_ticket11111_1(topology):
 
     # start M2 to do the next updates
     topology.master2.start(10)
-    
-    # ADD 'description' by '5' 
+
+    # ADD 'description' by '5'
     mods = [(ldap.MOD_DELETE, 'description', '5')]
     topology.master2.modify_s(dn, mods)
-    
+
     # DEL 'description' by '5'
     mods = [(ldap.MOD_ADD, 'description', '5')]
     topology.master2.modify_s(dn, mods)
-    
+
     # sleep of purgedelay so that the next update will purge the CSN_7
     time.sleep(6)
-    
+
     # ADD 'description' by '8' that purge the state info
     mods = [(ldap.MOD_ADD, 'description', '6')]
     topology.master2.modify_s(dn, mods)
@@ -204,22 +204,22 @@ def test_ticket11111_1(topology):
         log.error('test_csnset_dup: Memory leak is present!')
     else:
         log.info('test_csnset_dup: No leak is present!')
-    
+
     if valgrind_check_leak(topology.master2, 'Invalid'):
         log.info('Valgrind reported invalid!')
     else:
         log.info('Valgrind is happy!')
-    
+
     #log.info("You can attach yourself")
     #time.sleep(60)
-    
+
     # Enable valgrind
     valgrind_disable(sbin_dir)
 
     topology.master1.start(10)
 
-    
-def test_ticket11111_final(topology):
+
+def test_ticket48226_final(topology):
     topology.master1.delete()
     topology.master2.delete()
     log.info('Testcase PASSED')
@@ -230,9 +230,9 @@ def run_isolated():
     installation1_prefix = None
 
     topo = topology(True)
-    test_ticket11111_set_purgedelay(topo)
-    test_ticket11111_1(topo)
-    
+    test_ticket48226_set_purgedelay(topo)
+    test_ticket48226_1(topo)
+
 
 if __name__ == '__main__':
     run_isolated()
-- 
1.9.3

