pbs_passing_environment_variable.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_passing_environment_variable_via_qsub(TestFunctional):
  38. """
  39. Test to check passing environment variables via qsub
  40. """
  41. def create_and_submit_job(self, user=None, attribs=None, content=None,
  42. content_interactive=None, preserve_env=False):
  43. """
  44. create the job object and submit it to the server as 'user',
  45. attributes list 'attribs' script 'content' or 'content_interactive',
  46. and to 'preserve_env' if interactive job.
  47. """
  48. # A user=None value means job will be executed by current user
  49. # where the environment is set up
  50. if attribs is None:
  51. use_attribs = {}
  52. else:
  53. use_attribs = attribs
  54. retjob = Job(username=user, attrs=use_attribs)
  55. if content is not None:
  56. retjob.create_script(body=content)
  57. elif content_interactive is not None:
  58. retjob.interactive_script = content_interactive
  59. retjob.preserve_env = preserve_env
  60. return self.server.submit(retjob)
  61. def test_commas_in_custom_variable(self):
  62. """
  63. Submit a job with -v "var1='A,B,C,D'" and check that the value
  64. is passed correctly
  65. """
  66. a = {'Resource_List.select': '1:ncpus=1',
  67. 'Resource_List.walltime': 10}
  68. script = ['#PBS -v "var1=\'A,B,C,D\'"']
  69. script += ['env | grep var1']
  70. jid = self.create_and_submit_job(content=script)
  71. qstat = self.server.status(JOB, ATTR_o, id=jid)
  72. job_outfile = qstat[0][ATTR_o].split(':')[1]
  73. self.server.expect(JOB, 'queue', op=UNSET, id=jid, offset=10)
  74. job_output = ""
  75. with open(job_outfile, 'r') as f:
  76. job_output = f.read().strip()
  77. self.assertEqual(job_output, "var1=A,B,C,D")
  78. def test_passing_shell_function(self):
  79. """
  80. Define a shell function with new line characters and check that
  81. the function is passed correctly
  82. """
  83. os.environ['foo'] = '() { if [ /bin/true ]; then\necho hello;\nfi\n}'
  84. script = ['#PBS -V']
  85. script += ['env | grep -A 3 foo\n']
  86. script += ['foo\n']
  87. # Submit a job without hooks in the system
  88. jid = self.create_and_submit_job(content=script)
  89. qstat = self.server.status(JOB, ATTR_o, id=jid)
  90. job_outfile = qstat[0][ATTR_o].split(':')[1]
  91. self.server.expect(JOB, 'queue', op=UNSET, id=jid, offset=2)
  92. job_output = ""
  93. with open(job_outfile, 'r') as f:
  94. job_output = f.read().strip()
  95. match = 'foo=() { if [ /bin/true ]; then\n echo hello;\n fi\n}\nhello'
  96. self.assertEqual(job_output, match,
  97. msg="Environment variable foo content does "
  98. "not match original")
  99. def test_option_V_dfltqsubargs(self):
  100. """
  101. Test exporting environment variable when -V is enabled
  102. in default_qsub_arguments.
  103. """
  104. os.environ["SET_IN_SUBMISSION"] = "true"
  105. self.server.manager(MGR_CMD_SET, SERVER,
  106. {'default_qsub_arguments': '-V'})
  107. j = Job(self.du.get_current_user())
  108. jid = self.server.submit(j)
  109. self.server.expect(JOB, {'Variable_List': (MATCH_RE,
  110. 'SET_IN_SUBMISSION=true')},
  111. id=jid)
  112. def test_option_V_cmdline(self):
  113. """
  114. Test exporting environment variable when -V is passed
  115. through command line.
  116. """
  117. os.environ["SET_IN_SUBMISSION"] = "true"
  118. self.ATTR_V = 'Full_Variable_List'
  119. api_to_cli.setdefault(self.ATTR_V, 'V')
  120. a = {self.ATTR_V: None}
  121. j = Job(self.du.get_current_user(), attrs=a)
  122. jid = self.server.submit(j)
  123. self.server.expect(JOB, {'Variable_List': (MATCH_RE,
  124. 'SET_IN_SUBMISSION=true')},
  125. id=jid)
  126. def test_option_V_dfltqsubargs_qsub_daemon(self):
  127. """
  128. Test whether the changed value of the exported
  129. environment variable is reflected if the submitted job
  130. goes to qsub daemon.
  131. """
  132. self.server.manager(MGR_CMD_SET, SERVER,
  133. {'default_qsub_arguments': '-V'})
  134. os.environ["SET_IN_SUBMISSION"] = "true"
  135. j = Job(self.du.get_current_user())
  136. jid = self.server.submit(j)
  137. os.environ["SET_IN_SUBMISSION"] = "false"
  138. j1 = Job(self.du.get_current_user())
  139. jid1 = self.server.submit(j1)
  140. self.server.expect(JOB, {'Variable_List': (MATCH_RE,
  141. 'SET_IN_SUBMISSION=true')},
  142. id=jid)
  143. self.server.expect(JOB, {'Variable_List': (MATCH_RE,
  144. 'SET_IN_SUBMISSION=false')},
  145. id=jid1)