blob: 0edf2cb93a37a1947fc7f52b7160e57200ec5780 [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.
4
5"""Script adapter used by automation client for testing dejagnu.
6 This is not intended to be run on command line.
7 To kick off a single dejagnu run, use chromeos/v14/dejagnu/run_dejagnu.py
8"""
9
10__author__ = 'shenhan@google.com (Han Shen)'
11
12import optparse
13import os
14from os import path
15import sys
16import setup_chromeos
17import build_tc
18
19from dejagnu import run_dejagnu
20from utils import command_executer
21from utils import email_sender
22
23class DejagnuAdapter(object):
24
25 # TODO(shenhan): move these to constants.py.
26 _CHROMIUM_GCC_GIT="http://git.chromium.org/chromiumos/third_party/gcc.git"
27 _CHROMIUM_GCC_BRANCH="gcc.gnu.org/branches/google/gcc-4_7-mobile"
28
29 _cmd_exec = command_executer.GetCommandExecuter()
30
shenhanc0c0b352013-02-19 21:11:36 +000031 def __init__(self, board, remote, gcc_dir,
shenhanc60576f2013-02-19 21:11:00 +000032 chromeos_root, runtestflags, cleanup):
shenhanf844b432013-02-19 20:42:07 +000033 self._board = board
34 self._remote = remote
35 self._gcc_dir = gcc_dir
36 self._chromeos_root = chromeos_root
shenhanc60576f2013-02-19 21:11:00 +000037 self._runtestflags = runtestflags
38 self._cleanup = cleanup
shenhanf844b432013-02-19 20:42:07 +000039
40 def SetupChromeOS(self):
41 cmd = [setup_chromeos.__file__,
42 "--dir=" + self._chromeos_root, "--minilayout", "--jobs=8"]
43 ret = setup_chromeos.Main(cmd)
44 if ret:
45 raise Exception("Failed to checkout chromeos")
46 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
47 cmd = "cd {0} && cros_sdk --download".format(self._chromeos_root)
48 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
49 if ret:
50 raise Exception("Failed to create chroot.")
51
52 def SetupBoard(self):
53 cmd = "./setup_board --board=" + self._board
54 ret = self._cmd_exec.ChrootRunCommand(self._chromeos_root,
55 cmd, terminated_timeout=4000)
56 if ret:
57 raise Exception("Failed to setup board.")
58
59 def CheckoutGCC(self):
60 cmd = "git clone {0} {1} && cd {1} && git checkout {2}".format(
61 self._CHROMIUM_GCC_GIT, self._gcc_dir, self._CHROMIUM_GCC_BRANCH)
62
63 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=300)
64 if ret:
65 raise Exception("Failed to checkout gcc.")
66 ## Handle build_tc bug.
67 cmd = ("touch {0}/gcc/config/arm/arm-tune.md " + \
68 "{0}/gcc/config/arm/arm-tables.opt").format(self._gcc_dir)
69 ret = self._cmd_exec.RunCommand(cmd)
70
71 def BuildGCC(self):
72 build_gcc_args = [build_tc.__file__,
73 "--board=" + self._board,
74 "--chromeos_root=" + self._chromeos_root,
75 "--gcc_dir=" + self._gcc_dir]
76 ret = build_tc.Main(build_gcc_args)
77 if ret:
78 raise Exception("Building gcc failed.")
79
80 def CheckGCC(self):
81 args = [run_dejagnu.__file__,
82 "--board=" + self._board,
83 "--chromeos_root=" + self._chromeos_root,
84 "--mount=" + self._gcc_dir,
85 "--remote=" + self._remote]
shenhanc60576f2013-02-19 21:11:00 +000086 if self._cleanup:
87 args.append("--cleanup=" + self._cleanup)
88 if self._runtestflags:
89 args.append("--flags=" + self._runtestflags)
shenhanf844b432013-02-19 20:42:07 +000090 return run_dejagnu.Main(args)
91
shenhanc0c0b352013-02-19 21:11:36 +000092
93# Parse the output log to determine how many failures we have.
94# Return -1 if parse output log failed.
95def GetNumNewFailures(str):
96 if not str:
97 return 0
98 start_counting = False
99 n_failures = 0
100 for l in str.splitlines():
101 print l
102 if not start_counting and 'Build results not in the manifest' in l:
103 start_counting = True
104 elif start_counting and l and (
105 l.find('UNRESOLVED:') == 0 or l.find('FAIL:') == 0 or \
106 l.find('XFAIL:') == 0 or l.find('XPASS:') == 0):
107 n_failures = n_failures + 1
108 if not start_counting:
109 return -1
110 return n_failures
111
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.
146 print ('Sending email failed - {0}'
147 'Subject: {1}'
148 'Text: {2}').format(
149 str(e), subject, email_text)
150
151
152def ProcessArguments(argv):
153 """Processing script arguments."""
154 parser = optparse.OptionParser(description=(
155 'This script is used by nightly client to test gcc. '
156 'DO NOT run it unless you know what you are doing.'),
157 usage='test_gcc_dejagnu.py options')
158 parser.add_option('-b', '--board', dest='board',
159 help=('Required. Specify board type. For example '
160 '\'lumpy\' and \'daisy\''))
161 parser.add_option('-r', '--remote', dest='remote',
162 help=('Required. Specify remote board address'))
163 parser.add_option('-g', '--gcc_dir', dest='gcc_dir', default='gcc.live',
164 help=('Optional. Specify gcc checkout directory.'))
165 parser.add_option('-c', '--chromeos_root', dest='chromeos_root',
166 default='chromeos.live',
167 help=('Optional. Specify chromeos checkout directory.'))
shenhanc60576f2013-02-19 21:11:00 +0000168 parser.add_option('--cleanup', dest='cleanup', default=None,
169 help=('Optional. Do cleanup after the test.'))
170 parser.add_option('--runtestflags', dest='runtestflags', default=None,
171 help=('Optional. Options to RUNTESTFLAGS env var '
172 'while invoking make check. '
173 '(Mainly used for testing purpose.)'))
shenhanf844b432013-02-19 20:42:07 +0000174
175 options, args = parser.parse_args(argv)
176
177 if not options.board or not options.remote:
178 raise Exception("--board and --remote are mandatory options.")
179
180 return options
181
shenhanc60576f2013-02-19 21:11:00 +0000182
shenhanf844b432013-02-19 20:42:07 +0000183def Main(argv):
184 opt = ProcessArguments(argv)
shenhanc60576f2013-02-19 21:11:00 +0000185 adapter = DejagnuAdapter(
186 opt.board, opt.remote, opt.gcc_dir, opt.chromeos_root,
187 opt.runtestflags, opt.cleanup)
shenhanf844b432013-02-19 20:42:07 +0000188 try:
shenhanf844b432013-02-19 20:42:07 +0000189 adapter.SetupChromeOS()
190 adapter.SetupBoard()
191 adapter.CheckoutGCC()
192 adapter.BuildGCC()
193 ret = adapter.CheckGCC()
194 except Exception as e:
195 print e
196 ret = (1, '', '', str(e))
197 finally:
198 EmailResult(ret)
199 return ret
200
201
202if __name__ == "__main__":
203 retval = Main(sys.argv)
204 sys.exit(retval[0])