1
0

ticket47815_test.py 5.7 KB

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