ticket47560_test.py 7.0 KB

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