From f50360a21b8542d3b13f33cc004e9e789dd5eaaa Mon Sep 17 00:00:00 2001
From: Simon Pichugin <spichugi@redhat.com>
Date: Wed, 19 Aug 2015 17:46:10 +0200
Subject: [PATCH] Ticket 48251 - Improve the basic test suite

Description: Refactor basic suite code, because it should
             correspond pytest and PEP 8 standarts.

             Added a minor fix(that addresses false failures)
             written by Mark Reynolds

https://fedorahosted.org/389/ticket/48251
---
 dirsrvtests/suites/basic/basic_test.py | 265 ++++++++++++++++-----------------
 1 file changed, 132 insertions(+), 133 deletions(-)

diff --git a/dirsrvtests/suites/basic/basic_test.py b/dirsrvtests/suites/basic/basic_test.py
index d35f7d9..8061e36 100644
--- a/dirsrvtests/suites/basic/basic_test.py
+++ b/dirsrvtests/suites/basic/basic_test.py
@@ -40,9 +40,8 @@ class TopologyStandalone(object):
 
 @pytest.fixture(scope="module")
 def topology(request):
-    '''
-        This fixture is used to standalone topology for the 'module'.
-    '''
+    """This fixture is used to standalone topology for the 'module'."""
+
     global installation_prefix
 
     if installation_prefix:
@@ -70,6 +69,11 @@ def topology(request):
     # Used to retrieve configuration information (dbdir, confdir...)
     standalone.open()
 
+    # Delete each instance in the end
+    def fin():
+        standalone.delete()
+    request.addfinalizer(fin)
+
     # clear the tmp directory
     standalone.clearTmpDir(__file__)
 
@@ -77,27 +81,24 @@ def topology(request):
     return TopologyStandalone(standalone)
 
 
-def test_basic_init(topology):
-    '''
-    Initialize our setup
-    '''
-    #
-    # Import the Example LDIF for the tests in this suite
-    #
+@pytest.fixture(scope="module")
+def import_example_ldif(topology):
+    """Import the Example LDIF for the tests in this suite"""
+
     log.info('Initializing the "basic" test suite')
 
     import_ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
     try:
-        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX, input_file=import_ldif, args={TASK_WAIT: True})
+        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
+                                             input_file=import_ldif,
+                                             args={TASK_WAIT: True})
     except ValueError:
         log.error('Online import failed')
         assert False
 
 
-def test_basic_ops(topology):
-    '''
-    Test doing adds, mods, modrdns, and deletes
-    '''
+def test_basic_ops(topology, import_example_ldif):
+    """Test doing adds, mods, modrdns, and deletes"""
 
     log.info('Running test_basic_ops...')
 
@@ -113,31 +114,34 @@ def test_basic_ops(topology):
     # Adds
     #
     try:
-        topology.standalone.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
-                                 'sn': '1',
-                                 'cn': 'user1',
-                                 'uid': 'user1',
-                                 'userpassword': 'password'})))
+        topology.standalone.add_s(Entry((USER1_DN,
+                                         {'objectclass': "top extensibleObject".split(),
+                                          'sn': '1',
+                                          'cn': 'user1',
+                                          'uid': 'user1',
+                                          'userpassword': 'password'})))
     except ldap.LDAPError, e:
         log.error('Failed to add test user' + USER1_DN + ': error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
-                                 'sn': '2',
-                                 'cn': 'user2',
-                                 'uid': 'user2',
-                                 'userpassword': 'password'})))
+        topology.standalone.add_s(Entry((USER2_DN,
+                                         {'objectclass': "top extensibleObject".split(),
+                                          'sn': '2',
+                                          'cn': 'user2',
+                                          'uid': 'user2',
+                                          'userpassword': 'password'})))
     except ldap.LDAPError, e:
         log.error('Failed to add test user' + USER2_DN + ': error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.add_s(Entry((USER3_DN, {'objectclass': "top extensibleObject".split(),
-                                 'sn': '3',
-                                 'cn': 'user3',
-                                 'uid': 'user3',
-                                 'userpassword': 'password'})))
+        topology.standalone.add_s(Entry((USER3_DN,
+                                         {'objectclass': "top extensibleObject".split(),
+                                          'sn': '3',
+                                          'cn': 'user3',
+                                          'uid': 'user3',
+                                          'userpassword': 'password'})))
     except ldap.LDAPError, e:
         log.error('Failed to add test user' + USER3_DN + ': error ' + e.message['desc'])
         assert False
@@ -146,19 +150,22 @@ def test_basic_ops(topology):
     # Mods
     #
     try:
