1
0

ticket48325_test.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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, tasks
  8. from lib389.tools import DirSrvTools
  9. from lib389._constants import *
  10. from lib389.properties import *
  11. from lib389.tasks import *
  12. from lib389.utils import *
  13. logging.getLogger(__name__).setLevel(logging.DEBUG)
  14. log = logging.getLogger(__name__)
  15. installation1_prefix = None
  16. class TopologyReplication(object):
  17. def __init__(self, master1, hub1, consumer1):
  18. master1.open()
  19. self.master1 = master1
  20. hub1.open()
  21. self.hub1 = hub1
  22. consumer1.open()
  23. self.consumer1 = consumer1
  24. @pytest.fixture(scope="module")
  25. def topology(request):
  26. global installation1_prefix
  27. if installation1_prefix:
  28. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  29. # Creating master 1...
  30. master1 = DirSrv(verbose=False)
  31. args_instance[SER_HOST] = HOST_MASTER_1
  32. args_instance[SER_PORT] = PORT_MASTER_1
  33. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_1
  34. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  35. args_master = args_instance.copy()
  36. master1.allocate(args_master)
  37. instance_master1 = master1.exists()
  38. if instance_master1:
  39. master1.delete()
  40. master1.create()
  41. master1.open()
  42. master1.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER,
  43. replicaId=REPLICAID_MASTER_1)
  44. # Creating hub 1...
  45. hub1 = DirSrv(verbose=False)
  46. args_instance[SER_HOST] = HOST_HUB_1
  47. args_instance[SER_PORT] = PORT_HUB_1
  48. args_instance[SER_SERVERID_PROP] = SERVERID_HUB_1
  49. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  50. args_hub = args_instance.copy()
  51. hub1.allocate(args_hub)
  52. instance_hub1 = hub1.exists()
  53. if instance_hub1:
  54. hub1.delete()
  55. hub1.create()
  56. hub1.open()
  57. hub1.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_HUB,
  58. replicaId=REPLICAID_HUB_1)
  59. # Creating consumer 1...
  60. consumer1 = DirSrv(verbose=False)
  61. args_instance[SER_HOST] = HOST_CONSUMER_1
  62. args_instance[SER_PORT] = PORT_CONSUMER_1
  63. args_instance[SER_SERVERID_PROP] = SERVERID_CONSUMER_1
  64. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  65. args_consumer = args_instance.copy()
  66. consumer1.allocate(args_consumer)
  67. instance_consumer1 = consumer1.exists()
  68. if instance_consumer1:
  69. consumer1.delete()
  70. consumer1.create()
  71. consumer1.open()
  72. consumer1.changelog.create()
  73. consumer1.replica.enableReplication(suffix=SUFFIX,
  74. role=REPLICAROLE_CONSUMER,
  75. replicaId=CONSUMER_REPLICAID)
  76. #
  77. # Create all the agreements
  78. #
  79. # Creating agreement from master 1 to hub 1
  80. properties = {RA_NAME: r'meTo_$host:$port',
  81. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  82. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  83. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  84. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  85. m1_h1_agmt = master1.agreement.create(suffix=SUFFIX, host=hub1.host,
  86. port=hub1.port,
  87. properties=properties)
  88. if not m1_h1_agmt:
  89. log.fatal("Fail to create a master -> hub replica agreement")
  90. sys.exit(1)
  91. log.debug("%s created" % m1_h1_agmt)
  92. # Creating agreement from hub 1 to consumer 1
  93. properties = {RA_NAME: r'meTo_$host:$port',
  94. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  95. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  96. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  97. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  98. h1_c1_agmt = hub1.agreement.create(suffix=SUFFIX, host=consumer1.host,
  99. port=consumer1.port,
  100. properties=properties)
  101. if not h1_c1_agmt:
  102. log.fatal("Fail to create a hub -> consumer replica agreement")
  103. sys.exit(1)
  104. log.debug("%s created" % h1_c1_agmt)
  105. # Allow the replicas to get situated with the new agreements...
  106. time.sleep(5)
  107. #
  108. # Initialize all the agreements
  109. #
  110. master1.agreement.init(SUFFIX, HOST_HUB_1, PORT_HUB_1)
  111. master1.waitForReplInit(m1_h1_agmt)
  112. hub1.agreement.init(SUFFIX, HOST_CONSUMER_1, PORT_CONSUMER_1)
  113. hub1.waitForReplInit(h1_c1_agmt)
  114. # Check replication is working...
  115. if master1.testReplication(DEFAULT_SUFFIX, consumer1):
  116. log.info('Replication is working.')
  117. else:
  118. log.fatal('Replication is not working.')
  119. assert False
  120. # Delete each instance in the end
  121. def fin():
  122. master1.delete()
  123. hub1.delete()
  124. consumer1.delete()
  125. pass
  126. request.addfinalizer(fin)
  127. # Clear out the tmp dir
  128. master1.clearTmpDir(__file__)
  129. return TopologyReplication(master1, hub1, consumer1)
  130. def checkFirstElement(ds, rid):
  131. """
  132. Return True if the first RUV element is for the specified rid
  133. """
  134. try:
  135. entry = ds.search_s(DEFAULT_SUFFIX,
  136. ldap.SCOPE_SUBTREE,
  137. REPLICA_RUV_FILTER,
  138. ['nsds50ruv'])
  139. assert entry
  140. entry = entry[0]
  141. except ldap.LDAPError as e:
  142. log.fatal('Failed to retrieve RUV entry: %s' % str(e))
  143. assert False
  144. ruv_elements = entry.getValues('nsds50ruv')
  145. if ('replica %s ' % rid) in ruv_elements[1]:
  146. return True
  147. else:
  148. return False
  149. def test_ticket48325(topology):
  150. """
  151. Test that the RUV element order is correctly maintained when promoting
  152. a hub or consumer.
  153. """
  154. #
  155. # Promote consumer to master
  156. #
  157. try:
  158. DN = topology.consumer1.replica._get_mt_entry(DEFAULT_SUFFIX)
  159. topology.consumer1.modify_s(DN, [(ldap.MOD_REPLACE,
  160. 'nsDS5ReplicaType',
  161. '3'),
  162. (ldap.MOD_REPLACE,
  163. 'nsDS5ReplicaID',
  164. '1234'),
  165. (ldap.MOD_REPLACE,
  166. 'nsDS5Flags',
  167. '1')])
  168. except ldap.LDAPError as e:
  169. log.fatal('Failed to promote consuemr to master: error %s' % str(e))
  170. assert False
  171. time.sleep(1)
  172. #
  173. # Check ruv has been reordered
  174. #
  175. if not checkFirstElement(topology.consumer1, '1234'):
  176. log.fatal('RUV was not reordered')
  177. assert False
  178. #
  179. # Create repl agreement from the newly promoted master to master1
  180. #
  181. properties = {RA_NAME: r'meTo_$host:$port',
  182. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  183. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  184. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  185. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  186. new_agmt = topology.consumer1.agreement.create(suffix=SUFFIX,
  187. host=topology.master1.host,
  188. port=topology.master1.port,
  189. properties=properties)
  190. if not new_agmt:
  191. log.fatal("Fail to create new agmt from old consumer to the master")
  192. assert False
  193. #
  194. # Test replication is working
  195. #
  196. if topology.consumer1.testReplication(DEFAULT_SUFFIX, topology.master1):
  197. log.info('Replication is working.')
  198. else:
  199. log.fatal('Replication is not working.')
  200. assert False
  201. #
  202. # Promote hub to master
  203. #
  204. try:
  205. DN = topology.hub1.replica._get_mt_entry(DEFAULT_SUFFIX)
  206. topology.hub1.modify_s(DN, [(ldap.MOD_REPLACE,
  207. 'nsDS5ReplicaType',
  208. '3'),
  209. (ldap.MOD_REPLACE,
  210. 'nsDS5ReplicaID',
  211. '5678')])
  212. except ldap.LDAPError as e:
  213. log.fatal('Failed to promote consuemr to master: error %s' % str(e))
  214. assert False
  215. time.sleep(1)
  216. #
  217. # Check ruv has been reordered
  218. #
  219. if not checkFirstElement(topology.hub1, '5678'):
  220. log.fatal('RUV was not reordered')
  221. assert False
  222. #
  223. # Test replication is working
  224. #
  225. if topology.hub1.testReplication(DEFAULT_SUFFIX, topology.master1):
  226. log.info('Replication is working.')
  227. else:
  228. log.fatal('Replication is not working.')
  229. assert False
  230. # Done
  231. log.info('Test complete')
  232. if __name__ == '__main__':
  233. # Run isolated
  234. # -s for DEBUG mode
  235. CURRENT_FILE = os.path.realpath(__file__)
  236. pytest.main("-s %s" % CURRENT_FILE)