blob: d7a21abc85da62820ae57b74bc550e00d2907bbd [file] [log] [blame]
Yunlian Jiangdffb0a92013-12-20 14:10:16 -08001#!/usr/bin/python
2
3import optparse
4import sys
5import setup_chromeos
6
7from dejagnu import gdb_dejagnu
8from utils import command_executer
9from utils import email_sender
10
11
12class DejagnuAdapter(object):
13
14 def __init__(self, board, remote, gdb_dir,
15 chromeos_root, cleanup):
16 self._board = board
17 self._remote = remote
18 self._gdb_dir = gdb_dir
19 self._chromeos_root = chromeos_root
20 self._cleanup = cleanup
21 self._cmd_exec = command_executer.GetCommandExecuter()
22
23 def SetupChromeOS(self):
24 cmd = [setup_chromeos.__file__,
25 '--dir=' + self._chromeos_root, '--minilayout', '--jobs=8']
26 ret = setup_chromeos.Main(cmd)
27 if ret:
28 raise Exception('Failed to checkout chromeos')
29 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
30 cmd = 'cd {0} && cros_sdk --download'.format(self._chromeos_root)
31 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
32 if ret:
33 raise Exception('Failed to create chroot.')
34
35 def SetupBoard(self):
36 cmd = './setup_board --board=' + self._board
37 ret = self._cmd_exec.ChrootRunCommand(self._chromeos_root,
38 cmd, terminated_timeout=4000)
39 if ret:
40 raise Exception('Failed to setup board.')
41
42 def CheckGDB(self):
43 args = [gdb_dejagnu.__file__,
44 '--board=' + self._board,
45 '--chromeos_root=' + self._chromeos_root,
46 '--mount=' + self._gdb_dir,
47 '--remote=' + self._remote]
48 if self._cleanup:
49 args.append('--cleanup=' + self._cleanup)
50 return gdb_dejagnu.Main(args)
51
52
53# Parse the output log to determine how many failures we have.
54# Return -1 if parse output log failed.
Yunlian Jianga9c561a2014-01-02 13:51:15 -080055def GetNumNewFailures(result):
56 if not result:
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080057 return 0
Yunlian Jianga9c561a2014-01-02 13:51:15 -080058 return len(result)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080059
60
61# Do not throw any exception in this function!
62def EmailResult(result):
63 email_to = ['yunlian@google.com']
64 if len(result) == 4:
65 subject = 'Job failed: dejagnu test didn\'t finish'
66 email_text = ('Job failed prematurely, check exception below.\n' +
67 result[3])
68 elif result[0]:
69 subject = 'Job finished: dejagnu test failed'
70 num_new_failures = GetNumNewFailures(result[1])
71 if num_new_failures >= 0:
72 summary = '{0} new fail(s), check log below.'.format(num_new_failures)
73 else:
74 summary = 'At least 1 new fail found, check log below.'
75 email_text = (summary +
76 ('\nStdout ====\n'
77 '{0}\n'
78 '\nStderr ===\n'
79 '{1}\n').format(result[1], result[2]))
80 else:
81 subject = 'Job finished: dejagnu test passed'
82 email_text = ('Cool! No new fail found.\n'
83 '\nStdout ====\n'
84 '{0}\n'
85 '\nStderr ====\n'
86 '{1}\n').format(result[1], result[2])
87
88 try:
89 email_sender.EmailSender().SendEmail(email_to, subject, email_text)
90 print 'Email sent.'
91 except Exception as e:
92 # Do not propagate this email sending exception, you want to email an
93 # email exception? Just log it on console.
94 print ('Sending email failed - {0}'
95 'Subject: {1}'
96 'Text: {2}').format(
97 str(e), subject, email_text)
98
99
100def ProcessArguments(argv):
101 """Processing script arguments."""
102 parser = optparse.OptionParser(description=(
103 'This script is used by nightly client to test gdb. '
104 'DO NOT run it unless you know what you are doing.'),
105 usage='test_gdb_dejagnu.py options')
106 parser.add_option('-b', '--board', dest='board',
107 help=('Required. Specify board type. For example '
108 '\'lumpy\' and \'daisy\''))
109 parser.add_option('-r', '--remote', dest='remote',
110 help=('Required. Specify remote board address'))
111 parser.add_option('-g', '--gdb_dir', dest='gdb_dir', default='',
112 help=('Optional. Specify gdb checkout directory.'))
113 parser.add_option('-c', '--chromeos_root', dest='chromeos_root',
114 default='chromeos.live',
115 help=('Optional. Specify chromeos checkout directory.'))
116 parser.add_option('--cleanup', dest='cleanup', default=None,
117 help=('Optional. Do cleanup after the test.'))
118
119 options, _ = parser.parse_args(argv)
120
121 if not options.board or not options.remote:
122 raise Exception('--board and --remote are mandatory options.')
123
124 return options
125
126
127def Main(argv):
128 opt = ProcessArguments(argv)
129 print opt
130 adapter = DejagnuAdapter(
131 opt.board, opt.remote, opt.gdb_dir, opt.chromeos_root,
132 opt.cleanup)
133 try:
134 adapter.SetupChromeOS()
135 adapter.SetupBoard()
136 ret = adapter.CheckGDB()
137 except Exception as e:
138 print e
139 ret = (1, '', '', str(e))
140 finally:
141 EmailResult(ret)
142 return ret
143
144if __name__ == '__main__':
145 retval = Main(sys.argv)
146 sys.exit(retval[0])