blob: ee38876864b14e26b3a2faacb79418c948b3f596 [file] [log] [blame]
Aníbal Limón0d09fb82017-11-08 14:30:15 -06001#!/usr/bin/env python3
2
3# LAVA/OE ptest script
4#
5# Copyright (C) 2017, Linaro Limited.
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License
9# as published by the Free Software Foundation; either version 2
10# of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21# Author: Aníbal Limón <anibal.limon@linaro.org>
22#
23
24import sys
25import argparse
26import subprocess
27import re
28import os
29
30OUTPUT_LOG = os.path.join(os.getcwd(), 'result.txt')
31
32
33def get_ptest_dir():
34 ptest_dirs = ['/usr/lib', '/usr/lib64', '/usr/lib32']
35
36 for pdir in ptest_dirs:
37 try:
38 ptests = subprocess.check_output('ptest-runner -l -d %s' %
39 pdir, shell=True,
40 stderr=subprocess.STDOUT)
41 except subprocess.CalledProcessError:
42 continue
43
44 return pdir
45
46 return None
47
48
49def get_available_ptests(ptest_dir):
50 output = subprocess.check_output('ptest-runner -l -d %s' %
51 ptest_dir, shell=True,
52 stderr=subprocess.STDOUT)
53
54 ptests = []
55 ptest_rex = re.compile("^(?P<ptest_name>.*)\t")
56 for line in output.decode('utf-8').split('\n'):
57 m = ptest_rex.search(line)
58 if m:
59 ptests.append(m.group('ptest_name'))
60
61 return ptests
62
63
64def filter_ptests(ptests, requested_ptests):
65 filter_ptests = []
66
67 if not requested_ptests:
68 return ptests
69
70 for ptest_name in ptests:
71 if ptest_name in requested_ptests:
72 requested_ptests[ptest_name] = True
73 filter_ptests.append(ptest_name)
74
75 for request_ptest in requested_ptests.keys():
76 if not requested_ptests[request_ptest]:
77 print("ERROR: Ptest %s was requested and isn't available" %
78 request_ptest)
79 sys.exit(1)
80
81 return filter_ptests
82
83
84def check_ptest(ptest_dir, ptest_name, output_log):
85 status = 'pass'
86
87 try:
88 output = subprocess.check_call('ptest-runner -d %s %s' %
89 (ptest_dir, ptest_name), shell=True,
90 stderr=subprocess.STDOUT)
91 except subprocess.CalledProcessError:
92 status = 'fail'
93
94 with open(output_log, 'a+') as f:
95 f.write("%s %s\n" % (ptest_name, status))
96
97
98def main():
99 parser = argparse.ArgumentParser(description="LAVA/OE ptest script",
100 add_help=False)
101 parser.add_argument('-t', '--tests', action='store', nargs='*',
102 help='Ptests to run')
103 parser.add_argument('-d', '--ptest-dir',
104 help='Directory where ptests are stored (optional)',
105 action='store')
106 parser.add_argument('-o', '--output-log',
107 help='File to output log (optional)', action='store',
108 default=OUTPUT_LOG)
109 parser.add_argument('-h', '--help', action='help',
110 default=argparse.SUPPRESS,
111 help='show this help message and exit')
112 args = parser.parse_args()
113
114 if args.ptest_dir:
115 ptest_dir = args.ptest_dir
116 else:
117 ptest_dir = get_ptest_dir()
118 if not ptest_dir:
119 print("ERROR: No ptest dir found\n")
120 return 1
121
122 ptests = get_available_ptests(ptest_dir)
123 if not ptests:
124 print("ERROR: No ptests found in dir: %s\n" % ptest_dir)
125 return 1
126
127 # filter empty strings caused by -t ""
128 tests = []
129 if args.tests:
130 tests = [x for x in args.tests if x]
131
132 required_ptests = dict.fromkeys(tests, False)
133 ptests_to_run = filter_ptests(ptests, required_ptests)
134 for ptest_name in ptests_to_run:
135 check_ptest(ptest_dir, ptest_name, args.output_log)
136
137 return 0
138
139
140if __name__ == '__main__':
141 try:
142 ret = main()
143 except SystemExit as e:
144 ret = e.code
145 except:
146 ret = 1
147 import traceback
148 traceback.print_exc()
149
150 sys.exit(ret)