The Gaudi Framework  v36r1 (3e2fb5a8)
Run.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 
12 from __future__ import print_function
13 import importlib
14 import os
15 import sys
16 import xml.sax.saxutils as XSS
17 from GaudiTesting import BaseTest as GT
18 import logging
19 
20 # FIXME: module alias for backward compatibility
21 sys.modules['GaudiTest'] = GT
22 
23 
24 def basic_report(results):
25  '''
26  Report function taking the dictionary from BasicTest.run() and display
27  stdout and stderr from it.
28  '''
29  print('=== stdout ===')
30  print(results.get('stdout', ''))
31  print('=== stderr ===')
32  print(results.get('stderr', ''))
33  print('=== result ===')
34  print(results.get('Status'))
35  if results.get('Status') != 'passed' and 'Causes' in results:
36  print(' ', 'unexpected ' + ', '.join(results['Causes']))
37 
38 
39 def quiet_report(results):
40  '''
41  Do not report anything from the result of the test.
42  '''
43  pass
44 
45 
46 def ctest_report(results):
47  '''
48  Report function taking the dictionary from BasicTest.run() and report data
49  from it in a CTest-friendly way.
50  '''
51  # It's weird, I know, but it tells CTest not to cut the output.
52  print('CTEST_FULL_OUTPUT')
53  print(results.get('stdout', ''))
54  handler = {'Runtime Environment': lambda v: "<pre>%s</pre>" % '\n'.join('{0}={1}'.format(*item)
55  for item in sorted(v.items())),
56  'Causes': lambda v: 'unexpected ' + ', '.join(v)}
57 
58  def id_handler(v):
59  return str(v)
60 
61  ignore = set(['Status', 'Name', 'stdout', 'Exit Code'])
62  template = (
63  '<DartMeasurement type="text/string" name="{0}">{1}</DartMeasurement>')
64 
65  for key in results:
66  if key in ignore:
67  continue
68  hndlr = handler.get(key, id_handler)
69  data = XSS.escape(GT.sanitize_for_xml(hndlr(results[key])))
70  sys.stdout.write(template.format(key, data))
71 
72 
73 def pprint_report(results):
74  '''
75  Report function taking the dictionary from BasicTest.run() and print it with
76  the pprint module.
77  '''
78  from pprint import pprint
79  pprint(results)
80 
81 
82 def main():
83  '''
84  Main function of the script.
85  '''
86  from optparse import OptionParser, OptionGroup
87  parser = OptionParser()
88 
89  parser.add_option(
90  '--report',
91  action='store',
92  choices=[
93  n.replace('_report', '') for n in globals()
94  if n.endswith('_report')
95  ],
96  help='choose a report method [default %default]')
97  parser.add_option(
98  '--common-tmpdir',
99  action='store',
100  help='directory to be used as common temporary directory')
101  parser.add_option(
102  '-C',
103  '--workdir',
104  action='store',
105  help='directory to change to before starting the test')
106 
107  parser.add_option(
108  '--skip-return-code',
109  type='int',
110  help='return code to use to flag a test as skipped '
111  '[default %default]')
112 
113  verbosity_opts = OptionGroup(parser, 'Verbosity Level',
114  'set the verbosity level of messages')
115  verbosity_opts.add_option(
116  '--silent',
117  action='store_const',
118  dest='log_level',
119  const=logging.CRITICAL,
120  help='only critical error messages')
121  verbosity_opts.add_option(
122  '--quiet',
123  action='store_const',
124  dest='log_level',
125  const=logging.ERROR,
126  help='error messages')
127  verbosity_opts.add_option(
128  '--warning',
129  action='store_const',
130  dest='log_level',
131  const=logging.WARNING,
132  help='warning and error messages')
133  verbosity_opts.add_option(
134  '--verbose',
135  action='store_const',
136  dest='log_level',
137  const=logging.INFO,
138  help='progress information messages')
139  verbosity_opts.add_option(
140  '--debug',
141  action='store_const',
142  dest='log_level',
143  const=logging.DEBUG,
144  help='debugging messages')
145  parser.add_option_group(verbosity_opts)
146 
147  parser.set_defaults(
148  log_level=logging.WARNING,
149  report='basic',
150  workdir=os.curdir,
151  skip_return_code=0)
152 
153  opts, args = parser.parse_args()
154  if len(args) != 1:
155  parser.error('only one test allowed')
156  filename = args[0]
157 
158  logging.basicConfig(level=opts.log_level)
159 
160  if opts.common_tmpdir:
161  if not os.path.isdir(opts.common_tmpdir):
162  os.makedirs(opts.common_tmpdir)
163  GT.BaseTest._common_tmpdir = opts.common_tmpdir
164 
165  os.chdir(opts.workdir)
166 
167  # FIXME: whithout this, we get some spurious '\x1b[?1034' in the std out on SLC6
168  if "slc6" in (os.environ.get('BINARY_TAG', '')
169  or os.environ.get('CMTCONFIG', '')):
170  os.environ['TERM'] = 'dumb'
171 
172  # If running sanitizer builds, set LD_PRELOAD in environment
173  sanitizer = os.environ.get('PRELOAD_SANITIZER_LIB', '')
174  ld_preload = os.environ.get('LD_PRELOAD', '')
175  if sanitizer and sanitizer not in ld_preload:
176  if ld_preload:
177  os.environ['LD_PRELOAD'] = sanitizer + " " + ld_preload
178  else:
179  os.environ['LD_PRELOAD'] = sanitizer
180 
181  # Testing the file beginning with "Test" or if it is a qmt file and doing the test
182  logging.debug('processing %s', filename)
183  if filename.endswith('_test.py'):
184  indexFilePart = filename.rfind("/")
185  fileToImport = filename[indexFilePart + 1:]
186  sys.path.append(GT.RationalizePath(filename)[:-len(fileToImport) - 1])
187  imp = __import__(fileToImport[:-3])
188  fileToExec = imp.Test()
189  results = fileToExec.run()
190  elif filename.endswith(".qmt"):
191  # Check which class should be used to instantiate QMTests
192  # by default it is QMTTest but this can be overwritten via the environment
193  test_module = os.environ.get('GAUDI_QMTEST_MODULE',
194  'GaudiTesting.QMTTest')
195  test_class = os.environ.get('GAUDI_QMTEST_CLASS', 'QMTTest')
196  test_class = getattr(importlib.import_module(test_module), test_class)
197  fileToTest = test_class(filename)
198  results = fileToTest.run()
199 
200  report = globals()[opts.report + '_report']
201  report(results)
202 
203  if results.get('Status') == 'failed':
204  logging.debug('test failed: unexpected %s',
205  ', '.join(results['Causes']))
206  return int(results.get('Exit Code', '1'))
207  elif results.get('Status') == 'skipped':
208  return opts.skip_return_code
209  return 0
210 
211 
212 if __name__ == '__main__':
213  sys.exit(main())
GaudiTesting.Run.main
def main()
Definition: Run.py:82
GaudiTesting.Run.quiet_report
def quiet_report(results)
Definition: Run.py:39
GaudiTesting.Run.basic_report
def basic_report(results)
Definition: Run.py:24
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
GaudiTesting.Run.pprint_report
def pprint_report(results)
Definition: Run.py:73
GaudiTesting.Run.ctest_report
def ctest_report(results)
Definition: Run.py:46