ticket47313_test.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #
  91. # Here we have standalone instance up and running
  92. # Either coming from a backup recovery
  93. # or from a fresh (re)init
  94. # Time to return the topology
  95. return TopologyStandalone(standalone)
  96. def test_ticket47313_run(topology):
  97. """
  98. It adds 2 test entries
  99. Search with filters including subtype and !
  100. It deletes the added entries
  101. """
  102. # bind as directory manager
  103. topology.standalone.log.info("Bind as %s" % DN_DM)
  104. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  105. # enable filter error logging
  106. mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '32')]
  107. topology.standalone.modify_s(DN_CONFIG, mod)
  108. topology.standalone.log.info("\n\n######################### ADD ######################\n")
  109. # Prepare the entry with cn;fr & cn;en
  110. entry_name_fr = '%s fr' % (ENTRY_NAME)
  111. entry_name_en = '%s en' % (ENTRY_NAME)
  112. entry_name_both = '%s both' % (ENTRY_NAME)
  113. entry_dn_both = 'cn=%s, %s' % (entry_name_both, SUFFIX)
  114. entry_both = Entry(entry_dn_both)
  115. entry_both.setValues('objectclass', 'top', 'person')
  116. entry_both.setValues('sn', entry_name_both)
  117. entry_both.setValues('cn', entry_name_both)
  118. entry_both.setValues('cn;fr', entry_name_fr)
  119. entry_both.setValues('cn;en', entry_name_en)
  120. # Prepare the entry with one member
  121. entry_name_en_only = '%s en only' % (ENTRY_NAME)
  122. entry_dn_en_only = 'cn=%s, %s' % (entry_name_en_only, SUFFIX)
  123. entry_en_only = Entry(entry_dn_en_only)
  124. entry_en_only.setValues('objectclass', 'top', 'person')
  125. entry_en_only.setValues('sn', entry_name_en_only)
  126. entry_en_only.setValues('cn', entry_name_en_only)
  127. entry_en_only.setValues('cn;en', entry_name_en)
  128. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_both, entry_both))
  129. topology.standalone.add_s(entry_both)
  130. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_en_only, entry_en_only))
  131. topology.standalone.add_s(entry_en_only)
  132. topology.standalone.log.info("\n\n######################### SEARCH ######################\n")
  133. # filter: (&(cn=test_entry en only)(!(cn=test_entry fr)))
  134. myfilter = '(&(sn=%s)(!(cn=%s)))' % (entry_name_en_only, entry_name_fr)
  135. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  136. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  137. assert len(ents) == 1
  138. assert ents[0].sn == entry_name_en_only
  139. topology.standalone.log.info("Found %s" % ents[0].dn)
  140. # filter: (&(cn=test_entry en only)(!(cn;fr=test_entry fr)))
  141. myfilter = '(&(sn=%s)(!(cn;fr=%s)))' % (entry_name_en_only, entry_name_fr)
  142. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  143. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  144. assert len(ents) == 1
  145. assert ents[0].sn == entry_name_en_only
  146. topology.standalone.log.info("Found %s" % ents[0].dn)
  147. # filter: (&(cn=test_entry en only)(!(cn;en=test_entry en)))
  148. myfilter = '(&(sn=%s)(!(cn;en=%s)))' % (entry_name_en_only, entry_name_en)
  149. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  150. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  151. assert len(ents) == 0
  152. topology.standalone.log.info("Found none")
  153. topology.standalone.log.info("\n\n######################### DELETE ######################\n")
  154. topology.standalone.log.info("Try to delete %s " % entry_dn_both)
  155. topology.standalone.delete_s(entry_dn_both)
  156. topology.standalone.log.info("Try to delete %s " % entry_dn_en_only)
  157. topology.standalone.delete_s(entry_dn_en_only)
  158. def test_ticket47313_final(topology):
  159. topology.standalone.stop(timeout=10)
  160. def run_isolated():
  161. '''
  162. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  163. To run isolated without py.test, you need to
  164. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  165. - set the installation prefix
  166. - run this program
  167. '''
  168. global installation_prefix
  169. installation_prefix = None
  170. topo = topology(True)
  171. test_ticket47313_run(topo)
  172. test_ticket47313_final(topo)
  173. if __name__ == '__main__':
  174. run_isolated()