ticket47815_test.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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
  8. from lib389.tools import DirSrvTools
  9. from lib389._constants import *
  10. from lib389.properties import *
  11. log = logging.getLogger(__name__)
  12. installation_prefix = None
  13. class TopologyStandalone(object):
  14. def __init__(self, standalone):
  15. standalone.open()
  16. self.standalone = standalone
  17. @pytest.fixture(scope="module")
  18. def topology(request):
  19. '''
  20. This fixture is used to standalone topology for the 'module'.
  21. '''
  22. global installation_prefix
  23. if installation_prefix:
  24. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  25. standalone = DirSrv(verbose=False)
  26. # Args for the standalone instance
  27. args_instance[SER_HOST] = HOST_STANDALONE
  28. args_instance[SER_PORT] = PORT_STANDALONE
  29. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  30. args_standalone = args_instance.copy()
  31. standalone.allocate(args_standalone)
  32. # Get the status of the instance and restart it if it exists
  33. instance_standalone = standalone.exists()
  34. # Remove the instance
  35. if instance_standalone:
  36. standalone.delete()
  37. # Create the instance
  38. standalone.create()
  39. # Used to retrieve configuration information (dbdir, confdir...)
  40. standalone.open()
  41. # clear the tmp directory
  42. standalone.clearTmpDir(__file__)
  43. # Here we have standalone instance up and running
  44. return TopologyStandalone(standalone)
  45. def test_ticket47815(topology):
  46. """
  47. Test betxn plugins reject an invalid option, and make sure that the rejected entry
  48. is not in the entry cache.
  49. Enable memberOf, automember, and retrocl plugins
  50. Add the automember config entry
  51. Add the automember group
  52. Add a user that will be rejected by a betxn plugin - result error 53
  53. Attempt the same add again, and it should result in another error 53 (not error 68)
  54. """
  55. result = 0
  56. result2 = 0
  57. log.info('Testing Ticket 47815 - Add entries that should be rejected by the betxn plugins, and are not left in the entry cache')
  58. # Enabled the plugins
  59. topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  60. topology.standalone.plugins.enable(name=PLUGIN_AUTOMEMBER)
  61. topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  62. # configure automember config entry
  63. log.info('Adding automember config')
  64. try:
  65. topology.standalone.add_s(Entry(('cn=group cfg,cn=Auto Membership Plugin,cn=plugins,cn=config', {
  66. 'objectclass': 'top autoMemberDefinition'.split(),
  67. 'autoMemberScope': 'dc=example,dc=com',
  68. 'autoMemberFilter': 'cn=user',
  69. 'autoMemberDefaultGroup': 'cn=group,dc=example,dc=com',
  70. 'autoMemberGroupingAttr': 'member:dn',
  71. 'cn': 'group cfg'})))
  72. except:
  73. log.error('Failed to add automember config')
  74. exit(1)
  75. topology.standalone.stop(timeout=120)
  76. time.sleep(1)
  77. topology.standalone.start(timeout=120)
  78. time.sleep(3)
  79. # need to reopen a connection toward the instance
  80. topology.standalone.open()
  81. # add automember group
  82. log.info('Adding automember group')
  83. try:
  84. topology.standalone.add_s(Entry(('cn=group,dc=example,dc=com', {
  85. 'objectclass': 'top groupOfNames'.split(),
  86. 'cn': 'group'})))
  87. except:
  88. log.error('Failed to add automember group')
  89. exit(1)
  90. # add user that should result in an error 53
  91. log.info('Adding invalid entry')
  92. try:
  93. topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
  94. 'objectclass': 'top person'.split(),
  95. 'sn': 'user',
  96. 'cn': 'user'})))
  97. except ldap.UNWILLING_TO_PERFORM:
  98. log.debug('Adding invalid entry failed as expected')
  99. result = 53
  100. except ldap.LDAPError, e:
  101. log.error('Unexpected result ' + e.message['desc'])
  102. assert False
  103. if result == 0:
  104. log.error('Add operation unexpectedly succeeded')
  105. assert False
  106. # Attempt to add user again, should result in error 53 again
  107. try:
  108. topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
  109. 'objectclass': 'top person'.split(),
  110. 'sn': 'user',
  111. 'cn': 'user'})))
  112. except ldap.UNWILLING_TO_PERFORM:
  113. log.debug('2nd add of invalid entry failed as expected')
  114. result2 = 53
  115. except ldap.LDAPError, e:
  116. log.error('Unexpected result ' + e.message['desc'])
  117. assert False
  118. if result2 == 0:
  119. log.error('2nd Add operation unexpectedly succeeded')
  120. assert False
  121. def test_ticket47815_final(topology):
  122. topology.standalone.delete()
  123. log.info('Testcase PASSED')
  124. def run_isolated():
  125. '''
  126. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  127. To run isolated without py.test, you need to
  128. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  129. - set the installation prefix
  130. - run this program
  131. '''
  132. global installation_prefix
  133. installation_prefix = None
  134. topo = topology(True)
  135. test_ticket47815(topo)
  136. test_ticket47815_final(topo)
  137. if __name__ == '__main__':
  138. run_isolated()