ticket47313_test.py 5.5 KB

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