ticket47669_test.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. def fin():
  59. standalone.delete()
  60. request.addfinalizer(fin)
  61. # Here we have standalone instance up and running
  62. return TopologyStandalone(standalone)
  63. def test_ticket47669_init(topology):
  64. """
  65. Add cn=changelog5,cn=config
  66. Enable cn=Retro Changelog Plugin,cn=plugins,cn=config
  67. """
  68. log.info('Testing Ticket 47669 - Test duration syntax in the changelogs')
  69. # bind as directory manager
  70. topology.standalone.log.info("Bind as %s" % DN_DM)
  71. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  72. try:
  73. changelogdir = "%s/changelog" % topology.standalone.dbdir
  74. topology.standalone.add_s(Entry((CHANGELOG,
  75. {'objectclass': 'top extensibleObject'.split(),
  76. 'nsslapd-changelogdir': changelogdir})))
  77. except ldap.LDAPError as e:
  78. log.error('Failed to add ' + CHANGELOG + ': error ' + e.message['desc'])
  79. assert False
  80. try:
  81. topology.standalone.modify_s(RETROCHANGELOG, [(ldap.MOD_REPLACE, 'nsslapd-pluginEnabled', 'on')])
  82. except ldap.LDAPError as e:
  83. log.error('Failed to enable ' + RETROCHANGELOG + ': error ' + e.message['desc'])
  84. assert False
  85. # restart the server
  86. topology.standalone.restart(timeout=10)
  87. def add_and_check(topology, plugin, attr, val, isvalid):
  88. """
  89. Helper function to add/replace attr: val and check the added value
  90. """
  91. if isvalid:
  92. log.info('Test %s: %s -- valid' % (attr, val))
  93. try:
  94. topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  95. except ldap.LDAPError as e:
  96. log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
  97. assert False
  98. else:
  99. log.info('Test %s: %s -- invalid' % (attr, val))
  100. if plugin == CHANGELOG:
  101. try:
  102. topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  103. except ldap.LDAPError as e:
  104. log.error('Expectedly failed to add ' + attr + ': ' + val +
  105. ' to ' + plugin + ': error ' + e.message['desc'])
  106. else:
  107. try:
  108. topology.standalone.modify_s(plugin, [(ldap.MOD_REPLACE, attr, val)])
  109. except ldap.LDAPError as e:
  110. log.error('Failed to add ' + attr + ': ' + val + ' to ' + plugin + ': error ' + e.message['desc'])
  111. try:
  112. entries = topology.standalone.search_s(plugin, ldap.SCOPE_BASE, FILTER, [attr])
  113. if isvalid:
  114. if not entries[0].hasValue(attr, val):
  115. log.fatal('%s does not have expected (%s: %s)' % (plugin, attr, val))
  116. assert False
  117. else:
  118. if plugin == CHANGELOG:
  119. if entries[0].hasValue(attr, val):
  120. log.fatal('%s has unexpected (%s: %s)' % (plugin, attr, val))
  121. assert False
  122. else:
  123. if not entries[0].hasValue(attr, val):
  124. log.fatal('%s does not have expected (%s: %s)' % (plugin, attr, val))
  125. assert False
  126. except ldap.LDAPError as e:
  127. log.fatal('Unable to search for entry %s: error %s' % (plugin, e.message['desc']))
  128. assert False
  129. def test_ticket47669_changelog_maxage(topology):
  130. """
  131. Test nsslapd-changelogmaxage in cn=changelog5,cn=config
  132. """
  133. log.info('1. Test nsslapd-changelogmaxage in cn=changelog5,cn=config')
  134. # bind as directory manager
  135. topology.standalone.log.info("Bind as %s" % DN_DM)
  136. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  137. add_and_check(topology, CHANGELOG, MAXAGE, '12345', True)
  138. add_and_check(topology, CHANGELOG, MAXAGE, '10s', True)
  139. add_and_check(topology, CHANGELOG, MAXAGE, '30M', True)
  140. add_and_check(topology, CHANGELOG, MAXAGE, '12h', True)
  141. add_and_check(topology, CHANGELOG, MAXAGE, '2D', True)
  142. add_and_check(topology, CHANGELOG, MAXAGE, '4w', True)
  143. add_and_check(topology, CHANGELOG, MAXAGE, '-123', False)
  144. add_and_check(topology, CHANGELOG, MAXAGE, 'xyz', False)
  145. def test_ticket47669_changelog_triminterval(topology):
  146. """
  147. Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config
  148. """
  149. log.info('2. Test nsslapd-changelogtrim-interval in cn=changelog5,cn=config')
  150. # bind as directory manager
  151. topology.standalone.log.info("Bind as %s" % DN_DM)
  152. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  153. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '12345', True)
  154. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '10s', True)
  155. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '30M', True)
  156. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '12h', True)
  157. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '2D', True)
  158. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '4w', True)
  159. add_and_check(topology, CHANGELOG, TRIMINTERVAL, '-123', False)
  160. add_and_check(topology, CHANGELOG, TRIMINTERVAL, 'xyz', False)
  161. def test_ticket47669_changelog_compactdbinterval(topology):
  162. """
  163. Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config
  164. """
  165. log.info('3. Test nsslapd-changelogcompactdb-interval in cn=changelog5,cn=config')
  166. # bind as directory manager
  167. topology.standalone.log.info("Bind as %s" % DN_DM)
  168. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  169. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '12345', True)
  170. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '10s', True)
  171. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '30M', True)
  172. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '12h', True)
  173. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '2D', True)
  174. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '4w', True)
  175. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, '-123', False)
  176. add_and_check(topology, CHANGELOG, COMPACTDBINTERVAL, 'xyz', False)
  177. def test_ticket47669_retrochangelog_maxage(topology):
  178. """
  179. Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config
  180. """
  181. log.info('4. Test nsslapd-changelogmaxage in cn=Retro Changelog Plugin,cn=plugins,cn=config')
  182. # bind as directory manager
  183. topology.standalone.log.info("Bind as %s" % DN_DM)
  184. topology.standalone.simple_bind_s(DN_DM, PASSWORD)
  185. add_and_check(topology, RETROCHANGELOG, MAXAGE, '12345', True)
  186. add_and_check(topology, RETROCHANGELOG, MAXAGE, '10s', True)
  187. add_and_check(topology, RETROCHANGELOG, MAXAGE, '30M', True)
  188. add_and_check(topology, RETROCHANGELOG, MAXAGE, '12h', True)
  189. add_and_check(topology, RETROCHANGELOG, MAXAGE, '2D', True)
  190. add_and_check(topology, RETROCHANGELOG, MAXAGE, '4w', True)
  191. add_and_check(topology, RETROCHANGELOG, MAXAGE, '-123', False)
  192. add_and_check(topology, RETROCHANGELOG, MAXAGE, 'xyz', False)
  193. topology.standalone.log.info("ticket47669 was successfully verified.")
  194. def test_ticket47669_final(topology):
  195. log.info('Testcase PASSED')
  196. def run_isolated():
  197. """
  198. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  199. To run isolated without py.test, you need to
  200. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  201. - set the installation prefix
  202. - run this program
  203. """
  204. global installation_prefix
  205. installation_prefix = None
  206. topo = topology(True)
  207. test_ticket47669_init(topo)
  208. test_ticket47669_changelog_maxage(topo)
  209. test_ticket47669_changelog_triminterval(topo)
  210. test_ticket47669_changelog_compactdbinterval(topo)
  211. test_ticket47669_retrochangelog_maxage(topo)
  212. test_ticket47669_final(topo)
  213. if __name__ == '__main__':
  214. run_isolated()