ticket47560_test.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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
  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. def fin():
  50. standalone.delete()
  51. request.addfinalizer(fin)
  52. # Here we have standalone instance up and running
  53. return TopologyStandalone(standalone)
  54. def test_ticket47560(topology):
  55. """
  56. This test case does the following:
  57. SETUP
  58. - Create entry cn=group,SUFFIX
  59. - Create entry cn=member,SUFFIX
  60. - Update 'cn=member,SUFFIX' to add "memberOf: cn=group,SUFFIX"
  61. - Enable Memberof Plugins
  62. # Here the cn=member entry has a 'memberOf' but
  63. # cn=group entry does not contain 'cn=member' in its member
  64. TEST CASE
  65. - start the fixupmemberof task
  66. - read the cn=member entry
  67. - check 'memberOf is now empty
  68. TEARDOWN
  69. - Delete entry cn=group,SUFFIX
  70. - Delete entry cn=member,SUFFIX
  71. - Disable Memberof Plugins
  72. """
  73. def _enable_disable_mbo(value):
  74. """
  75. Enable or disable mbo plugin depending on 'value' ('on'/'off')
  76. """
  77. # enable/disable the mbo plugin
  78. if value == 'on':
  79. topology.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  80. else:
  81. topology.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  82. log.debug("-------------> _enable_disable_mbo(%s)" % value)
  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. def _test_ticket47560_setup():
  90. """
  91. - Create entry cn=group,SUFFIX
  92. - Create entry cn=member,SUFFIX
  93. - Update 'cn=member,SUFFIX' to add "memberOf: cn=group,SUFFIX"
  94. - Enable Memberof Plugins
  95. """
  96. log.debug("-------- > _test_ticket47560_setup\n")
  97. #
  98. # By default the memberof plugin is disabled create
  99. # - create a group entry
  100. # - create a member entry
  101. # - set the member entry as memberof the group entry
  102. #
  103. entry = Entry(group_DN)
  104. entry.setValues('objectclass', 'top', 'groupOfNames', 'inetUser')
  105. entry.setValues('cn', 'group')
  106. try:
  107. topology.standalone.add_s(entry)
  108. except ldap.ALREADY_EXISTS:
  109. log.debug("Entry %s already exists" % (group_DN))
  110. entry = Entry(member_DN)
  111. entry.setValues('objectclass', 'top', 'person', 'organizationalPerson', 'inetorgperson', 'inetUser')
  112. entry.setValues('uid', 'member')
  113. entry.setValues('cn', 'member')
  114. entry.setValues('sn', 'member')
  115. try:
  116. topology.standalone.add_s(entry)
  117. except ldap.ALREADY_EXISTS:
  118. log.debug("Entry %s already exists" % (member_DN))
  119. replace = [(ldap.MOD_REPLACE, 'memberof', group_DN)]
  120. topology.standalone.modify_s(member_DN, replace)
  121. #
  122. # enable the memberof plugin and restart the instance
  123. #
  124. _enable_disable_mbo('on')
  125. #
  126. # check memberof attribute is still present
  127. #
  128. filt = 'uid=member'
  129. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  130. assert len(ents) == 1
  131. ent = ents[0]
  132. #print ent
  133. value = ent.getValue('memberof')
  134. #print "memberof: %s" % (value)
  135. assert value == group_DN
  136. def _test_ticket47560_teardown():
  137. """
  138. - Delete entry cn=group,SUFFIX
  139. - Delete entry cn=member,SUFFIX
  140. - Disable Memberof Plugins
  141. """
  142. log.debug("-------- > _test_ticket47560_teardown\n")
  143. # remove the entries group_DN and member_DN
  144. try:
  145. topology.standalone.delete_s(group_DN)
  146. except:
  147. log.warning("Entry %s fail to delete" % (group_DN))
  148. try:
  149. topology.standalone.delete_s(member_DN)
  150. except:
  151. log.warning("Entry %s fail to delete" % (member_DN))
  152. #
  153. # disable the memberof plugin and restart the instance
  154. #
  155. _enable_disable_mbo('off')
  156. group_DN = "cn=group,%s" % (SUFFIX)
  157. member_DN = "uid=member,%s" % (SUFFIX)
  158. #
  159. # Initialize the test case
  160. #
  161. _test_ticket47560_setup()
  162. #
  163. # start the test
  164. # - start the fixup task
  165. # - check the entry is fixed (no longer memberof the group)
  166. #
  167. log.debug("-------- > Start ticket tests\n")
  168. filt = 'uid=member'
  169. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  170. assert len(ents) == 1
  171. ent = ents[0]
  172. log.debug("Unfixed entry %r\n" % ent)
  173. # run the fixup task
  174. topology.standalone.tasks.fixupMemberOf(suffix=SUFFIX, args={TASK_WAIT: True})
  175. ents = topology.standalone.search_s(member_DN, ldap.SCOPE_BASE, filt)
  176. assert len(ents) == 1
  177. ent = ents[0]
  178. log.debug("Fixed entry %r\n" % ent)
  179. if ent.getValue('memberof') == group_DN:
  180. log.warning("Error the fixupMemberOf did not fix %s" % (member_DN))
  181. result_successful = False
  182. else:
  183. result_successful = True
  184. #
  185. # cleanup up the test case
  186. #
  187. _test_ticket47560_teardown()
  188. assert result_successful is True
  189. def test_ticket47560_final(topology):
  190. log.info('Testcase PASSED')
  191. def run_isolated():
  192. '''
  193. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  194. To run isolated without py.test, you need to
  195. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  196. - set the installation prefix
  197. - run this program
  198. '''
  199. global installation_prefix
  200. installation_prefix = None
  201. topo = topology(True)
  202. test_ticket47560(topo)
  203. test_ticket47560_final(topo)
  204. if __name__ == '__main__':
  205. run_isolated()