pbs_runjob_hook.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 TestRunJobHook(TestFunctional):
  38. """
  39. This test suite tests the runjob hook
  40. """
  41. index_hook_script = """
  42. import pbs
  43. e = pbs.event()
  44. j = e.job
  45. pbs.logmsg(pbs.LOG_DEBUG, "job_id=%s" % j.id)
  46. pbs.logmsg(pbs.LOG_DEBUG, "sub_job_array_index=%s"
  47. % j.array_index)
  48. e.accept()
  49. """
  50. reject_hook_script = """
  51. import pbs
  52. pbs.event().reject("runjob hook rejected the job")
  53. """
  54. def test_array_sub_job_index(self):
  55. """
  56. Submit a job array. Check the array sub-job index value
  57. """
  58. hook_name = "runjob_hook"
  59. attrs = {'event': "runjob"}
  60. rv = self.server.create_import_hook(hook_name, attrs,
  61. self.index_hook_script,
  62. overwrite=True)
  63. self.assertTrue(rv)
  64. a = {'resources_available.ncpus': 3}
  65. self.server.manager(MGR_CMD_SET, NODE, a, self.mom.shortname)
  66. lower = 1
  67. upper = 3
  68. j1 = Job(TEST_USER)
  69. j1.set_attributes({ATTR_J: '%d-%d' % (lower, upper)})
  70. self.server.submit(j1)
  71. for i in range(lower, upper + 1):
  72. self.server.log_match("sub_job_array_index=%d" % (i),
  73. starttime=self.server.ctime)
  74. def test_normal_job_index(self):
  75. """
  76. Submit a normal job. Check the job index value which should be None
  77. """
  78. hook_name = "runjob_hook"
  79. attrs = {'event': "runjob"}
  80. rv = self.server.create_import_hook(hook_name, attrs,
  81. self.index_hook_script,
  82. overwrite=True)
  83. self.assertTrue(rv)
  84. j1 = Job(TEST_USER)
  85. self.server.submit(j1)
  86. self.server.log_match("sub_job_array_index=None",
  87. starttime=self.server.ctime)
  88. def test_reject_array_sub_job(self):
  89. """
  90. Test to check array subjobs,
  91. jobs should run after runjob hook enabled set to false.
  92. """
  93. hook_name = "runjob_hook"
  94. attrs = {'event': "runjob"}
  95. rv = self.server.create_import_hook(hook_name, attrs,
  96. self.reject_hook_script,
  97. overwrite=True)
  98. self.assertTrue(rv)
  99. a = {'resources_available.ncpus': 3}
  100. self.server.manager(MGR_CMD_SET, NODE, a, self.mom.shortname)
  101. j1 = Job(TEST_USER)
  102. j1.set_attributes({ATTR_J: '1-3'})
  103. jid = self.server.submit(j1)
  104. msg = "Not Running: PBS Error: runjob hook rejected the job"
  105. self.server.expect(JOB, {'job_state': 'Q', 'comment': msg}, id=jid)
  106. a = {'enabled': 'false'}
  107. self.server.manager(MGR_CMD_SET, HOOK, a, id=hook_name,
  108. expect=True, sudo=True)
  109. self.server.manager(MGR_CMD_SET, SERVER, {'scheduling': 'True'},
  110. expect=True)
  111. self.server.expect(JOB, {'job_state': 'B'}, id=jid)
  112. self.server.expect(JOB, {'job_state=R': 3}, count=True,
  113. id=jid, extend='t')