pbs_hook_debug_input.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import os
  38. import fnmatch
  39. from ptl.utils.pbs_logutils import PBSLogUtils
  40. class TestHookDebugInput(TestFunctional):
  41. """
  42. Tests related to hook debug input files
  43. """
  44. def setUp(self):
  45. TestFunctional.setUp(self)
  46. if not hasattr(self, 'server_hooks_tmp_dir'):
  47. self.server_hooks_tmp_dir = \
  48. os.path.join(self.server.pbs_conf['PBS_HOME'],
  49. 'server_priv', 'hooks', 'tmp')
  50. def remove_files_match(self, pattern):
  51. """
  52. Remove hook debug files in hooks/tmp folder that
  53. match pattern
  54. """
  55. for item in self.du.listdir(path=self.server_hooks_tmp_dir, sudo=True):
  56. if fnmatch.fnmatch(item, pattern):
  57. self.du.rm(path=item, sudo=True)
  58. # Check if the file was removed
  59. ret = self.du.isfile(path=item, sudo=True)
  60. self.assertFalse(ret)
  61. def match_queue_name_in_input_file(self, input_file_pattern, qname):
  62. """
  63. Assert that qname appears in the hook debug input file
  64. that matches input_file_pattern
  65. """
  66. input_file = None
  67. for item in self.du.listdir(path=self.server_hooks_tmp_dir, sudo=True):
  68. if fnmatch.fnmatch(item, input_file_pattern):
  69. input_file = item
  70. break
  71. self.assertTrue(input_file is not None)
  72. with PBSLogUtils().open_log(input_file, sudo=True) as f:
  73. search_str = 'pbs.event().job.queue=%s' % qname
  74. self.assertTrue(search_str in f.read())
  75. self.remove_files_match(input_file_pattern)
  76. def test_queuejob_hook_debug_input_has_queue_name(self):
  77. """
  78. Test that user requested queue name is written to
  79. queuejob hook debug input file
  80. """
  81. hook_name = "queuejob_debug"
  82. hook_body = ("import pbs\n"
  83. "pbs.event().accept()")
  84. attr = {'enabled': 'true', 'event': 'queuejob', 'debug': 'true'}
  85. self.server.create_import_hook(hook_name, attr, hook_body)
  86. new_queue = 'happyq'
  87. attr = {ATTR_qtype: 'execution', ATTR_enable: 'True'}
  88. self.server.manager(MGR_CMD_CREATE, QUEUE, attr, id=new_queue)
  89. input_file_pattern = os.path.join(self.server_hooks_tmp_dir,
  90. 'hook_queuejob_%s*.in' % hook_name)
  91. self.remove_files_match(input_file_pattern)
  92. j1 = Job(TEST_USER)
  93. self.server.submit(j1)
  94. self.match_queue_name_in_input_file(input_file_pattern,
  95. self.server.default_queue)
  96. attr = {ATTR_queue: new_queue}
  97. j2 = Job(TEST_USER, attrs=attr)
  98. self.server.submit(j2)
  99. self.match_queue_name_in_input_file(input_file_pattern, new_queue)