-        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_ADD, 'description', 'New description')])
+        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_ADD, 'description',
+                                                 'New description')])
     except ldap.LDAPError, e:
         log.error('Failed to add description: error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'description', 'Modified description')])
+        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'description',
+                                                 'Modified description')])
     except ldap.LDAPError, e:
         log.error('Failed to modify description: error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'description', None)])
+        topology.standalone.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'description',
+                                                 None)])
     except ldap.LDAPError, e:
         log.error('Failed to delete description: error ' + e.message['desc'])
         assert False
@@ -180,7 +187,8 @@ def test_basic_ops(topology):
 
     # Modrdn - New superior
     try:
-        topology.standalone.rename_s(USER3_DN, USER3_NEWDN, newsuperior=NEW_SUPERIOR, delold=1)
+        topology.standalone.rename_s(USER3_DN, USER3_NEWDN,
+                                     newsuperior=NEW_SUPERIOR, delold=1)
     except ldap.LDAPError, e:
         log.error('Failed to modrdn(new superior) user3: error ' + e.message['desc'])
         assert False
@@ -209,10 +217,8 @@ def test_basic_ops(topology):
     log.info('test_basic_ops: PASSED')
 
 
-def test_basic_import_export(topology):
-    '''
-    Test online and offline LDIF imports & exports
-    '''
+def test_basic_import_export(topology, import_example_ldif):
+    """Test online and offline LDIF imports & exports"""
 
     log.info('Running test_basic_import_export...')
 
@@ -227,12 +233,14 @@ def test_basic_import_export(topology):
     try:
         topology.standalone.buildLDIF(50000, import_ldif)
     except OSError as e:
-        log.fatal('test_basic_import_export: failed to create test ldif, error: %s - %s' % (e.errno, e.strerror))
+        log.fatal('test_basic_import_export: failed to create test ldif,\
+                  error: %s - %s' % (e.errno, e.strerror))
         assert False
 
     # Online
     try:
-        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX, input_file=import_ldif,
+        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
+                                             input_file=import_ldif,
                                              args={TASK_WAIT: True})
     except ValueError:
         log.fatal('test_basic_import_export: Online import failed')
@@ -258,7 +266,8 @@ def test_basic_import_export(topology):
         assert False
 
     # Offline export
-    if not topology.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,), None, None, None, export_ldif):
+    if not topology.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,),
+                                       None, None, None, export_ldif):
         log.fatal('test_basic_import_export: Failed to run offline db2ldif')
         assert False
 
@@ -267,7 +276,9 @@ def test_basic_import_export(topology):
     #
     import_ldif = '%s/Example.ldif' % get_data_dir(topology.standalone.prefix)
     try:
-        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX, input_file=import_ldif, args={TASK_WAIT: True})
+        topology.standalone.tasks.importLDIF(suffix=DEFAULT_SUFFIX,
+                                             input_file=import_ldif,
+                                             args={TASK_WAIT: True})
     except ValueError:
         log.fatal('test_basic_import_export: Online import failed')
         assert False
@@ -275,10 +286,8 @@ def test_basic_import_export(topology):
     log.info('test_basic_import_export: PASSED')
 
 
-def test_basic_backup(topology):
-    '''
-    Test online and offline back and restore
-    '''
+def test_basic_backup(topology, import_example_ldif):
+    """Test online and offline back and restore"""
 
     log.info('Running test_basic_backup...')
 
@@ -286,14 +295,16 @@ def test_basic_backup(topology):
 
     # Test online backup
     try:
-        topology.standalone.tasks.db2bak(backup_dir=backup_dir, args={TASK_WAIT: True})
+        topology.standalone.tasks.db2bak(backup_dir=backup_dir,
+                                         args={TASK_WAIT: True})
     except ValueError:
         log.fatal('test_basic_backup: Online backup failed')
         assert False
 
     # Test online restore
     try:
-        topology.standalone.tasks.bak2db(backup_dir=backup_dir, args={TASK_WAIT: True})
+        topology.standalone.tasks.bak2db(backup_dir=backup_dir,
+                                         args={TASK_WAIT: True})
     except ValueError:
         log.fatal('test_basic_backup: Online restore failed')
         assert False
@@ -311,10 +322,8 @@ def test_basic_backup(topology):
     log.info('test_basic_backup: PASSED')
 
 
-def test_basic_acl(topology):
-    '''
-    Run some basic access control(ACL) tests
-    '''
+def test_basic_acl(topology, import_example_ldif):
+    """Run some basic access control(ACL) tests"""
 
     log.info('Running test_basic_acl...')
 
@@ -325,27 +334,32 @@ def test_basic_acl(topology):
     # Add two users
     #
     try:
