ticket47669_test.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 ldap.controls import SimplePagedResultsControl
  21. from ldap.controls.simple import GetEffectiveRightsControl
  22. log = logging.getLogger(__name__)
  23. installation_prefix = None
  24. CHANGELOG = 'cn=changelog5,cn=config'
  25. RETROCHANGELOG = 'cn=Retro Changelog Plugin,cn=plugins,cn=config'
  26. MAXAGE = 'nsslapd-changelogmaxage'
  27. TRIMINTERVAL = 'nsslapd-changelogtrim-interval'
  28. COMPACTDBINTERVAL = 'nsslapd-changelogcompactdb-interval'
  29. FILTER = '(cn=*)'
  30. class TopologyStandalone(object):
  31. def __init__(self, standalone):
  32. standalone.open()
  33. self.standalone = standalone
  34. @pytest.fixture(scope="module")
  35. def topology(request):
  36. '''
  37. This fixture is used to standalone topology for the 'module'.
  38. '''
  39. global installation_prefix
  40. if installation_prefix:
  41. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  42. standalone = DirSrv(verbose=False)
  43. # Args for the standalone instance
  44. args_instance[SER_HOST] = HOST_STANDALONE
  45. args_instance[SER_PORT] = PORT_STANDALONE
  46. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  47. args_standalone = args_instance.copy()
  48. standalone.allocate(args_standalone)
  49. # Get the status of the instance and restart it if it exists
  50. instance_standalone = standalone.exists()
  51. # Remove the instance
  52. if instance_standalone:
  53. standalone.delete()
  54. # Create the instance
  55. standalone.create()
  56. # Used to retrieve configuration information (dbdir, confdir...)
  57. standalone.open()
  58. # clear the tmp directory
  59. standalone.clearTmpDir(__file__)
  60. # Here we have standalone instance up and running
  61. return TopologyStandalone(standalone)
  62. def test_ticket47669_init(topo):
  63. """
  64. Add cn=changelog5,cn=config
  65. Enable cn=Retro Changelog Plugin,cn=plugins,cn=config
  66. """
  67. log.info('Testing Ticket 47669 - Test duration syntax in the changelogs')
  68. # bind as directory manager
  69. topo.standalone.log.info("Bind as %s" % DN_DM)
  70. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  71. try:
  72. changelogdir = "%s/changelog" % topo.standalone.dbdir
  73. topo.standalone.add_s(Entry((CHANGELOG,
  74. {'objectclass': 'top extensibleObject'.split(),
  75. 'nsslapd-changelogdir': changelogdir})))
  76. except ldap.LDAPError as e:
  77. log.error('Failed to add ' + CHANGELOG + ': error ' + e.message['desc'])
  78. assert False
  79. try:
  80. topo.standalone.modify_s(RETROCHANGELOG, [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', 'on')])
  81. except ldap.LDAPError as e:
  82. log.error('Failed to enable ' + RETROCHANGELOG + ': error ' + e.message['desc'])
  83. assert False
  84. # restart the server
  85. topo.standalone.restart(timeout=10)
  86. def add_and_check(topo, plugin, attr, val, isvalid):
  87. """
  88. Helper function to add/replace attr: val and check the added value
  89. """
  90. if isvalid:
  91. log.info('Test %s: %s -- valid' % (attr, val))
  92. try:
  93. topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  94. except ldap.LDAPError as e:
  95. log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
  96. assert False
  97. else:
  98. log.info('Test %s: %s -- invalid' % (attr, val))
  99. if plugin == CHANGELOG:
  100. try:
  101. topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  102. except ldap.LDAPError as e:
  103. log.error('Expectedly failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
  104. else:
  105. try:
  106. topo.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  107. except ldap.LDAPError as e:
  108. log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
  109. try:
  110. entries = topo.standalone.search_s(plugin, ldap.SCOPE_BASE, FILTER, [attr])
  111. if isvalid:
  112. if not entries[0].hasValue(attr, val):
  113. log.fatal('%s does not have expected (%s: %s)' % (plugin, attr, val))
  114. assert False
  115. else:
  116. if plugin == CHANGELOG:
  117. if entries[0].hasValue(attr, val):
  118. log.fatal('%s has unexpected (%s: %s)' % (plugin, attr, val))
  119. assert False
  120. else:
  121. if not entries[0].hasValue(attr, val):
  122. log.fatal('%s does not have expected (%s: %s)' % (plugin, attr, val))
  123. assert False
  124. except ldap.LDAPError as e:
  125. log.fatal('Unable to search for entry %s: error %s' % (plugin, e.message['desc']))
  126. assert False
  127. def test_ticket47669_changelog_maxage(topo):
  128. """
  129. Test nsslapd-changelogmaxage in cn=changelog5,cn=config
  130. """
  131. log.info('1. Test nsslapd-changelogmaxage in cn=changelog5,cn=config')
  132. # bind as directory manager
  133. topo.standalone.log.info("Bind as %s" % DN_DM)
  134. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  135. add_and_check(topo, CHANGELOG, MAXAGE, '12345', True)
  136. add_and_check(topo, CHANGELOG, MAXAGE, '10s', True)
  137. add_and_check(topo, CHANGELOG, MAXAGE, '30M', True)
  138. add_and_check(topo, CHANGELOG, MAXAGE, '12h', True)
  139. add_and_check(topo, CHANGELOG, MAXAGE, '2D', True)
  140. add_and_check(topo, CHANGELOG, MAXAGE, '4w', True)
  141. add_and_check(topo, CHANGELOG, MAXAGE, '-123', False)
  142. add_and_check(topo, CHANGELOG, MAXAGE, 'xyz', False)
  143. def test_ticket47669_changelog_triminterval(topo):
  144. """
  145. Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config
  146. """
  147. log.info('2. Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config')
  148. # bind as directory manager
  149. topo.standalone.log.info("Bind as %s" % DN_DM)
  150. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  151. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '12345', True)
  152. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '10s', True)
  153. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '30M', True)
  154. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '12h', True)
  155. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '2D', True)
  156. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '4w', True)
  157. add_and_check(topo, CHANGELOG, TRIMINTERVAL, '-123', False)
  158. add_and_check(topo, CHANGELOG, TRIMINTERVAL, 'xyz', False)
  159. def test_ticket47669_changelog_compactdbinterval(topo):
  160. """
  161. Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config
  162. """
  163. log.info('3. Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config')
  164. # bind as directory manager
  165. topo.standalone.log.info("Bind as %s" % DN_DM)
  166. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  167. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '12345', True)
  168. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '10s', True)
  169. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '30M', True)
  170. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '12h', True)
  171. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '2D', True)
  172. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '4w', True)
  173. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, '-123', False)
  174. add_and_check(topo, CHANGELOG, COMPACTDBINTERVAL, 'xyz', False)
  175. def test_ticket47669_retrochangelog_maxage(topo):
  176. """
  177. Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config
  178. """
  179. log.info('4. Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config')
  180. # bind as directory manager
  181. topo.standalone.log.info("Bind as %s" % DN_DM)
  182. topo.standalone.simple_bind_s(DN_DM, PASSWORD)
  183. add_and_check(topo, RETROCHANGELOG, MAXAGE, '12345', True)
  184. add_and_check(topo, RETROCHANGELOG, MAXAGE, '10s', True)
  185. add_and_check(topo, RETROCHANGELOG, MAXAGE, '30M', True)
  186. add_and_check(topo, RETROCHANGELOG, MAXAGE, '12h', True)
  187. add_and_check(topo, RETROCHANGELOG, MAXAGE, '2D', True)
  188. add_and_check(topo, RETROCHANGELOG, MAXAGE, '4w', True)
  189. add_and_check(topo, RETROCHANGELOG, MAXAGE, '-123', False)
  190. add_and_check(topo, RETROCHANGELOG, MAXAGE, 'xyz', False)
  191. topo.standalone.log.info("ticket47669 was successfully verified.")
  192. def test_ticket47669_final(topology):
  193. topology.standalone.delete()
  194. log.info('Testcase PASSED')
  195. def run_isolated():
  196. """
  197. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  198. To run isolated without py.test, you need to
  199. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  200. - set the installation prefix
  201. - run this program
  202. """
  203. global installation_prefix
  204. installation_prefix = None
  205. topo = topology(True)
  206. test_ticket47669_init(topo)
  207. test_ticket47669_changelog_maxage(topo)
  208. test_ticket47669_changelog_triminterval(topo)
  209. test_ticket47669_changelog_compactdbinterval(topo)
  210. test_ticket47669_retrochangelog_maxage(topo)
  211. test_ticket47669_final(topo)
  212. if __name__ == '__main__':
  213. run_isolated()