ticket47824_test.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import os
  2. import sys
  3. import time
  4. import ldap
  5. import logging
  6. import pytest
  7. from lib389 import DirSrv, Entry, tools, tasks
  8. from lib389.tools import DirSrvTools
  9. from lib389._constants import *
  10. from lib389.properties import *
  11. from lib389.tasks import *
  12. from ldap.controls import SimplePagedResultsControl
  13. log = logging.getLogger(__name__)
  14. installation_prefix = None
  15. MYSUFFIX = 'o=ticket47824.org'
  16. MYSUFFIXBE = 'ticket47824'
  17. SUBSUFFIX0 = 'ou=OU0,o=ticket47824.org'
  18. SUBSUFFIX0BE = 'OU0'
  19. SUBSUFFIX1 = 'ou=OU1,o=ticket47824.org'
  20. SUBSUFFIX1BE = 'OU1'
  21. SUBSUFFIX2 = 'ou=OU2,o=ticket47824.org'
  22. SUBSUFFIX2BE = 'OU2'
  23. _MYLDIF = 'ticket47824.ldif'
  24. _SUBLDIF0TMP = 'ticket47824_0.tmp'
  25. _SUBLDIF0 = 'ticket47824_0.ldif'
  26. _SUBLDIF1TMP = 'ticket47824_1.tmp'
  27. _SUBLDIF1 = 'ticket47824_1.ldif'
  28. _SUBLDIF2TMP = 'ticket47824_2.tmp'
  29. _SUBLDIF2 = 'ticket47824_2.ldif'
  30. SEARCHFILTER = '(objectclass=*)'
  31. class TopologyStandalone(object):
  32. def __init__(self, standalone):
  33. standalone.open()
  34. self.standalone = standalone
  35. @pytest.fixture(scope="module")
  36. def topology(request):
  37. '''
  38. This fixture is used to standalone topology for the 'module'.
  39. '''
  40. global installation_prefix
  41. if installation_prefix:
  42. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  43. standalone = DirSrv(verbose=False)
  44. # Args for the standalone instance
  45. args_instance[SER_HOST] = HOST_STANDALONE
  46. args_instance[SER_PORT] = PORT_STANDALONE
  47. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  48. args_standalone = args_instance.copy()
  49. standalone.allocate(args_standalone)
  50. # Get the status of the instance and restart it if it exists
  51. instance_standalone = standalone.exists()
  52. # Remove the instance
  53. if instance_standalone:
  54. standalone.delete()
  55. # Create the instance
  56. standalone.create()
  57. # Used to retrieve configuration information (dbdir, confdir...)
  58. standalone.open()
  59. # clear the tmp directory
  60. standalone.clearTmpDir(__file__)
  61. # Here we have standalone instance up and running
  62. return TopologyStandalone(standalone)
  63. def test_ticket47824_run(topology):
  64. """
  65. Add 3 sub suffixes under the primary suffix
  66. Import 16 entries each
  67. Search with Simple Paged Results Control from the primary suffix (pagesize = 4)
  68. If all of them are returned, the bug is verified
  69. """
  70. log.info('Testing Ticket 47824 - paged results control is not working in some cases when we have a subsuffix')
  71. # bind as directory manager
  72. topology.standalone.log.info("Bind as %s" % DN_DM)
  73. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  74. topology.standalone.log.info("\n\n######################### SETUP SUFFIX o=ticket47824.org ######################\n")
  75. topology.standalone.backend.create(MYSUFFIX, {BACKEND_NAME: MYSUFFIXBE})
  76. topology.standalone.mappingtree.create(MYSUFFIX, bename=MYSUFFIXBE)
  77. topology.standalone.log.info("\n\n######################### SETUP SUB SUFFIX ou=OU0 ######################\n")
  78. topology.standalone.backend.create(SUBSUFFIX0, {BACKEND_NAME: SUBSUFFIX0BE})
  79. topology.standalone.mappingtree.create(SUBSUFFIX0, bename=SUBSUFFIX0BE, parent=MYSUFFIX)
  80. topology.standalone.log.info("\n\n######################### SETUP SUB SUFFIX ou=OU1 ######################\n")
  81. topology.standalone.backend.create(SUBSUFFIX1, {BACKEND_NAME: SUBSUFFIX1BE})
  82. topology.standalone.mappingtree.create(SUBSUFFIX1, bename=SUBSUFFIX1BE, parent=MYSUFFIX)
  83. topology.standalone.log.info("\n\n######################### SETUP SUB SUFFIX ou=OU2 ######################\n")
  84. topology.standalone.backend.create(SUBSUFFIX2, {BACKEND_NAME: SUBSUFFIX2BE})
  85. topology.standalone.mappingtree.create(SUBSUFFIX2, bename=SUBSUFFIX2BE, parent=MYSUFFIX)
  86. topology.standalone.log.info("\n\n######################### Generate Test data ######################\n")
  87. # get tmp dir
  88. mytmp = topology.standalone.getDir(__file__, TMP_DIR)
  89. if mytmp is None:
  90. mytmp = "/tmp"
  91. MYLDIF = '%s%s' % (mytmp, _MYLDIF)
  92. SUBLDIF0TMP = '%s%s' % (mytmp, _SUBLDIF0TMP)
  93. SUBLDIF0 = '%s%s' % (mytmp, _SUBLDIF0)
  94. SUBLDIF1TMP = '%s%s' % (mytmp, _SUBLDIF1TMP)
  95. SUBLDIF1 = '%s%s' % (mytmp, _SUBLDIF1)
  96. SUBLDIF2TMP = '%s%s' % (mytmp, _SUBLDIF2TMP)
  97. SUBLDIF2 = '%s%s' % (mytmp, _SUBLDIF2)
  98. os.system('ls %s' % MYLDIF)
  99. os.system('ls %s' % SUBLDIF0TMP)
  100. os.system('ls %s' % SUBLDIF1TMP)
  101. os.system('ls %s' % SUBLDIF2TMP)
  102. os.system('rm -f %s' % MYLDIF)
  103. os.system('rm -f %s' % SUBLDIF0TMP)
  104. os.system('rm -f %s' % SUBLDIF1TMP)
  105. os.system('rm -f %s' % SUBLDIF2TMP)
  106. if hasattr(topology.standalone, 'prefix'):
  107. prefix = topology.standalone.prefix
  108. else:
  109. prefix = None
  110. dbgen_prog = prefix + '/bin/dbgen.pl'
  111. topology.standalone.log.info('dbgen: %s' % dbgen_prog)
  112. os.system('%s -s %s -o %s -n 10' % (dbgen_prog, MYSUFFIX, MYLDIF))
  113. os.system('%s -s %s -o %s -n 10' % (dbgen_prog, SUBSUFFIX0, SUBLDIF0TMP))
  114. os.system('%s -s %s -o %s -n 10' % (dbgen_prog, SUBSUFFIX1, SUBLDIF1TMP))
  115. os.system('%s -s %s -o %s -n 10' % (dbgen_prog, SUBSUFFIX2, SUBLDIF2TMP))
  116. os.system('cat %s | sed -e "s/\<objectClass: organization\>/objectClass: organizationalUnit/" | sed -e "/^o:.*/d" > %s' % (SUBLDIF0TMP, SUBLDIF0))
  117. os.system('cat %s | sed -e "s/\<objectClass: organization\>/objectClass: organizationalUnit/" | sed -e "/^o:.*/d" > %s' % (SUBLDIF1TMP, SUBLDIF1))
  118. os.system('cat %s | sed -e "s/\<objectClass: organization\>/objectClass: organizationalUnit/" | sed -e "/^o:.*/d" > %s' % (SUBLDIF2TMP, SUBLDIF2))
  119. cmdline = 'egrep dn: %s %s %s %s | wc -l' % (MYLDIF, SUBLDIF0, SUBLDIF1, SUBLDIF2)
  120. p = os.popen(cmdline, "r")
  121. dnnumstr = p.readline()
  122. dnnum = int(dnnumstr)
  123. topology.standalone.log.info("We have %d entries.\n", dnnum)
  124. topology.standalone.log.info("\n\n######################### Import Test data ######################\n")
  125. args = {TASK_WAIT: True}
  126. importTask = Tasks(topology.standalone)
  127. importTask.importLDIF(MYSUFFIX, MYSUFFIXBE, MYLDIF, args)
  128. importTask.importLDIF(SUBSUFFIX0, SUBSUFFIX0BE, SUBLDIF0, args)
  129. importTask.importLDIF(SUBSUFFIX1, SUBSUFFIX1BE, SUBLDIF1, args)
  130. importTask.importLDIF(SUBSUFFIX2, SUBSUFFIX2BE, SUBLDIF2, args)
  131. topology.standalone.log.info("\n\n######################### SEARCH ALL ######################\n")
  132. topology.standalone.log.info("Bind as %s and add the READ/SEARCH SELFDN aci" % DN_DM)
  133. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  134. entries = topology.standalone.search_s(MYSUFFIX, ldap.SCOPE_SUBTREE, SEARCHFILTER)
  135. topology.standalone.log.info("Returned %d entries.\n", len(entries))
  136. #print entries
  137. assert dnnum == len(entries)
  138. topology.standalone.log.info('%d entries are successfully imported.' % dnnum)
  139. topology.standalone.log.info("\n\n######################### SEARCH WITH SIMPLE PAGED RESULTS CONTROL ######################\n")
  140. page_size = 4
  141. req_ctrl = SimplePagedResultsControl(True, size=page_size, cookie='')
  142. known_ldap_resp_ctrls = {
  143. SimplePagedResultsControl.controlType: SimplePagedResultsControl,
  144. }
  145. topology.standalone.log.info("Calling search_ext...")
  146. msgid = topology.standalone.search_ext(MYSUFFIX, ldap.SCOPE_SUBTREE, SEARCHFILTER, None, serverctrls=[req_ctrl])
  147. pageddncnt = 0
  148. pages = 0
  149. while True:
  150. pages += 1
  151. topology.standalone.log.info("Getting page %d" % pages)
  152. rtype, rdata, rmsgid, serverctrls = topology.standalone.result3(msgid, resp_ctrl_classes=known_ldap_resp_ctrls)
  153. topology.standalone.log.info("%d results" % len(rdata))
  154. pageddncnt += len(rdata)
  155. topology.standalone.log.info("Results:")
  156. for dn, attrs in rdata:
  157. topology.standalone.log.info("dn: %s" % dn)
  158. pctrls = [
  159. c for c in serverctrls if c.controlType == SimplePagedResultsControl.controlType
  160. ]
  161. if not pctrls:
  162. topology.standalone.log.info('Warning: Server ignores RFC 2696 control.')
  163. break
  164. if pctrls[0].cookie:
  165. req_ctrl.cookie = pctrls[0].cookie
  166. topology.standalone.log.info("cookie: %s" % req_ctrl.cookie)
  167. msgid = topology.standalone.search_ext(MYSUFFIX,
  168. ldap.SCOPE_SUBTREE,
  169. SEARCHFILTER,
  170. None,
  171. serverctrls=[req_ctrl])
  172. else:
  173. topology.standalone.log.info("No cookie")
  174. break
  175. topology.standalone.log.info("Paged result search returned %d entries.\n", pageddncnt)
  176. assert dnnum == len(entries)
  177. topology.standalone.log.info("ticket47824 was successfully verified.")
  178. def test_ticket47824_final(topology):
  179. topology.standalone.delete()
  180. log.info('Testcase PASSED')
  181. def run_isolated():
  182. '''
  183. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  184. To run isolated without py.test, you need to
  185. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  186. - set the installation prefix
  187. - run this program
  188. '''
  189. global installation_prefix
  190. installation_prefix = None
  191. topo = topology(True)
  192. test_ticket47824_run(topo)
  193. test_ticket47824_final(topo)
  194. if __name__ == '__main__':
  195. run_isolated()