ticket47973_test.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 time
  10. import ldap
  11. import ldap.sasl
  12. import logging
  13. import pytest
  14. from lib389 import DirSrv, Entry
  15. from lib389._constants import *
  16. from lib389.properties import *
  17. from lib389.tasks import *
  18. log = logging.getLogger(__name__)
  19. installation_prefix = None
  20. USER_DN = 'uid=user1,%s' % (DEFAULT_SUFFIX)
  21. SCHEMA_RELOAD_COUNT = 10
  22. class TopologyStandalone(object):
  23. def __init__(self, standalone):
  24. standalone.open()
  25. self.standalone = standalone
  26. @pytest.fixture(scope="module")
  27. def topology(request):
  28. '''
  29. This fixture is used to standalone topology for the 'module'.
  30. '''
  31. global installation_prefix
  32. if installation_prefix:
  33. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  34. standalone = DirSrv(verbose=False)
  35. # Args for the standalone instance
  36. args_instance[SER_HOST] = HOST_STANDALONE
  37. args_instance[SER_PORT] = PORT_STANDALONE
  38. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. # Get the status of the instance and restart it if it exists
  42. instance_standalone = standalone.exists()
  43. # Remove the instance
  44. if instance_standalone:
  45. standalone.delete()
  46. # Create the instance
  47. standalone.create()
  48. # Used to retrieve configuration information (dbdir, confdir...)
  49. standalone.open()
  50. def fin():
  51. standalone.delete()
  52. request.addfinalizer(fin)
  53. # Here we have standalone instance up and running
  54. return TopologyStandalone(standalone)
  55. def task_complete(conn, task_dn):
  56. finished = False
  57. try:
  58. task_entry = conn.search_s(task_dn, ldap.SCOPE_BASE, 'objectclass=*')
  59. if not task_entry:
  60. log.fatal('wait_for_task: Search failed to find task: ' + task_dn)
  61. assert False
  62. if task_entry[0].hasAttr('nstaskexitcode'):
  63. # task is done
  64. finished = True
  65. except ldap.LDAPError as e:
  66. log.fatal('wait_for_task: Search failed: ' + e.message['desc'])
  67. assert False
  68. return finished
  69. def test_ticket47973(topology):
  70. """
  71. During the schema reload task there is a small window where the new schema is not loaded
  72. into the asi hashtables - this results in searches not returning entries.
  73. """
  74. log.info('Testing Ticket 47973 - Test the searches still work as expected during schema reload tasks')
  75. #
  76. # Add a user
  77. #
  78. try:
  79. topology.standalone.add_s(Entry((USER_DN, {
  80. 'objectclass': 'top extensibleObject'.split(),
  81. 'uid': 'user1'
  82. })))
  83. except ldap.LDAPError as e:
  84. log.error('Failed to add user1: error ' + e.message['desc'])
  85. assert False
  86. #
  87. # Run a series of schema_reload tasks while searching for our user. Since
  88. # this is a race condition, run it several times.
  89. #
  90. task_count = 0
  91. while task_count < SCHEMA_RELOAD_COUNT:
  92. #
  93. # Add a schema reload task
  94. #
  95. TASK_DN = 'cn=task-' + str(task_count) + ',cn=schema reload task, cn=tasks, cn=config'
  96. try:
  97. topology.standalone.add_s(Entry((TASK_DN, {
  98. 'objectclass': 'top extensibleObject'.split(),
  99. 'cn': 'task-' + str(task_count)
  100. })))
  101. except ldap.LDAPError as e:
  102. log.error('Failed to add task entry: error ' + e.message['desc'])
  103. assert False
  104. #
  105. # While we wait for the task to complete keep searching for our user
  106. #
  107. search_count = 0
  108. while search_count < 100:
  109. #
  110. # Now check the user is still being returned
  111. #
  112. try:
  113. entries = topology.standalone.search_s(DEFAULT_SUFFIX,
  114. ldap.SCOPE_SUBTREE,
  115. '(uid=user1)')
  116. if not entries or not entries[0]:
  117. log.fatal('User was not returned from search!')
  118. assert False
  119. except ldap.LDAPError as e:
  120. log.fatal('Unable to search for entry %s: error %s' % (USER_DN, e.message['desc']))
  121. assert False
  122. #
  123. # Check if task is complete
  124. #
  125. if task_complete(topology.standalone, TASK_DN):
  126. break
  127. search_count += 1
  128. task_count += 1
  129. if __name__ == '__main__':
  130. # Run isolated
  131. # -s for DEBUG mode
  132. CURRENT_FILE = os.path.realpath(__file__)
  133. pytest.main("-s %s" % CURRENT_FILE)