ticket47560_test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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
  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_ticket47560(topology):
  51. """
  52. This test case does the following:
  53. SETUP
  54. - Create entry cn=group,SUFFIX
  55. - Create entry cn=member,SUFFIX
  56. - Update 'cn=member,SUFFIX' to add "memberOf: cn=group,SUFFIX"
  57. - Enable Memberof Plugins
  58. # Here the cn=member entry has a 'memberOf' but
  59. # cn=group entry does not contain 'cn=member' in its member
  60. TEST CASE
  61. - start the fixupmemberof task
  62. - read the cn=member entry
  63. - check 'memberOf is now empty
  64. TEARDOWN
  65. - Delete entry cn=group,SUFFIX
  66. - Delete entry cn=member,SUFFIX
  67. - Disable Memberof Plugins
  68. """
  69. def _enable_disable_mbo(value):
  70. """
  71. Enable or disable mbo plugin depending on 'value' ('on'/'off')
  72. """
  73. # enable/disable the mbo plugin
  74. if value == 'on':
  75. topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  76. else:
  77. topology.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  78. log.debug("-------------> _enable_disable_mbo(%s)" % value)
  79. topology.standalone.stop(timeout=120)
  80. time.sleep(1)
  81. topology.standalone.start(timeout=120)
  82. time.sleep(3)
  83. # need to reopen a connection toward the instance
  84. topology.standalone.open()
  85. def _test_ticket47560_setup():
  86. """
  87. - Create entry cn=group,SUFFIX
  88. - Create entry cn=member,SUFFIX
  89. - Update 'cn=member,SUFFIX' to add "memberOf: cn=group,SUFFIX"
  90. - Enable Memberof Plugins
  91. """
  92. log.debug("-------- > _test_ticket47560_setup\n")
  93. #
  94. # By default the memberof plugin is disabled create
  95. # - create a group entry
  96. # - create a member entry
  97. # - set the member entry as memberof the group entry
  98. #
  99. entry = Entry(group_DN)
  100. entry.setValues('objectclass', 'top', 'groupOfNames', 'inetUser')
  101. entry.setValues('cn', 'group')
  102. try:
  103. topology.standalone.add_s(entry)
  104. except ldap.ALREADY_EXISTS:
  105. log.debug("Entry %s already exists" % (group_DN))
  106. entry = Entry(member_DN)
  107. entry.setValues('objectclass', 'top', 'person', 'organizationalPerson', 'inetorgperson', 'inetUser')
  108. entry.setValues('uid', 'member')
  109. entry.setValues('cn', 'member')
  110. entry.setValues('sn', 'member')
  111. try:
  112. topology.standalone.add_s(entry)
  113. except ldap.ALREADY_EXISTS:
  114. log.debug("Entry %s already exists" % (member_DN))
  115. replace = [(ldap.MOD_REPLACE, 'memberof', group_DN)]
  116. topology.standalone.modify_s(member_DN, replace)
  117. #
  118. # enable the memberof plugin and restart the instance
  119. #
  120. _enable_disable_mbo('on')
  121. #
  122. # check memberof attribute is still present
  123. #
  124. filt = 'uid=member'
  125. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  126. assert len(ents) == 1
  127. ent = ents[0]
  128. #print ent
  129. value = ent.getValue('memberof')
  130. #print "memberof: %s" % (value)
  131. assert value == group_DN
  132. def _test_ticket47560_teardown():
  133. """
  134. - Delete entry cn=group,SUFFIX
  135. - Delete entry cn=member,SUFFIX
  136. - Disable Memberof Plugins
  137. """
  138. log.debug("-------- > _test_ticket47560_teardown\n")
  139. # remove the entries group_DN and member_DN
  140. try:
  141. topology.standalone.delete_s(group_DN)
  142. except:
  143. log.warning("Entry %s fail to delete" % (group_DN))
  144. try:
  145. topology.standalone.delete_s(member_DN)
  146. except:
  147. log.warning("Entry %s fail to delete" % (member_DN))
  148. #
  149. # disable the memberof plugin and restart the instance
  150. #
  151. _enable_disable_mbo('off')
  152. group_DN = "cn=group,%s" % (SUFFIX)
  153. member_DN = "uid=member,%s" % (SUFFIX)
  154. #
  155. # Initialize the test case
  156. #
  157. _test_ticket47560_setup()
  158. #
  159. # start the test
  160. # - start the fixup task
  161. # - check the entry is fixed (no longer memberof the group)
  162. #
  163. log.debug("-------- > Start ticket tests\n")
  164. filt = 'uid=member'
  165. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  166. assert len(ents) == 1
  167. ent = ents[0]
  168. log.debug("Unfixed entry %r\n" % ent)
  169. # run the fixup task
  170. topology.standalone.tasks.fixupMemberOf(suffix=SUFFIX, args={TASK_WAIT: True})
  171. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  172. assert len(ents) == 1
  173. ent = ents[0]
  174. log.debug("Fixed entry %r\n" % ent)
  175. if ent.getValue('memberof') == group_DN:
  176. log.warning("Error the fixupMemberOf did not fix %s" % (member_DN))
  177. result_successful = False
  178. else:
  179. result_successful = True
  180. #
  181. # cleanup up the test case
  182. #
  183. _test_ticket47560_teardown()
  184. assert result_successful is True
  185. log.info('Testcase PASSED')
  186. if __name__ == '__main__':
  187. # Run isolated
  188. # -s for DEBUG mode
  189. CURRENT_FILE = os.path.realpath(__file__)
  190. pytest.main("-s %s" % CURRENT_FILE)