ticket47669_test.py 9.0 KB

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