ticket47871_test.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. '''
  10. Created on Nov 7, 2013
  11. @author: tbordaz
  12. '''
  13. import os
  14. import sys
  15. import time
  16. import ldap
  17. import logging
  18. import pytest
  19. from lib389 import DirSrv, Entry, tools
  20. from lib389.tools import DirSrvTools
  21. from lib389._constants import *
  22. from lib389.properties import *
  23. logging.getLogger(__name__).setLevel(logging.DEBUG)
  24. log = logging.getLogger(__name__)
  25. installation_prefix = None
  26. TEST_REPL_DN = "cn=test_repl, %s" % SUFFIX
  27. ENTRY_DN = "cn=test_entry, %s" % SUFFIX
  28. OTHER_NAME = 'other_entry'
  29. MAX_OTHERS = 10
  30. ATTRIBUTES = ['street', 'countryName', 'description', 'postalAddress', 'postalCode', 'title', 'l', 'roomNumber']
  31. class TopologyMasterConsumer(object):
  32. def __init__(self, master, consumer):
  33. master.open()
  34. self.master = master
  35. consumer.open()
  36. self.consumer = consumer
  37. def __repr__(self):
  38. return "Master[%s] -> Consumer[%s" % (self.master, self.consumer)
  39. @pytest.fixture(scope="module")
  40. def topology(request):
  41. '''
  42. This fixture is used to create a replicated topology for the 'module'.
  43. The replicated topology is MASTER -> Consumer.
  44. '''
  45. global installation_prefix
  46. if installation_prefix:
  47. args_instance[SER_DEPLOYED_DIR] = installation_prefix
  48. master = DirSrv(verbose=False)
  49. consumer = DirSrv(verbose=False)
  50. # Args for the master instance
  51. args_instance[SER_HOST] = HOST_MASTER_1
  52. args_instance[SER_PORT] = PORT_MASTER_1
  53. args_instance[SER_SERVERID_PROP] = SERVERID_MASTER_1
  54. args_master = args_instance.copy()
  55. master.allocate(args_master)
  56. # Args for the consumer instance
  57. args_instance[SER_HOST] = HOST_CONSUMER_1
  58. args_instance[SER_PORT] = PORT_CONSUMER_1
  59. args_instance[SER_SERVERID_PROP] = SERVERID_CONSUMER_1
  60. args_consumer = args_instance.copy()
  61. consumer.allocate(args_consumer)
  62. # Get the status of the instance and restart it if it exists
  63. instance_master = master.exists()
  64. instance_consumer = consumer.exists()
  65. # Remove all the instances
  66. if instance_master:
  67. master.delete()
  68. if instance_consumer:
  69. consumer.delete()
  70. # Create the instances
  71. master.create()
  72. master.open()
  73. consumer.create()
  74. consumer.open()
  75. #
  76. # Now prepare the Master-Consumer topology
  77. #
  78. # First Enable replication
  79. master.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_MASTER, replicaId=REPLICAID_MASTER_1)
  80. consumer.replica.enableReplication(suffix=SUFFIX, role=REPLICAROLE_CONSUMER)
  81. # Initialize the supplier->consumer
  82. properties = {RA_NAME: r'meTo_$host:$port',
  83. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  84. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  85. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  86. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  87. repl_agreement = master.agreement.create(suffix=SUFFIX, host=consumer.host, port=consumer.port, properties=properties)
  88. if not repl_agreement:
  89. log.fatal("Fail to create a replica agreement")
  90. sys.exit(1)
  91. log.debug("%s created" % repl_agreement)
  92. master.agreement.init(SUFFIX, HOST_CONSUMER_1, PORT_CONSUMER_1)
  93. master.waitForReplInit(repl_agreement)
  94. # Check replication is working fine
  95. if master.testReplication(DEFAULT_SUFFIX, consumer):
  96. log.info('Replication is working.')
  97. else:
  98. log.fatal('Replication is not working.')
  99. assert False
  100. def fin():
  101. master.delete()
  102. consumer.delete()
  103. request.addfinalizer(fin)
  104. #
  105. # Here we have two instances master and consumer
  106. # with replication working. Either coming from a backup recovery
  107. # or from a fresh (re)init
  108. # Time to return the topology
  109. return TopologyMasterConsumer(master, consumer)
  110. def test_ticket47871_init(topology):
  111. """
  112. Initialize the test environment
  113. """
  114. topology.master.plugins.enable(name=PLUGIN_RETRO_CHANGELOG)
  115. mod = [(ldap.MOD_REPLACE, 'nsslapd-changelogmaxage', "10s"), # 10 second triming
  116. (ldap.MOD_REPLACE, 'nsslapd-changelog-trim-interval', "5s")]
  117. topology.master.modify_s("cn=%s,%s" % (PLUGIN_RETRO_CHANGELOG, DN_PLUGIN), mod)
  118. #topology.master.plugins.enable(name=PLUGIN_MEMBER_OF)
  119. #topology.master.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  120. topology.master.stop(timeout=10)
  121. topology.master.start(timeout=10)
  122. topology.master.log.info("test_ticket47871_init topology %r" % (topology))
  123. # the test case will check if a warning message is logged in the
  124. # error log of the supplier
  125. topology.master.errorlog_file = open(topology.master.errlog, "r")
  126. def test_ticket47871_1(topology):
  127. '''
  128. ADD entries and check they are all in the retrocl
  129. '''
  130. # add dummy entries
  131. for cpt in range(MAX_OTHERS):
  132. name = "%s%d" % (OTHER_NAME, cpt)
  133. topology.master.add_s(Entry(("cn=%s,%s" % (name, SUFFIX), {
  134. 'objectclass': "top person".split(),
  135. 'sn': name,
  136. 'cn': name})))
  137. topology.master.log.info("test_ticket47871_init: %d entries ADDed %s[0..%d]" % (MAX_OTHERS, OTHER_NAME, MAX_OTHERS - 1))
  138. # Check the number of entries in the retro changelog
  139. time.sleep(1)
  140. ents = topology.master.search_s(RETROCL_SUFFIX, ldap.SCOPE_ONELEVEL, "(objectclass=*)")
  141. assert len(ents) == MAX_OTHERS
  142. topology.master.log.info("Added entries are")
  143. for ent in ents:
  144. topology.master.log.info("%s" % ent.dn)
  145. def test_ticket47871_2(topology):
  146. '''
  147. Wait until there is just a last entries
  148. '''
  149. MAX_TRIES = 10
  150. TRY_NO = 1
  151. while TRY_NO <= MAX_TRIES:
  152. time.sleep(6) # at least 1 trimming occurred
  153. ents = topology.master.search_s(RETROCL_SUFFIX, ldap.SCOPE_ONELEVEL, "(objectclass=*)")
  154. assert len(ents) <= MAX_OTHERS
  155. topology.master.log.info("\nTry no %d it remains %d entries" % (TRY_NO, len(ents)))
  156. for ent in ents:
  157. topology.master.log.info("%s" % ent.dn)
  158. if len(ents) > 1:
  159. TRY_NO += 1
  160. else:
  161. break
  162. assert TRY_NO <= MAX_TRIES
  163. assert len(ents) <= 1
  164. def test_ticket47871_final(topology):
  165. log.info('Testcase PASSED')
  166. def run_isolated():
  167. '''
  168. run_isolated is used to run these test cases independently of a test scheduler (xunit, py.test..)
  169. To run isolated without py.test, you need to
  170. - edit this file and comment '@pytest.fixture' line before 'topology' function.
  171. - set the installation prefix
  172. - run this program
  173. '''
  174. global installation_prefix
  175. installation_prefix = None
  176. topo = topology(True)
  177. test_ticket47871_init(topo)
  178. test_ticket47871_1(topo)
  179. test_ticket47871_2(topo)
  180. test_ticket47871_final(topo)
  181. if __name__ == '__main__':
  182. run_isolated()