range_search_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 pytest
  15. from lib389 import DirSrv, Entry, tools, tasks
  16. from lib389.tools import DirSrvTools
  17. from lib389._constants import *
  18. from lib389.properties import *
  19. from lib389.tasks import *
  20. from lib389.utils import *
  21. logging.getLogger(__name__).setLevel(logging.DEBUG)
  22. log = logging.getLogger(__name__)
  23. installation1_prefix = None
  24. class TopologyStandalone(object):
  25. def __init__(self, standalone):
  26. standalone.open()
  27. self.standalone = standalone
  28. @pytest.fixture(scope="module")
  29. def topology(request):
  30. global installation1_prefix
  31. if installation1_prefix:
  32. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  33. # Creating standalone instance ...
  34. standalone = DirSrv(verbose=False)
  35. args_instance[SER_HOST] = HOST_STANDALONE
  36. args_instance[SER_PORT] = PORT_STANDALONE
  37. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  38. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  39. args_standalone = args_instance.copy()
  40. standalone.allocate(args_standalone)
  41. instance_standalone = standalone.exists()
  42. if instance_standalone:
  43. standalone.delete()
  44. standalone.create()
  45. standalone.open()
  46. # Clear out the tmp dir
  47. standalone.clearTmpDir(__file__)
  48. def fin():
  49. standalone.delete()
  50. sbin_dir = get_sbin_dir(prefix=standalone.prefix)
  51. valgrind_disable(sbin_dir)
  52. request.addfinalizer(fin)
  53. return TopologyStandalone(standalone)
  54. def test_range_search_init(topology):
  55. '''
  56. Enable retro cl, and valgrind. Since valgrind tests move the ns-slapd binary
  57. around it's important to always "valgrind_disable" before "assert False"ing,
  58. otherwise we leave the wrong ns-slapd in place if there is a failure
  59. '''
  60. log.info('Initializing test_range_search...')
  61. topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  62. # First stop the instance
  63. topology.standalone.stop(timeout=10)
  64. # Get the sbin directory so we know where to replace 'ns-slapd'
  65. sbin_dir = get_sbin_dir(prefix=topology.standalone.prefix)
  66. # Enable valgrind
  67. valgrind_enable(sbin_dir)
  68. # Now start the server with a longer timeout
  69. topology.standalone.start(timeout=60)
  70. def test_range_search(topology):
  71. '''
  72. Add a 100 entries, and run a range search. When we encounter an error we
  73. still need to disable valgrind before exiting
  74. '''
  75. log.info('Running test_range_search...')
  76. success = True
  77. # Add 100 test entries
  78. for idx in range(1, 100):
  79. idx = str(idx)
  80. USER_DN = 'uid=user' + idx + ',' + DEFAULT_SUFFIX
  81. try:
  82. topology.standalone.add_s(Entry((USER_DN, {'objectclass': "top extensibleObject".split(),
  83. 'uid': 'user' + idx})))
  84. except ldap.LDAPError as e:
  85. log.fatal('test_range_search: Failed to add test user ' + USER_DN + ': error ' + e.message['desc'])
  86. success = False
  87. time.sleep(1)
  88. if success:
  89. # Issue range search
  90. try:
  91. topology.standalone.search_s(RETROCL_SUFFIX, ldap.SCOPE_SUBTREE,
  92. '(&(changenumber>=74)(changenumber<=84))')
  93. except ldap.LDAPError as e:
  94. log.fatal('test_range_search: Failed to search retro changelog(%s), error: %s' %
  95. (RETROCL_SUFFIX, e.message('desc')))
  96. success = False
  97. if success:
  98. # Get the results file, stop the server, and check for the leak
  99. results_file = valgrind_get_results_file(topology.standalone)
  100. topology.standalone.stop(timeout=30)
  101. if valgrind_check_file(results_file, VALGRIND_LEAK_STR, 'range_candidates'):
  102. log.fatal('test_range_search: Memory leak is still present!')
  103. assert False
  104. log.info('test_range_search: PASSED')
  105. if __name__ == '__main__':
  106. # Run isolated
  107. # -s for DEBUG mode
  108. CURRENT_FILE = os.path.realpath(__file__)
  109. pytest.main("-s %s" % CURRENT_FILE)