ptl_report_json.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. import re
  37. from ptl.utils.pbs_dshutils import DshUtils
  38. class PTLJsonData(object):
  39. """
  40. The intent of the class is to generate json format of PTL test data
  41. """
  42. def __init__(self, command):
  43. self.__du = DshUtils()
  44. self.__cmd = command
  45. def get_json(self, data, prev_data=None):
  46. """
  47. Method to generate test data in accordance to json schema
  48. :param data: dictionary of a test case details
  49. :type data: dict
  50. :param prev_data: dictionary of test run details that ran before
  51. the current test
  52. :type prev_data: dict
  53. :returns a formatted dictionary of the data
  54. """
  55. data_json = None
  56. if not prev_data:
  57. data_json = {
  58. 'command': self.__cmd,
  59. 'user': self.__du.get_current_user(),
  60. 'product_version': data['pbs_version'],
  61. 'run_id': data['start_time'].strftime('%s'),
  62. 'test_conf': {},
  63. 'machine_info': data['machinfo'],
  64. 'testsuites': {},
  65. 'additional_data': {},
  66. 'test_summary': {
  67. 'result_summary': {
  68. 'run': 0,
  69. 'succeeded': 0,
  70. 'failed': 0,
  71. 'errors': 0,
  72. 'skipped': 0,
  73. 'timedout': 0
  74. },
  75. 'test_start_time': str(data['start_time']),
  76. 'tests_with_failures': [],
  77. 'test_suites_with_failures': []
  78. }
  79. }
  80. if data['testparam']:
  81. for param in data['testparam'].split(','):
  82. par = param.split('=', 1)
  83. data_json['test_conf'][par[0]] = par[1]
  84. else:
  85. data_json = prev_data
  86. tsname = data['suite']
  87. tcname = data['testcase']
  88. if tsname not in data_json['testsuites']:
  89. data_json['testsuites'][tsname] = {
  90. 'module': data['module'],
  91. 'file': data['file'],
  92. 'testcases': {}
  93. }
  94. tsdoc = []
  95. if data['suitedoc']:
  96. tsdoc = (re.sub(r"[\t\n ]+", " ", data['suitedoc'])).strip()
  97. data_json['testsuites'][tsname]['docstring'] = tsdoc
  98. tcshort = {}
  99. tcdoc = []
  100. if data['testdoc']:
  101. tcdoc = (re.sub(r"[\t\n ]+", " ", data['testdoc'])).strip()
  102. tcshort['docstring'] = tcdoc
  103. if data['tags']:
  104. tcshort['tags'] = data['tags']
  105. tcshort['results'] = {
  106. 'status': data['status'],
  107. 'status_data': str(data['status_data']),
  108. 'duration': str(data['duration']),
  109. 'start_time': str(data['start_time']),
  110. 'end_time': str(data['end_time']),
  111. 'measurements': []
  112. }
  113. tcshort['requirements'] = {}
  114. if 'measurements' in data:
  115. tcshort['results']['measurements'] = data['measurements']
  116. data_json['testsuites'][tsname]['testcases'][tcname] = tcshort
  117. if 'additional_data' in data:
  118. data_json['additional_data'] = data['additional_data']
  119. data_json['test_summary']['test_end_time'] = str(data['end_time'])
  120. data_json['test_summary']['result_summary']['run'] += 1
  121. d_ts = data_json['test_summary']
  122. if data['status'] == 'PASS':
  123. d_ts['result_summary']['succeeded'] += 1
  124. elif data['status'] == 'SKIP':
  125. d_ts['result_summary']['skipped'] += 1
  126. elif data['status'] == 'TIMEDOUT':
  127. d_ts['result_summary']['timedout'] += 1
  128. d_ts['tests_with_failures'].append(data['testcase'])
  129. if data['suite'] not in d_ts['test_suites_with_failures']:
  130. d_ts['test_suites_with_failures'].append(data['suite'])
  131. elif data['status'] == 'ERROR':
  132. d_ts['result_summary']['errors'] += 1
  133. d_ts['tests_with_failures'].append(data['testcase'])
  134. if data['suite'] not in d_ts['test_suites_with_failures']:
  135. d_ts['test_suites_with_failures'].append(data['suite'])
  136. elif data['status'] == 'FAIL':
  137. d_ts['result_summary']['failed'] += 1
  138. d_ts['tests_with_failures'].append(data['testcase'])
  139. if data['suite'] not in d_ts['test_suites_with_failures']:
  140. d_ts['test_suites_with_failures'].append(data['suite'])
  141. return data_json