blob: a5289452be805f1b42fc2a09b44d8e23d9dff3f1 [file] [log] [blame]
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001#!/usr/bin/python -u
Hung-Te Linf2f78f72012-02-08 19:27:11 +08002# -*- coding: utf-8 -*-
3#
Jon Salz37eccbd2012-05-25 16:06:52 +08004# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08005# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8'''
9The main factory flow that runs the factory test and finalizes a device.
10'''
11
Jon Salz0405ab52012-03-16 15:26:52 +080012import logging
13import os
Jon Salz73e0fd02012-04-04 11:46:38 +080014import Queue
Jon Salz77c151e2012-08-28 07:20:37 +080015import signal
Jon Salz0405ab52012-03-16 15:26:52 +080016import sys
Jon Salz0405ab52012-03-16 15:26:52 +080017import threading
18import time
19import traceback
Jon Salz258a40c2012-04-19 12:34:01 +080020import uuid
Jon Salzb10cf512012-08-09 17:29:21 +080021from xmlrpclib import Binary
Hung-Te Linf2f78f72012-02-08 19:27:11 +080022from collections import deque
23from optparse import OptionParser
Hung-Te Linf2f78f72012-02-08 19:27:11 +080024
Jon Salz0697cbf2012-07-04 15:14:04 +080025import factory_common # pylint: disable=W0611
jcliangcd688182012-08-20 21:01:26 +080026from cros.factory import event_log
27from cros.factory import system
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +080028from cros.factory.event_log import EventLog, FloatDigit
Tom Wai-Hong Tamd33723e2013-04-10 21:14:37 +080029from cros.factory.event_log_watcher import EventLogWatcher
jcliangcd688182012-08-20 21:01:26 +080030from cros.factory.goofy import test_environment
31from cros.factory.goofy import time_sanitizer
Jon Salz83591782012-06-26 11:09:58 +080032from cros.factory.goofy import updater
jcliangcd688182012-08-20 21:01:26 +080033from cros.factory.goofy.goofy_rpc import GoofyRPC
34from cros.factory.goofy.invocation import TestInvocation
35from cros.factory.goofy.prespawner import Prespawner
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +080036from cros.factory.goofy.system_log_manager import SystemLogManager
jcliangcd688182012-08-20 21:01:26 +080037from cros.factory.goofy.web_socket_manager import WebSocketManager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +080038from cros.factory.system.board import Board, BoardException
jcliangcd688182012-08-20 21:01:26 +080039from cros.factory.system.charge_manager import ChargeManager
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +080040from cros.factory.system.core_dump_manager import CoreDumpManager
Jon Salzb92c5112012-09-21 15:40:11 +080041from cros.factory.system import disk_space
jcliangcd688182012-08-20 21:01:26 +080042from cros.factory.test import factory
43from cros.factory.test import state
Jon Salz51528e12012-07-02 18:54:45 +080044from cros.factory.test import shopfloor
Jon Salz83591782012-06-26 11:09:58 +080045from cros.factory.test import utils
46from cros.factory.test.event import Event
47from cros.factory.test.event import EventClient
48from cros.factory.test.event import EventServer
jcliangcd688182012-08-20 21:01:26 +080049from cros.factory.test.factory import TestState
Dean Liao592e4d52013-01-10 20:06:39 +080050from cros.factory.tools.key_filter import KeyFilter
Jon Salz78c32392012-07-25 14:18:29 +080051from cros.factory.utils.process_utils import Spawn
Hung-Te Linf2f78f72012-02-08 19:27:11 +080052
53
Jon Salz2f757d42012-06-27 17:06:42 +080054CUSTOM_DIR = os.path.join(factory.FACTORY_PATH, 'custom')
Hung-Te Linf2f78f72012-02-08 19:27:11 +080055HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Chun-ta Lin279e7e92013-02-19 17:40:39 +080056CACHES_DIR = os.path.join(factory.get_state_root(), "caches")
Hung-Te Linf2f78f72012-02-08 19:27:11 +080057
Jon Salz8796e362012-05-24 11:39:09 +080058# File that suppresses reboot if present (e.g., for development).
59NO_REBOOT_FILE = '/var/log/factory.noreboot'
60
Jon Salz5c344f62012-07-13 14:31:16 +080061# Value for tests_after_shutdown that forces auto-run (e.g., after
62# a factory update, when the available set of tests might change).
63FORCE_AUTO_RUN = 'force_auto_run'
64
cychiang21886742012-07-05 15:16:32 +080065RUN_QUEUE_TIMEOUT_SECS = 10
66
Jon Salz758e6cc2012-04-03 15:47:07 +080067GOOFY_IN_CHROOT_WARNING = '\n' + ('*' * 70) + '''
68You are running Goofy inside the chroot. Autotests are not supported.
69
70To use Goofy in the chroot, first install an Xvnc server:
71
Jon Salz0697cbf2012-07-04 15:14:04 +080072 sudo apt-get install tightvncserver
Jon Salz758e6cc2012-04-03 15:47:07 +080073
74...and then start a VNC X server outside the chroot:
75
Jon Salz0697cbf2012-07-04 15:14:04 +080076 vncserver :10 &
77 vncviewer :10
Jon Salz758e6cc2012-04-03 15:47:07 +080078
79...and run Goofy as follows:
80
Jon Salz0697cbf2012-07-04 15:14:04 +080081 env --unset=XAUTHORITY DISPLAY=localhost:10 python goofy.py
Jon Salz758e6cc2012-04-03 15:47:07 +080082''' + ('*' * 70)
Jon Salz73e0fd02012-04-04 11:46:38 +080083suppress_chroot_warning = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +080084
85def get_hwid_cfg():
Jon Salz0697cbf2012-07-04 15:14:04 +080086 '''
87 Returns the HWID config tag, or an empty string if none can be found.
88 '''
89 if 'CROS_HWID' in os.environ:
90 return os.environ['CROS_HWID']
91 if os.path.exists(HWID_CFG_PATH):
92 with open(HWID_CFG_PATH, 'rt') as hwid_cfg_handle:
93 return hwid_cfg_handle.read().strip()
94 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +080095
96
97def find_test_list():
Jon Salz0697cbf2012-07-04 15:14:04 +080098 '''
99 Returns the path to the active test list, based on the HWID config tag.
Jon Salzfb615892013-02-01 18:04:35 +0800100
101 The algorithm is:
102
103 - Try $FACTORY/test_lists/active (the symlink reflecting the option chosen
104 in the UI).
105 - For each of $FACTORY/custom, $FACTORY/test_lists (and
106 autotest/site_tests/suite_Factory for backward compatibility):
107 - Try test_list_${hwid_cfg} (if hwid_cfg is set)
108 - Try test_list
109 - Try test_list.generic
Jon Salz0697cbf2012-07-04 15:14:04 +0800110 '''
Jon Salzfb615892013-02-01 18:04:35 +0800111 # If the 'active' symlink is present, that trumps everything else.
112 if os.path.lexists(factory.ACTIVE_TEST_LIST_SYMLINK):
113 return os.path.realpath(factory.ACTIVE_TEST_LIST_SYMLINK)
114
Jon Salz0697cbf2012-07-04 15:14:04 +0800115 hwid_cfg = get_hwid_cfg()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800116
Jon Salzfb615892013-02-01 18:04:35 +0800117 search_dirs = [CUSTOM_DIR, factory.TEST_LISTS_PATH]
Jon Salz4be56b02012-12-22 07:30:46 +0800118 if not utils.in_chroot():
119 # Also look in suite_Factory. For backward compatibility only;
120 # new boards should just put the test list in the "test_lists"
121 # directory.
122 search_dirs.insert(0, os.path.join(
123 os.path.dirname(factory.FACTORY_PATH),
124 'autotest', 'site_tests', 'suite_Factory'))
Jon Salz2f757d42012-06-27 17:06:42 +0800125
Jon Salzfb615892013-02-01 18:04:35 +0800126
127 search_files = []
Jon Salz0697cbf2012-07-04 15:14:04 +0800128 if hwid_cfg:
Jon Salzfb615892013-02-01 18:04:35 +0800129 search_files += [hwid_cfg]
130 search_files += ['test_list', 'test_list.generic']
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800131
Jon Salz0697cbf2012-07-04 15:14:04 +0800132 for d in search_dirs:
133 for f in search_files:
134 test_list = os.path.join(d, f)
135 if os.path.exists(test_list):
136 return test_list
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800137
Jon Salz0697cbf2012-07-04 15:14:04 +0800138 logging.warn('Cannot find test lists named any of %s in any of %s',
139 search_files, search_dirs)
140 return None
Jon Salz73e0fd02012-04-04 11:46:38 +0800141
Jon Salzfb615892013-02-01 18:04:35 +0800142
Jon Salz73e0fd02012-04-04 11:46:38 +0800143_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800144
145class Goofy(object):
Jon Salz0697cbf2012-07-04 15:14:04 +0800146 '''
147 The main factory flow.
148
149 Note that all methods in this class must be invoked from the main
150 (event) thread. Other threads, such as callbacks and TestInvocation
151 methods, should instead post events on the run queue.
152
153 TODO: Unit tests. (chrome-os-partner:7409)
154
155 Properties:
156 uuid: A unique UUID for this invocation of Goofy.
157 state_instance: An instance of FactoryState.
158 state_server: The FactoryState XML/RPC server.
159 state_server_thread: A thread running state_server.
160 event_server: The EventServer socket server.
161 event_server_thread: A thread running event_server.
162 event_client: A client to the event server.
163 connection_manager: The connection_manager object.
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800164 system_log_manager: The SystemLogManager object.
165 core_dump_manager: The CoreDumpManager object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800166 ui_process: The factory ui process object.
167 run_queue: A queue of callbacks to invoke from the main thread.
168 invocations: A map from FactoryTest objects to the corresponding
169 TestInvocations objects representing active tests.
170 tests_to_run: A deque of tests that should be run when the current
171 test(s) complete.
172 options: Command-line options.
173 args: Command-line args.
174 test_list: The test list.
175 event_handlers: Map of Event.Type to the method used to handle that
176 event. If the method has an 'event' argument, the event is passed
177 to the handler.
178 exceptions: Exceptions encountered in invocation threads.
Jon Salz3c493bb2013-02-07 17:24:58 +0800179 last_log_disk_space_message: The last message we logged about disk space
180 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800181 last_kick_sync_time: The last time to kick system_log_manager to sync
182 because of core dump files (to avoid kicking too soon then abort the
183 sync.)
Jon Salz0697cbf2012-07-04 15:14:04 +0800184 '''
185 def __init__(self):
186 self.uuid = str(uuid.uuid4())
187 self.state_instance = None
188 self.state_server = None
189 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800190 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800191 self.event_server = None
192 self.event_server_thread = None
193 self.event_client = None
194 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800195 self.charge_manager = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800196 self.time_sanitizer = None
197 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800198 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800199 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800200 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800201 self.event_log = None
202 self.prespawner = None
203 self.ui_process = None
Jon Salzc79a9982012-08-30 04:42:01 +0800204 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800205 self.run_queue = Queue.Queue()
206 self.invocations = {}
207 self.tests_to_run = deque()
208 self.visible_test = None
209 self.chrome = None
210
211 self.options = None
212 self.args = None
213 self.test_list = None
214 self.on_ui_startup = []
215 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800216 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800217 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800218 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800219 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800220 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800221 self.last_log_disk_space_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800222 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800223 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800224 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800225 self.key_filter = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800226
Jon Salz85a39882012-07-05 16:45:04 +0800227 def test_or_root(event, parent_or_group=True):
228 '''Returns the test affected by a particular event.
229
230 Args:
231 event: The event containing an optional 'path' attribute.
232 parent_on_group: If True, returns the top-level parent for a test (the
233 root node of the tests that need to be run together if the given test
234 path is to be run).
235 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800236 try:
237 path = event.path
238 except AttributeError:
239 path = None
240
241 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800242 test = self.test_list.lookup_path(path)
243 if parent_or_group:
244 test = test.get_top_level_parent_or_group()
245 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800246 else:
247 return self.test_list
248
249 self.event_handlers = {
250 Event.Type.SWITCH_TEST: self.handle_switch_test,
251 Event.Type.SHOW_NEXT_ACTIVE_TEST:
252 lambda event: self.show_next_active_test(),
253 Event.Type.RESTART_TESTS:
254 lambda event: self.restart_tests(root=test_or_root(event)),
255 Event.Type.AUTO_RUN:
256 lambda event: self.auto_run(root=test_or_root(event)),
257 Event.Type.RE_RUN_FAILED:
258 lambda event: self.re_run_failed(root=test_or_root(event)),
259 Event.Type.RUN_TESTS_WITH_STATUS:
260 lambda event: self.run_tests_with_status(
261 event.status,
262 root=test_or_root(event)),
263 Event.Type.REVIEW:
264 lambda event: self.show_review_information(),
265 Event.Type.UPDATE_SYSTEM_INFO:
266 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800267 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800268 lambda event: self.stop(root=test_or_root(event, False),
269 fail=getattr(event, 'fail', False)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800270 Event.Type.SET_VISIBLE_TEST:
271 lambda event: self.set_visible_test(
272 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800273 Event.Type.CLEAR_STATE:
274 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800275 }
276
277 self.exceptions = []
278 self.web_socket_manager = None
279
280 def destroy(self):
281 if self.chrome:
282 self.chrome.kill()
283 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800284 if self.dummy_shopfloor:
285 self.dummy_shopfloor.kill()
286 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800287 if self.ui_process:
288 utils.kill_process_tree(self.ui_process, 'ui')
289 self.ui_process = None
290 if self.web_socket_manager:
291 logging.info('Stopping web sockets')
292 self.web_socket_manager.close()
293 self.web_socket_manager = None
294 if self.state_server_thread:
295 logging.info('Stopping state server')
296 self.state_server.shutdown()
297 self.state_server_thread.join()
298 self.state_server.server_close()
299 self.state_server_thread = None
300 if self.state_instance:
301 self.state_instance.close()
302 if self.event_server_thread:
303 logging.info('Stopping event server')
304 self.event_server.shutdown() # pylint: disable=E1101
305 self.event_server_thread.join()
306 self.event_server.server_close()
307 self.event_server_thread = None
308 if self.log_watcher:
309 if self.log_watcher.IsThreadStarted():
310 self.log_watcher.StopWatchThread()
311 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800312 if self.system_log_manager:
313 if self.system_log_manager.IsThreadRunning():
314 self.system_log_manager.StopSyncThread()
315 self.system_log_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800316 if self.prespawner:
317 logging.info('Stopping prespawner')
318 self.prespawner.stop()
319 self.prespawner = None
320 if self.event_client:
321 logging.info('Closing event client')
322 self.event_client.close()
323 self.event_client = None
324 if self.event_log:
325 self.event_log.Close()
326 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800327 if self.key_filter:
328 self.key_filter.Stop()
329
Jon Salz0697cbf2012-07-04 15:14:04 +0800330 self.check_exceptions()
331 logging.info('Done destroying Goofy')
332
333 def start_state_server(self):
334 self.state_instance, self.state_server = (
335 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800336 self.goofy_rpc = GoofyRPC(self)
337 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800338 logging.info('Starting state server')
339 self.state_server_thread = threading.Thread(
340 target=self.state_server.serve_forever,
341 name='StateServer')
342 self.state_server_thread.start()
343
344 def start_event_server(self):
345 self.event_server = EventServer()
346 logging.info('Starting factory event server')
347 self.event_server_thread = threading.Thread(
348 target=self.event_server.serve_forever,
349 name='EventServer') # pylint: disable=E1101
350 self.event_server_thread.start()
351
352 self.event_client = EventClient(
353 callback=self.handle_event, event_loop=self.run_queue)
354
355 self.web_socket_manager = WebSocketManager(self.uuid)
356 self.state_server.add_handler("/event",
357 self.web_socket_manager.handle_web_socket)
358
359 def start_ui(self):
360 ui_proc_args = [
361 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
362 self.options.test_list]
363 if self.options.verbose:
364 ui_proc_args.append('-v')
365 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800366 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800367 logging.info('Waiting for UI to come up...')
368 self.event_client.wait(
369 lambda event: event.type == Event.Type.UI_READY)
370 logging.info('UI has started')
371
372 def set_visible_test(self, test):
373 if self.visible_test == test:
374 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800375 if test and not test.has_ui:
376 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800377
378 if test:
379 test.update_state(visible=True)
380 if self.visible_test:
381 self.visible_test.update_state(visible=False)
382 self.visible_test = test
383
Jon Salzd4306c82012-11-30 15:16:36 +0800384 def _log_startup_messages(self):
385 '''Logs the tail of var/log/messages and mosys and EC console logs.'''
386 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
387 # for factory-3004.B only. Consolidate and merge back to ToT.
388 if utils.in_chroot():
389 return
390
391 try:
392 var_log_messages = (
393 utils.var_log_messages_before_reboot())
394 logging.info(
395 'Tail of /var/log/messages before last reboot:\n'
396 '%s', ('\n'.join(
397 ' ' + x for x in var_log_messages)))
398 except: # pylint: disable=W0702
399 logging.exception('Unable to grok /var/log/messages')
400
401 try:
402 mosys_log = utils.Spawn(
403 ['mosys', 'eventlog', 'list'],
404 read_stdout=True, log_stderr_on_error=True).stdout_data
405 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
406 except: # pylint: disable=W0702
407 logging.exception('Unable to read mosys eventlog')
408
409 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800410 board = system.GetBoard()
411 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800412 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
413 except: # pylint: disable=W0702
414 logging.exception('Error retrieving EC console log')
415
Jon Salz0697cbf2012-07-04 15:14:04 +0800416 def handle_shutdown_complete(self, test, test_state):
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800417 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800418 Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800419
Jon Salz0697cbf2012-07-04 15:14:04 +0800420 @param test: The ShutdownStep.
421 @param test_state: The test state.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800422 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800423 test_state = test.update_state(increment_shutdown_count=1)
424 logging.info('Detected shutdown (%d of %d)',
425 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800426
Jon Salz0697cbf2012-07-04 15:14:04 +0800427 def log_and_update_state(status, error_msg, **kw):
428 self.event_log.Log('rebooted',
429 status=status, error_msg=error_msg, **kw)
Jon Salzd4306c82012-11-30 15:16:36 +0800430 logging.info('Rebooted: status=%s, %s', status,
431 (('error_msg=%s' % error_msg) if error_msg else None))
Jon Salz0697cbf2012-07-04 15:14:04 +0800432 test.update_state(status=status, error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800433
Jon Salz0697cbf2012-07-04 15:14:04 +0800434 if not self.last_shutdown_time:
435 log_and_update_state(status=TestState.FAILED,
436 error_msg='Unable to read shutdown_time')
437 return
Jon Salz258a40c2012-04-19 12:34:01 +0800438
Jon Salz0697cbf2012-07-04 15:14:04 +0800439 now = time.time()
440 logging.info('%.03f s passed since reboot',
441 now - self.last_shutdown_time)
Jon Salz258a40c2012-04-19 12:34:01 +0800442
Jon Salz0697cbf2012-07-04 15:14:04 +0800443 if self.last_shutdown_time > now:
444 test.update_state(status=TestState.FAILED,
445 error_msg='Time moved backward during reboot')
446 elif (isinstance(test, factory.RebootStep) and
447 self.test_list.options.max_reboot_time_secs and
448 (now - self.last_shutdown_time >
449 self.test_list.options.max_reboot_time_secs)):
450 # A reboot took too long; fail. (We don't check this for
451 # HaltSteps, because the machine could be halted for a
452 # very long time, and even unplugged with battery backup,
453 # thus hosing the clock.)
454 log_and_update_state(
455 status=TestState.FAILED,
456 error_msg=('More than %d s elapsed during reboot '
457 '(%.03f s, from %s to %s)' % (
458 self.test_list.options.max_reboot_time_secs,
459 now - self.last_shutdown_time,
460 utils.TimeString(self.last_shutdown_time),
461 utils.TimeString(now))),
462 duration=(now-self.last_shutdown_time))
Jon Salzd4306c82012-11-30 15:16:36 +0800463 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800464 elif test_state.shutdown_count == test.iterations:
465 # Good!
466 log_and_update_state(status=TestState.PASSED,
467 duration=(now - self.last_shutdown_time),
468 error_msg='')
469 elif test_state.shutdown_count > test.iterations:
470 # Shut down too many times
471 log_and_update_state(status=TestState.FAILED,
472 error_msg='Too many shutdowns')
Jon Salzd4306c82012-11-30 15:16:36 +0800473 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800474 elif utils.are_shift_keys_depressed():
475 logging.info('Shift keys are depressed; cancelling restarts')
476 # Abort shutdown
477 log_and_update_state(
478 status=TestState.FAILED,
479 error_msg='Shutdown aborted with double shift keys')
Jon Salza6711d72012-07-18 14:33:03 +0800480 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800481 else:
482 def handler():
483 if self._prompt_cancel_shutdown(
484 test, test_state.shutdown_count + 1):
Jon Salza6711d72012-07-18 14:33:03 +0800485 factory.console.info('Shutdown aborted by operator')
Jon Salz0697cbf2012-07-04 15:14:04 +0800486 log_and_update_state(
487 status=TestState.FAILED,
488 error_msg='Shutdown aborted by operator')
Jon Salza6711d72012-07-18 14:33:03 +0800489 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800490 return
Jon Salz0405ab52012-03-16 15:26:52 +0800491
Jon Salz0697cbf2012-07-04 15:14:04 +0800492 # Time to shutdown again
493 log_and_update_state(
494 status=TestState.ACTIVE,
495 error_msg='',
496 iteration=test_state.shutdown_count)
Jon Salz73e0fd02012-04-04 11:46:38 +0800497
Jon Salz0697cbf2012-07-04 15:14:04 +0800498 self.event_log.Log('shutdown', operation='reboot')
499 self.state_instance.set_shared_data('shutdown_time',
500 time.time())
501 self.env.shutdown('reboot')
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800502
Jon Salz0697cbf2012-07-04 15:14:04 +0800503 self.on_ui_startup.append(handler)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800504
Jon Salz0697cbf2012-07-04 15:14:04 +0800505 def _prompt_cancel_shutdown(self, test, iteration):
506 if self.options.ui != 'chrome':
507 return False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800508
Jon Salz0697cbf2012-07-04 15:14:04 +0800509 pending_shutdown_data = {
510 'delay_secs': test.delay_secs,
511 'time': time.time() + test.delay_secs,
512 'operation': test.operation,
513 'iteration': iteration,
514 'iterations': test.iterations,
515 }
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800516
Jon Salz0697cbf2012-07-04 15:14:04 +0800517 # Create a new (threaded) event client since we
518 # don't want to use the event loop for this.
519 with EventClient() as event_client:
520 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN,
521 **pending_shutdown_data))
522 aborted = event_client.wait(
523 lambda event: event.type == Event.Type.CANCEL_SHUTDOWN,
524 timeout=test.delay_secs) is not None
525 if aborted:
526 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
527 return aborted
Jon Salz258a40c2012-04-19 12:34:01 +0800528
Jon Salz0697cbf2012-07-04 15:14:04 +0800529 def init_states(self):
530 '''
531 Initializes all states on startup.
532 '''
533 for test in self.test_list.get_all_tests():
534 # Make sure the state server knows about all the tests,
535 # defaulting to an untested state.
536 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800537
Jon Salz0697cbf2012-07-04 15:14:04 +0800538 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800539 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800540 ec_console_log = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800541
Jon Salz0697cbf2012-07-04 15:14:04 +0800542 # Any 'active' tests should be marked as failed now.
543 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800544 if not test.is_leaf():
545 # Don't bother with parents; they will be updated when their
546 # children are updated.
547 continue
548
Jon Salz0697cbf2012-07-04 15:14:04 +0800549 test_state = test.get_state()
550 if test_state.status != TestState.ACTIVE:
551 continue
552 if isinstance(test, factory.ShutdownStep):
553 # Shutdown while the test was active - that's good.
554 self.handle_shutdown_complete(test, test_state)
555 else:
556 # Unexpected shutdown. Grab /var/log/messages for context.
557 if var_log_messages is None:
558 try:
559 var_log_messages = (
560 utils.var_log_messages_before_reboot())
561 # Write it to the log, to make it easier to
562 # correlate with /var/log/messages.
563 logging.info(
564 'Unexpected shutdown. '
565 'Tail of /var/log/messages before last reboot:\n'
566 '%s', ('\n'.join(
567 ' ' + x for x in var_log_messages)))
568 except: # pylint: disable=W0702
569 logging.exception('Unable to grok /var/log/messages')
570 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800571
Jon Salz008f4ea2012-08-28 05:39:45 +0800572 if mosys_log is None and not utils.in_chroot():
573 try:
574 mosys_log = utils.Spawn(
575 ['mosys', 'eventlog', 'list'],
576 read_stdout=True, log_stderr_on_error=True).stdout_data
577 # Write it to the log also.
578 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
579 except: # pylint: disable=W0702
580 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800581
Vic Yange4c275d2012-08-28 01:50:20 +0800582 if ec_console_log is None:
583 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800584 board = system.GetBoard()
585 ec_console_log = board.GetECConsoleLog()
Vic Yange4c275d2012-08-28 01:50:20 +0800586 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Jon Salzfe1f6652012-09-07 05:40:14 +0800587 except: # pylint: disable=W0702
Vic Yange4c275d2012-08-28 01:50:20 +0800588 logging.exception('Error retrieving EC console log')
589
Jon Salz0697cbf2012-07-04 15:14:04 +0800590 error_msg = 'Unexpected shutdown while test was running'
591 self.event_log.Log('end_test',
592 path=test.path,
593 status=TestState.FAILED,
594 invocation=test.get_state().invocation,
595 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800596 var_log_messages='\n'.join(var_log_messages),
597 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800598 test.update_state(
599 status=TestState.FAILED,
600 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800601
Jon Salz50efe942012-07-26 11:54:10 +0800602 if not test.never_fails:
603 # For "never_fails" tests (such as "Start"), don't cancel
604 # pending tests, since reboot is expected.
605 factory.console.info('Unexpected shutdown while test %s '
606 'running; cancelling any pending tests',
607 test.path)
608 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800609
Jon Salz008f4ea2012-08-28 05:39:45 +0800610 self.update_skipped_tests()
611
612 def update_skipped_tests(self):
613 '''
614 Updates skipped states based on run_if.
615 '''
616 for t in self.test_list.walk():
617 if t.is_leaf() and t.run_if_table_name:
618 skip = False
619 try:
620 aux = shopfloor.get_selected_aux_data(t.run_if_table_name)
621 value = aux.get(t.run_if_col)
622 if value is not None:
623 skip = (not value) ^ t.run_if_not
624 except ValueError:
625 # Not available; assume it shouldn't be skipped
626 pass
627
628 test_state = t.get_state()
629 if ((not skip) and
630 (test_state.status == TestState.PASSED) and
631 (test_state.error_msg == TestState.SKIPPED_MSG)):
632 # It was marked as skipped before, but now we need to run it.
633 # Mark as untested.
634 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
635 else:
636 t.update_state(skip=skip)
637
Jon Salz0697cbf2012-07-04 15:14:04 +0800638 def show_next_active_test(self):
639 '''
640 Rotates to the next visible active test.
641 '''
642 self.reap_completed_tests()
643 active_tests = [
644 t for t in self.test_list.walk()
645 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
646 if not active_tests:
647 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800648
Jon Salz0697cbf2012-07-04 15:14:04 +0800649 try:
650 next_test = active_tests[
651 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
652 except ValueError: # visible_test not present in active_tests
653 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800654
Jon Salz0697cbf2012-07-04 15:14:04 +0800655 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800656
Jon Salz0697cbf2012-07-04 15:14:04 +0800657 def handle_event(self, event):
658 '''
659 Handles an event from the event server.
660 '''
661 handler = self.event_handlers.get(event.type)
662 if handler:
663 handler(event)
664 else:
665 # We don't register handlers for all event types - just ignore
666 # this event.
667 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800668
Vic Yangaabf9fd2013-04-09 18:56:13 +0800669 def check_critical_factory_note(self):
670 '''
671 Returns True if the last factory note is critical.
672 '''
673 notes = self.state_instance.get_shared_data('factory_note', True)
674 return notes and notes[-1]['level'] == 'CRITICAL'
675
Jon Salz0697cbf2012-07-04 15:14:04 +0800676 def run_next_test(self):
677 '''
678 Runs the next eligible test (or tests) in self.tests_to_run.
679 '''
680 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800681 if self.tests_to_run and self.check_critical_factory_note():
682 self.tests_to_run.clear()
683 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800684 while self.tests_to_run:
685 logging.debug('Tests to run: %s',
686 [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800687
Jon Salz0697cbf2012-07-04 15:14:04 +0800688 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800689
Jon Salz0697cbf2012-07-04 15:14:04 +0800690 if test in self.invocations:
691 logging.info('Next test %s is already running', test.path)
692 self.tests_to_run.popleft()
693 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800694
Jon Salza1412922012-07-23 16:04:17 +0800695 for requirement in test.require_run:
696 for i in requirement.test.walk():
697 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800698 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800699 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800700 return
701
Jon Salz0697cbf2012-07-04 15:14:04 +0800702 if self.invocations and not (test.backgroundable and all(
703 [x.backgroundable for x in self.invocations])):
704 logging.debug('Waiting for non-backgroundable tests to '
705 'complete before running %s', test.path)
706 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800707
Jon Salz3e6f5202012-10-15 15:08:29 +0800708 if test.get_state().skip:
709 factory.console.info('Skipping test %s', test.path)
710 test.update_state(status=TestState.PASSED,
711 error_msg=TestState.SKIPPED_MSG)
712 self.tests_to_run.popleft()
713 continue
714
Jon Salz0697cbf2012-07-04 15:14:04 +0800715 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800716
Jon Salz304a75d2012-07-06 11:14:15 +0800717 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800718 for requirement in test.require_run:
719 for i in requirement.test.walk():
720 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800721 # We've hit this test itself; stop checking
722 break
Jon Salza1412922012-07-23 16:04:17 +0800723 if ((i.get_state().status == TestState.UNTESTED) or
724 (requirement.passed and i.get_state().status !=
725 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800726 # Found an untested test; move on to the next
727 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800728 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800729 break
730
731 if untested:
732 untested_paths = ', '.join(sorted([x.path for x in untested]))
733 if self.state_instance.get_shared_data('engineering_mode',
734 optional=True):
735 # In engineering mode, we'll let it go.
736 factory.console.warn('In engineering mode; running '
737 '%s even though required tests '
738 '[%s] have not completed',
739 test.path, untested_paths)
740 else:
741 # Not in engineering mode; mark it failed.
742 error_msg = ('Required tests [%s] have not been run yet'
743 % untested_paths)
744 factory.console.error('Not running %s: %s',
745 test.path, error_msg)
746 test.update_state(status=TestState.FAILED,
747 error_msg=error_msg)
748 continue
749
Jon Salz0697cbf2012-07-04 15:14:04 +0800750 if isinstance(test, factory.ShutdownStep):
751 if os.path.exists(NO_REBOOT_FILE):
752 test.update_state(
753 status=TestState.FAILED, increment_count=1,
754 error_msg=('Skipped shutdown since %s is present' %
Jon Salz304a75d2012-07-06 11:14:15 +0800755 NO_REBOOT_FILE))
Jon Salz0697cbf2012-07-04 15:14:04 +0800756 continue
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800757
Jon Salz0697cbf2012-07-04 15:14:04 +0800758 test.update_state(status=TestState.ACTIVE, increment_count=1,
759 error_msg='', shutdown_count=0)
760 if self._prompt_cancel_shutdown(test, 1):
761 self.event_log.Log('reboot_cancelled')
762 test.update_state(
763 status=TestState.FAILED, increment_count=1,
764 error_msg='Shutdown aborted by operator',
765 shutdown_count=0)
chungyiafe8f772012-08-15 19:36:29 +0800766 continue
Jon Salz2f757d42012-06-27 17:06:42 +0800767
Jon Salz0697cbf2012-07-04 15:14:04 +0800768 # Save pending test list in the state server
Jon Salzdbf398f2012-06-14 17:30:01 +0800769 self.state_instance.set_shared_data(
Jon Salz0697cbf2012-07-04 15:14:04 +0800770 'tests_after_shutdown',
771 [t.path for t in self.tests_to_run])
772 # Save shutdown time
773 self.state_instance.set_shared_data('shutdown_time',
774 time.time())
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800775
Jon Salz0697cbf2012-07-04 15:14:04 +0800776 with self.env.lock:
777 self.event_log.Log('shutdown', operation=test.operation)
778 shutdown_result = self.env.shutdown(test.operation)
779 if shutdown_result:
780 # That's all, folks!
781 self.run_queue.put(None)
782 return
783 else:
784 # Just pass (e.g., in the chroot).
785 test.update_state(status=TestState.PASSED)
786 self.state_instance.set_shared_data(
787 'tests_after_shutdown', None)
788 # Send event with no fields to indicate that there is no
789 # longer a pending shutdown.
790 self.event_client.post_event(Event(
791 Event.Type.PENDING_SHUTDOWN))
792 continue
Jon Salz258a40c2012-04-19 12:34:01 +0800793
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800794 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800795
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800796 def _run_test(self, test, iterations_left=None, retries_left=None):
Jon Salz1acc8742012-07-17 17:45:55 +0800797 invoc = TestInvocation(self, test, on_completion=self.run_next_test)
798 new_state = test.update_state(
799 status=TestState.ACTIVE, increment_count=1, error_msg='',
Jon Salzbd42ce12012-09-18 08:03:59 +0800800 invocation=invoc.uuid, iterations_left=iterations_left,
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800801 retries_left=retries_left,
Jon Salzbd42ce12012-09-18 08:03:59 +0800802 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800803 invoc.count = new_state.count
804
805 self.invocations[test] = invoc
806 if self.visible_test is None and test.has_ui:
807 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800808 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800809 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800810
Vic Yang311ddb82012-09-26 12:08:28 +0800811 def check_exclusive(self):
812 current_exclusive_items = set([
813 item
814 for item in factory.FactoryTest.EXCLUSIVE_OPTIONS
815 if any([test.is_exclusive(item) for test in self.invocations])])
816
817 new_exclusive_items = current_exclusive_items - self.exclusive_items
818 if factory.FactoryTest.EXCLUSIVE_OPTIONS.NETWORKING in new_exclusive_items:
819 logging.info('Disabling network')
820 self.connection_manager.DisableNetworking()
821 if factory.FactoryTest.EXCLUSIVE_OPTIONS.CHARGER in new_exclusive_items:
822 logging.info('Stop controlling charger')
823
824 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
825 if (factory.FactoryTest.EXCLUSIVE_OPTIONS.NETWORKING in
826 new_non_exclusive_items):
827 logging.info('Re-enabling network')
828 self.connection_manager.EnableNetworking()
829 if factory.FactoryTest.EXCLUSIVE_OPTIONS.CHARGER in new_non_exclusive_items:
830 logging.info('Start controlling charger')
831
832 # Only adjust charge state if not excluded
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800833 if (factory.FactoryTest.EXCLUSIVE_OPTIONS.CHARGER not in
Vic Yange83d9a12013-04-19 20:00:20 +0800834 current_exclusive_items and not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800835 if self.charge_manager:
836 self.charge_manager.AdjustChargeState()
837 else:
838 try:
839 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
840 except BoardException:
841 logging.exception('Unable to set charge state on this board')
Vic Yang311ddb82012-09-26 12:08:28 +0800842
843 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800844
cychiang21886742012-07-05 15:16:32 +0800845 def check_for_updates(self):
846 '''
847 Schedules an asynchronous check for updates if necessary.
848 '''
849 if not self.test_list.options.update_period_secs:
850 # Not enabled.
851 return
852
853 now = time.time()
854 if self.last_update_check and (
855 now - self.last_update_check <
856 self.test_list.options.update_period_secs):
857 # Not yet time for another check.
858 return
859
860 self.last_update_check = now
861
862 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
863 if reached_shopfloor:
864 new_update_md5sum = md5sum if needs_update else None
865 if system.SystemInfo.update_md5sum != new_update_md5sum:
866 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
867 system.SystemInfo.update_md5sum = new_update_md5sum
868 self.run_queue.put(self.update_system_info)
869
870 updater.CheckForUpdateAsync(
871 handle_check_for_update,
872 self.test_list.options.shopfloor_timeout_secs)
873
Jon Salza6711d72012-07-18 14:33:03 +0800874 def cancel_pending_tests(self):
875 '''Cancels any tests in the run queue.'''
876 self.run_tests([])
877
Jon Salz0697cbf2012-07-04 15:14:04 +0800878 def run_tests(self, subtrees, untested_only=False):
879 '''
880 Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800881
Jon Salz0697cbf2012-07-04 15:14:04 +0800882 The tests are run in order unless one fails (then stops).
883 Backgroundable tests are run simultaneously; when a foreground test is
884 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800885
Jon Salz0697cbf2012-07-04 15:14:04 +0800886 @param subtrees: Node or nodes containing tests to run (may either be
887 a single test or a list). Duplicates will be ignored.
888 '''
889 if type(subtrees) != list:
890 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800891
Jon Salz0697cbf2012-07-04 15:14:04 +0800892 # Nodes we've seen so far, to avoid duplicates.
893 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800894
Jon Salz0697cbf2012-07-04 15:14:04 +0800895 self.tests_to_run = deque()
896 for subtree in subtrees:
897 for test in subtree.walk():
898 if test in seen:
899 continue
900 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800901
Jon Salz0697cbf2012-07-04 15:14:04 +0800902 if not test.is_leaf():
903 continue
904 if (untested_only and
905 test.get_state().status != TestState.UNTESTED):
906 continue
907 self.tests_to_run.append(test)
908 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800909
Jon Salz0697cbf2012-07-04 15:14:04 +0800910 def reap_completed_tests(self):
911 '''
912 Removes completed tests from the set of active tests.
913
914 Also updates the visible test if it was reaped.
915 '''
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800916 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800917 for t, v in dict(self.invocations).iteritems():
918 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800919 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800920 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800921 del self.invocations[t]
922
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800923 # Stop on failure if flag is true.
924 if (self.test_list.options.stop_on_failure and
925 new_state.status == TestState.FAILED):
926 # Clean all the tests to cause goofy to stop.
927 self.tests_to_run = []
928 factory.console.info("Stop on failure triggered. Empty the queue.")
929
Jon Salz1acc8742012-07-17 17:45:55 +0800930 if new_state.iterations_left and new_state.status == TestState.PASSED:
931 # Play it again, Sam!
932 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800933 # new_state.retries_left is obtained after update.
934 # For retries_left == 0, test can still be run for the last time.
935 elif (new_state.retries_left >= 0 and
936 new_state.status == TestState.FAILED):
937 # Still have to retry, Sam!
938 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +0800939
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800940 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +0800941 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800942
Jon Salz0697cbf2012-07-04 15:14:04 +0800943 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +0800944 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +0800945 self.set_visible_test(None)
946 # Make the first running test, if any, the visible test
947 for t in self.test_list.walk():
948 if t in self.invocations:
949 self.set_visible_test(t)
950 break
951
Jon Salz85a39882012-07-05 16:45:04 +0800952 def kill_active_tests(self, abort, root=None):
Jon Salz0697cbf2012-07-04 15:14:04 +0800953 '''
954 Kills and waits for all active tests.
955
Jon Salz85a39882012-07-05 16:45:04 +0800956 Args:
957 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +0800958 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +0800959 root: If set, only kills tests with root as an ancestor.
Jon Salz0697cbf2012-07-04 15:14:04 +0800960 '''
961 self.reap_completed_tests()
962 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +0800963 if root and not test.has_ancestor(root):
964 continue
965
Jon Salz0697cbf2012-07-04 15:14:04 +0800966 factory.console.info('Killing active test %s...' % test.path)
967 invoc.abort_and_join()
968 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +0800969 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800970 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +0800971
Jon Salz0697cbf2012-07-04 15:14:04 +0800972 if not abort:
973 test.update_state(status=TestState.UNTESTED)
974 self.reap_completed_tests()
975
Jon Salz85a39882012-07-05 16:45:04 +0800976 def stop(self, root=None, fail=False):
977 self.kill_active_tests(fail, root)
978 # Remove any tests in the run queue under the root.
979 self.tests_to_run = deque([x for x in self.tests_to_run
980 if root and not x.has_ancestor(root)])
981 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +0800982
Jon Salz4712ac72013-02-07 17:12:05 +0800983 def clear_state(self, root=None):
984 self.stop(root)
985 for f in root.walk():
986 if f.is_leaf():
987 f.update_state(status=TestState.UNTESTED)
988
Jon Salz0697cbf2012-07-04 15:14:04 +0800989 def abort_active_tests(self):
990 self.kill_active_tests(True)
991
992 def main(self):
993 try:
994 self.init()
995 self.event_log.Log('goofy_init',
996 success=True)
997 except:
998 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800999 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001000 self.event_log.Log('goofy_init',
1001 success=False,
1002 trace=traceback.format_exc())
1003 except: # pylint: disable=W0702
1004 pass
1005 raise
1006
1007 self.run()
1008
1009 def update_system_info(self):
1010 '''Updates system info.'''
1011 system_info = system.SystemInfo()
1012 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1013 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1014 system_info=system_info.__dict__))
1015 logging.info('System info: %r', system_info.__dict__)
1016
Jon Salzeb42f0d2012-07-27 19:14:04 +08001017 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
1018 '''Commences updating factory software.
1019
1020 Args:
1021 auto_run_on_restart: Auto-run when the machine comes back up.
1022 post_update_hook: Code to call after update but immediately before
1023 restart.
1024
1025 Returns:
1026 Never if the update was successful (we just reboot).
1027 False if the update was unnecessary (no update available).
1028 '''
Jon Salz0697cbf2012-07-04 15:14:04 +08001029 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001030 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001031
Jon Salz5c344f62012-07-13 14:31:16 +08001032 def pre_update_hook():
1033 if auto_run_on_restart:
1034 self.state_instance.set_shared_data('tests_after_shutdown',
1035 FORCE_AUTO_RUN)
1036 self.state_instance.close()
1037
Jon Salzeb42f0d2012-07-27 19:14:04 +08001038 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1039 if post_update_hook:
1040 post_update_hook()
1041 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001042
Jon Salzcef132a2012-08-30 04:58:08 +08001043 def handle_sigint(self, dummy_signum, dummy_frame):
Jon Salz77c151e2012-08-28 07:20:37 +08001044 logging.error('Received SIGINT')
1045 self.run_queue.put(None)
1046 raise KeyboardInterrupt()
1047
Jon Salz0697cbf2012-07-04 15:14:04 +08001048 def init(self, args=None, env=None):
1049 '''Initializes Goofy.
1050
1051 Args:
1052 args: A list of command-line arguments. Uses sys.argv if
1053 args is None.
1054 env: An Environment instance to use (or None to choose
1055 FakeChrootEnvironment or DUTEnvironment as appropriate).
1056 '''
Jon Salz77c151e2012-08-28 07:20:37 +08001057 signal.signal(signal.SIGINT, self.handle_sigint)
1058
Jon Salz0697cbf2012-07-04 15:14:04 +08001059 parser = OptionParser()
1060 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001061 action='store_true',
1062 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001063 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001064 metavar='FILE',
1065 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001066 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001067 action='store_true',
1068 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001069 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz8fa8e832012-07-13 19:04:09 +08001070 choices=['none', 'gtk', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001071 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001072 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001073 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001074 type='int', default=1,
1075 help=('Factor by which to scale UI '
1076 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001077 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001078 metavar='FILE',
1079 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001080 parser.add_option('--dummy_shopfloor', action='store_true',
1081 help='Use a dummy shopfloor server')
chungyiafe8f772012-08-15 19:36:29 +08001082 parser.add_option('--automation', dest='automation',
1083 action='store_true',
1084 help='Enable automation on running factory test')
Ricky Liang09216dc2013-02-22 17:26:45 +08001085 parser.add_option('--one_pixel_less', dest='one_pixel_less',
1086 action='store_true',
1087 help=('Start Chrome one pixel less than the full screen.'
1088 'Needed by Exynos platform to run GTK.'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001089 (self.options, self.args) = parser.parse_args(args)
1090
Jon Salz46b89562012-07-05 11:49:22 +08001091 # Make sure factory directories exist.
1092 factory.get_log_root()
1093 factory.get_state_root()
1094 factory.get_test_data_root()
1095
Jon Salz0697cbf2012-07-04 15:14:04 +08001096 global _inited_logging # pylint: disable=W0603
1097 if not _inited_logging:
1098 factory.init_logging('goofy', verbose=self.options.verbose)
1099 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001100
Jon Salz0f996602012-10-03 15:26:48 +08001101 if self.options.print_test_list:
1102 print factory.read_test_list(
1103 self.options.print_test_list).__repr__(recursive=True)
1104 sys.exit(0)
1105
Jon Salzee85d522012-07-17 14:34:46 +08001106 event_log.IncrementBootSequence()
Jon Salz0697cbf2012-07-04 15:14:04 +08001107 self.event_log = EventLog('goofy')
1108
1109 if (not suppress_chroot_warning and
1110 factory.in_chroot() and
1111 self.options.ui == 'gtk' and
1112 os.environ.get('DISPLAY') in [None, '', ':0', ':0.0']):
1113 # That's not going to work! Tell the user how to run
1114 # this way.
1115 logging.warn(GOOFY_IN_CHROOT_WARNING)
1116 time.sleep(1)
1117
1118 if env:
1119 self.env = env
1120 elif factory.in_chroot():
1121 self.env = test_environment.FakeChrootEnvironment()
1122 logging.warn(
1123 'Using chroot environment: will not actually run autotests')
1124 else:
1125 self.env = test_environment.DUTEnvironment()
1126 self.env.goofy = self
1127
1128 if self.options.restart:
1129 state.clear_state()
1130
Jon Salz0697cbf2012-07-04 15:14:04 +08001131 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1132 logging.warn(
1133 'In QEMU; ignoring ui_scale_factor argument')
1134 self.options.ui_scale_factor = 1
1135
1136 logging.info('Started')
1137
1138 self.start_state_server()
1139 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1140 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001141 self.options.ui_scale_factor)
1142 self.state_instance.set_shared_data('one_pixel_less',
1143 self.options.one_pixel_less)
Jon Salz0697cbf2012-07-04 15:14:04 +08001144 self.last_shutdown_time = (
1145 self.state_instance.get_shared_data('shutdown_time', optional=True))
1146 self.state_instance.del_shared_data('shutdown_time', optional=True)
1147
Jon Salzb19ea072013-02-07 16:35:00 +08001148 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001149 if not self.options.test_list:
1150 self.options.test_list = find_test_list()
Jon Salzb19ea072013-02-07 16:35:00 +08001151 if self.options.test_list:
Jon Salz0697cbf2012-07-04 15:14:04 +08001152 logging.info('Using test list %s', self.options.test_list)
Jon Salzb19ea072013-02-07 16:35:00 +08001153 try:
1154 self.test_list = factory.read_test_list(
1155 self.options.test_list,
1156 self.state_instance)
1157 except: # pylint: disable=W0702
1158 logging.exception('Unable to read test list %r', self.options.test_list)
1159 self.state_instance.set_shared_data('startup_error',
1160 'Unable to read test list %s\n%s' % (
1161 self.options.test_list,
1162 traceback.format_exc()))
1163 else:
1164 logging.error('No test list found.')
1165 self.state_instance.set_shared_data('startup_error',
1166 'No test list found.')
Jon Salz0697cbf2012-07-04 15:14:04 +08001167
Jon Salzb19ea072013-02-07 16:35:00 +08001168 if not self.test_list:
1169 if self.options.ui == 'chrome':
1170 # Create an empty test list with default options so that the rest of
1171 # startup can proceed.
1172 self.test_list = factory.FactoryTestList(
1173 [], self.state_instance, factory.Options())
1174 else:
1175 # Bail with an error; no point in starting up.
1176 sys.exit('No valid test list; exiting.')
1177
Jon Salz822838b2013-03-25 17:32:33 +08001178 if self.test_list.options.clear_state_on_start:
1179 self.state_instance.clear_test_state()
1180
Jon Salz0697cbf2012-07-04 15:14:04 +08001181 if not self.state_instance.has_shared_data('ui_lang'):
1182 self.state_instance.set_shared_data('ui_lang',
1183 self.test_list.options.ui_lang)
1184 self.state_instance.set_shared_data(
1185 'test_list_options',
1186 self.test_list.options.__dict__)
1187 self.state_instance.test_list = self.test_list
1188
Jon Salz83ef34b2012-11-01 19:46:35 +08001189 if not utils.in_chroot() and self.test_list.options.disable_log_rotation:
1190 open('/var/lib/cleanup_logs_paused', 'w').close()
1191
Jon Salz23926422012-09-01 03:38:13 +08001192 if self.options.dummy_shopfloor:
1193 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1194 'http://localhost:%d/' % shopfloor.DEFAULT_SERVER_PORT)
1195 self.dummy_shopfloor = Spawn(
1196 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1197 '--dummy'])
1198 elif self.test_list.options.shopfloor_server_url:
1199 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001200 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001201
Jon Salz0f996602012-10-03 15:26:48 +08001202 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001203 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1204 base_time=time_sanitizer.GetBaseTimeFromFile(
1205 # lsb-factory is written by the factory install shim during
1206 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001207 # the mini-Omaha server. If it's not available, we'll use
1208 # /etc/lsb-factory (which will be much older, but reasonably
1209 # sane) and rely on a shopfloor sync to set a more accurate
1210 # time.
1211 '/usr/local/etc/lsb-factory',
1212 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001213 self.time_sanitizer.RunOnce()
1214
Jon Salz0697cbf2012-07-04 15:14:04 +08001215 self.init_states()
1216 self.start_event_server()
1217 self.connection_manager = self.env.create_connection_manager(
Tai-Hsu Lin371351a2012-08-27 14:17:14 +08001218 self.test_list.options.wlans,
1219 self.test_list.options.scan_wifi_period_secs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001220 # Note that we create a log watcher even if
1221 # sync_event_log_period_secs isn't set (no background
1222 # syncing), since we may use it to flush event logs as well.
1223 self.log_watcher = EventLogWatcher(
1224 self.test_list.options.sync_event_log_period_secs,
Jon Salz16d10542012-07-23 12:18:45 +08001225 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001226 if self.test_list.options.sync_event_log_period_secs:
1227 self.log_watcher.StartWatchThread()
1228
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001229 # Note that we create a system log manager even if
1230 # sync_log_period_secs isn't set (no background
1231 # syncing), since we may kick it to sync logs in its
1232 # thread.
1233 self.system_log_manager = SystemLogManager(
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +08001234 sync_log_paths=self.test_list.options.sync_log_paths,
1235 sync_period_sec=self.test_list.options.sync_log_period_secs,
1236 clear_log_paths=self.test_list.options.clear_log_paths)
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001237 self.system_log_manager.StartSyncThread()
1238
Jon Salz0697cbf2012-07-04 15:14:04 +08001239 self.update_system_info()
1240
Vic Yang4953fc12012-07-26 16:19:53 +08001241 assert ((self.test_list.options.min_charge_pct is None) ==
1242 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001243 if utils.in_chroot():
1244 logging.info('In chroot, ignoring charge manager and charge state')
1245 elif self.test_list.options.min_charge_pct is not None:
Vic Yang4953fc12012-07-26 16:19:53 +08001246 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1247 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001248 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001249 else:
1250 # Goofy should set charger state to charge if charge_manager is disabled.
1251 try:
1252 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
1253 except BoardException:
1254 logging.exception('Unable to set charge state on this board')
Vic Yang4953fc12012-07-26 16:19:53 +08001255
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001256 self.core_dump_manager = CoreDumpManager(
1257 self.test_list.options.core_dump_watchlist)
1258
Jon Salz0697cbf2012-07-04 15:14:04 +08001259 os.environ['CROS_FACTORY'] = '1'
1260 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1261
1262 # Set CROS_UI since some behaviors in ui.py depend on the
1263 # particular UI in use. TODO(jsalz): Remove this (and all
1264 # places it is used) when the GTK UI is removed.
1265 os.environ['CROS_UI'] = self.options.ui
1266
1267 if self.options.ui == 'chrome':
1268 self.env.launch_chrome()
1269 logging.info('Waiting for a web socket connection')
Cheng-Yi Chiangfd8ed392013-03-08 21:37:31 +08001270 self.web_socket_manager.wait()
Jon Salz0697cbf2012-07-04 15:14:04 +08001271
1272 # Wait for the test widget size to be set; this is done in
1273 # an asynchronous RPC so there is a small chance that the
1274 # web socket might be opened first.
1275 for _ in range(100): # 10 s
1276 try:
1277 if self.state_instance.get_shared_data('test_widget_size'):
1278 break
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001279 except KeyError:
Jon Salz0697cbf2012-07-04 15:14:04 +08001280 pass # Retry
1281 time.sleep(0.1) # 100 ms
1282 else:
1283 logging.warn('Never received test_widget_size from UI')
1284 elif self.options.ui == 'gtk':
1285 self.start_ui()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001286
Ricky Liang650f6bf2012-09-28 13:22:54 +08001287 # Create download path for autotest beforehand or autotests run at
1288 # the same time might fail due to race condition.
1289 if not factory.in_chroot():
1290 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1291 'download'))
1292
Jon Salz0697cbf2012-07-04 15:14:04 +08001293 def state_change_callback(test, test_state):
1294 self.event_client.post_event(
1295 Event(Event.Type.STATE_CHANGE,
1296 path=test.path, state=test_state))
1297 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001298
Jon Salza6711d72012-07-18 14:33:03 +08001299 for handler in self.on_ui_startup:
1300 handler()
1301
1302 self.prespawner = Prespawner()
1303 self.prespawner.start()
1304
Jon Salz0697cbf2012-07-04 15:14:04 +08001305 try:
1306 tests_after_shutdown = self.state_instance.get_shared_data(
1307 'tests_after_shutdown')
1308 except KeyError:
1309 tests_after_shutdown = None
Jon Salz57717ca2012-04-04 16:47:25 +08001310
Jon Salz5c344f62012-07-13 14:31:16 +08001311 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1312 if not force_auto_run and tests_after_shutdown is not None:
Jon Salz0697cbf2012-07-04 15:14:04 +08001313 logging.info('Resuming tests after shutdown: %s',
1314 tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001315 self.tests_to_run.extend(
1316 self.test_list.lookup_path(t) for t in tests_after_shutdown)
1317 self.run_queue.put(self.run_next_test)
1318 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001319 if force_auto_run or self.test_list.options.auto_run_on_start:
Jon Salz0697cbf2012-07-04 15:14:04 +08001320 self.run_queue.put(
1321 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001322 self.state_instance.set_shared_data('tests_after_shutdown', None)
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001323
Dean Liao592e4d52013-01-10 20:06:39 +08001324 self.may_disable_cros_shortcut_keys()
1325
1326 def may_disable_cros_shortcut_keys(self):
1327 test_options = self.test_list.options
1328 if test_options.disable_cros_shortcut_keys:
1329 logging.info('Filter ChromeOS shortcut keys.')
1330 self.key_filter = KeyFilter(
1331 unmap_caps_lock=test_options.disable_caps_lock,
1332 caps_lock_keycode=test_options.caps_lock_keycode)
1333 self.key_filter.Start()
1334
Jon Salz0697cbf2012-07-04 15:14:04 +08001335 def run(self):
1336 '''Runs Goofy.'''
1337 # Process events forever.
1338 while self.run_once(True):
1339 pass
Jon Salz73e0fd02012-04-04 11:46:38 +08001340
Jon Salz0697cbf2012-07-04 15:14:04 +08001341 def run_once(self, block=False):
1342 '''Runs all items pending in the event loop.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001343
Jon Salz0697cbf2012-07-04 15:14:04 +08001344 Args:
1345 block: If true, block until at least one event is processed.
Jon Salz7c15e8b2012-06-19 17:10:37 +08001346
Jon Salz0697cbf2012-07-04 15:14:04 +08001347 Returns:
1348 True to keep going or False to shut down.
1349 '''
1350 events = utils.DrainQueue(self.run_queue)
cychiang21886742012-07-05 15:16:32 +08001351 while not events:
Jon Salz0697cbf2012-07-04 15:14:04 +08001352 # Nothing on the run queue.
1353 self._run_queue_idle()
1354 if block:
1355 # Block for at least one event...
cychiang21886742012-07-05 15:16:32 +08001356 try:
1357 events.append(self.run_queue.get(timeout=RUN_QUEUE_TIMEOUT_SECS))
1358 except Queue.Empty:
1359 # Keep going (calling _run_queue_idle() again at the top of
1360 # the loop)
1361 continue
Jon Salz0697cbf2012-07-04 15:14:04 +08001362 # ...and grab anything else that showed up at the same
1363 # time.
1364 events.extend(utils.DrainQueue(self.run_queue))
cychiang21886742012-07-05 15:16:32 +08001365 else:
1366 break
Jon Salz51528e12012-07-02 18:54:45 +08001367
Jon Salz0697cbf2012-07-04 15:14:04 +08001368 for event in events:
1369 if not event:
1370 # Shutdown request.
1371 self.run_queue.task_done()
1372 return False
Jon Salz51528e12012-07-02 18:54:45 +08001373
Jon Salz0697cbf2012-07-04 15:14:04 +08001374 try:
1375 event()
Jon Salz85a39882012-07-05 16:45:04 +08001376 except: # pylint: disable=W0702
1377 logging.exception('Error in event loop')
Jon Salz0697cbf2012-07-04 15:14:04 +08001378 self.record_exception(traceback.format_exception_only(
1379 *sys.exc_info()[:2]))
1380 # But keep going
1381 finally:
1382 self.run_queue.task_done()
1383 return True
Jon Salz0405ab52012-03-16 15:26:52 +08001384
Jon Salz0e6532d2012-10-25 16:30:11 +08001385 def _should_sync_time(self, foreground=False):
1386 '''Returns True if we should attempt syncing time with shopfloor.
1387
1388 Args:
1389 foreground: If True, synchronizes even if background syncing
1390 is disabled (e.g., in explicit sync requests from the
1391 SyncShopfloor test).
1392 '''
1393 return ((foreground or
1394 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001395 self.time_sanitizer and
1396 (not self.time_synced) and
1397 (not factory.in_chroot()))
1398
Jon Salz0e6532d2012-10-25 16:30:11 +08001399 def sync_time_with_shopfloor_server(self, foreground=False):
Jon Salz54882d02012-08-31 01:57:54 +08001400 '''Syncs time with shopfloor server, if not yet synced.
1401
Jon Salz0e6532d2012-10-25 16:30:11 +08001402 Args:
1403 foreground: If True, synchronizes even if background syncing
1404 is disabled (e.g., in explicit sync requests from the
1405 SyncShopfloor test).
1406
Jon Salz54882d02012-08-31 01:57:54 +08001407 Returns:
1408 False if no time sanitizer is available, or True if this sync (or a
1409 previous sync) succeeded.
1410
1411 Raises:
1412 Exception if unable to contact the shopfloor server.
1413 '''
Jon Salz0e6532d2012-10-25 16:30:11 +08001414 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001415 self.time_sanitizer.SyncWithShopfloor()
1416 self.time_synced = True
1417 return self.time_synced
1418
Jon Salzb92c5112012-09-21 15:40:11 +08001419 def log_disk_space_stats(self):
1420 if not self.test_list.options.log_disk_space_period_secs:
1421 return
1422
1423 now = time.time()
1424 if (self.last_log_disk_space_time and
1425 now - self.last_log_disk_space_time <
1426 self.test_list.options.log_disk_space_period_secs):
1427 return
1428 self.last_log_disk_space_time = now
1429
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001430 # Upload event if stateful partition usage is above threshold.
1431 # Stateful partition is mounted on /usr/local, while
1432 # encrypted stateful partition is mounted on /var.
1433 # If there are too much logs in the factory process,
1434 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001435 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001436 vfs_infos = disk_space.GetAllVFSInfo()
1437 stateful_info, encrypted_info = None, None
1438 for vfs_info in vfs_infos.values():
1439 if '/usr/local' in vfs_info.mount_points:
1440 stateful_info = vfs_info
1441 if '/var' in vfs_info.mount_points:
1442 encrypted_info = vfs_info
1443
1444 stateful = disk_space.GetPartitionUsage(stateful_info)
1445 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1446
1447 above_threshold = (
1448 self.test_list.options.stateful_usage_threshold and
1449 max(stateful.bytes_used_pct,
1450 stateful.inodes_used_pct,
1451 encrypted.bytes_used_pct,
1452 encrypted.inodes_used_pct) >
1453 self.test_list.options.stateful_usage_threshold)
1454
1455 if above_threshold:
1456 self.event_log.Log('stateful_partition_usage',
1457 partitions={
1458 'stateful': {
1459 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1460 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1461 'encrypted_stateful': {
1462 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1463 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1464 })
1465 self.log_watcher.ScanEventLogs()
1466
1467 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001468 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001469 if above_threshold:
1470 logging.warning(message)
1471 else:
1472 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001473 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001474 except: # pylint: disable=W0702
1475 logging.exception('Unable to get disk space used')
1476
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001477 def check_core_dump(self):
1478 '''Checks if there is any core dumped file.
1479
1480 Removes unwanted core dump files immediately.
1481 Syncs those files matching watch list to server with a delay between
1482 each sync. After the files have been synced to server, deletes the files.
1483 '''
1484 core_dump_files = self.core_dump_manager.ScanFiles()
1485 if core_dump_files:
1486 now = time.time()
1487 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1488 self.test_list.options.kick_sync_min_interval_secs):
1489 return
1490 self.last_kick_sync_time = now
1491
1492 # Sends event to server
1493 self.event_log.Log('core_dumped', files=core_dump_files)
1494 self.log_watcher.KickWatchThread()
1495
1496 # Syncs files to server
1497 self.system_log_manager.KickSyncThread(
1498 core_dump_files, self.core_dump_manager.ClearFiles)
1499
Jon Salz8fa8e832012-07-13 19:04:09 +08001500 def sync_time_in_background(self):
Jon Salzb22d1172012-08-06 10:38:57 +08001501 '''Writes out current time and tries to sync with shopfloor server.'''
1502 if not self.time_sanitizer:
1503 return
1504
1505 # Write out the current time.
1506 self.time_sanitizer.SaveTime()
1507
Jon Salz54882d02012-08-31 01:57:54 +08001508 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001509 return
1510
1511 now = time.time()
1512 if self.last_sync_time and (
1513 now - self.last_sync_time <
1514 self.test_list.options.sync_time_period_secs):
1515 # Not yet time for another check.
1516 return
1517 self.last_sync_time = now
1518
1519 def target():
1520 try:
Jon Salz54882d02012-08-31 01:57:54 +08001521 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001522 except: # pylint: disable=W0702
1523 # Oh well. Log an error (but no trace)
1524 logging.info(
1525 'Unable to get time from shopfloor server: %s',
1526 utils.FormatExceptionOnly())
1527
1528 thread = threading.Thread(target=target)
1529 thread.daemon = True
1530 thread.start()
1531
Jon Salz0697cbf2012-07-04 15:14:04 +08001532 def _run_queue_idle(self):
Vic Yang4953fc12012-07-26 16:19:53 +08001533 '''Invoked when the run queue has no events.
1534
1535 This method must not raise exception.
1536 '''
Jon Salzb22d1172012-08-06 10:38:57 +08001537 now = time.time()
1538 if (self.last_idle and
1539 now < (self.last_idle + RUN_QUEUE_TIMEOUT_SECS - 1)):
1540 # Don't run more often than once every (RUN_QUEUE_TIMEOUT_SECS -
1541 # 1) seconds.
1542 return
1543
1544 self.last_idle = now
1545
Vic Yang311ddb82012-09-26 12:08:28 +08001546 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001547 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001548 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001549 self.log_disk_space_stats()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001550 self.check_core_dump()
Jon Salz57717ca2012-04-04 16:47:25 +08001551
Jon Salz16d10542012-07-23 12:18:45 +08001552 def handle_event_logs(self, log_name, chunk):
Jon Salz0697cbf2012-07-04 15:14:04 +08001553 '''Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001554
Jon Salz0697cbf2012-07-04 15:14:04 +08001555 Attempts to upload the event logs to the shopfloor server.
1556 '''
1557 description = 'event logs (%s, %d bytes)' % (log_name, len(chunk))
1558 start_time = time.time()
Jon Salz0697cbf2012-07-04 15:14:04 +08001559 shopfloor_client = shopfloor.get_instance(
1560 detect=True,
1561 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzb10cf512012-08-09 17:29:21 +08001562 shopfloor_client.UploadEvent(log_name, Binary(chunk))
Jon Salz0697cbf2012-07-04 15:14:04 +08001563 logging.info(
1564 'Successfully synced %s in %.03f s',
1565 description, time.time() - start_time)
Jon Salz57717ca2012-04-04 16:47:25 +08001566
Jon Salz0697cbf2012-07-04 15:14:04 +08001567 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1568 root=None):
1569 '''Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001570
Jon Salz0697cbf2012-07-04 15:14:04 +08001571 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001572
Jon Salz0697cbf2012-07-04 15:14:04 +08001573 Args:
1574 starting_at: If provided, only auto-runs tests beginning with
1575 this test.
1576 '''
1577 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001578
Jon Salz0697cbf2012-07-04 15:14:04 +08001579 if starting_at:
1580 # Make sure they passed a test, not a string.
1581 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001582
Jon Salz0697cbf2012-07-04 15:14:04 +08001583 tests_to_reset = []
1584 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001585
Jon Salz0697cbf2012-07-04 15:14:04 +08001586 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001587
Jon Salz0697cbf2012-07-04 15:14:04 +08001588 for test in root.get_top_level_tests():
1589 if starting_at:
1590 if test == starting_at:
1591 # We've found starting_at; do auto-run on all
1592 # subsequent tests.
1593 found_starting_at = True
1594 if not found_starting_at:
1595 # Don't start this guy yet
1596 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001597
Jon Salz0697cbf2012-07-04 15:14:04 +08001598 status = test.get_state().status
1599 if status == TestState.ACTIVE or status in statuses_to_run:
1600 # Reset the test (later; we will need to abort
1601 # all active tests first).
1602 tests_to_reset.append(test)
1603 if status in statuses_to_run:
1604 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001605
Jon Salz0697cbf2012-07-04 15:14:04 +08001606 self.abort_active_tests()
Jon Salz258a40c2012-04-19 12:34:01 +08001607
Jon Salz0697cbf2012-07-04 15:14:04 +08001608 # Reset all statuses of the tests to run (in case any tests were active;
1609 # we want them to be run again).
1610 for test_to_reset in tests_to_reset:
1611 for test in test_to_reset.walk():
1612 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001613
Jon Salz0697cbf2012-07-04 15:14:04 +08001614 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001615
Jon Salz0697cbf2012-07-04 15:14:04 +08001616 def restart_tests(self, root=None):
1617 '''Restarts all tests.'''
1618 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001619
Jon Salz0697cbf2012-07-04 15:14:04 +08001620 self.abort_active_tests()
1621 for test in root.walk():
1622 test.update_state(status=TestState.UNTESTED)
1623 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001624
Jon Salz0697cbf2012-07-04 15:14:04 +08001625 def auto_run(self, starting_at=None, root=None):
1626 '''"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001627
Jon Salz0697cbf2012-07-04 15:14:04 +08001628 Args:
1629 starting_at: If provide, only auto-runs tests beginning with
1630 this test.
1631 '''
1632 root = root or self.test_list
1633 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1634 starting_at=starting_at,
1635 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001636
Jon Salz0697cbf2012-07-04 15:14:04 +08001637 def re_run_failed(self, root=None):
1638 '''Re-runs failed tests.'''
1639 root = root or self.test_list
1640 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001641
Jon Salz0697cbf2012-07-04 15:14:04 +08001642 def show_review_information(self):
1643 '''Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001644
Jon Salz0697cbf2012-07-04 15:14:04 +08001645 The information screene is rendered by main UI program (ui.py), so in
1646 goofy we only need to kill all active tests, set them as untested, and
1647 clear remaining tests.
1648 '''
1649 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001650 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001651
Jon Salz0697cbf2012-07-04 15:14:04 +08001652 def handle_switch_test(self, event):
1653 '''Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001654
Jon Salz0697cbf2012-07-04 15:14:04 +08001655 @param event: The SWITCH_TEST event.
1656 '''
1657 test = self.test_list.lookup_path(event.path)
1658 if not test:
1659 logging.error('Unknown test %r', event.key)
1660 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001661
Jon Salz0697cbf2012-07-04 15:14:04 +08001662 invoc = self.invocations.get(test)
1663 if invoc and test.backgroundable:
1664 # Already running: just bring to the front if it
1665 # has a UI.
1666 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001667 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001668 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001669
Jon Salz0697cbf2012-07-04 15:14:04 +08001670 self.abort_active_tests()
1671 for t in test.walk():
1672 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001673
Jon Salz0697cbf2012-07-04 15:14:04 +08001674 if self.test_list.options.auto_run_on_keypress:
1675 self.auto_run(starting_at=test)
1676 else:
1677 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001678
Jon Salz0697cbf2012-07-04 15:14:04 +08001679 def wait(self):
1680 '''Waits for all pending invocations.
1681
1682 Useful for testing.
1683 '''
Jon Salz1acc8742012-07-17 17:45:55 +08001684 while self.invocations:
1685 for k, v in self.invocations.iteritems():
1686 logging.info('Waiting for %s to complete...', k)
1687 v.thread.join()
1688 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001689
1690 def check_exceptions(self):
1691 '''Raises an error if any exceptions have occurred in
1692 invocation threads.'''
1693 if self.exceptions:
1694 raise RuntimeError('Exception in invocation thread: %r' %
1695 self.exceptions)
1696
1697 def record_exception(self, msg):
1698 '''Records an exception in an invocation thread.
1699
1700 An exception with the given message will be rethrown when
1701 Goofy is destroyed.'''
1702 self.exceptions.append(msg)
Jon Salz73e0fd02012-04-04 11:46:38 +08001703
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001704
1705if __name__ == '__main__':
Jon Salz77c151e2012-08-28 07:20:37 +08001706 goofy = Goofy()
1707 try:
1708 goofy.main()
Jon Salz0f996602012-10-03 15:26:48 +08001709 except SystemExit:
1710 # Propagate SystemExit without logging.
1711 raise
Jon Salz31373eb2012-09-21 16:19:49 +08001712 except:
Jon Salz0f996602012-10-03 15:26:48 +08001713 # Log the error before trying to shut down (unless it's a graceful
1714 # exit).
Jon Salz31373eb2012-09-21 16:19:49 +08001715 logging.exception('Error in main loop')
1716 raise
Jon Salz77c151e2012-08-28 07:20:37 +08001717 finally:
1718 goofy.destroy()