ticket47815_test.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. class TopologyStandalone(object):
  21. def __init__(self, standalone):
  22. standalone.open()
  23. self.standalone = standalone
  24. @pytest.fixture(scope="module")
  25. def topology(request):
  26. '''
  27. This fixture is used to standalone topology for the 'module'.
  28. '''
  29. standalone = DirSrv(verbose=False)
  30. # Args for the standalone instance
  31. args_instance[SER_HOST] = HOST_STANDALONE
  32. args_instance[SER_PORT] = PORT_STANDALONE
  33. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  34. args_standalone = args_instance.copy()
  35. standalone.allocate(args_standalone)
  36. # Get the status of the instance and restart it if it exists
  37. instance_standalone = standalone.exists()
  38. # Remove the instance
  39. if instance_standalone:
  40. standalone.delete()
  41. # Create the instance
  42. standalone.create()
  43. # Used to retrieve configuration information (dbdir, confdir...)
  44. standalone.open()
  45. def fin():
  46. standalone.delete()
  47. request.addfinalizer(fin)
  48. # Here we have standalone instance up and running
  49. return TopologyStandalone(standalone)
  50. def test_ticket47815(topology):
  51. """
  52. Test betxn plugins reject an invalid option, and make sure that the rejected entry
  53. is not in the entry cache.
  54. Enable memberOf, automember, and retrocl plugins
  55. Add the automember config entry
  56. Add the automember group
  57. Add a user that will be rejected by a betxn plugin - result error 53
  58. Attempt the same add again, and it should result in another error 53 (not error 68)
  59. """
  60. result = 0
  61. result2 = 0
  62. log.info('Testing Ticket 47815 - Add entries that should be rejected by the betxn plugins, and are not left in the entry cache')
  63. # Enabled the plugins
  64. topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  65. topology.standalone.plugins.enable(name=PLUGIN_AUTOMEMBER)
  66. topology.standalone.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  67. # configure automember config entry
  68. log.info('Adding automember config')
  69. try:
  70. topology.standalone.add_s(Entry(('cn=group cfg,cn=Auto Membership Plugin,cn=plugins,cn=config', {
  71. 'objectclass': 'top autoMemberDefinition'.split(),
  72. 'autoMemberScope': 'dc=example,dc=com',
  73. 'autoMemberFilter': 'cn=user',
  74. 'autoMemberDefaultGroup': 'cn=group,dc=example,dc=com',
  75. 'autoMemberGroupingAttr': 'member:dn',
  76. 'cn': 'group cfg'})))
  77. except:
  78. log.error('Failed to add automember config')
  79. exit(1)
  80. topology.standalone.stop(timeout=120)
  81. time.sleep(1)
  82. topology.standalone.start(timeout=120)
  83. time.sleep(3)
  84. # need to reopen a connection toward the instance
  85. topology.standalone.open()
  86. # add automember group
  87. log.info('Adding automember group')
  88. try:
  89. topology.standalone.add_s(Entry(('cn=group,dc=example,dc=com', {
  90. 'objectclass': 'top groupOfNames'.split(),
  91. 'cn': 'group'})))
  92. except:
  93. log.error('Failed to add automember group')
  94. exit(1)
  95. # add user that should result in an error 53
  96. log.info('Adding invalid entry')
  97. try:
  98. topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
  99. 'objectclass': 'top person'.split(),
  100. 'sn': 'user',
  101. 'cn': 'user'})))
  102. except ldap.UNWILLING_TO_PERFORM:
  103. log.debug('Adding invalid entry failed as expected')
  104. result = 53
  105. except ldap.LDAPError as e:
  106. log.error('Unexpected result ' + e.message['desc'])
  107. assert False
  108. if result == 0:
  109. log.error('Add operation unexpectedly succeeded')
  110. assert False
  111. # Attempt to add user again, should result in error 53 again
  112. try:
  113. topology.standalone.add_s(Entry(('cn=user,dc=example,dc=com', {
  114. 'objectclass': 'top person'.split(),
  115. 'sn': 'user',
  116. 'cn': 'user'})))
  117. except ldap.UNWILLING_TO_PERFORM:
  118. log.debug('2nd add of invalid entry failed as expected')
  119. result2 = 53
  120. except ldap.LDAPError as e:
  121. log.error('Unexpected result ' + e.message['desc'])
  122. assert False
  123. if result2 == 0:
  124. log.error('2nd Add operation unexpectedly succeeded')
  125. assert False
  126. if __name__ == '__main__':
  127. # Run isolated
  128. # -s for DEBUG mode
  129. CURRENT_FILE = os.path.realpath(__file__)
  130. pytest.main("-s %s" % CURRENT_FILE)