-        topology.standalone.add_s(Entry((USER1_DN, {'objectclass': "top extensibleObject".split(),
-                                 'sn': '1',
-                                 'cn': 'user 1',
-                                 'uid': 'user1',
-                                 'userpassword': PASSWORD})))
+        topology.standalone.add_s(Entry((USER1_DN,
+                                         {'objectclass': "top extensibleObject".split(),
+                                          'sn': '1',
+                                          'cn': 'user 1',
+                                          'uid': 'user1',
+                                          'userpassword': PASSWORD})))
     except ldap.LDAPError, e:
-        log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN + ': error ' + e.message['desc'])
+        log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
+                  + ': error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.add_s(Entry((USER2_DN, {'objectclass': "top extensibleObject".split(),
-                                 'sn': '2',
-                                 'cn': 'user 2',
-                                 'uid': 'user2',
-                                 'userpassword': PASSWORD})))
+        topology.standalone.add_s(Entry((USER2_DN,
+                                         {'objectclass': "top extensibleObject".split(),
+                                          'sn': '2',
+                                          'cn': 'user 2',
+                                          'uid': 'user2',
+                                          'userpassword': PASSWORD})))
     except ldap.LDAPError, e:
-        log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN + ': error ' + e.message['desc'])
+        log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN
+                  + ': error ' + e.message['desc'])
         assert False
 
     #
-    # Add an aci that denies USER1 from doing anything, and also set the default anonymous access
+    # Add an aci that denies USER1 from doing anything,
+    # and also set the default anonymous access
     #
     try:
         topology.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', DENY_ACI)])
@@ -363,7 +377,9 @@ def test_basic_acl(topology):
         assert False
 
     try:
-        entries = topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
+        entries = topology.standalone.search_s(DEFAULT_SUFFIX,
+                                               ldap.SCOPE_SUBTREE,
+                                               '(uid=*)')
         if entries:
             log.fatal('test_basic_acl: User1 was incorrectly able to search the suffix!')
             assert False
@@ -379,7 +395,9 @@ def test_basic_acl(topology):
         assert False
 
     try:
-        entries = topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=user1)')
+        entries = topology.standalone.search_s(DEFAULT_SUFFIX,
+                                               ldap.SCOPE_SUBTREE,
+                                               '(uid=user1)')
         if not entries:
             log.fatal('test_basic_acl: User1 incorrectly not able to search the suffix')
             assert False
@@ -400,7 +418,9 @@ def test_basic_acl(topology):
         assert False
 
     try:
-        entries = topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, '(uid=*)')
+        entries = topology.standalone.search_s(DEFAULT_SUFFIX,
+                                               ldap.SCOPE_SUBTREE,
+                                               '(uid=*)')
         if not entries:
             log.fatal('test_basic_acl: Root DN incorrectly not able to search the suffix')
             assert False
@@ -432,10 +452,8 @@ def test_basic_acl(topology):
     log.info('test_basic_acl: PASSED')
 
 
-def test_basic_searches(topology):
-    '''
-        The search results are gathered from testing with Example.ldif
-    '''
+def test_basic_searches(topology, import_example_ldif):
+    """The search results are gathered from testing with Example.ldif"""
 
     log.info('Running test_basic_searches...')
 
@@ -458,9 +476,12 @@ def test_basic_searches(topology):
 
     for (search_filter, search_result) in filters:
         try:
-            entries = topology.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, search_filter)
+            entries = topology.standalone.search_s(DEFAULT_SUFFIX,
+                                                   ldap.SCOPE_SUBTREE,
+                                                   search_filter)
             if len(entries) != search_result:
-                log.fatal('test_basic_searches: An incorrect number of entries was returned from filter (%s): (%d) expected (%d)' %
+                log.fatal('test_basic_searches: An incorrect number of entries\
+                          was returned from filter (%s): (%d) expected (%d)' %
                           (search_filter, len(entries), search_result))
                 assert False
         except ldap.LDAPError, e:
@@ -470,10 +491,10 @@ def test_basic_searches(topology):
     log.info('test_basic_searches: PASSED')
 
 
-def test_basic_referrals(topology):
-    '''
-        Set the server to referral mode, and make sure we recive the referal error(10)
-    '''
+def test_basic_referrals(topology, import_example_ldif):
+    """Set the server to referral mode,
+    and make sure we recive the referal error(10)
+    """
 
     log.info('Running test_basic_referrals...')
 
@@ -483,16 +504,20 @@ def test_basic_referrals(topology):
     # Set the referral, adn the backend state
     #
     try:
-        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-referral',
-                      'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
+        topology.standalone.modify_s(SUFFIX_CONFIG,
+                                     [(ldap.MOD_REPLACE,
+                                       'nsslapd-referral',
+                                       'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
     except ldap.LDAPError, e:
         log.fatal('test_basic_referrals: Failed to set referral: error ' + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-state', 'Referral')])
+        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
+                                                      'nsslapd-state', 'Referral')])
     except ldap.LDAPError, e:
-        log.fatal('test_basic_referrals: Failed to set backend state: error ' + e.message['desc'])
+        log.fatal('test_basic_referrals: Failed to set backend state: error '
+                  + e.message['desc'])
         assert False
 
     #
@@ -516,26 +541,29 @@ def test_basic_referrals(topology):
     # Cleanup
     #
     try:
-        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-state', 'Backend')])
+        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
+                                                      'nsslapd-state', 'Backend')])
     except ldap.LDAPError, e:
