pbs_server_periodic_hook.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. # coding: utf-8
  2. # Copyright (C) 1994-2018 Altair Engineering, Inc.
  3. # For more information, contact Altair at www.altair.com.
  4. #
  5. # This file is part of the PBS Professional ("PBS Pro") software.
  6. #
  7. # Open Source License Information:
  8. #
  9. # PBS Pro is free software. You can redistribute it and/or modify it under the
  10. # terms of the GNU Affero General Public License as published by the Free
  11. # Software Foundation, either version 3 of the License, or (at your option) any
  12. # later version.
  13. #
  14. # PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
  15. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. # FOR A PARTICULAR PURPOSE.
  17. # See the GNU Affero General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Affero General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. # Commercial License Information:
  23. #
  24. # For a copy of the commercial license terms and conditions,
  25. # go to: (http://www.pbspro.com/UserArea/agreement.html)
  26. # or contact the Altair Legal Department.
  27. #
  28. # Altair’s dual-license business model allows companies, individuals, and
  29. # organizations to create proprietary derivative works of PBS Pro and
  30. # distribute them - whether embedded or bundled with other software -
  31. # under a commercial license agreement.
  32. #
  33. # Use of Altair’s trademarks, including but not limited to "PBS™",
  34. # "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
  35. # trademark licensing policies.
  36. from tests.functional import *
  37. class Test_server_periodic_hook(TestFunctional):
  38. hook_string = """
  39. import pbs
  40. import time
  41. e = pbs.event()
  42. pbs.logmsg(pbs.LOG_DEBUG, "periodic hook started at %%d" %% time.time())
  43. time.sleep(%d)
  44. pbs.logmsg(pbs.LOG_DEBUG, "periodic hook ended at %%d" %% time.time())
  45. %s
  46. """
  47. def create_hook(self, accept, sleep_time):
  48. """
  49. function to create a hook script.
  50. It accepts 2 arguments
  51. - accept If set to true, then hook will accept else reject
  52. - sleep_time Number of seconds we want the hook to sleep
  53. """
  54. hook_action = "e.accept()"
  55. if accept is False:
  56. hook_action = "e.reject()"
  57. final_hook = self.hook_string % (int(sleep_time), hook_action)
  58. return final_hook
  59. start_msg = "periodic hook started at "
  60. end_msg = "periodic hook ended at "
  61. def get_timestamp(self, msg):
  62. a = msg.rsplit(' ', 1)
  63. return int(a[1])
  64. def check_next_occurances(self, count, freq,
  65. hook_run_time, check_for_hook_end):
  66. """
  67. Helper function to check the occurances of hook by matching their
  68. logs in server logs.
  69. It needs 4 arguments:
  70. - count to know how many times to repeat
  71. checking these logs
  72. - freq is the frequency set in pbs server to run this hook
  73. - hook_run_time is the amount of time hook takes to run.
  74. - check_for_hook_end If it is true then the functions checks for
  75. hook end messages.
  76. """
  77. occurance = 0
  78. time_expected = int(time.time()) + freq
  79. # time after which we want to start matching log
  80. search_after = int(time.time())
  81. intr = freq
  82. while (occurance < count):
  83. msg_expected = self.start_msg
  84. msg = self.server.log_match(msg_expected,
  85. interval=(intr + 1),
  86. starttime=search_after)
  87. time_logged = self.get_timestamp(msg[1])
  88. self.assertFalse((time_logged - time_expected) > 1)
  89. if check_for_hook_end is True:
  90. time_expected += hook_run_time
  91. # set it to a second before we expect the hook to end
  92. search_after = time_expected - 1
  93. msg_expected = self.end_msg
  94. msg = self.server.log_match(msg_expected, max_attempts=2,
  95. interval=(hook_run_time + 1),
  96. starttime=search_after)
  97. time_logged = self.get_timestamp(msg[1])
  98. self.assertFalse((time_logged - time_expected) > 1)
  99. if hook_run_time <= freq:
  100. intr = freq - hook_run_time
  101. else:
  102. intr = freq - (hook_run_time % freq)
  103. else:
  104. if hook_run_time <= freq:
  105. intr = freq
  106. else:
  107. intr = hook_run_time + (freq - (hook_run_time % freq))
  108. # we just matched hook start/end message, next start message is
  109. # surely after time_expected.
  110. search_after = time_expected
  111. time_expected = time_expected + intr
  112. occurance += 1
  113. def test_sp_hook_run(self):
  114. """
  115. Submit a server periodic hook that rejects
  116. """
  117. hook_name = "medium_hook"
  118. freq = 20
  119. hook_run_time = 10
  120. scr = self.create_hook(True, hook_run_time)
  121. attrs = {'event': "periodic"}
  122. rv = self.server.create_import_hook(
  123. hook_name,
  124. attrs,
  125. scr,
  126. overwrite=True)
  127. self.assertTrue(rv)
  128. attrs = {'freq': freq}
  129. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  130. self.assertEqual(rv, 0)
  131. attrs = {'enabled': 'True'}
  132. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  133. self.assertEqual(rv, 0)
  134. self.check_next_occurances(2, freq, hook_run_time, True)
  135. def test_sp_hook_reject(self):
  136. """
  137. Submit a server periodic hook that rejects
  138. """
  139. hook_name = "reject_hook"
  140. freq = 20
  141. hook_run_time = 10
  142. scr = self.create_hook(False, hook_run_time)
  143. attrs = {'event': "periodic"}
  144. msg_expected = ";periodic request rejected by " + "'" + hook_name + "'"
  145. rv = self.server.create_import_hook(
  146. hook_name,
  147. attrs,
  148. scr,
  149. overwrite=True)
  150. self.assertTrue(rv)
  151. attrs = {'freq': freq}
  152. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  153. self.assertEqual(rv, 0)
  154. attrs = {'enabled': 'True'}
  155. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  156. self.assertEqual(rv, 0)
  157. self.check_next_occurances(2, freq, hook_run_time, True)
  158. self.server.log_match(msg_expected, interval=1)
  159. def test_sp_hook_long_run(self):
  160. """
  161. Submit a hook that runs longer than the frequency set by the hook and
  162. see if the hook starts at the next subsequent freq interval.
  163. in this case hook runs for 20 seconds and freq is 6. So if a hook
  164. starts at time 'x' then it's next occurance should be at 'x +24'.
  165. """
  166. hook_name = "long_hook"
  167. freq = 6
  168. hook_run_time = 20
  169. scr = self.create_hook(True, hook_run_time)
  170. attrs = {'event': "periodic"}
  171. rv = self.server.create_import_hook(
  172. hook_name,
  173. attrs,
  174. scr,
  175. overwrite=True)
  176. self.assertTrue(rv)
  177. attrs = {'freq': freq}
  178. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  179. self.assertEqual(rv, 0)
  180. attrs = {'enabled': 'True'}
  181. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  182. self.assertEqual(rv, 0)
  183. self.check_next_occurances(2, freq, hook_run_time, True)
  184. def test_sp_hook_aborts_after_short_alarm(self):
  185. """
  186. Submit a hook that runs longer than the frequency set by the hook and
  187. see if the hook starts at the next subsequent freq interval.
  188. in this case hook runs for 20 seconds and freq is 15 and alarm is 12.
  189. So if a hook starts at time 'x' then it's next occurance should be
  190. at 'x +15' because alarm is going to kill it at 12th second of it's
  191. run.
  192. """
  193. hook_name = "long_hook"
  194. freq = 15
  195. alarm = 12
  196. hook_run_time = alarm
  197. scr = self.create_hook(True, 20)
  198. attrs = {'event': "periodic"}
  199. rv = self.server.create_import_hook(
  200. hook_name,
  201. attrs,
  202. scr,
  203. overwrite=True)
  204. self.assertTrue(rv)
  205. attrs = {'freq': freq, 'alarm': alarm}
  206. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  207. self.assertEqual(rv, 0)
  208. attrs = {'enabled': 'True'}
  209. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  210. self.assertEqual(rv, 0)
  211. self.check_next_occurances(2, freq, hook_run_time, False)
  212. def test_sp_hook_aborts_after_long_alarm(self):
  213. """
  214. Submit a hook that runs longer than the frequency set by the hook and
  215. see if the hook starts at the next subsequent freq interval.
  216. in this case hook runs for 20 seconds and freq is 12 and alarm is 15.
  217. So if a hook starts at time 'x' then it's next occurance should be
  218. at 'x +12' but it is going to run and get killed due to an alarm at
  219. x +15 and then again start execution at x+24.
  220. """
  221. hook_name = "long_hook"
  222. freq = 12
  223. alarm = 15
  224. hook_run_time = alarm
  225. scr = self.create_hook(True, 20)
  226. attrs = {'event': "periodic"}
  227. rv = self.server.create_import_hook(
  228. hook_name,
  229. attrs,
  230. scr,
  231. overwrite=True)
  232. self.assertTrue(rv)
  233. attrs = {'freq': freq, 'alarm': alarm}
  234. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  235. self.assertEqual(rv, 0)
  236. attrs = {'enabled': 'True'}
  237. rv = self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  238. self.assertEqual(rv, 0)
  239. self.check_next_occurances(2, freq, hook_run_time, False)
  240. def test_sp_with_queuejob(self):
  241. """
  242. This test case checks that periodic and queuejob
  243. event can be set for the same hook
  244. """
  245. events = "periodic,queuejob"
  246. hook_name = "TestHook"
  247. hook_attrib = {'event': events, 'freq': 100}
  248. scr = self.create_hook(True, 10)
  249. retval = self.server.create_import_hook(hook_name,
  250. hook_attrib,
  251. scr,
  252. overwrite=True)
  253. self.assertTrue(retval)
  254. attrs = {'enabled': 'True'}
  255. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  256. job = Job(TEST_USER1, attrs={ATTR_l: 'select=1:ncpus=1',
  257. ATTR_l: 'walltime=1:00:00'})
  258. jid = self.server.submit(job)
  259. self.server.log_match(self.start_msg, interval=3)
  260. self.server.log_match(self.end_msg, interval=3)
  261. self.server.expect(JOB, {'job_state': 'R'}, id=jid)
  262. def test_alarm_more_than_freq(self):
  263. """
  264. Test when alarm is more than freq. Ensure multiple
  265. instances do not get launched
  266. """
  267. hook_name = "medium_hook"
  268. scr = self.create_hook(accept=True, sleep_time=10)
  269. attrs = {'event': 'periodic', 'alarm': 15, 'freq': 5}
  270. self.server.create_import_hook(hook_name, attrs, scr, overwrite=True)
  271. self.check_next_occurances(2, freq=5, hook_run_time=10,
  272. check_for_hook_end=True)
  273. def test_check_for_negative_freq(self):
  274. """
  275. Check for the correct messages thrown if negative values of freq is set
  276. """
  277. hook_name = "med_hook"
  278. attrs = {'event': "periodic", 'freq': "0"}
  279. match_str1 = "set_hook_freq: freq value '0'"
  280. match_str1 += " of a hook must be > 0"
  281. try:
  282. self.server.create_hook(hook_name, attrs)
  283. except PbsManagerError as e:
  284. self.assertIn(match_str1, e.msg[0])
  285. self.logger.info('Expected error: ' + match_str1)
  286. else:
  287. msg = "Able to set freq to zero"
  288. self.assertTrue(False, msg)
  289. attrs = {'enabled': "False", 'event': "periodic", 'freq': '120'}
  290. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name, expect=True)
  291. attrs = {'freq': "-1"}
  292. match_str1 = "set_hook_freq: freq value '-1'"
  293. match_str1 += " of a hook must be > 0"
  294. try:
  295. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  296. except PbsManagerError as e:
  297. self.assertIn(match_str1, e.msg[0])
  298. self.logger.info('Expected error: ' + match_str1)
  299. else:
  300. msg = "Able to set freq to negative value"
  301. self.assertTrue(False, msg)
  302. @timeout('600')
  303. def test_with_other_hooks(self):
  304. """
  305. Test periodic hook works fine with other hooks
  306. """
  307. hook_name = "periodic_hook"
  308. freq = 30
  309. scr = self.create_hook(accept=True, sleep_time=25)
  310. attrs = {'event': "periodic", 'alarm': "28"}
  311. self.server.create_import_hook(hook_name, attrs, scr, overwrite=True)
  312. start_time = int(time.time())
  313. attrs = {'freq': 30, 'enabled': 'True'}
  314. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  315. expected_msg = "periodic hook started at "
  316. self.server.log_match(expected_msg, starttime=start_time,
  317. interval=(freq+1))
  318. self.check_next_occurances(count=1, freq=freq, hook_run_time=25,
  319. check_for_hook_end=False)
  320. hook_name = "exechost_periodic_hook3"
  321. freq = 8
  322. hook_run_time = 5
  323. scr = self.create_hook(True, sleep_time=5)
  324. attrs = {'event': "exechost_periodic", 'alarm': "7", 'freq': "8",
  325. 'enabled': 'True'}
  326. self.server.create_import_hook(hook_name, attrs, scr)
  327. start_time = int(time.time())
  328. expected_msg = "periodic hook started at "
  329. self.mom.log_match(expected_msg, interval=(freq+1),
  330. starttime=start_time)
  331. expected_msg = "periodic hook ended at "
  332. self.mom.log_match(expected_msg, interval=(hook_run_time+1),
  333. starttime=start_time)
  334. def test_other_pbs_operations_work(self):
  335. """
  336. Test that when periodic hook is launched PBS operations do not get
  337. hampered
  338. """
  339. hook_name = "medium_hook"
  340. freq = 20
  341. scr = self.create_hook(accept=True, sleep_time=15)
  342. attrs = {'event': "periodic"}
  343. self.server.create_import_hook(hook_name, attrs, scr)
  344. attrs = {'alarm': "18", 'freq': freq}
  345. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  346. attrs = {'enabled': 'True'}
  347. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name)
  348. self.start_msg = ";periodic hook started at "
  349. self.check_next_occurances(count=1, freq=freq, hook_run_time=25,
  350. check_for_hook_end=False)
  351. a = {'Resource_List.select': '1:ncpus=1',
  352. 'Resource_List.walltime': 3}
  353. j = Job(TEST_USER, attrs=a)
  354. j.set_sleep_time(3)
  355. jid1 = self.server.submit(j)
  356. self.server.expect(JOB, {'job_state': 'R'}, id=jid1)
  357. self.server.expect(JOB, 'queue', id=jid1, op=UNSET, offset=3)
  358. self.server.log_match(jid1 + ";Exit_status=0")
  359. j1 = Job(TEST_USER)
  360. jid2 = self.server.submit(j1)
  361. self.server.delete(jid2)
  362. def test_set_as_non_admin(self):
  363. """
  364. Check for the correct messages thrown if user other
  365. than pbsadmin tries to set
  366. """
  367. hook_name = "medium_hook"
  368. host_name = str(self.server.hostname)
  369. self.server.create_hook(hook_name, attrs={'enabled': "False"})
  370. attrs = {'event': "periodic", 'freq': '120'}
  371. match_str1 = str(TEST_USER1) + "@" + host_name + \
  372. " is unauthorized to access hooks data from server " + host_name
  373. try:
  374. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name,
  375. runas=TEST_USER1)
  376. except PbsManagerError as e:
  377. self.assertIn(match_str1, e.msg[0])
  378. self.logger.info('Expected error: ' + match_str1)
  379. else:
  380. msg = "Able to create hook as other user"
  381. self.assertTrue(False, msg)
  382. self.server.manager(MGR_CMD_SET, HOOK, attrs, hook_name,
  383. expect=True)
  384. self.server.manager(MGR_CMD_LIST, HOOK, {'freq': '120'}, hook_name,
  385. expect=True)