pbs_nodes_json.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 json
  38. class TestPbsnodes_json(TestFunctional):
  39. """
  40. Tests the json output of pbsnodes
  41. """
  42. def test_check_no_escape_json(self):
  43. """
  44. Test if the comment with no special characters is valid json
  45. """
  46. self.server.manager(MGR_CMD_SET, NODE,
  47. {'comment': '"hiha"'}, id=self.mom.shortname)
  48. cmd = os.path.join(self.server.pbs_conf['PBS_EXEC'],
  49. 'bin', 'pbsnodes') + ' -av -Fjson'
  50. n_out = self.du.run_cmd(self.server.hostname, cmd=cmd)['out']
  51. try:
  52. json.loads("\n".join(n_out))
  53. except ValueError:
  54. self.assertFalse(True, "Json failed to load")
  55. def test_check_newline_escape_json(self):
  56. """
  57. Test if the comment with newline is valid json
  58. """
  59. self.server.manager(MGR_CMD_SET, NODE,
  60. {'comment': '"hi\nha"'}, id=self.mom.shortname)
  61. cmd = os.path.join(self.server.pbs_conf['PBS_EXEC'],
  62. 'bin', 'pbsnodes') + ' -av -Fjson'
  63. n_out = self.du.run_cmd(self.server.hostname, cmd=cmd)['out']
  64. try:
  65. json.loads("\n".join(n_out))
  66. except ValueError:
  67. self.assertFalse(True, "Json failed to load")
  68. def test_check_tab_escape_json(self):
  69. """
  70. Test if the comment with tab is valid json
  71. """
  72. self.server.manager(MGR_CMD_SET, NODE,
  73. {'comment': '"hi\tha"'}, id=self.mom.shortname)
  74. cmd = os.path.join(self.server.pbs_conf['PBS_EXEC'],
  75. 'bin', 'pbsnodes') + ' -av -Fjson'
  76. n_out = self.du.run_cmd(self.server.hostname, cmd=cmd)['out']
  77. try:
  78. json.loads("\n".join(n_out))
  79. except ValueError:
  80. self.assertFalse(True, "Json failed to load")
  81. def test_check_quotes_escape_json(self):
  82. """
  83. Test if the comment with quotes is valid json
  84. """
  85. self.server.manager(MGR_CMD_SET, NODE,
  86. {'comment': '\'hi\"ha\''}, id=self.mom.shortname)
  87. cmd = os.path.join(self.server.pbs_conf['PBS_EXEC'],
  88. 'bin', 'pbsnodes') + ' -av -Fjson'
  89. n_out = self.du.run_cmd(self.server.hostname, cmd=cmd)['out']
  90. try:
  91. json.loads("\n".join(n_out))
  92. except ValueError:
  93. self.assertFalse(True, "Json failed to load")
  94. def test_check_reverse_solidus_escape_json(self):
  95. """
  96. Test if the comment with reverse solidus is valid json
  97. """
  98. self.server.manager(MGR_CMD_SET, NODE,
  99. {'comment': '"hi\\ha"'}, id=self.mom.shortname)
  100. cmd = os.path.join(self.server.pbs_conf['PBS_EXEC'],
  101. 'bin', 'pbsnodes') + ' -av -Fjson'
  102. n_out = self.du.run_cmd(self.server.hostname, cmd=cmd)['out']
  103. try:
  104. json.loads("\n".join(n_out))
  105. except ValueError:
  106. self.assertFalse(True, "Json failed to load")