blob: 37640710637f3d8a1c37c7d5f7546ce67d1658e8 [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/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):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080044 cmd = [setup_chromeos.__file__, '--dir=' + self._chromeos_root,
45 '--minilayout', '--jobs=8']
shenhanf844b432013-02-19 20:42:07 +000046 ret = setup_chromeos.Main(cmd)
47 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080048 raise Exception('Failed to checkout chromeos')
shenhanf844b432013-02-19 20:42:07 +000049 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080050 cmd = 'cd {0} && cros_sdk --download'.format(self._chromeos_root)
shenhanf844b432013-02-19 20:42:07 +000051 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
52 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080053 raise Exception('Failed to create chroot.')
shenhanf844b432013-02-19 20:42:07 +000054
55 def SetupBoard(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 cmd = './setup_board --board=' + self._board
shenhanf844b432013-02-19 20:42:07 +000057 ret = self._cmd_exec.ChrootRunCommand(self._chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080058 cmd,
59 terminated_timeout=4000)
shenhanf844b432013-02-19 20:42:07 +000060 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080061 raise Exception('Failed to setup board.')
shenhanf844b432013-02-19 20:42:07 +000062
63 def CheckoutGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080064 cmd = 'git clone {0} {1} && cd {1} && git checkout {2}'.format(
65 self._CHROMIUM_GCC_GIT, self._gcc_dir, self._CHROMIUM_GCC_BRANCH)
shenhanf844b432013-02-19 20:42:07 +000066
67 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=300)
68 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080069 raise Exception('Failed to checkout gcc.')
shenhanf844b432013-02-19 20:42:07 +000070 ## Handle build_tc bug.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080071 cmd = ('touch {0}/gcc/config/arm/arm-tune.md ' + \
72 '{0}/gcc/config/arm/arm-tables.opt').format(self._gcc_dir)
shenhanf844b432013-02-19 20:42:07 +000073 ret = self._cmd_exec.RunCommand(cmd)
74
75 def BuildGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080076 build_gcc_args = [build_tc.__file__, '--board=' + self._board,
77 '--chromeos_root=' + self._chromeos_root,
78 '--gcc_dir=' + self._gcc_dir]
shenhanf844b432013-02-19 20:42:07 +000079 ret = build_tc.Main(build_gcc_args)
80 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 raise Exception('Building gcc failed.')
shenhanf844b432013-02-19 20:42:07 +000082
83 def CheckGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080084 args = [run_dejagnu.__file__, '--board=' + self._board,
85 '--chromeos_root=' + self._chromeos_root,
86 '--mount=' + self._gcc_dir, '--remote=' + self._remote]
shenhanc60576f2013-02-19 21:11:00 +000087 if self._cleanup:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080088 args.append('--cleanup=' + self._cleanup)
shenhanc60576f2013-02-19 21:11:00 +000089 if self._runtestflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080090 args.append('--flags=' + self._runtestflags)
shenhanf844b432013-02-19 20:42:07 +000091 return run_dejagnu.Main(args)
92
shenhanc0c0b352013-02-19 21:11:36 +000093
94# Parse the output log to determine how many failures we have.
95# Return -1 if parse output log failed.
Caroline Tice88272d42016-01-13 09:48:29 -080096def GetNumNewFailures(input_str):
97 if not input_str:
shenhanc0c0b352013-02-19 21:11:36 +000098 return 0
99 start_counting = False
100 n_failures = 0
Caroline Tice88272d42016-01-13 09:48:29 -0800101 for l in input_str.splitlines():
102 print(l)
shenhanc0c0b352013-02-19 21:11:36 +0000103 if not start_counting and 'Build results not in the manifest' in l:
104 start_counting = True
105 elif start_counting and l and (
Caroline Tice88272d42016-01-13 09:48:29 -0800106 l.find('UNRESOLVED:') == 0 or l.find('FAIL:') == 0 or
shenhanc0c0b352013-02-19 21:11:36 +0000107 l.find('XFAIL:') == 0 or l.find('XPASS:') == 0):
108 n_failures = n_failures + 1
109 if not start_counting:
110 return -1
111 return n_failures
112
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800113
shenhanc60576f2013-02-19 21:11:00 +0000114# Do not throw any exception in this function!
shenhanf844b432013-02-19 20:42:07 +0000115def EmailResult(result):
shenhane0a6d8a2013-02-19 20:42:29 +0000116 email_to = ['c-compiler-chrome@google.com']
shenhanf844b432013-02-19 20:42:07 +0000117 if len(result) == 4:
118 subject = 'Job failed: dejagnu test didn\'t finish'
119 email_text = 'Job failed prematurely, check exception below.\n' + \
120 result[3]
121 elif result[0]:
122 subject = 'Job finished: dejagnu test failed'
shenhanc0c0b352013-02-19 21:11:36 +0000123 num_new_failures = GetNumNewFailures(result[1])
124 if num_new_failures >= 0:
125 summary = '{0} new fail(s), check log below.'.format(num_new_failures)
126 else:
127 summary = 'At least 1 new fail found, check log below.'
128 email_text = summary + \
129 ('\nStdout ====\n'
130 '{0}\n'
131 '\nStderr ===\n'
132 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000133 else:
134 subject = 'Job finished: dejagnu test passed'
135 email_text = ('Cool! No new fail found.\n'
136 '\nStdout ====\n'
137 '{0}\n'
138 '\nStderr ====\n'
139 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000140
141 try:
142 email_sender.EmailSender().SendEmail(email_to, subject, email_text)
Caroline Tice88272d42016-01-13 09:48:29 -0800143 print('Email sent.')
shenhanf844b432013-02-19 20:42:07 +0000144 except Exception as e:
145 # Do not propagate this email sending exception, you want to email an
146 # email exception? Just log it on console.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800147 print('Sending email failed - {0}'
148 'Subject: {1}'
149 'Text: {2}').format(
150 str(e), subject, email_text)
shenhanf844b432013-02-19 20:42:07 +0000151
152
153def ProcessArguments(argv):
154 """Processing script arguments."""
Caroline Tice88272d42016-01-13 09:48:29 -0800155 parser = argparse.ArgumentParser(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800156 description=('This script is used by nightly client to test gcc. '
157 'DO NOT run it unless you know what you are doing.'),
shenhanf844b432013-02-19 20:42:07 +0000158 usage='test_gcc_dejagnu.py options')
Caroline Tice88272d42016-01-13 09:48:29 -0800159 parser.add_argument('-b',
160 '--board',
161 dest='board',
162 help=('Required. Specify board type. For example '
163 '\'lumpy\' and \'daisy\''))
164 parser.add_argument('-r',
165 '--remote',
166 dest='remote',
167 help=('Required. Specify remote board address'))
168 parser.add_argument('-g',
169 '--gcc_dir',
170 dest='gcc_dir',
171 default='gcc.live',
172 help=('Optional. Specify gcc checkout directory.'))
173 parser.add_argument('-c',
174 '--chromeos_root',
175 dest='chromeos_root',
176 default='chromeos.live',
177 help=('Optional. Specify chromeos checkout directory.'))
178 parser.add_argument('--cleanup',
179 dest='cleanup',
180 default=None,
181 help=('Optional. Do cleanup after the test.'))
182 parser.add_argument('--runtestflags',
183 dest='runtestflags',
184 default=None,
185 help=('Optional. Options to RUNTESTFLAGS env var '
186 'while invoking make check. '
187 '(Mainly used for testing purpose.)'))
shenhanf844b432013-02-19 20:42:07 +0000188
Caroline Tice88272d42016-01-13 09:48:29 -0800189 options = parser.parse_args(argv[1:])
shenhanf844b432013-02-19 20:42:07 +0000190
191 if not options.board or not options.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800192 raise Exception('--board and --remote are mandatory options.')
shenhanf844b432013-02-19 20:42:07 +0000193
194 return options
195
shenhanc60576f2013-02-19 21:11:00 +0000196
shenhanf844b432013-02-19 20:42:07 +0000197def Main(argv):
198 opt = ProcessArguments(argv)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800199 adapter = DejagnuAdapter(opt.board, opt.remote, opt.gcc_dir,
200 opt.chromeos_root, opt.runtestflags, opt.cleanup)
shenhanf844b432013-02-19 20:42:07 +0000201 try:
shenhanf844b432013-02-19 20:42:07 +0000202 adapter.SetupChromeOS()
203 adapter.SetupBoard()
204 adapter.CheckoutGCC()
205 adapter.BuildGCC()
206 ret = adapter.CheckGCC()
207 except Exception as e:
Caroline Tice88272d42016-01-13 09:48:29 -0800208 print(e)
shenhanf844b432013-02-19 20:42:07 +0000209 ret = (1, '', '', str(e))
210 finally:
211 EmailResult(ret)
Caroline Tice88272d42016-01-13 09:48:29 -0800212
213 return ret
shenhanf844b432013-02-19 20:42:07 +0000214
215
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800216if __name__ == '__main__':
shenhanf844b432013-02-19 20:42:07 +0000217 retval = Main(sys.argv)
218 sys.exit(retval[0])