test_dynamic_plugins.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. # --- BEGIN COPYRIGHT BLOCK ---
  2. # Copyright (C) 2016 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 Dec 09, 2014
  11. @author: mreynolds
  12. '''
  13. import os
  14. import sys
  15. import time
  16. import ldap
  17. import ldap.sasl
  18. import logging
  19. import pytest
  20. import plugin_tests
  21. import stress_tests
  22. from lib389 import DirSrv, Entry, tools, tasks
  23. from lib389.tools import DirSrvTools
  24. from lib389._constants import *
  25. from lib389.properties import *
  26. from lib389.tasks import *
  27. from lib389.topologies import topology_st
  28. log = logging.getLogger(__name__)
  29. def repl_fail(replica):
  30. """Remove replica instance, and assert failure"""
  31. replica.delete()
  32. assert False
  33. def test_dynamic_plugins(topology_st):
  34. """
  35. Test Dynamic Plugins - exercise each plugin and its main features, while
  36. changing the configuration without restarting the server.
  37. Need to test: functionality, stability, and stress. These tests need to run
  38. with replication disabled, and with replication setup with a
  39. second instance. Then test if replication is working, and we have
  40. same entries on each side.
  41. Functionality - Make sure that as configuration changes are made they take
  42. effect immediately. Cross plugin interaction (e.g. automember/memberOf)
  43. needs to tested, as well as plugin tasks. Need to test plugin
  44. config validation(dependencies, etc).
  45. Memory Corruption - Restart the plugins many times, and in different orders and test
  46. functionality, and stability. This will excerise the internal
  47. plugin linked lists, dse callbacks, and task handlers.
  48. Stress - Put the server under load that will trigger multiple plugins(MO, RI, DNA, etc)
  49. Restart various plugins while these operations are going on. Perform this test
  50. 5 times(stress_max_run).
  51. """
  52. REPLICA_PORT = 33334
  53. RUV_FILTER = '(&(nsuniqueid=ffffffff-ffffffff-ffffffff-ffffffff)(objectclass=nstombstone))'
  54. master_maxcsn = 0
  55. replica_maxcsn = 0
  56. msg = ' (no replication)'
  57. replication_run = False
  58. stress_max_runs = 5
  59. # First enable dynamic plugins
  60. try:
  61. topology_st.standalone.modify_s(DN_CONFIG, [(ldap.MOD_REPLACE, 'nsslapd-dynamic-plugins', 'on')])
  62. except ldap.LDAPError as e:
  63. ldap.fatal('Failed to enable dynamic plugin!' + e.message['desc'])
  64. assert False
  65. # Test that critical plugins can be updated even though the change might not be applied
  66. try:
  67. topology_st.standalone.modify_s(DN_LDBM, [(ldap.MOD_REPLACE, 'description', 'test')])
  68. except ldap.LDAPError as e:
  69. ldap.fatal('Failed to apply change to critical plugin' + e.message['desc'])
  70. assert False
  71. while 1:
  72. #
  73. # First run the tests with replication disabled, then rerun them with replication set up
  74. #
  75. ############################################################################
  76. # Test plugin functionality
  77. ############################################################################
  78. log.info('####################################################################')
  79. log.info('Testing Dynamic Plugins Functionality' + msg + '...')
  80. log.info('####################################################################\n')
  81. plugin_tests.test_all_plugins(topology_st.standalone)
  82. log.info('####################################################################')
  83. log.info('Successfully Tested Dynamic Plugins Functionality' + msg + '.')
  84. log.info('####################################################################\n')
  85. ############################################################################
  86. # Test the stability by exercising the internal lists, callabcks, and task handlers
  87. ############################################################################
  88. log.info('####################################################################')
  89. log.info('Testing Dynamic Plugins for Memory Corruption' + msg + '...')
  90. log.info('####################################################################\n')
  91. prev_plugin_test = None
  92. prev_prev_plugin_test = None
  93. for plugin_test in plugin_tests.func_tests:
  94. #
  95. # Restart the plugin several times (and prev plugins) - work that linked list
  96. #
  97. plugin_test(topology_st.standalone, "restart")
  98. if prev_prev_plugin_test:
  99. prev_prev_plugin_test(topology_st.standalone, "restart")
  100. plugin_test(topology_st.standalone, "restart")
  101. if prev_plugin_test:
  102. prev_plugin_test(topology_st.standalone, "restart")
  103. plugin_test(topology_st.standalone, "restart")
  104. # Now run the functional test
  105. plugin_test(topology_st.standalone)
  106. # Set the previous tests
  107. if prev_plugin_test:
  108. prev_prev_plugin_test = prev_plugin_test
  109. prev_plugin_test = plugin_test
  110. log.info('####################################################################')
  111. log.info('Successfully Tested Dynamic Plugins for Memory Corruption' + msg + '.')
  112. log.info('####################################################################\n')
  113. ############################################################################
  114. # Stress two plugins while restarting it, and while restarting other plugins.
  115. # The goal is to not crash, and have the plugins work after stressing them.
  116. ############################################################################
  117. log.info('####################################################################')
  118. log.info('Stressing Dynamic Plugins' + msg + '...')
  119. log.info('####################################################################\n')
  120. stress_tests.configureMO(topology_st.standalone)
  121. stress_tests.configureRI(topology_st.standalone)
  122. stress_count = 0
  123. while stress_count < stress_max_runs:
  124. log.info('####################################################################')
  125. log.info('Running stress test' + msg + '. Run (%d/%d)...' % (stress_count + 1, stress_max_runs))
  126. log.info('####################################################################\n')
  127. try:
  128. # Launch three new threads to add a bunch of users
  129. add_users = stress_tests.AddUsers(topology_st.standalone, 'employee', True)
  130. add_users.start()
  131. add_users2 = stress_tests.AddUsers(topology_st.standalone, 'entry', True)
  132. add_users2.start()
  133. add_users3 = stress_tests.AddUsers(topology_st.standalone, 'person', True)
  134. add_users3.start()
  135. time.sleep(1)
  136. # While we are adding users restart the MO plugin and an idle plugin
  137. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  138. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  139. time.sleep(1)
  140. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  141. time.sleep(1)
  142. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  143. topology_st.standalone.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  144. topology_st.standalone.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  145. time.sleep(1)
  146. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  147. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  148. time.sleep(2)
  149. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  150. time.sleep(1)
  151. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  152. topology_st.standalone.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  153. topology_st.standalone.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  154. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  155. time.sleep(1)
  156. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  157. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  158. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  159. # Wait for the 'adding' threads to complete
  160. add_users.join()
  161. add_users2.join()
  162. add_users3.join()
  163. # Now launch three threads to delete the users
  164. del_users = stress_tests.DelUsers(topology_st.standalone, 'employee')
  165. del_users.start()
  166. del_users2 = stress_tests.DelUsers(topology_st.standalone, 'entry')
  167. del_users2.start()
  168. del_users3 = stress_tests.DelUsers(topology_st.standalone, 'person')
  169. del_users3.start()
  170. time.sleep(1)
  171. # Restart both the MO, RI plugins during these deletes, and an idle plugin
  172. topology_st.standalone.plugins.disable(name=PLUGIN_REFER_INTEGRITY)
  173. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  174. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  175. topology_st.standalone.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  176. time.sleep(1)
  177. topology_st.standalone.plugins.disable(name=PLUGIN_REFER_INTEGRITY)
  178. time.sleep(1)
  179. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  180. time.sleep(1)
  181. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  182. time.sleep(1)
  183. topology_st.standalone.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  184. topology_st.standalone.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  185. topology_st.standalone.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  186. topology_st.standalone.plugins.disable(name=PLUGIN_REFER_INTEGRITY)
  187. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  188. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  189. topology_st.standalone.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  190. time.sleep(2)
  191. topology_st.standalone.plugins.disable(name=PLUGIN_REFER_INTEGRITY)
  192. time.sleep(1)
  193. topology_st.standalone.plugins.disable(name=PLUGIN_MEMBER_OF)
  194. time.sleep(1)
  195. topology_st.standalone.plugins.enable(name=PLUGIN_MEMBER_OF)
  196. time.sleep(1)
  197. topology_st.standalone.plugins.enable(name=PLUGIN_REFER_INTEGRITY)
  198. topology_st.standalone.plugins.disable(name=PLUGIN_LINKED_ATTRS)
  199. topology_st.standalone.plugins.enable(name=PLUGIN_LINKED_ATTRS)
  200. # Wait for the 'deleting' threads to complete
  201. del_users.join()
  202. del_users2.join()
  203. del_users3.join()
  204. # Now make sure both the MO and RI plugins still work correctly
  205. plugin_tests.func_tests[8](topology_st.standalone) # RI plugin
  206. plugin_tests.func_tests[5](topology_st.standalone) # MO plugin
  207. # Cleanup the stress tests
  208. stress_tests.cleanup(topology_st.standalone)
  209. except:
  210. log.info('Stress test failed!')
  211. if replication_run:
  212. repl_fail(replica_inst)
  213. stress_count += 1
  214. log.info('####################################################################')
  215. log.info('Successfully Stressed Dynamic Plugins' + msg +
  216. '. Completed (%d/%d)' % (stress_count, stress_max_runs))
  217. log.info('####################################################################\n')
  218. if replication_run:
  219. # We're done.
  220. break
  221. else:
  222. #
  223. # Enable replication and run everything one more time
  224. #
  225. log.info('Setting up replication, and rerunning the tests...\n')
  226. # Create replica instance
  227. replica_inst = DirSrv(verbose=False)
  228. args_instance[SER_HOST] = LOCALHOST
  229. args_instance[SER_PORT] = REPLICA_PORT
  230. args_instance[SER_SERVERID_PROP] = 'replica'
  231. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  232. args_replica_inst = args_instance.copy()
  233. replica_inst.allocate(args_replica_inst)
  234. replica_inst.create()
  235. replica_inst.open()
  236. try:
  237. topology_st.standalone.replica.enableReplication(suffix=DEFAULT_SUFFIX,
  238. role=REPLICAROLE_MASTER,
  239. replicaId=1)
  240. replica_inst.replica.enableReplication(suffix=DEFAULT_SUFFIX,
  241. role=REPLICAROLE_CONSUMER,
  242. replicaId=65535)
  243. properties = {RA_NAME: r'to_replica',
  244. RA_BINDDN: defaultProperties[REPLICATION_BIND_DN],
  245. RA_BINDPW: defaultProperties[REPLICATION_BIND_PW],
  246. RA_METHOD: defaultProperties[REPLICATION_BIND_METHOD],
  247. RA_TRANSPORT_PROT: defaultProperties[REPLICATION_TRANSPORT]}
  248. repl_agreement = topology_st.standalone.agreement.create(suffix=DEFAULT_SUFFIX,
  249. host=LOCALHOST,
  250. port=REPLICA_PORT,
  251. properties=properties)
  252. if not repl_agreement:
  253. log.fatal("Fail to create a replica agreement")
  254. repl_fail(replica_inst)
  255. topology_st.standalone.agreement.init(DEFAULT_SUFFIX, LOCALHOST, REPLICA_PORT)
  256. topology_st.standalone.waitForReplInit(repl_agreement)
  257. except:
  258. log.info('Failed to setup replication!')
  259. repl_fail(replica_inst)
  260. replication_run = True
  261. msg = ' (replication enabled)'
  262. time.sleep(1)
  263. ############################################################################
  264. # Check replication, and data are in sync, and remove the instance
  265. ############################################################################
  266. log.info('Checking if replication is in sync...')
  267. try:
  268. # Grab master's max CSN
  269. entry = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, RUV_FILTER)
  270. if not entry:
  271. log.error('Failed to find db tombstone entry from master')
  272. repl_fail(replica_inst)
  273. elements = entry[0].getValues('nsds50ruv')
  274. for ruv in elements:
  275. if 'replica 1' in ruv:
  276. parts = ruv.split()
  277. if len(parts) == 5:
  278. master_maxcsn = parts[4]
  279. break
  280. else:
  281. log.error('RUV is incomplete')
  282. repl_fail(replica_inst)
  283. if master_maxcsn == 0:
  284. log.error('Failed to find maxcsn on master')
  285. repl_fail(replica_inst)
  286. except ldap.LDAPError as e:
  287. log.fatal('Unable to search masterfor db tombstone: ' + e.message['desc'])
  288. repl_fail(replica_inst)
  289. # Loop on the consumer - waiting for it to catch up
  290. count = 0
  291. insync = False
  292. while count < 60:
  293. try:
  294. # Grab master's max CSN
  295. entry = replica_inst.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, RUV_FILTER)
  296. if not entry:
  297. log.error('Failed to find db tombstone entry on consumer')
  298. repl_fail(replica_inst)
  299. elements = entry[0].getValues('nsds50ruv')
  300. for ruv in elements:
  301. if 'replica 1' in ruv:
  302. parts = ruv.split()
  303. if len(parts) == 5:
  304. replica_maxcsn = parts[4]
  305. break
  306. if replica_maxcsn == 0:
  307. log.error('Failed to find maxcsn on consumer')
  308. repl_fail(replica_inst)
  309. except ldap.LDAPError as e:
  310. log.fatal('Unable to search for db tombstone on consumer: ' + e.message['desc'])
  311. repl_fail(replica_inst)
  312. if master_maxcsn == replica_maxcsn:
  313. insync = True
  314. log.info('Replication is in sync.\n')
  315. break
  316. count += 1
  317. time.sleep(1)
  318. # Report on replication status
  319. if not insync:
  320. log.error('Consumer not in sync with master!')
  321. repl_fail(replica_inst)
  322. #
  323. # Verify the databases are identical. There should not be any "user, entry, employee" entries
  324. #
  325. log.info('Checking if the data is the same between the replicas...')
  326. # Check the master
  327. try:
  328. entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
  329. ldap.SCOPE_SUBTREE,
  330. "(|(uid=person*)(uid=entry*)(uid=employee*))")
  331. if len(entries) > 0:
  332. log.error('Master database has incorrect data set!\n')
  333. repl_fail(replica_inst)
  334. except ldap.LDAPError as e:
  335. log.fatal('Unable to search db on master: ' + e.message['desc'])
  336. repl_fail(replica_inst)
  337. # Check the consumer
  338. try:
  339. entries = replica_inst.search_s(DEFAULT_SUFFIX,
  340. ldap.SCOPE_SUBTREE,
  341. "(|(uid=person*)(uid=entry*)(uid=employee*))")
  342. if len(entries) > 0:
  343. log.error('Consumer database in not consistent with master database')
  344. repl_fail(replica_inst)
  345. except ldap.LDAPError as e:
  346. log.fatal('Unable to search db on consumer: ' + e.message['desc'])
  347. repl_fail(replica_inst)
  348. log.info('Data is consistent across the replicas.\n')
  349. log.info('####################################################################')
  350. log.info('Replication consistency test passed')
  351. log.info('####################################################################\n')
  352. # Remove the replica instance
  353. replica_inst.delete()
  354. ############################################################################
  355. # We made it to the end!
  356. ############################################################################
  357. log.info('#####################################################')
  358. log.info('#####################################################')
  359. log.info("Dynamic Plugins Testsuite: Completed Successfully!")
  360. log.info('#####################################################')
  361. log.info('#####################################################\n')
  362. if __name__ == '__main__':
  363. # Run isolated
  364. # -s for DEBUG mode
  365. CURRENT_FILE = os.path.realpath(__file__)
  366. pytest.main("-s %s" % CURRENT_FILE)