-        log.fatal('test_basic_referrals: Failed to set backend state: error ' + e.message['desc'])
+        log.fatal('test_basic_referrals: Failed to set backend state: error '
+                  + e.message['desc'])
         assert False
 
     try:
-        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE, 'nsslapd-referral', None)])
+        topology.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE,
+                                                      'nsslapd-referral', None)])
     except ldap.LDAPError, e:
-        log.fatal('test_basic_referrals: Failed to delete referral: error ' + e.message['desc'])
+        log.fatal('test_basic_referrals: Failed to delete referral: error '
+                  + e.message['desc'])
         assert False
     topology.standalone.set_option(ldap.OPT_REFERRALS, 1)
 
     log.info('test_basic_referrals: PASSED')
 
 
-def test_basic_systemctl(topology):
-    '''
-    Test systemctl can stop and start the server.  Also test that start reports an
+def test_basic_systemctl(topology, import_example_ldif):
+    """Test systemctl can stop and start the server.  Also test that start reports an
     error when the instance does not start.  Only for RPM builds
-    '''
+    """
 
     log.info('Running test_basic_systemctl...')
 
@@ -596,12 +624,13 @@ def test_basic_systemctl(topology):
     log.info('Server failed to start as expected')
 
     #
-    # Fix the dse.ldif, nad make sure the server starts up,
+    # 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...')
     rc = os.system(start_ds)
+    time.sleep(10)
     log.info('Check the status...')
     if rc != 0 or os.system(is_running) != 0:
         log.fatal('test_basic_systemctl: Failed to start the server')
@@ -612,17 +641,16 @@ def test_basic_systemctl(topology):
     log.info('test_basic_systemctl: PASSED')
 
 
-def test_basic_ldapagent(topology):
-    '''
-    Test that the ldap agnet starts
-    '''
+def test_basic_ldapagent(topology, import_example_ldif):
+    """Test that the ldap agnet starts"""
 
     log.info('Running test_basic_ldapagent...')
 
     tmp_dir = topology.standalone.getDir(__file__, TMP_DIR)
     var_dir = topology.standalone.prefix + '/var'
     config_file = tmp_dir + '/agent.conf'
-    cmd = 'sudo %s/ldap-agent %s' % (get_sbin_dir(prefix=topology.standalone.prefix), config_file)
+    cmd = 'sudo %s/ldap-agent %s' % (get_sbin_dir(prefix=topology.standalone.prefix),
+                                     config_file)
 
     agent_config_file = open(config_file, 'w')
     agent_config_file.write('agentx-master ' + var_dir + '/agentx/master\n')
@@ -647,10 +675,10 @@ def test_basic_ldapagent(topology):
     log.info('test_basic_ldapagent: PASSED')
 
 
-def test_basic_dse(topology):
-    '''
-    Test that the dse.ldif is not wipped out after the process is killed (bug 910581)
-    '''
+def test_basic_dse(topology, import_example_ldif):
+    """Test that the dse.ldif is not wipped out
+    after the process is killed (bug 910581)
+    """
 
     log.info('Running test_basic_dse...')
 
@@ -667,37 +695,8 @@ def test_basic_dse(topology):
     log.info('test_basic_dse: PASSED')
 
 
-def test_basic_final(topology):
-    topology.standalone.delete()
-    log.info('Basic test suite PASSED')
-
-
-def run_isolated():
-    '''
-        run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
-        To run isolated without py.test, you need to
-            - edit this file and comment '@pytest.fixture' line before 'topology' function.
-            - set the installation prefix
-            - run this program
-    '''
-    global installation_prefix
-    installation_prefix = None
-
-    topo = topology(True)
-
-    test_basic_init(topo)
-    test_basic_ops(topo)
-    test_basic_import_export(topo)
-    test_basic_backup(topo)
-    test_basic_acl(topo)
-    test_basic_searches(topo)
-    test_basic_referrals(topo)
-    test_basic_systemctl(topo)
-    test_basic_ldapagent(topo)
-    test_basic_dse(topo)
-
-    test_basic_final(topo)
-
-
 if __name__ == '__main__':
-    run_isolated()
+    # Run isolated
+    # -s for DEBUG mode
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s %s" % CURRENT_FILE)
-- 
2.4.3

