blob: 97dbfa2a2d1233ae0685a2e0a4488d485dce8580 [file] [log] [blame]
shenhanc60576f2013-02-19 21:11:00 +00001#!/usr/bin/python
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.
5 This is not intended to be run on command line.
6 To kick off a single dejagnu run, use chromeos/v14/dejagnu/run_dejagnu.py
7"""
8
9__author__ = 'shenhan@google.com (Han Shen)'
10
11import optparse
12import os
13from os import path
14import sys
15import setup_chromeos
16import build_tc
17
18from dejagnu import run_dejagnu
19from utils import command_executer
20from utils import email_sender
21
Luis Lozanof2a3ef42015-12-15 13:49:30 -080022
shenhanf844b432013-02-19 20:42:07 +000023class DejagnuAdapter(object):
24
25 # TODO(shenhan): move these to constants.py.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080026 _CHROMIUM_GCC_GIT = ('https://chromium.googlesource.com/'
27 'chromiumos/third_party/gcc.git')
28 _CHROMIUM_GCC_BRANCH = 'gcc.gnu.org/branches/google/gcc-4_7-mobile'
shenhanf844b432013-02-19 20:42:07 +000029
30 _cmd_exec = command_executer.GetCommandExecuter()
31
Luis Lozanof2a3ef42015-12-15 13:49:30 -080032 def __init__(self, board, remote, gcc_dir, chromeos_root, runtestflags,
33 cleanup):
shenhanf844b432013-02-19 20:42:07 +000034 self._board = board
35 self._remote = remote
36 self._gcc_dir = gcc_dir
37 self._chromeos_root = chromeos_root
shenhanc60576f2013-02-19 21:11:00 +000038 self._runtestflags = runtestflags
39 self._cleanup = cleanup
shenhanf844b432013-02-19 20:42:07 +000040
41 def SetupChromeOS(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 cmd = [setup_chromeos.__file__, '--dir=' + self._chromeos_root,
43 '--minilayout', '--jobs=8']
shenhanf844b432013-02-19 20:42:07 +000044 ret = setup_chromeos.Main(cmd)
45 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 raise Exception('Failed to checkout chromeos')
shenhanf844b432013-02-19 20:42:07 +000047 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080048 cmd = 'cd {0} && cros_sdk --download'.format(self._chromeos_root)
shenhanf844b432013-02-19 20:42:07 +000049 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
50 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 raise Exception('Failed to create chroot.')
shenhanf844b432013-02-19 20:42:07 +000052
53 def SetupBoard(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080054 cmd = './setup_board --board=' + self._board
shenhanf844b432013-02-19 20:42:07 +000055 ret = self._cmd_exec.ChrootRunCommand(self._chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080056 cmd,
57 terminated_timeout=4000)
shenhanf844b432013-02-19 20:42:07 +000058 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080059 raise Exception('Failed to setup board.')
shenhanf844b432013-02-19 20:42:07 +000060
61 def CheckoutGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080062 cmd = 'git clone {0} {1} && cd {1} && git checkout {2}'.format(
63 self._CHROMIUM_GCC_GIT, self._gcc_dir, self._CHROMIUM_GCC_BRANCH)
shenhanf844b432013-02-19 20:42:07 +000064
65 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=300)
66 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067 raise Exception('Failed to checkout gcc.')
shenhanf844b432013-02-19 20:42:07 +000068 ## Handle build_tc bug.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080069 cmd = ('touch {0}/gcc/config/arm/arm-tune.md ' + \
70 '{0}/gcc/config/arm/arm-tables.opt').format(self._gcc_dir)
shenhanf844b432013-02-19 20:42:07 +000071 ret = self._cmd_exec.RunCommand(cmd)
72
73 def BuildGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080074 build_gcc_args = [build_tc.__file__, '--board=' + self._board,
75 '--chromeos_root=' + self._chromeos_root,
76 '--gcc_dir=' + self._gcc_dir]
shenhanf844b432013-02-19 20:42:07 +000077 ret = build_tc.Main(build_gcc_args)
78 if ret:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 raise Exception('Building gcc failed.')
shenhanf844b432013-02-19 20:42:07 +000080
81 def CheckGCC(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080082 args = [run_dejagnu.__file__, '--board=' + self._board,
83 '--chromeos_root=' + self._chromeos_root,
84 '--mount=' + self._gcc_dir, '--remote=' + self._remote]
shenhanc60576f2013-02-19 21:11:00 +000085 if self._cleanup:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080086 args.append('--cleanup=' + self._cleanup)
shenhanc60576f2013-02-19 21:11:00 +000087 if self._runtestflags:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080088 args.append('--flags=' + self._runtestflags)
shenhanf844b432013-02-19 20:42:07 +000089 return run_dejagnu.Main(args)
90
shenhanc0c0b352013-02-19 21:11:36 +000091
92# Parse the output log to determine how many failures we have.
93# Return -1 if parse output log failed.
94def GetNumNewFailures(str):
95 if not str:
96 return 0
97 start_counting = False
98 n_failures = 0
99 for l in str.splitlines():
100 print l
101 if not start_counting and 'Build results not in the manifest' in l:
102 start_counting = True
103 elif start_counting and l and (
104 l.find('UNRESOLVED:') == 0 or l.find('FAIL:') == 0 or \
105 l.find('XFAIL:') == 0 or l.find('XPASS:') == 0):
106 n_failures = n_failures + 1
107 if not start_counting:
108 return -1
109 return n_failures
110
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800111
shenhanc60576f2013-02-19 21:11:00 +0000112# Do not throw any exception in this function!
shenhanf844b432013-02-19 20:42:07 +0000113def EmailResult(result):
shenhane0a6d8a2013-02-19 20:42:29 +0000114 email_to = ['c-compiler-chrome@google.com']
115 email_from = ['dejagnu-job@google.com']
shenhanf844b432013-02-19 20:42:07 +0000116 if len(result) == 4:
117 subject = 'Job failed: dejagnu test didn\'t finish'
118 email_text = 'Job failed prematurely, check exception below.\n' + \
119 result[3]
120 elif result[0]:
121 subject = 'Job finished: dejagnu test failed'
shenhanc0c0b352013-02-19 21:11:36 +0000122 num_new_failures = GetNumNewFailures(result[1])
123 if num_new_failures >= 0:
124 summary = '{0} new fail(s), check log below.'.format(num_new_failures)
125 else:
126 summary = 'At least 1 new fail found, check log below.'
127 email_text = summary + \
128 ('\nStdout ====\n'
129 '{0}\n'
130 '\nStderr ===\n'
131 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000132 else:
133 subject = 'Job finished: dejagnu test passed'
134 email_text = ('Cool! No new fail found.\n'
135 '\nStdout ====\n'
136 '{0}\n'
137 '\nStderr ====\n'
138 '{1}\n').format(result[1], result[2])
shenhanf844b432013-02-19 20:42:07 +0000139
140 try:
141 email_sender.EmailSender().SendEmail(email_to, subject, email_text)
142 print 'Email sent.'
143 except Exception as e:
144 # Do not propagate this email sending exception, you want to email an
145 # email exception? Just log it on console.
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 print('Sending email failed - {0}'
147 'Subject: {1}'
148 'Text: {2}').format(
149 str(e), subject, email_text)
shenhanf844b432013-02-19 20:42:07 +0000150
151
152def ProcessArguments(argv):
153 """Processing script arguments."""
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800154 parser = optparse.OptionParser(
155 description=('This script is used by nightly client to test gcc. '
156 'DO NOT run it unless you know what you are doing.'),
shenhanf844b432013-02-19 20:42:07 +0000157 usage='test_gcc_dejagnu.py options')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800158 parser.add_option('-b',
159 '--board',
160 dest='board',
shenhanf844b432013-02-19 20:42:07 +0000161 help=('Required. Specify board type. For example '
162 '\'lumpy\' and \'daisy\''))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800163 parser.add_option('-r',
164 '--remote',
165 dest='remote',
shenhanf844b432013-02-19 20:42:07 +0000166 help=('Required. Specify remote board address'))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800167 parser.add_option('-g',
168 '--gcc_dir',
169 dest='gcc_dir',
170 default='gcc.live',
shenhanf844b432013-02-19 20:42:07 +0000171 help=('Optional. Specify gcc checkout directory.'))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800172 parser.add_option('-c',
173 '--chromeos_root',
174 dest='chromeos_root',
shenhanf844b432013-02-19 20:42:07 +0000175 default='chromeos.live',
176 help=('Optional. Specify chromeos checkout directory.'))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800177 parser.add_option('--cleanup',
178 dest='cleanup',
179 default=None,
shenhanc60576f2013-02-19 21:11:00 +0000180 help=('Optional. Do cleanup after the test.'))
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800181 parser.add_option('--runtestflags',
182 dest='runtestflags',
183 default=None,
shenhanc60576f2013-02-19 21:11:00 +0000184 help=('Optional. Options to RUNTESTFLAGS env var '
185 'while invoking make check. '
186 '(Mainly used for testing purpose.)'))
shenhanf844b432013-02-19 20:42:07 +0000187
188 options, args = parser.parse_args(argv)
189
190 if not options.board or not options.remote:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800191 raise Exception('--board and --remote are mandatory options.')
shenhanf844b432013-02-19 20:42:07 +0000192
193 return options
194
shenhanc60576f2013-02-19 21:11:00 +0000195
shenhanf844b432013-02-19 20:42:07 +0000196def Main(argv):
197 opt = ProcessArguments(argv)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800198 adapter = DejagnuAdapter(opt.board, opt.remote, opt.gcc_dir,
199 opt.chromeos_root, opt.runtestflags, opt.cleanup)
shenhanf844b432013-02-19 20:42:07 +0000200 try:
shenhanf844b432013-02-19 20:42:07 +0000201 adapter.SetupChromeOS()
202 adapter.SetupBoard()
203 adapter.CheckoutGCC()
204 adapter.BuildGCC()
205 ret = adapter.CheckGCC()
206 except Exception as e:
207 print e
208 ret = (1, '', '', str(e))
209 finally:
210 EmailResult(ret)
211 return ret
212
213
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800214if __name__ == '__main__':
shenhanf844b432013-02-19 20:42:07 +0000215 retval = Main(sys.argv)
216 sys.exit(retval[0])