blob: a1c44dc93b6f422ab090d3a90658f6d81db6a5d9 [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/python2
2"""Script adapter used by automation client for testing dejagnu.
Yunlian Jiangdffb0a92013-12-20 14:10:16 -08003
Caroline Tice88272d42016-01-13 09:48:29 -08004 This is not intended to be run on command line.
5 To kick off a single dejagnu run, use ./dejagnu/run_dejagnu.py
6"""
7
8from __future__ import print_function
9
10import argparse
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080011import sys
12import setup_chromeos
13
14from dejagnu import gdb_dejagnu
Caroline Tice88272d42016-01-13 09:48:29 -080015from cros_utils import command_executer
16from cros_utils import email_sender
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080017
18
19class DejagnuAdapter(object):
Caroline Tice88272d42016-01-13 09:48:29 -080020 """Dejagnu Adapter class."""
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080021
Luis Lozanof2a3ef42015-12-15 13:49:30 -080022 def __init__(self, board, remote, gdb_dir, chromeos_root, cleanup):
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080023 self._board = board
24 self._remote = remote
25 self._gdb_dir = gdb_dir
26 self._chromeos_root = chromeos_root
27 self._cleanup = cleanup
28 self._cmd_exec = command_executer.GetCommandExecuter()
29
30 def SetupChromeOS(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080031 cmd = [setup_chromeos.__file__, '--dir=' + self._chromeos_root,
32 '--minilayout', '--jobs=8']
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080033 ret = setup_chromeos.Main(cmd)
34 if ret:
35 raise Exception('Failed to checkout chromeos')
36 ## Do cros_sdk and setup_board, otherwise build_tc in next step will fail.
37 cmd = 'cd {0} && cros_sdk --download'.format(self._chromeos_root)
38 ret = self._cmd_exec.RunCommand(cmd, terminated_timeout=9000)
39 if ret:
40 raise Exception('Failed to create chroot.')
41
42 def SetupBoard(self):
43 cmd = './setup_board --board=' + self._board
44 ret = self._cmd_exec.ChrootRunCommand(self._chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080045 cmd,
46 terminated_timeout=4000)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080047 if ret:
48 raise Exception('Failed to setup board.')
49
50 def CheckGDB(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 args = [gdb_dejagnu.__file__, '--board=' + self._board,
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080052 '--chromeos_root=' + self._chromeos_root,
Luis Lozanof2a3ef42015-12-15 13:49:30 -080053 '--mount=' + self._gdb_dir, '--remote=' + self._remote]
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080054 if self._cleanup:
55 args.append('--cleanup=' + self._cleanup)
56 return gdb_dejagnu.Main(args)
57
58
59# Parse the output log to determine how many failures we have.
60# Return -1 if parse output log failed.
Yunlian Jianga9c561a2014-01-02 13:51:15 -080061def GetNumNewFailures(result):
62 if not result:
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080063 return 0
Yunlian Jianga9c561a2014-01-02 13:51:15 -080064 return len(result)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080065
66
67# Do not throw any exception in this function!
68def EmailResult(result):
69 email_to = ['yunlian@google.com']
70 if len(result) == 4:
71 subject = 'Job failed: dejagnu test didn\'t finish'
Luis Lozanof2a3ef42015-12-15 13:49:30 -080072 email_text = (
73 'Job failed prematurely, check exception below.\n' + result[3])
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080074 elif result[0]:
75 subject = 'Job finished: dejagnu test failed'
76 num_new_failures = GetNumNewFailures(result[1])
77 if num_new_failures >= 0:
78 summary = '{0} new fail(s), check log below.'.format(num_new_failures)
79 else:
80 summary = 'At least 1 new fail found, check log below.'
Luis Lozanof2a3ef42015-12-15 13:49:30 -080081 email_text = (summary + ('\nStdout ====\n'
82 '{0}\n'
83 '\nStderr ===\n'
84 '{1}\n').format(result[1], result[2]))
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080085 else:
86 subject = 'Job finished: dejagnu test passed'
87 email_text = ('Cool! No new fail found.\n'
88 '\nStdout ====\n'
89 '{0}\n'
90 '\nStderr ====\n'
91 '{1}\n').format(result[1], result[2])
92
93 try:
94 email_sender.EmailSender().SendEmail(email_to, subject, email_text)
Caroline Tice88272d42016-01-13 09:48:29 -080095 print('Email sent.')
Yunlian Jiangdffb0a92013-12-20 14:10:16 -080096 except Exception as e:
97 # Do not propagate this email sending exception, you want to email an
98 # email exception? Just log it on console.
Luis Lozanof2a3ef42015-12-15 13:49:30 -080099 print('Sending email failed - {0}'
100 'Subject: {1}'
101 'Text: {2}').format(
102 str(e), subject, email_text)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800103
104
105def ProcessArguments(argv):
106 """Processing script arguments."""
Caroline Tice88272d42016-01-13 09:48:29 -0800107 parser = argparse.ArgumentParser(
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 description=('This script is used by nightly client to test gdb. '
109 'DO NOT run it unless you know what you are doing.'),
110 usage='test_gdb_dejagnu.py options')
Caroline Tice88272d42016-01-13 09:48:29 -0800111 parser.add_argument('-b',
112 '--board',
113 dest='board',
114 help=('Required. Specify board type. For example '
115 '\'lumpy\' and \'daisy\''))
116 parser.add_argument('-r',
117 '--remote',
118 dest='remote',
119 help=('Required. Specify remote board address'))
120 parser.add_argument('-g',
121 '--gdb_dir',
122 dest='gdb_dir',
123 default='',
124 help=('Optional. Specify gdb checkout directory.'))
125 parser.add_argument('-c',
126 '--chromeos_root',
127 dest='chromeos_root',
128 default='chromeos.live',
129 help=('Optional. Specify chromeos checkout directory.'))
130 parser.add_argument('--cleanup',
131 dest='cleanup',
132 default=None,
133 help=('Optional. Do cleanup after the test.'))
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800134
Caroline Tice88272d42016-01-13 09:48:29 -0800135 options = parser.parse_args(argv)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800136
137 if not options.board or not options.remote:
138 raise Exception('--board and --remote are mandatory options.')
139
140 return options
141
142
143def Main(argv):
144 opt = ProcessArguments(argv)
Caroline Tice88272d42016-01-13 09:48:29 -0800145 print(opt)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 adapter = DejagnuAdapter(opt.board, opt.remote, opt.gdb_dir,
147 opt.chromeos_root, opt.cleanup)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800148 try:
149 adapter.SetupChromeOS()
150 adapter.SetupBoard()
151 ret = adapter.CheckGDB()
152 except Exception as e:
Caroline Tice88272d42016-01-13 09:48:29 -0800153 print(e)
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800154 ret = (1, '', '', str(e))
155 finally:
156 EmailResult(ret)
Caroline Tice88272d42016-01-13 09:48:29 -0800157
158 return ret
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800159
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800160
161if __name__ == '__main__':
Caroline Tice88272d42016-01-13 09:48:29 -0800162 retval = Main(sys.argv[1:])
Yunlian Jiangdffb0a92013-12-20 14:10:16 -0800163 sys.exit(retval[0])