ticket47313_test.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import socket
  7. import time
  8. import logging
  9. import pytest
  10. from lib389 import DirSrv, Entry, tools
  11. from lib389.tools import DirSrvTools
  12. from lib389._constants import *
  13. from lib389.properties import *
  14. from constants import *
  15. log = logging.getLogger(__name__)
  16. installation_prefix = None
  17. ENTRY_NAME = 'test_entry'
  18. class TopologyStandalone(object):
  19. def __init__(self, standalone):
  20. standalone.open()
  21. self.standalone = standalone
  22. @pytest.fixture(scope="module")
  23. def topology(request):
  24. '''
  25. This fixture is used to standalone topology for the 'module'.
  26. At the beginning, It may exists a standalone instance.
  27. It may also exists a backup for the standalone instance.
  28. Principle:
  29. If standalone instance exists:
  30. restart it
  31. If backup of standalone exists:
  32. create/rebind to standalone
  33. restore standalone instance from backup
  34. else:
  35. Cleanup everything
  36. remove instance
  37. remove backup
  38. Create instance
  39. Create backup
  40. '''
  41. global installation_prefix
  42. if installation_prefix:
  43. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  44. standalone = DirSrv(verbose=False)
  45. # Args for the standalone instance
  46. args_instance[SER_HOST] = HOST_STANDALONE
  47. args_instance[SER_PORT] = PORT_STANDALONE
  48. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  49. args_standalone = args_instance.copy()
  50. standalone.allocate(args_standalone)
  51. # Get the status of the backups
  52. backup_standalone = standalone.checkBackupFS()
  53. # Get the status of the instance and restart it if it exists
  54. instance_standalone = standalone.exists()
  55. if instance_standalone:
  56. # assuming the instance is already stopped, just wait 5 sec max
  57. standalone.stop(timeout=5)
  58. standalone.start(timeout=10)
  59. if backup_standalone:
  60. # The backup exist, assuming it is correct
  61. # we just re-init the instance with it
  62. if not instance_standalone:
  63. standalone.create()
  64. # Used to retrieve configuration information (dbdir, confdir...)
  65. standalone.open()
  66. # restore standalone instance from backup
  67. standalone.stop(timeout=10)
  68. standalone.restoreFS(backup_standalone)
  69. standalone.start(timeout=10)
  70. else:
  71. # We should be here only in two conditions
  72. # - This is the first time a test involve standalone instance
  73. # - Something weird happened (instance/backup destroyed)
  74. # so we discard everything and recreate all
  75. # Remove the backup. So even if we have a specific backup file
  76. # (e.g backup_standalone) we clear backup that an instance may have created
  77. if backup_standalone:
  78. standalone.clearBackupFS()
  79. # Remove the instance
  80. if instance_standalone:
  81. standalone.delete()
  82. # Create the instance
  83. standalone.create()
  84. # Used to retrieve configuration information (dbdir, confdir...)
  85. standalone.open()
  86. # Time to create the backups
  87. standalone.stop(timeout=10)
  88. standalone.backupfile = standalone.backupFS()
  89. standalone.start(timeout=10)
  90. # clear the tmp directory
  91. standalone.clearTmpDir(__file__)
  92. #
  93. # Here we have standalone instance up and running
  94. # Either coming from a backup recovery
  95. # or from a fresh (re)init
  96. # Time to return the topology
  97. return TopologyStandalone(standalone)
  98. def test_ticket47313_run(topology):
  99. """
  100. It adds 2 test entries
  101. Search with filters including subtype and !
  102. It deletes the added entries
  103. """
  104. # bind as directory manager
  105. topology.standalone.log.info("Bind as %s" % DN_DM)
  106. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  107. # enable filter error logging
  108. mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '32')]
  109. topology.standalone.modify_s(DN_CONFIG, mod)
  110. topology.standalone.log.info("\n\n######################### ADD ######################\n")
  111. # Prepare the entry with cn;fr & cn;en
  112. entry_name_fr = '%s fr' % (ENTRY_NAME)
  113. entry_name_en = '%s en' % (ENTRY_NAME)
  114. entry_name_both = '%s both' % (ENTRY_NAME)
  115. entry_dn_both = 'cn=%s, %s' % (entry_name_both, SUFFIX)
  116. entry_both = Entry(entry_dn_both)
  117. entry_both.setValues('objectclass', 'top', 'person')
  118. entry_both.setValues('sn', entry_name_both)
  119. entry_both.setValues('cn', entry_name_both)
  120. entry_both.setValues('cn;fr', entry_name_fr)
  121. entry_both.setValues('cn;en', entry_name_en)
  122. # Prepare the entry with one member
  123. entry_name_en_only = '%s en only' % (ENTRY_NAME)
  124. entry_dn_en_only = 'cn=%s, %s' % (entry_name_en_only, SUFFIX)
  125. entry_en_only = Entry(entry_dn_en_only)
  126. entry_en_only.setValues('objectclass', 'top', 'person')
  127. entry_en_only.setValues('sn', entry_name_en_only)
  128. entry_en_only.setValues('cn', entry_name_en_only)
  129. entry_en_only.setValues('cn;en', entry_name_en)
  130. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_both, entry_both))
  131. topology.standalone.add_s(entry_both)
  132. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_en_only, entry_en_only))
  133. topology.standalone.add_s(entry_en_only)
  134. topology.standalone.log.info("\n\n######################### SEARCH ######################\n")
  135. # filter: (&(cn=test_entry en only)(!(cn=test_entry fr)))
  136. myfilter = '(&(sn=%s)(!(cn=%s)))' % (entry_name_en_only, entry_name_fr)
  137. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  138. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  139. assert len(ents) == 1
  140. assert ents[0].sn == entry_name_en_only
  141. topology.standalone.log.info("Found %s" % ents[0].dn)
  142. # filter: (&(cn=test_entry en only)(!(cn;fr=test_entry fr)))
  143. myfilter = '(&(sn=%s)(!(cn;fr=%s)))' % (entry_name_en_only, entry_name_fr)
  144. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  145. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  146. assert len(ents) == 1
  147. assert ents[0].sn == entry_name_en_only
  148. topology.standalone.log.info("Found %s" % ents[0].dn)
  149. # filter: (&(cn=test_entry en only)(!(cn;en=test_entry en)))
  150. myfilter = '(&(sn=%s)(!(cn;en=%s)))' % (entry_name_en_only, entry_name_en)
  151. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  152. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  153. assert len(ents) == 0
  154. topology.standalone.log.info("Found none")
  155. topology.standalone.log.info("\n\n######################### DELETE ######################\n")
  156. topology.standalone.log.info("Try to delete %s " % entry_dn_both)
  157. topology.standalone.delete_s(entry_dn_both)
  158. topology.standalone.log.info("Try to delete %s " % entry_dn_en_only)
  159. topology.standalone.delete_s(entry_dn_en_only)
  160. def test_ticket47313_final(topology):
  161. topology.standalone.delete()
  162. def run_isolated():
  163. '''
  164. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  165. To run isolated without py.test, you need to
  166. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  167. - set the installation prefix
  168. - run this program
  169. '''
  170. global installation_prefix
  171. installation_prefix = None
  172. topo = topology(True)
  173. test_ticket47313_run(topo)
  174. test_ticket47313_final(topo)
  175. if __name__ == '__main__':
  176. run_isolated()