blob: cd2e0cdedcb3a179f0c8bc9cc388e5f017ff3096 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env python2
shenhanf844b432013-02-19 20:42:07 +00002#
3# Copyright 2010 Google Inc. All Rights Reserved.
shenhanf844b432013-02-19 20:42:07 +00004"""Script adapter used by automation client for testing dejagnu.
Caroline Tice88272d42016-01-13 09:48:29 -08005
shenhanf844b432013-02-19 20:42:07 +00006 This is not intended to be run on command line.
Caroline Tice88272d42016-01-13 09:48:29 -08007 To kick off a single dejagnu run, use ./dejagnu/run_dejagnu.py
shenhanf844b432013-02-19 20:42:07 +00008"""
9
Caroline Tice88272d42016-01-13 09:48:29 -080010from __future__ import print_function
11
shenhanf844b432013-02-19 20:42:07 +000012__author__ = 'shenhan@google.com (Han Shen)'
13
Caroline Tice88272d42016-01-13 09:48:29 -080014import argparse
shenhanf844b432013-02-19 20:42:07 +000015import sys
16import setup_chromeos
17import build_tc
18
19from dejagnu import run_dejagnu
Caroline Tice88272d42016-01-13 09:48:29 -080020from cros_utils import command_executer
21from cros_utils import email_sender
shenhanf844b432013-02-19 20:42:07 +000022
Luis Lozanof2a3ef42015-12-15 13:49:30 -080023
shenhanf844b432013-02-19 20:42:07 +000024class DejagnuAdapter(object):
Caroline Tice88272d42016-01-13 09:48:29 -080025 """Dejagnu Adapter class"""
shenhanf844b432013-02-19 20:42:07 +000026
27 # TODO(shenhan): move these to constants.py.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080028 _CHROMIUM_GCC_GIT = ('https://chromium.googlesource.com/'
29 'chromiumos/third_party/gcc.git')
30 _CHROMIUM_GCC_BRANCH = 'gcc.gnu.org/branches/google/gcc-4_7-mobile'
shenhanf844b432013-02-19 20:42:07 +000031
32 _cmd_exec = command_executer.GetCommandExecuter()
33
Luis Lozanof2a3ef42015-12-15 13:49:30 -080034 def __init__(self, board, remote, gcc_dir, chromeos_root, runtestflags,
35 cleanup):
shenhanf844b432013-02-19 20:42:07 +000036 self._board = board
37 self._remote = remote
38 self._gcc_dir = gcc_dir
39 self._chromeos_root = chromeos_root
shenhanc60576f2013-02-19 21:11:00 +000040 self._runtestflags = runtestflags
41 self._cleanup = cleanup
shenhanf844b432013-02-19 20:42:07 +000042
43 def SetupChromeOS(self):
Caroline Ticef6ef4392017-04-06 17:16:05 -070044 cmd = [
45 setup_chromeos.__file__, '--dir=' + self._chromeos_root, '--minilayout',
46 '--jobs=8'
47 ]
shenhanf844b432013-02-19 20:42:07 +000048 ret = setup_chromeos.Main(cmd)
49 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -070050 raise RuntimeError('Failed to checkout chromeos')
shenhanf844b432013-02-19 20:42:07 +000051 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080052 cmd = 'cd {0} && cros_sdk --download'.format(self._chromeos_root)
shenhanf844b432013-02-19 20:42:07 +000053 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
54 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -070055 raise RuntimeError('Failed to create chroot.')
shenhanf844b432013-02-19 20:42:07 +000056
57 def SetupBoard(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080058 cmd = './setup_board --board=' + self._board
Caroline Ticef6ef4392017-04-06 17:16:05 -070059 ret = self._cmd_exec.ChrootRunCommand(
60 self._chromeos_root, cmd, terminated_timeout=4000)
shenhanf844b432013-02-19 20:42:07 +000061 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -070062 raise RuntimeError('Failed to setup board.')
shenhanf844b432013-02-19 20:42:07 +000063
64 def CheckoutGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080065 cmd = 'git clone {0} {1} && cd {1} && git checkout {2}'.format(
66 self._CHROMIUM_GCC_GIT, self._gcc_dir, self._CHROMIUM_GCC_BRANCH)
shenhanf844b432013-02-19 20:42:07 +000067
68 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=300)
69 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -070070 raise RuntimeError('Failed to checkout gcc.')
shenhanf844b432013-02-19 20:42:07 +000071 ## Handle build_tc bug.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080072 cmd = ('touch {0}/gcc/config/arm/arm-tune.md ' + \
73 '{0}/gcc/config/arm/arm-tables.opt').format(self._gcc_dir)
shenhanf844b432013-02-19 20:42:07 +000074 ret = self._cmd_exec.RunCommand(cmd)
75
76 def BuildGCC(self):
Caroline Ticef6ef4392017-04-06 17:16:05 -070077 build_gcc_args = [
78 build_tc.__file__, '--board=' + self._board,
79 '--chromeos_root=' + self._chromeos_root, '--gcc_dir=' + self._gcc_dir
80 ]
shenhanf844b432013-02-19 20:42:07 +000081 ret = build_tc.Main(build_gcc_args)
82 if ret:
Caroline Tice9099a782016-07-22 16:28:12 -070083 raise RuntimeError('Building gcc failed.')
shenhanf844b432013-02-19 20:42:07 +000084
85 def CheckGCC(self):
Caroline Ticef6ef4392017-04-06 17:16:05 -070086 args = [
87 run_dejagnu.__file__, '--board=' + self._board,
88 '--chromeos_root=' + self._chromeos_root, '--mount=' + self._gcc_dir,
89 '--remote=' + self._remote
90 ]
shenhanc60576f2013-02-19 21:11:00 +000091 if self._cleanup:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080092 args.append('--cleanup=' + self._cleanup)
shenhanc60576f2013-02-19 21:11:00 +000093 if self._runtestflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 args.append('--flags=' + self._runtestflags)
shenhanf844b432013-02-19 20:42:07 +000095 return run_dejagnu.Main(args)
96
shenhanc0c0b352013-02-19 21:11:36 +000097
98# Parse the output log to determine how many failures we have.
99# Return -1 if parse output log failed.
Caroline Tice88272d42016-01-13 09:48:29 -0800100def GetNumNewFailures(input_str):
101 if not input_str:
shenhanc0c0b352013-02-19 21:11:36 +0000102 return 0
103 start_counting = False
104 n_failures = 0
Caroline Tice88272d42016-01-13 09:48:29 -0800105 for l in input_str.splitlines():
106 print(l)
shenhanc0c0b352013-02-19 21:11:36 +0000107 if not start_counting and 'Build results not in the manifest' in l:
108 start_counting = True
Caroline Ticef6ef4392017-04-06 17:16:05 -0700109 elif start_counting and l and (l.find('UNRESOLVED:') == 0 or
110 l.find('FAIL:') == 0 or l.find('XFAIL:') == 0
111 or l.find('XPASS:') == 0):
shenhanc0c0b352013-02-19 21:11:36 +0000112 n_failures = n_failures + 1
113 if not start_counting:
114 return -1
115 return n_failures
116
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800117
shenhanc60576f2013-02-19 21:11:00 +0000118# Do not throw any exception in this function!
shenhanf844b432013-02-19 20:42:07 +0000119def EmailResult(result):
shenhane0a6d8a2013-02-19 20:42:29 +0000120 email_to = ['c-compiler-chrome@google.com']
shenhanf844b432013-02-19 20:42:07 +0000121 if len(result) == 4:
122 subject = 'Job failed: dejagnu test didn\'t finish'
123 email_text = 'Job failed prematurely, check exception below.\n' + \
124 result[3]
125 elif result[0]:
126 subject = 'Job finished: dejagnu test failed'
shenhanc0c0b352013-02-19 21:11:36 +0000127 num_new_failures = GetNumNewFailures(result[1])
128 if num_new_failures >= 0:
129 summary = '{0} new fail(s), check log below.'.format(num_new_failures)
130 else:
131 summary = 'At least 1 new fail found, check log below.'
132 email_text = summary + \
133 ('\nStdout ====\n'
134 '{0}\n'
135 '\nStderr ===\n'
136 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000137 else:
138 subject = 'Job finished: dejagnu test passed'
139 email_text = ('Cool! No new fail found.\n'
140 '\nStdout ====\n'
141 '{0}\n'
142 '\nStderr ====\n'
143 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000144
145 try:
146 email_sender.EmailSender().SendEmail(email_to, subject, email_text)
Caroline Tice88272d42016-01-13 09:48:29 -0800147 print('Email sent.')
shenhanf844b432013-02-19 20:42:07 +0000148 except Exception as e:
149 # Do not propagate this email sending exception, you want to email an
150 # email exception? Just log it on console.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800151 print('Sending email failed - {0}'
152 'Subject: {1}'
Caroline Ticef6ef4392017-04-06 17:16:05 -0700153 'Text: {2}').format(str(e), subject, email_text)
shenhanf844b432013-02-19 20:42:07 +0000154
155
156def ProcessArguments(argv):
157 """Processing script arguments."""
Caroline Tice88272d42016-01-13 09:48:29 -0800158 parser = argparse.ArgumentParser(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800159 description=('This script is used by nightly client to test gcc. '
160 'DO NOT run it unless you know what you are doing.'),
shenhanf844b432013-02-19 20:42:07 +0000161 usage='test_gcc_dejagnu.py options')
Caroline Ticef6ef4392017-04-06 17:16:05 -0700162 parser.add_argument(
163 '-b',
164 '--board',
165 dest='board',
166 help=('Required. Specify board type. For example '
167 '\'lumpy\' and \'daisy\''))
168 parser.add_argument(
169 '-r',
170 '--remote',
171 dest='remote',
172 help=('Required. Specify remote board address'))
173 parser.add_argument(
174 '-g',
175 '--gcc_dir',
176 dest='gcc_dir',
177 default='gcc.live',
178 help=('Optional. Specify gcc checkout directory.'))
179 parser.add_argument(
180 '-c',
181 '--chromeos_root',
182 dest='chromeos_root',
183 default='chromeos.live',
184 help=('Optional. Specify chromeos checkout directory.'))
185 parser.add_argument(
186 '--cleanup',
187 dest='cleanup',
188 default=None,
189 help=('Optional. Do cleanup after the test.'))
190 parser.add_argument(
191 '--runtestflags',
192 dest='runtestflags',
193 default=None,
194 help=('Optional. Options to RUNTESTFLAGS env var '
195 'while invoking make check. '
196 '(Mainly used for testing purpose.)'))
shenhanf844b432013-02-19 20:42:07 +0000197
Caroline Tice88272d42016-01-13 09:48:29 -0800198 options = parser.parse_args(argv[1:])
shenhanf844b432013-02-19 20:42:07 +0000199
200 if not options.board or not options.remote:
Caroline Tice9099a782016-07-22 16:28:12 -0700201 raise SyntaxError('--board and --remote are mandatory options.')
shenhanf844b432013-02-19 20:42:07 +0000202
203 return options
204
shenhanc60576f2013-02-19 21:11:00 +0000205
shenhanf844b432013-02-19 20:42:07 +0000206def Main(argv):
207 opt = ProcessArguments(argv)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800208 adapter = DejagnuAdapter(opt.board, opt.remote, opt.gcc_dir,
209 opt.chromeos_root, opt.runtestflags, opt.cleanup)
shenhanf844b432013-02-19 20:42:07 +0000210 try:
shenhanf844b432013-02-19 20:42:07 +0000211 adapter.SetupChromeOS()
212 adapter.SetupBoard()
213 adapter.CheckoutGCC()
214 adapter.BuildGCC()
215 ret = adapter.CheckGCC()
216 except Exception as e:
Caroline Tice88272d42016-01-13 09:48:29 -0800217 print(e)
shenhanf844b432013-02-19 20:42:07 +0000218 ret = (1, '', '', str(e))
219 finally:
220 EmailResult(ret)
Caroline Tice88272d42016-01-13 09:48:29 -0800221
222 return ret
shenhanf844b432013-02-19 20:42:07 +0000223
224
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800225if __name__ == '__main__':
shenhanf844b432013-02-19 20:42:07 +0000226 retval = Main(sys.argv)
227 sys.exit(retval[0])