blob: 13feb4debba1355b961ad6a17efe57463d920eb3 [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
Aníbal Limón2e73a672018-05-07 14:52:49 -050064def filter_ptests(ptests, requested_ptests, exclude):
Aníbal Limón0d09fb82017-11-08 14:30:15 -060065 filter_ptests = []
66
Aníbal Limón2e73a672018-05-07 14:52:49 -050067 for ptest in exclude:
68 if ptest in ptests:
69 ptests.remove(ptest)
70
Aníbal Limón0d09fb82017-11-08 14:30:15 -060071 if not requested_ptests:
72 return ptests
73
74 for ptest_name in ptests:
75 if ptest_name in requested_ptests:
76 requested_ptests[ptest_name] = True
77 filter_ptests.append(ptest_name)
78
79 for request_ptest in requested_ptests.keys():
80 if not requested_ptests[request_ptest]:
81 print("ERROR: Ptest %s was requested and isn't available" %
82 request_ptest)
83 sys.exit(1)
84
85 return filter_ptests
86
87
88def check_ptest(ptest_dir, ptest_name, output_log):
89 status = 'pass'
90
91 try:
92 output = subprocess.check_call('ptest-runner -d %s %s' %
93 (ptest_dir, ptest_name), shell=True,
94 stderr=subprocess.STDOUT)
95 except subprocess.CalledProcessError:
96 status = 'fail'
97
98 with open(output_log, 'a+') as f:
99 f.write("%s %s\n" % (ptest_name, status))
100
101
102def main():
103 parser = argparse.ArgumentParser(description="LAVA/OE ptest script",
104 add_help=False)
105 parser.add_argument('-t', '--tests', action='store', nargs='*',
106 help='Ptests to run')
Aníbal Limón2e73a672018-05-07 14:52:49 -0500107 parser.add_argument('-e', '--exclude', action='store', nargs='*',
108 help='Ptests to exclude')
Aníbal Limón0d09fb82017-11-08 14:30:15 -0600109 parser.add_argument('-d', '--ptest-dir',
110 help='Directory where ptests are stored (optional)',
111 action='store')
112 parser.add_argument('-o', '--output-log',
113 help='File to output log (optional)', action='store',
114 default=OUTPUT_LOG)
115 parser.add_argument('-h', '--help', action='help',
116 default=argparse.SUPPRESS,
117 help='show this help message and exit')
118 args = parser.parse_args()
119
120 if args.ptest_dir:
121 ptest_dir = args.ptest_dir
122 else:
123 ptest_dir = get_ptest_dir()
124 if not ptest_dir:
125 print("ERROR: No ptest dir found\n")
126 return 1
127
128 ptests = get_available_ptests(ptest_dir)
129 if not ptests:
130 print("ERROR: No ptests found in dir: %s\n" % ptest_dir)
131 return 1
132
133 # filter empty strings caused by -t ""
134 tests = []
135 if args.tests:
136 tests = [x for x in args.tests if x]
137
Aníbal Limón2e73a672018-05-07 14:52:49 -0500138 # filter empty strings caused by -e ""
139 exclude = []
140 if args.exclude:
141 exclude = [e for e in args.exclude if e]
142
Aníbal Limón0d09fb82017-11-08 14:30:15 -0600143 required_ptests = dict.fromkeys(tests, False)
Aníbal Limón2e73a672018-05-07 14:52:49 -0500144 ptests_to_run = filter_ptests(ptests, required_ptests, exclude)
Aníbal Limón0d09fb82017-11-08 14:30:15 -0600145 for ptest_name in ptests_to_run:
146 check_ptest(ptest_dir, ptest_name, args.output_log)
147
148 return 0
149
150
151if __name__ == '__main__':
152 try:
153 ret = main()
154 except SystemExit as e:
155 ret = e.code
156 except:
157 ret = 1
158 import traceback
159 traceback.print_exc()
160
161 sys.exit(ret)