ATLAS Offline Software
trigvalsteering-unit-tester.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 #
5 
6 import sys
7 import os
8 import re
9 import argparse
10 import subprocess
11 from TrigValTools.TrigValSteering import Common
12 
13 
14 def grep_errors(filename):
15  '''Grep for error messages but ignore missing reference errors and echo commands'''
16  error_pattern = '^.*ERROR.*$'
17  ignore_patterns = [
18  'Missing reference',
19  'echo "ERROR',
20  'echo ERROR',
21  ]
22  def filter_fn(msg):
23  for p in ignore_patterns:
24  if p in msg:
25  return False
26  return True
27 
28  lines = []
29  with open(filename) as f:
30  lines = re.findall(error_pattern, f.read(), re.MULTILINE)
31  return list(filter(filter_fn, lines))
32 
33 
34 def main():
35  log = Common.get_logger()
36 
37  parser = argparse.ArgumentParser(usage='%(prog)s PATH [PATH] ...')
38  parser.add_argument('paths',
39  metavar='PATH',
40  nargs='+',
41  help='Path containing test python scripts to test')
42  args = parser.parse_args()
43 
44  n_failed = 0
45  for p in args.paths:
46  if not os.path.exists(p):
47  log.error('Path %s does not exist', p)
48  return 1
49  tests = [f for f in os.listdir(p) if f.startswith('test_') and f.endswith('.py')]
50  log.info('Testing %d test scripts from path %s', len(tests), p)
51  for test in tests:
52  cmd = 'TRIGVALSTEERING_DRY_RUN=1 {:s}'.format(p+'/'+test)
53  log_file = '{:s}.unitTest.log'.format(test)
54  cmd += ' >{:s} 2>&1'.format(log_file)
55  ret_code = subprocess.call(cmd, shell=True)
56  failed = False
57  status_str = 'OK'
58  if ret_code != 0:
59  failed = True
60  status_str = 'FAILED WITH CODE {:d}'.format(ret_code)
61  errors = grep_errors(log_file)
62  if len(errors) > 0:
63  failed = True
64  status_str = 'ERROR IN LOG {:s}:'.format(log_file)
65  log.info('---- %s ---- %s', test, status_str)
66 
67  if failed:
68  n_failed += 1
69  if len(errors) > 0:
70  for msg in errors:
71  print(msg)
72  else:
73  log.error('Test failed but no ERROR messages found, printing full log below')
74  with open(log_file) as f:
75  for msg in f:
76  print(msg)
77 
78  return n_failed
79 
80 
81 if "__main__" in __name__:
82  sys.exit(main())
vtune_athena.format
format
Definition: vtune_athena.py:14
covarianceTool.filter
filter
Definition: covarianceTool.py:514
trigvalsteering-unit-tester.grep_errors
def grep_errors(filename)
Definition: trigvalsteering-unit-tester.py:14
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
Trk::open
@ open
Definition: BinningType.h:40
trigvalsteering-unit-tester.main
def main()
Definition: trigvalsteering-unit-tester.py:34
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28