test_dynamic_plugins.py 20 KB

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