1
0

test_dynamic_plugins.py 20 KB

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