ticket47313_test.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import time
  7. import pytest
  8. from lib389 import DirSrv, Entry, tools
  9. from lib389.tools import DirSrvTools
  10. from lib389._constants import *
  11. from lib389.properties import *
  12. log = logging.getLogger(__name__)
  13. installation_prefix = None
  14. ENTRY_NAME = 'test_entry'
  15. class TopologyStandalone(object):
  16. def __init__(self, standalone):
  17. standalone.open()
  18. self.standalone = standalone
  19. @pytest.fixture(scope="module")
  20. def topology(request):
  21. '''
  22. This fixture is used to standalone topology for the 'module'.
  23. '''
  24. global installation_prefix
  25. if installation_prefix:
  26. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  27. standalone = DirSrv(verbose=False)
  28. # Args for the standalone instance
  29. args_instance[SER_HOST] = HOST_STANDALONE
  30. args_instance[SER_PORT] = PORT_STANDALONE
  31. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  32. args_standalone = args_instance.copy()
  33. standalone.allocate(args_standalone)
  34. # Get the status of the instance
  35. instance_standalone = standalone.exists()
  36. # Remove the instance
  37. if instance_standalone:
  38. standalone.delete()
  39. # Create the instance
  40. standalone.create()
  41. # Used to retrieve configuration information (dbdir, confdir...)
  42. standalone.open()
  43. # clear the tmp directory
  44. standalone.clearTmpDir(__file__)
  45. return TopologyStandalone(standalone)
  46. def test_ticket47313_run(topology):
  47. """
  48. It adds 2 test entries
  49. Search with filters including subtype and !
  50. It deletes the added entries
  51. """
  52. # bind as directory manager
  53. topology.standalone.log.info("Bind as %s" % DN_DM)
  54. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  55. # enable filter error logging
  56. #mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', '32')]
  57. #topology.standalone.modify_s(DN_CONFIG, mod)
  58. topology.standalone.log.info("\n\n######################### ADD ######################\n")
  59. # Prepare the entry with cn;fr & cn;en
  60. entry_name_fr = '%s fr' % (ENTRY_NAME)
  61. entry_name_en = '%s en' % (ENTRY_NAME)
  62. entry_name_both = '%s both' % (ENTRY_NAME)
  63. entry_dn_both = 'cn=%s, %s' % (entry_name_both, SUFFIX)
  64. entry_both = Entry(entry_dn_both)
  65. entry_both.setValues('objectclass', 'top', 'person')
  66. entry_both.setValues('sn', entry_name_both)
  67. entry_both.setValues('cn', entry_name_both)
  68. entry_both.setValues('cn;fr', entry_name_fr)
  69. entry_both.setValues('cn;en', entry_name_en)
  70. # Prepare the entry with one member
  71. entry_name_en_only = '%s en only' % (ENTRY_NAME)
  72. entry_dn_en_only = 'cn=%s, %s' % (entry_name_en_only, SUFFIX)
  73. entry_en_only = Entry(entry_dn_en_only)
  74. entry_en_only.setValues('objectclass', 'top', 'person')
  75. entry_en_only.setValues('sn', entry_name_en_only)
  76. entry_en_only.setValues('cn', entry_name_en_only)
  77. entry_en_only.setValues('cn;en', entry_name_en)
  78. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_both, entry_both))
  79. topology.standalone.add_s(entry_both)
  80. topology.standalone.log.info("Try to add Add %s: %r" % (entry_dn_en_only, entry_en_only))
  81. topology.standalone.add_s(entry_en_only)
  82. topology.standalone.log.info("\n\n######################### SEARCH ######################\n")
  83. # filter: (&(cn=test_entry en only)(!(cn=test_entry fr)))
  84. myfilter = '(&(sn=%s)(!(cn=%s)))' % (entry_name_en_only, entry_name_fr)
  85. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  86. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  87. assert len(ents) == 1
  88. assert ents[0].sn == entry_name_en_only
  89. topology.standalone.log.info("Found %s" % ents[0].dn)
  90. # filter: (&(cn=test_entry en only)(!(cn;fr=test_entry fr)))
  91. myfilter = '(&(sn=%s)(!(cn;fr=%s)))' % (entry_name_en_only, entry_name_fr)
  92. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  93. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  94. assert len(ents) == 1
  95. assert ents[0].sn == entry_name_en_only
  96. topology.standalone.log.info("Found %s" % ents[0].dn)
  97. # filter: (&(cn=test_entry en only)(!(cn;en=test_entry en)))
  98. myfilter = '(&(sn=%s)(!(cn;en=%s)))' % (entry_name_en_only, entry_name_en)
  99. topology.standalone.log.info("Try to search with filter %s" % myfilter)
  100. ents = topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, myfilter)
  101. assert len(ents) == 0
  102. topology.standalone.log.info("Found none")
  103. topology.standalone.log.info("\n\n######################### DELETE ######################\n")
  104. topology.standalone.log.info("Try to delete %s " % entry_dn_both)
  105. topology.standalone.delete_s(entry_dn_both)
  106. topology.standalone.log.info("Try to delete %s " % entry_dn_en_only)
  107. topology.standalone.delete_s(entry_dn_en_only)
  108. def test_ticket47313_final(topology):
  109. topology.standalone.delete()
  110. log.info('Testcase PASSED')
  111. def run_isolated():
  112. '''
  113. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  114. To run isolated without py.test, you need to
  115. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  116. - set the installation prefix
  117. - run this program
  118. '''
  119. global installation_prefix
  120. installation_prefix = None
  121. topo = topology(True)
  122. test_ticket47313_run(topo)
  123. test_ticket47313_final(topo)
  124. if __name__ == '__main__':
  125. run_isolated()