corotests.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. __copyright__='''
  2. Copyright (c) 2010 Red Hat, Inc.
  3. '''
  4. # All rights reserved.
  5. #
  6. # Author: Angus Salkeld <asalkeld@redhat.com>
  7. #
  8. # This software licensed under BSD license, the text of which follows:
  9. #
  10. # Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are met:
  12. #
  13. # - Redistributions of source code must retain the above copyright notice,
  14. # this list of conditions and the following disclaimer.
  15. # - Redistributions in binary form must reproduce the above copyright notice,
  16. # this list of conditions and the following disclaimer in the documentation
  17. # and/or other materials provided with the distribution.
  18. # - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. # contributors may be used to endorse or promote products derived from this
  20. # software without specific prior written permission.
  21. #
  22. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. # THE POSSIBILITY OF SUCH DAMAGE.
  33. from cts.CTStests import *
  34. ###################################################################
  35. class CoroTest(CTSTest):
  36. '''
  37. basic class to make sure that new configuration is applied
  38. and old configuration is removed.
  39. '''
  40. def __init__(self, cm):
  41. CTSTest.__init__(self,cm)
  42. self.start = StartTest(cm)
  43. self.stop = StopTest(cm)
  44. def setup(self, node):
  45. ret = CTSTest.setup(self, node)
  46. self.CM.apply_new_config()
  47. for n in self.CM.Env["nodes"]:
  48. if not self.CM.StataCM(n):
  49. self.incr("started")
  50. self.start(n)
  51. return ret
  52. def setup_sec_key(self, node):
  53. localauthkey = '/tmp/authkey'
  54. if not os.path.exists(localauthkey):
  55. self.CM.rsh(node, 'corosync-keygen')
  56. self.CM.rsh.cp("%s:%s" % (node, "/etc/corosync/authkey"), localauthkey)
  57. for n in self.CM.Env["nodes"]:
  58. if n is not node:
  59. #copy key onto other nodes
  60. self.CM.rsh.cp(localauthkey, "%s:%s" % (n, "/etc/corosync/authkey"))
  61. def teardown(self, node):
  62. self.CM.apply_default_config()
  63. return CTSTest.teardown(self, node)
  64. ###################################################################
  65. class CpgConfigChangeBase(CoroTest):
  66. '''
  67. join a cpg group on each node, and test that the following
  68. causes a leave event:
  69. - a call to cpg_leave()
  70. - app exit
  71. - node leave
  72. - node leave (with large token timeout)
  73. '''
  74. def setup(self, node):
  75. ret = CoroTest.setup(self, node)
  76. self.listener = None
  77. self.wobbly = None
  78. for n in self.CM.Env["nodes"]:
  79. self.CM.agent[n].clean_start()
  80. self.CM.agent[n].cpg_join(self.name)
  81. if self.listener is None:
  82. self.listener = n
  83. elif self.wobbly is None:
  84. self.wobbly = n
  85. self.wobbly_id = self.CM.agent[self.wobbly].cpg_local_get()
  86. self.CM.agent[self.listener].record_config_events(truncate=True)
  87. return ret
  88. def wait_for_config_change(self):
  89. found = False
  90. max_timeout = 5 * 60
  91. waited = 0
  92. printit = 0
  93. self.CM.log("Waiting for config change on " + self.listener)
  94. while not found:
  95. try:
  96. event = self.CM.agent[self.listener].read_config_event()
  97. except:
  98. return self.failure('connection to test agent failed.')
  99. if not event == None:
  100. self.CM.debug("RECEIVED: " + str(event))
  101. if event == None:
  102. if waited >= max_timeout:
  103. return self.failure("timedout(" + str(waited) + " sec) == no event!")
  104. else:
  105. time.sleep(1)
  106. waited = waited + 1
  107. printit = printit + 1
  108. if printit is 60:
  109. print 'waited 60 seconds'
  110. printit = 0
  111. elif str(event.node_id) in str(self.wobbly_id) and not event.is_member:
  112. self.CM.log("Got the config change in " + str(waited) + " seconds")
  113. found = True
  114. else:
  115. self.CM.debug("No match")
  116. self.CM.debug("wobbly nodeid:" + str(self.wobbly_id))
  117. self.CM.debug("event nodeid:" + str(event.node_id))
  118. self.CM.debug("event.is_member:" + str(event.is_member))
  119. if found:
  120. return self.success()
  121. ###################################################################
  122. class CpgCfgChgOnGroupLeave(CpgConfigChangeBase):
  123. def __init__(self, cm):
  124. CpgConfigChangeBase.__init__(self,cm)
  125. self.name="CpgCfgChgOnGroupLeave"
  126. def failure_action(self):
  127. self.CM.log("calling cpg_leave() on " + self.wobbly)
  128. self.CM.agent[self.wobbly].cpg_leave(self.name)
  129. def __call__(self, node):
  130. self.incr("calls")
  131. self.failure_action()
  132. return self.wait_for_config_change()
  133. ###################################################################
  134. class CpgCfgChgOnNodeLeave(CpgConfigChangeBase):
  135. def __init__(self, cm):
  136. CpgConfigChangeBase.__init__(self,cm)
  137. self.name="CpgCfgChgOnNodeLeave"
  138. def failure_action(self):
  139. self.CM.log("stopping corosync on " + self.wobbly)
  140. self.stop(self.wobbly)
  141. def __call__(self, node):
  142. self.incr("calls")
  143. self.failure_action()
  144. return self.wait_for_config_change()
  145. ###################################################################
  146. class CpgCfgChgOnExecCrash(CpgConfigChangeBase):
  147. def __init__(self, cm):
  148. CpgConfigChangeBase.__init__(self,cm)
  149. self.name="CpgCfgChgOnExecCrash"
  150. def failure_action(self):
  151. self.CM.log("sending SIGSEGV to corosync on " + self.wobbly)
  152. self.CM.rsh(self.wobbly, "killall -9 corosync")
  153. self.CM.rsh(self.wobbly, "rm -f /var/run/corosync.pid")
  154. def __call__(self, node):
  155. self.incr("calls")
  156. self.failure_action()
  157. return self.wait_for_config_change()
  158. ###################################################################
  159. class CpgCfgChgOnNodeLeave_v2(CpgConfigChangeBase):
  160. def __init__(self, cm):
  161. CpgConfigChangeBase.__init__(self,cm)
  162. self.name="CpgCfgChgOnNodeLeave_v2"
  163. def setup(self, node):
  164. self.CM.new_config['compatibility'] = 'none'
  165. self.CM.new_config['totem/token'] = 10000
  166. return CpgConfigChangeBase.setup(self, node)
  167. def failure_action(self):
  168. self.CM.log("isolating node " + self.wobbly)
  169. self.CM.isolate_node(self.wobbly)
  170. def __call__(self, node):
  171. self.incr("calls")
  172. self.failure_action()
  173. return self.wait_for_config_change()
  174. def teardown(self, node):
  175. self.CM.unisolate_node (self.wobbly)
  176. return CpgConfigChangeBase.teardown(self, node)
  177. ###################################################################
  178. class CpgCfgChgOnNodeLeave_v1(CpgConfigChangeBase):
  179. def __init__(self, cm):
  180. CpgConfigChangeBase.__init__(self,cm)
  181. self.name="CpgCfgChgOnNodeLeave_v1"
  182. def setup(self, node):
  183. self.CM.new_config['compatibility'] = 'whitetank'
  184. self.CM.new_config['totem/token'] = 10000
  185. return CpgConfigChangeBase.setup(self, node)
  186. def failure_action(self):
  187. self.CM.log("isolating node " + self.wobbly)
  188. self.CM.isolate_node(self.wobbly)
  189. def __call__(self, node):
  190. self.incr("calls")
  191. self.failure_action()
  192. return self.wait_for_config_change()
  193. def teardown(self, node):
  194. self.CM.unisolate_node (self.wobbly)
  195. return CpgConfigChangeBase.teardown(self, node)
  196. ###################################################################
  197. class CpgMsgOrderBase(CoroTest):
  198. def __init__(self, cm):
  199. CoroTest.__init__(self,cm)
  200. self.num_msgs_per_node = 0
  201. self.total_num_msgs = 0
  202. def setup(self, node):
  203. ret = CoroTest.setup(self, node)
  204. for n in self.CM.Env["nodes"]:
  205. self.total_num_msgs = self.total_num_msgs + self.num_msgs_per_node
  206. self.CM.agent[n].clean_start()
  207. self.CM.agent[n].cpg_join(self.name)
  208. self.CM.agent[n].record_messages()
  209. time.sleep(1)
  210. return ret
  211. def cpg_msg_blaster(self):
  212. for n in self.CM.Env["nodes"]:
  213. self.CM.agent[n].msg_blaster(self.num_msgs_per_node)
  214. def wait_and_validate_order(self):
  215. msgs = {}
  216. for n in self.CM.Env["nodes"]:
  217. msgs[n] = []
  218. got = False
  219. stopped = False
  220. self.CM.debug( " getting messages from " + n )
  221. while len(msgs[n]) < self.total_num_msgs and not stopped:
  222. msg = self.CM.agent[n].read_messages(25)
  223. if not msg == None:
  224. got = True
  225. msgl = msg.split(";")
  226. # remove empty entries
  227. not_done=True
  228. while not_done:
  229. try:
  230. msgl.remove('')
  231. except:
  232. not_done = False
  233. msgs[n].extend(msgl)
  234. elif msg == None and got:
  235. self.CM.debug(" done getting messages from " + n)
  236. stopped = True
  237. if not got:
  238. time.sleep(1)
  239. fail = False
  240. for i in range(0, self.total_num_msgs):
  241. first = None
  242. for n in self.CM.Env["nodes"]:
  243. if first == None:
  244. first = n
  245. else:
  246. if not msgs[first][i] == msgs[n][i]:
  247. # message order not the same!
  248. fail = True
  249. self.CM.log(msgs[first][i] + " != " + msgs[n][i])
  250. if fail:
  251. return self.failure()
  252. else:
  253. return self.success()
  254. ###################################################################
  255. class CpgMsgOrderBasic(CpgMsgOrderBase):
  256. '''
  257. each sends & logs 100 messages
  258. '''
  259. def __init__(self, cm):
  260. CpgMsgOrderBase.__init__(self,cm)
  261. self.name="CpgMsgOrderBasic"
  262. def __call__(self, node):
  263. self.incr("calls")
  264. self.num_msgs_per_node = 100
  265. self.cpg_msg_blaster()
  266. return self.wait_and_validate_order()
  267. class CpgMsgOrderThreads(CpgMsgOrderBase):
  268. '''
  269. each sends & logs 100 messages
  270. '''
  271. def __init__(self, cm):
  272. CpgMsgOrderBase.__init__(self,cm)
  273. self.name="CpgMsgOrderThreads"
  274. def setup(self, node):
  275. self.CM.new_config['totem/threads'] = 4
  276. return CpgMsgOrderBase.setup(self, node)
  277. def __call__(self, node):
  278. self.incr("calls")
  279. self.num_msgs_per_node = 100
  280. self.cpg_msg_blaster()
  281. return self.wait_and_validate_order()
  282. class CpgMsgOrderSecNss(CpgMsgOrderBase):
  283. '''
  284. each sends & logs 100 messages
  285. '''
  286. def __init__(self, cm):
  287. CpgMsgOrderBase.__init__(self,cm)
  288. self.name="CpgMsgOrderSecNss"
  289. def setup(self, node):
  290. self.setup_sec_key(node)
  291. self.CM.new_config['totem/secauth'] = 'on'
  292. self.CM.new_config['totem/crypto_accept'] = 'new'
  293. self.CM.new_config['totem/crypto_type'] = 'nss'
  294. return CpgMsgOrderBase.setup(self, node)
  295. def __call__(self, node):
  296. self.incr("calls")
  297. self.num_msgs_per_node = 100
  298. self.cpg_msg_blaster()
  299. return self.wait_and_validate_order()
  300. class CpgMsgOrderSecSober(CpgMsgOrderBase):
  301. '''
  302. each sends & logs 100 messages
  303. '''
  304. def __init__(self, cm):
  305. CpgMsgOrderBase.__init__(self,cm)
  306. self.name="CpgMsgOrderSecSober"
  307. def setup(self, node):
  308. self.setup_sec_key(node)
  309. self.CM.new_config['totem/secauth'] = 'on'
  310. self.CM.new_config['totem/crypto_type'] = 'sober'
  311. return CpgMsgOrderBase.setup(self, node)
  312. def __call__(self, node):
  313. self.incr("calls")
  314. self.num_msgs_per_node = 100
  315. self.cpg_msg_blaster()
  316. return self.wait_and_validate_order()
  317. ###################################################################
  318. class MemLeakObject(CoroTest):
  319. '''
  320. run mem_leak_test.sh -1
  321. '''
  322. def __init__(self, cm):
  323. CoroTest.__init__(self,cm)
  324. self.name="MemLeakObject"
  325. def __call__(self, node):
  326. self.incr("calls")
  327. mem_leaked = self.CM.rsh(node, "/usr/share/corosync/tests/mem_leak_test.sh -1")
  328. if mem_leaked is 0:
  329. return self.success()
  330. else:
  331. return self.failure(str(mem_leaked) + 'kB memory leaked.')
  332. ###################################################################
  333. class MemLeakSession(CoroTest):
  334. '''
  335. run mem_leak_test.sh -2
  336. '''
  337. def __init__(self, cm):
  338. CoroTest.__init__(self,cm)
  339. self.name="MemLeakSession"
  340. def __call__(self, node):
  341. self.incr("calls")
  342. mem_leaked = self.CM.rsh(node, "/usr/share/corosync/tests/mem_leak_test.sh -2")
  343. if mem_leaked is 0:
  344. return self.success()
  345. else:
  346. return self.failure(str(mem_leaked) + 'kB memory leaked.')
  347. ###################################################################
  348. class ServiceLoadTest(CoroTest):
  349. '''
  350. Test loading and unloading of service engines
  351. '''
  352. def __init__(self, cm):
  353. CoroTest.__init__(self, cm)
  354. self.name="ServiceLoadTest"
  355. def is_loaded(self, node):
  356. check = 'corosync-objctl runtime.services. | grep evs'
  357. (res, out) = self.CM.rsh(node, check, stdout=2)
  358. if res is 0:
  359. return True
  360. else:
  361. return False
  362. def service_unload(self, node):
  363. # unload evs
  364. pats = []
  365. pats.append("%s .*Service engine unloaded: corosync extended.*" % node)
  366. unloaded = self.create_watch(pats, 60)
  367. unloaded.setwatch()
  368. self.CM.rsh(node, 'corosync-cfgtool -u corosync_evs')
  369. if not unloaded.lookforall():
  370. self.CM.log("Patterns not found: " + repr(unloaded.unmatched))
  371. self.error_message = "evs service not unloaded"
  372. return False
  373. if self.is_loaded(node):
  374. self.error_message = "evs has been unload, why are it's session objects are still there?"
  375. return False
  376. return True
  377. def service_load(self, node):
  378. # now reload it.
  379. pats = []
  380. pats.append("%s .*Service engine loaded.*" % node)
  381. loaded = self.create_watch(pats, 60)
  382. loaded.setwatch()
  383. self.CM.rsh(node, 'corosync-cfgtool -l corosync_evs')
  384. if not loaded.lookforall():
  385. self.CM.log("Patterns not found: " + repr(loaded.unmatched))
  386. self.error_message = "evs service not unloaded"
  387. return False
  388. return True
  389. def __call__(self, node):
  390. self.incr("calls")
  391. should_be_loaded = True
  392. if self.is_loaded(node):
  393. ret = self.service_unload(node)
  394. should_be_loaded = False
  395. else:
  396. ret = self.service_load(node)
  397. should_be_loaded = True
  398. if not ret:
  399. return self.failure(self.error_message)
  400. if self.is_loaded(node):
  401. ret = self.service_unload(node)
  402. else:
  403. ret = self.service_load(node)
  404. if not ret:
  405. return self.failure(self.error_message)
  406. return self.success()
  407. AllTestClasses = []
  408. AllTestClasses.append(ServiceLoadTest)
  409. AllTestClasses.append(CpgMsgOrderBasic)
  410. AllTestClasses.append(CpgMsgOrderThreads)
  411. AllTestClasses.append(CpgMsgOrderSecNss)
  412. AllTestClasses.append(CpgMsgOrderSecSober)
  413. AllTestClasses.append(MemLeakObject)
  414. AllTestClasses.append(MemLeakSession)
  415. AllTestClasses.append(CpgCfgChgOnExecCrash)
  416. AllTestClasses.append(CpgCfgChgOnGroupLeave)
  417. AllTestClasses.append(CpgCfgChgOnNodeLeave)
  418. AllTestClasses.append(CpgCfgChgOnNodeLeave_v1)
  419. AllTestClasses.append(CpgCfgChgOnNodeLeave_v2)
  420. AllTestClasses.append(FlipTest)
  421. AllTestClasses.append(RestartTest)
  422. AllTestClasses.append(StartOnebyOne)
  423. AllTestClasses.append(SimulStart)
  424. AllTestClasses.append(StopOnebyOne)
  425. AllTestClasses.append(SimulStop)
  426. AllTestClasses.append(RestartOnebyOne)
  427. #AllTestClasses.append(PartialStart)
  428. def CoroTestList(cm, audits):
  429. result = []
  430. for testclass in AllTestClasses:
  431. bound_test = testclass(cm)
  432. if bound_test.is_applicable():
  433. bound_test.Audits = audits
  434. result.append(bound_test)
  435. return result