blob: 31cf4ef06aa7abc03853595fff060522823560ab [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 Salze12c2b32013-06-25 16:24:34 +080012import glob
Jon Salz0405ab52012-03-16 15:26:52 +080013import logging
14import os
Jon Salz73e0fd02012-04-04 11:46:38 +080015import Queue
Jon Salze12c2b32013-06-25 16:24:34 +080016import shutil
Jon Salz77c151e2012-08-28 07:20:37 +080017import signal
Jon Salz0405ab52012-03-16 15:26:52 +080018import sys
Jon Salzeff94182013-06-19 15:06:28 +080019import syslog
Jon Salz0405ab52012-03-16 15:26:52 +080020import threading
21import time
22import traceback
Jon Salz258a40c2012-04-19 12:34:01 +080023import uuid
Jon Salzb10cf512012-08-09 17:29:21 +080024from xmlrpclib import Binary
Hung-Te Linf2f78f72012-02-08 19:27:11 +080025from collections import deque
26from optparse import OptionParser
Hung-Te Linf2f78f72012-02-08 19:27:11 +080027
Jon Salz0697cbf2012-07-04 15:14:04 +080028import factory_common # pylint: disable=W0611
jcliangcd688182012-08-20 21:01:26 +080029from cros.factory import event_log
30from cros.factory import system
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +080031from cros.factory.event_log import EventLog, FloatDigit
Tom Wai-Hong Tamd33723e2013-04-10 21:14:37 +080032from cros.factory.event_log_watcher import EventLogWatcher
jcliangcd688182012-08-20 21:01:26 +080033from cros.factory.goofy import test_environment
34from cros.factory.goofy import time_sanitizer
Jon Salz83591782012-06-26 11:09:58 +080035from cros.factory.goofy import updater
jcliangcd688182012-08-20 21:01:26 +080036from cros.factory.goofy.goofy_rpc import GoofyRPC
Jon Salz885dcac2013-07-23 16:39:50 +080037from cros.factory.goofy.invocation import TestArgEnv
jcliangcd688182012-08-20 21:01:26 +080038from cros.factory.goofy.invocation import TestInvocation
39from cros.factory.goofy.prespawner import Prespawner
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +080040from cros.factory.goofy.system_log_manager import SystemLogManager
jcliangcd688182012-08-20 21:01:26 +080041from cros.factory.goofy.web_socket_manager import WebSocketManager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +080042from cros.factory.system.board import Board, BoardException
jcliangcd688182012-08-20 21:01:26 +080043from cros.factory.system.charge_manager import ChargeManager
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +080044from cros.factory.system.core_dump_manager import CoreDumpManager
Jon Salzce6a7f82013-06-10 18:22:54 +080045from cros.factory.system.cpufreq_manager import CpufreqManager
Jon Salzb92c5112012-09-21 15:40:11 +080046from cros.factory.system import disk_space
jcliangcd688182012-08-20 21:01:26 +080047from cros.factory.test import factory
48from cros.factory.test import state
Jon Salz51528e12012-07-02 18:54:45 +080049from cros.factory.test import shopfloor
Jon Salz83591782012-06-26 11:09:58 +080050from cros.factory.test import utils
Jon Salz128b0932013-07-03 16:55:26 +080051from cros.factory.test.test_lists import test_lists
Jon Salz83591782012-06-26 11:09:58 +080052from cros.factory.test.event import Event
53from cros.factory.test.event import EventClient
54from cros.factory.test.event import EventServer
jcliangcd688182012-08-20 21:01:26 +080055from cros.factory.test.factory import TestState
Jon Salzd7550792013-07-12 05:49:27 +080056from cros.factory.test.utils import Enum
Dean Liao592e4d52013-01-10 20:06:39 +080057from cros.factory.tools.key_filter import KeyFilter
Jon Salz2af235d2013-06-24 14:47:21 +080058from cros.factory.utils import file_utils
Jon Salz78c32392012-07-25 14:18:29 +080059from cros.factory.utils.process_utils import Spawn
Hung-Te Linf2f78f72012-02-08 19:27:11 +080060
61
Hung-Te Linf2f78f72012-02-08 19:27:11 +080062HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Chun-ta Lin279e7e92013-02-19 17:40:39 +080063CACHES_DIR = os.path.join(factory.get_state_root(), "caches")
Hung-Te Linf2f78f72012-02-08 19:27:11 +080064
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +080065CLEANUP_LOGS_PAUSED = '/var/lib/cleanup_logs_paused'
66
Jon Salz8796e362012-05-24 11:39:09 +080067# File that suppresses reboot if present (e.g., for development).
68NO_REBOOT_FILE = '/var/log/factory.noreboot'
69
Jon Salz5c344f62012-07-13 14:31:16 +080070# Value for tests_after_shutdown that forces auto-run (e.g., after
71# a factory update, when the available set of tests might change).
72FORCE_AUTO_RUN = 'force_auto_run'
73
cychiang21886742012-07-05 15:16:32 +080074RUN_QUEUE_TIMEOUT_SECS = 10
75
Justin Chuang83813982013-05-13 01:26:32 +080076# Sync disks when battery level is higher than this value.
77# Otherwise, power loss during disk sync operation may incur even worse outcome.
78MIN_BATTERY_LEVEL_FOR_DISK_SYNC = 1.0
79
Jon Salze12c2b32013-06-25 16:24:34 +080080MAX_CRASH_FILE_SIZE = 64*1024
81
Jon Salz758e6cc2012-04-03 15:47:07 +080082GOOFY_IN_CHROOT_WARNING = '\n' + ('*' * 70) + '''
83You are running Goofy inside the chroot. Autotests are not supported.
84
85To use Goofy in the chroot, first install an Xvnc server:
86
Jon Salz0697cbf2012-07-04 15:14:04 +080087 sudo apt-get install tightvncserver
Jon Salz758e6cc2012-04-03 15:47:07 +080088
89...and then start a VNC X server outside the chroot:
90
Jon Salz0697cbf2012-07-04 15:14:04 +080091 vncserver :10 &
92 vncviewer :10
Jon Salz758e6cc2012-04-03 15:47:07 +080093
94...and run Goofy as follows:
95
Jon Salz0697cbf2012-07-04 15:14:04 +080096 env --unset=XAUTHORITY DISPLAY=localhost:10 python goofy.py
Jon Salz758e6cc2012-04-03 15:47:07 +080097''' + ('*' * 70)
Jon Salz73e0fd02012-04-04 11:46:38 +080098suppress_chroot_warning = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +080099
Jon Salzd7550792013-07-12 05:49:27 +0800100Status = Enum(['UNINITIALIZED', 'INITIALIZING', 'RUNNING',
101 'TERMINATING', 'TERMINATED'])
102
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800103def get_hwid_cfg():
Jon Salz0697cbf2012-07-04 15:14:04 +0800104 '''
105 Returns the HWID config tag, or an empty string if none can be found.
106 '''
107 if 'CROS_HWID' in os.environ:
108 return os.environ['CROS_HWID']
109 if os.path.exists(HWID_CFG_PATH):
110 with open(HWID_CFG_PATH, 'rt') as hwid_cfg_handle:
111 return hwid_cfg_handle.read().strip()
112 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800113
114
Jon Salz73e0fd02012-04-04 11:46:38 +0800115_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800116
117class Goofy(object):
Jon Salz0697cbf2012-07-04 15:14:04 +0800118 '''
119 The main factory flow.
120
121 Note that all methods in this class must be invoked from the main
122 (event) thread. Other threads, such as callbacks and TestInvocation
123 methods, should instead post events on the run queue.
124
125 TODO: Unit tests. (chrome-os-partner:7409)
126
127 Properties:
128 uuid: A unique UUID for this invocation of Goofy.
129 state_instance: An instance of FactoryState.
130 state_server: The FactoryState XML/RPC server.
131 state_server_thread: A thread running state_server.
132 event_server: The EventServer socket server.
133 event_server_thread: A thread running event_server.
134 event_client: A client to the event server.
135 connection_manager: The connection_manager object.
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800136 system_log_manager: The SystemLogManager object.
137 core_dump_manager: The CoreDumpManager object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800138 ui_process: The factory ui process object.
139 run_queue: A queue of callbacks to invoke from the main thread.
140 invocations: A map from FactoryTest objects to the corresponding
141 TestInvocations objects representing active tests.
142 tests_to_run: A deque of tests that should be run when the current
143 test(s) complete.
144 options: Command-line options.
145 args: Command-line args.
146 test_list: The test list.
Jon Salz128b0932013-07-03 16:55:26 +0800147 test_lists: All new-style test lists.
Jon Salz0697cbf2012-07-04 15:14:04 +0800148 event_handlers: Map of Event.Type to the method used to handle that
149 event. If the method has an 'event' argument, the event is passed
150 to the handler.
151 exceptions: Exceptions encountered in invocation threads.
Jon Salz3c493bb2013-02-07 17:24:58 +0800152 last_log_disk_space_message: The last message we logged about disk space
153 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800154 last_kick_sync_time: The last time to kick system_log_manager to sync
155 because of core dump files (to avoid kicking too soon then abort the
156 sync.)
Jon Salz416f9cc2013-05-10 18:32:50 +0800157 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800158 status: The current Goofy status (a member of the Status enum).
Jon Salz0697cbf2012-07-04 15:14:04 +0800159 '''
160 def __init__(self):
161 self.uuid = str(uuid.uuid4())
162 self.state_instance = None
163 self.state_server = None
164 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800165 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800166 self.event_server = None
167 self.event_server_thread = None
168 self.event_client = None
169 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800170 self.charge_manager = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800171 self.time_sanitizer = None
172 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800173 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800174 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800175 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800176 self.event_log = None
177 self.prespawner = None
178 self.ui_process = None
Jon Salzc79a9982012-08-30 04:42:01 +0800179 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800180 self.run_queue = Queue.Queue()
181 self.invocations = {}
182 self.tests_to_run = deque()
183 self.visible_test = None
184 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800185 self.hooks = None
Vic Yangd8990da2013-06-27 16:57:43 +0800186 self.cpu_usage_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800187
188 self.options = None
189 self.args = None
190 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800191 self.test_lists = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800192 self.on_ui_startup = []
193 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800194 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800195 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800196 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800197 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800198 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800199 self.last_log_disk_space_message = None
Justin Chuang83813982013-05-13 01:26:32 +0800200 self.last_check_battery_time = None
201 self.last_check_battery_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800202 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800203 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800204 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800205 self.key_filter = None
Jon Salzce6a7f82013-06-10 18:22:54 +0800206 self.cpufreq_manager = None
Jon Salzd7550792013-07-12 05:49:27 +0800207 self.status = Status.UNINITIALIZED
Jon Salz0697cbf2012-07-04 15:14:04 +0800208
Jon Salz85a39882012-07-05 16:45:04 +0800209 def test_or_root(event, parent_or_group=True):
210 '''Returns the test affected by a particular event.
211
212 Args:
213 event: The event containing an optional 'path' attribute.
214 parent_on_group: If True, returns the top-level parent for a test (the
215 root node of the tests that need to be run together if the given test
216 path is to be run).
217 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800218 try:
219 path = event.path
220 except AttributeError:
221 path = None
222
223 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800224 test = self.test_list.lookup_path(path)
225 if parent_or_group:
226 test = test.get_top_level_parent_or_group()
227 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800228 else:
229 return self.test_list
230
231 self.event_handlers = {
232 Event.Type.SWITCH_TEST: self.handle_switch_test,
233 Event.Type.SHOW_NEXT_ACTIVE_TEST:
234 lambda event: self.show_next_active_test(),
235 Event.Type.RESTART_TESTS:
236 lambda event: self.restart_tests(root=test_or_root(event)),
237 Event.Type.AUTO_RUN:
238 lambda event: self.auto_run(root=test_or_root(event)),
239 Event.Type.RE_RUN_FAILED:
240 lambda event: self.re_run_failed(root=test_or_root(event)),
241 Event.Type.RUN_TESTS_WITH_STATUS:
242 lambda event: self.run_tests_with_status(
243 event.status,
244 root=test_or_root(event)),
245 Event.Type.REVIEW:
246 lambda event: self.show_review_information(),
247 Event.Type.UPDATE_SYSTEM_INFO:
248 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800249 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800250 lambda event: self.stop(root=test_or_root(event, False),
Jon Salz6dc031d2013-06-19 13:06:23 +0800251 fail=getattr(event, 'fail', False),
252 reason=getattr(event, 'reason', None)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800253 Event.Type.SET_VISIBLE_TEST:
254 lambda event: self.set_visible_test(
255 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800256 Event.Type.CLEAR_STATE:
257 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800258 }
259
260 self.exceptions = []
261 self.web_socket_manager = None
262
263 def destroy(self):
Jon Salzd7550792013-07-12 05:49:27 +0800264 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800265 if self.chrome:
266 self.chrome.kill()
267 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800268 if self.dummy_shopfloor:
269 self.dummy_shopfloor.kill()
270 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800271 if self.ui_process:
272 utils.kill_process_tree(self.ui_process, 'ui')
273 self.ui_process = None
274 if self.web_socket_manager:
275 logging.info('Stopping web sockets')
276 self.web_socket_manager.close()
277 self.web_socket_manager = None
278 if self.state_server_thread:
279 logging.info('Stopping state server')
280 self.state_server.shutdown()
281 self.state_server_thread.join()
282 self.state_server.server_close()
283 self.state_server_thread = None
284 if self.state_instance:
285 self.state_instance.close()
286 if self.event_server_thread:
287 logging.info('Stopping event server')
288 self.event_server.shutdown() # pylint: disable=E1101
289 self.event_server_thread.join()
290 self.event_server.server_close()
291 self.event_server_thread = None
292 if self.log_watcher:
293 if self.log_watcher.IsThreadStarted():
294 self.log_watcher.StopWatchThread()
295 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800296 if self.system_log_manager:
297 if self.system_log_manager.IsThreadRunning():
298 self.system_log_manager.StopSyncThread()
299 self.system_log_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800300 if self.prespawner:
301 logging.info('Stopping prespawner')
302 self.prespawner.stop()
303 self.prespawner = None
304 if self.event_client:
305 logging.info('Closing event client')
306 self.event_client.close()
307 self.event_client = None
Jon Salzddf0d052013-06-18 12:52:44 +0800308 if self.cpufreq_manager:
309 self.cpufreq_manager.Stop()
Jon Salz0697cbf2012-07-04 15:14:04 +0800310 if self.event_log:
311 self.event_log.Close()
312 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800313 if self.key_filter:
314 self.key_filter.Stop()
Vic Yangd8990da2013-06-27 16:57:43 +0800315 if self.cpu_usage_watcher:
316 self.cpu_usage_watcher.terminate()
Dean Liao592e4d52013-01-10 20:06:39 +0800317
Jon Salz0697cbf2012-07-04 15:14:04 +0800318 self.check_exceptions()
319 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800320 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800321
322 def start_state_server(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800323 # Before starting state server, remount stateful partitions with
324 # no commit flag. The default commit time (commit=600) makes corruption
325 # too likely.
326 file_utils.ResetCommitTime()
327
Jon Salz0697cbf2012-07-04 15:14:04 +0800328 self.state_instance, self.state_server = (
329 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800330 self.goofy_rpc = GoofyRPC(self)
331 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800332 logging.info('Starting state server')
333 self.state_server_thread = threading.Thread(
334 target=self.state_server.serve_forever,
335 name='StateServer')
336 self.state_server_thread.start()
337
338 def start_event_server(self):
339 self.event_server = EventServer()
340 logging.info('Starting factory event server')
341 self.event_server_thread = threading.Thread(
342 target=self.event_server.serve_forever,
343 name='EventServer') # pylint: disable=E1101
344 self.event_server_thread.start()
345
346 self.event_client = EventClient(
347 callback=self.handle_event, event_loop=self.run_queue)
348
349 self.web_socket_manager = WebSocketManager(self.uuid)
350 self.state_server.add_handler("/event",
351 self.web_socket_manager.handle_web_socket)
352
353 def start_ui(self):
354 ui_proc_args = [
355 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
356 self.options.test_list]
357 if self.options.verbose:
358 ui_proc_args.append('-v')
359 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800360 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800361 logging.info('Waiting for UI to come up...')
362 self.event_client.wait(
363 lambda event: event.type == Event.Type.UI_READY)
364 logging.info('UI has started')
365
366 def set_visible_test(self, test):
367 if self.visible_test == test:
368 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800369 if test and not test.has_ui:
370 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800371
372 if test:
373 test.update_state(visible=True)
374 if self.visible_test:
375 self.visible_test.update_state(visible=False)
376 self.visible_test = test
377
Jon Salzd4306c82012-11-30 15:16:36 +0800378 def _log_startup_messages(self):
379 '''Logs the tail of var/log/messages and mosys and EC console logs.'''
380 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
381 # for factory-3004.B only. Consolidate and merge back to ToT.
382 if utils.in_chroot():
383 return
384
385 try:
386 var_log_messages = (
387 utils.var_log_messages_before_reboot())
388 logging.info(
389 'Tail of /var/log/messages before last reboot:\n'
390 '%s', ('\n'.join(
391 ' ' + x for x in var_log_messages)))
392 except: # pylint: disable=W0702
393 logging.exception('Unable to grok /var/log/messages')
394
395 try:
396 mosys_log = utils.Spawn(
397 ['mosys', 'eventlog', 'list'],
398 read_stdout=True, log_stderr_on_error=True).stdout_data
399 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
400 except: # pylint: disable=W0702
401 logging.exception('Unable to read mosys eventlog')
402
403 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800404 board = system.GetBoard()
405 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800406 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
407 except: # pylint: disable=W0702
408 logging.exception('Error retrieving EC console log')
409
Vic Yang079f9872013-07-01 11:32:00 +0800410 try:
411 board = system.GetBoard()
412 ec_panic_info = board.GetECPanicInfo()
413 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
414 except: # pylint: disable=W0702
415 logging.exception('Error retrieving EC panic info')
416
Jon Salz0697cbf2012-07-04 15:14:04 +0800417 def handle_shutdown_complete(self, test, test_state):
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800418 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800419 Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800420
Jon Salz0697cbf2012-07-04 15:14:04 +0800421 @param test: The ShutdownStep.
422 @param test_state: The test state.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800423 '''
Jon Salz0697cbf2012-07-04 15:14:04 +0800424 test_state = test.update_state(increment_shutdown_count=1)
425 logging.info('Detected shutdown (%d of %d)',
426 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800427
Jon Salz0697cbf2012-07-04 15:14:04 +0800428 def log_and_update_state(status, error_msg, **kw):
429 self.event_log.Log('rebooted',
430 status=status, error_msg=error_msg, **kw)
Jon Salzd4306c82012-11-30 15:16:36 +0800431 logging.info('Rebooted: status=%s, %s', status,
432 (('error_msg=%s' % error_msg) if error_msg else None))
Jon Salz0697cbf2012-07-04 15:14:04 +0800433 test.update_state(status=status, error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800434
Jon Salz0697cbf2012-07-04 15:14:04 +0800435 if not self.last_shutdown_time:
436 log_and_update_state(status=TestState.FAILED,
437 error_msg='Unable to read shutdown_time')
438 return
Jon Salz258a40c2012-04-19 12:34:01 +0800439
Jon Salz0697cbf2012-07-04 15:14:04 +0800440 now = time.time()
441 logging.info('%.03f s passed since reboot',
442 now - self.last_shutdown_time)
Jon Salz258a40c2012-04-19 12:34:01 +0800443
Jon Salz0697cbf2012-07-04 15:14:04 +0800444 if self.last_shutdown_time > now:
445 test.update_state(status=TestState.FAILED,
446 error_msg='Time moved backward during reboot')
447 elif (isinstance(test, factory.RebootStep) and
448 self.test_list.options.max_reboot_time_secs and
449 (now - self.last_shutdown_time >
450 self.test_list.options.max_reboot_time_secs)):
451 # A reboot took too long; fail. (We don't check this for
452 # HaltSteps, because the machine could be halted for a
453 # very long time, and even unplugged with battery backup,
454 # thus hosing the clock.)
455 log_and_update_state(
456 status=TestState.FAILED,
457 error_msg=('More than %d s elapsed during reboot '
458 '(%.03f s, from %s to %s)' % (
459 self.test_list.options.max_reboot_time_secs,
460 now - self.last_shutdown_time,
461 utils.TimeString(self.last_shutdown_time),
462 utils.TimeString(now))),
463 duration=(now-self.last_shutdown_time))
Jon Salzd4306c82012-11-30 15:16:36 +0800464 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800465 elif test_state.shutdown_count == test.iterations:
466 # Good!
467 log_and_update_state(status=TestState.PASSED,
468 duration=(now - self.last_shutdown_time),
469 error_msg='')
470 elif test_state.shutdown_count > test.iterations:
471 # Shut down too many times
472 log_and_update_state(status=TestState.FAILED,
473 error_msg='Too many shutdowns')
Jon Salzd4306c82012-11-30 15:16:36 +0800474 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800475 elif utils.are_shift_keys_depressed():
476 logging.info('Shift keys are depressed; cancelling restarts')
477 # Abort shutdown
478 log_and_update_state(
479 status=TestState.FAILED,
480 error_msg='Shutdown aborted with double shift keys')
Jon Salza6711d72012-07-18 14:33:03 +0800481 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800482 else:
483 def handler():
484 if self._prompt_cancel_shutdown(
485 test, test_state.shutdown_count + 1):
Jon Salza6711d72012-07-18 14:33:03 +0800486 factory.console.info('Shutdown aborted by operator')
Jon Salz0697cbf2012-07-04 15:14:04 +0800487 log_and_update_state(
488 status=TestState.FAILED,
489 error_msg='Shutdown aborted by operator')
Jon Salza6711d72012-07-18 14:33:03 +0800490 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800491 return
Jon Salz0405ab52012-03-16 15:26:52 +0800492
Jon Salz0697cbf2012-07-04 15:14:04 +0800493 # Time to shutdown again
494 log_and_update_state(
495 status=TestState.ACTIVE,
496 error_msg='',
497 iteration=test_state.shutdown_count)
Jon Salz73e0fd02012-04-04 11:46:38 +0800498
Jon Salz0697cbf2012-07-04 15:14:04 +0800499 self.event_log.Log('shutdown', operation='reboot')
500 self.state_instance.set_shared_data('shutdown_time',
501 time.time())
502 self.env.shutdown('reboot')
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800503
Jon Salz0697cbf2012-07-04 15:14:04 +0800504 self.on_ui_startup.append(handler)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800505
Jon Salz0697cbf2012-07-04 15:14:04 +0800506 def _prompt_cancel_shutdown(self, test, iteration):
507 if self.options.ui != 'chrome':
508 return False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800509
Jon Salz0697cbf2012-07-04 15:14:04 +0800510 pending_shutdown_data = {
511 'delay_secs': test.delay_secs,
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800512 'enable_guest_mode': test.enable_guest_mode,
Jon Salz0697cbf2012-07-04 15:14:04 +0800513 'time': time.time() + test.delay_secs,
514 'operation': test.operation,
515 'iteration': iteration,
516 'iterations': test.iterations,
517 }
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800518
Jon Salz0697cbf2012-07-04 15:14:04 +0800519 # Create a new (threaded) event client since we
520 # don't want to use the event loop for this.
521 with EventClient() as event_client:
522 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN,
523 **pending_shutdown_data))
524 aborted = event_client.wait(
525 lambda event: event.type == Event.Type.CANCEL_SHUTDOWN,
526 timeout=test.delay_secs) is not None
527 if aborted:
528 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
529 return aborted
Jon Salz258a40c2012-04-19 12:34:01 +0800530
Jon Salz0697cbf2012-07-04 15:14:04 +0800531 def init_states(self):
532 '''
533 Initializes all states on startup.
534 '''
535 for test in self.test_list.get_all_tests():
536 # Make sure the state server knows about all the tests,
537 # defaulting to an untested state.
538 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800539
Jon Salz0697cbf2012-07-04 15:14:04 +0800540 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800541 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800542 ec_console_log = None
Vic Yang079f9872013-07-01 11:32:00 +0800543 ec_panic_info = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800544
Jon Salz0697cbf2012-07-04 15:14:04 +0800545 # Any 'active' tests should be marked as failed now.
546 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800547 if not test.is_leaf():
548 # Don't bother with parents; they will be updated when their
549 # children are updated.
550 continue
551
Jon Salz0697cbf2012-07-04 15:14:04 +0800552 test_state = test.get_state()
553 if test_state.status != TestState.ACTIVE:
554 continue
555 if isinstance(test, factory.ShutdownStep):
556 # Shutdown while the test was active - that's good.
557 self.handle_shutdown_complete(test, test_state)
558 else:
559 # Unexpected shutdown. Grab /var/log/messages for context.
560 if var_log_messages is None:
561 try:
562 var_log_messages = (
563 utils.var_log_messages_before_reboot())
564 # Write it to the log, to make it easier to
565 # correlate with /var/log/messages.
566 logging.info(
567 'Unexpected shutdown. '
568 'Tail of /var/log/messages before last reboot:\n'
569 '%s', ('\n'.join(
570 ' ' + x for x in var_log_messages)))
571 except: # pylint: disable=W0702
572 logging.exception('Unable to grok /var/log/messages')
573 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800574
Jon Salz008f4ea2012-08-28 05:39:45 +0800575 if mosys_log is None and not utils.in_chroot():
576 try:
577 mosys_log = utils.Spawn(
578 ['mosys', 'eventlog', 'list'],
579 read_stdout=True, log_stderr_on_error=True).stdout_data
580 # Write it to the log also.
581 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
582 except: # pylint: disable=W0702
583 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800584
Vic Yange4c275d2012-08-28 01:50:20 +0800585 if ec_console_log is None:
586 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800587 board = system.GetBoard()
588 ec_console_log = board.GetECConsoleLog()
Vic Yange4c275d2012-08-28 01:50:20 +0800589 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Jon Salzfe1f6652012-09-07 05:40:14 +0800590 except: # pylint: disable=W0702
Vic Yange4c275d2012-08-28 01:50:20 +0800591 logging.exception('Error retrieving EC console log')
592
Vic Yang079f9872013-07-01 11:32:00 +0800593 if ec_panic_info is None:
594 try:
595 board = system.GetBoard()
596 ec_panic_info = board.GetECPanicInfo()
597 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
598 except: # pylint: disable=W0702
599 logging.exception('Error retrieving EC panic info')
600
Jon Salz0697cbf2012-07-04 15:14:04 +0800601 error_msg = 'Unexpected shutdown while test was running'
602 self.event_log.Log('end_test',
603 path=test.path,
604 status=TestState.FAILED,
605 invocation=test.get_state().invocation,
606 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800607 var_log_messages='\n'.join(var_log_messages),
608 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800609 test.update_state(
610 status=TestState.FAILED,
611 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800612
Jon Salz50efe942012-07-26 11:54:10 +0800613 if not test.never_fails:
614 # For "never_fails" tests (such as "Start"), don't cancel
615 # pending tests, since reboot is expected.
616 factory.console.info('Unexpected shutdown while test %s '
617 'running; cancelling any pending tests',
618 test.path)
619 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800620
Jon Salz008f4ea2012-08-28 05:39:45 +0800621 self.update_skipped_tests()
622
623 def update_skipped_tests(self):
624 '''
625 Updates skipped states based on run_if.
626 '''
Jon Salz885dcac2013-07-23 16:39:50 +0800627 env = TestArgEnv()
Jon Salz008f4ea2012-08-28 05:39:45 +0800628 for t in self.test_list.walk():
Jon Salz885dcac2013-07-23 16:39:50 +0800629 if t.is_leaf() and (t.run_if_table_name or t.run_if_expr):
630 value = None
631
632 if t.run_if_expr:
633 try:
634 value = t.run_if_expr(env)
635 except: # pylint: disable=W0702
636 logging.exception('Unable to evaluate run_if expression for %s',
637 t.path)
638 # But keep going; we have no choice. This will end up
639 # always activating the test.
640 else:
641 try:
642 aux = shopfloor.get_selected_aux_data(t.run_if_table_name)
643 value = aux.get(t.run_if_col)
644 except ValueError:
645 # Not available; assume it shouldn't be skipped
646 pass
647
648 if value is None:
649 skip = False
650 else:
651 skip = (not value) ^ t.run_if_not
Jon Salz008f4ea2012-08-28 05:39:45 +0800652
653 test_state = t.get_state()
654 if ((not skip) and
655 (test_state.status == TestState.PASSED) and
656 (test_state.error_msg == TestState.SKIPPED_MSG)):
657 # It was marked as skipped before, but now we need to run it.
658 # Mark as untested.
659 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
660 else:
661 t.update_state(skip=skip)
662
Jon Salz0697cbf2012-07-04 15:14:04 +0800663 def show_next_active_test(self):
664 '''
665 Rotates to the next visible active test.
666 '''
667 self.reap_completed_tests()
668 active_tests = [
669 t for t in self.test_list.walk()
670 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
671 if not active_tests:
672 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800673
Jon Salz0697cbf2012-07-04 15:14:04 +0800674 try:
675 next_test = active_tests[
676 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
677 except ValueError: # visible_test not present in active_tests
678 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800679
Jon Salz0697cbf2012-07-04 15:14:04 +0800680 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800681
Jon Salz0697cbf2012-07-04 15:14:04 +0800682 def handle_event(self, event):
683 '''
684 Handles an event from the event server.
685 '''
686 handler = self.event_handlers.get(event.type)
687 if handler:
688 handler(event)
689 else:
690 # We don't register handlers for all event types - just ignore
691 # this event.
692 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800693
Vic Yangaabf9fd2013-04-09 18:56:13 +0800694 def check_critical_factory_note(self):
695 '''
696 Returns True if the last factory note is critical.
697 '''
698 notes = self.state_instance.get_shared_data('factory_note', True)
699 return notes and notes[-1]['level'] == 'CRITICAL'
700
Jon Salz0697cbf2012-07-04 15:14:04 +0800701 def run_next_test(self):
702 '''
703 Runs the next eligible test (or tests) in self.tests_to_run.
704 '''
705 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800706 if self.tests_to_run and self.check_critical_factory_note():
707 self.tests_to_run.clear()
708 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800709 while self.tests_to_run:
710 logging.debug('Tests to run: %s',
711 [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800712
Jon Salz0697cbf2012-07-04 15:14:04 +0800713 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800714
Jon Salz0697cbf2012-07-04 15:14:04 +0800715 if test in self.invocations:
716 logging.info('Next test %s is already running', test.path)
717 self.tests_to_run.popleft()
718 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800719
Jon Salza1412922012-07-23 16:04:17 +0800720 for requirement in test.require_run:
721 for i in requirement.test.walk():
722 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800723 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800724 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800725 return
726
Jon Salz0697cbf2012-07-04 15:14:04 +0800727 if self.invocations and not (test.backgroundable and all(
728 [x.backgroundable for x in self.invocations])):
729 logging.debug('Waiting for non-backgroundable tests to '
730 'complete before running %s', test.path)
731 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800732
Jon Salz3e6f5202012-10-15 15:08:29 +0800733 if test.get_state().skip:
734 factory.console.info('Skipping test %s', test.path)
735 test.update_state(status=TestState.PASSED,
736 error_msg=TestState.SKIPPED_MSG)
737 self.tests_to_run.popleft()
738 continue
739
Jon Salz0697cbf2012-07-04 15:14:04 +0800740 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800741
Jon Salz304a75d2012-07-06 11:14:15 +0800742 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800743 for requirement in test.require_run:
744 for i in requirement.test.walk():
745 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800746 # We've hit this test itself; stop checking
747 break
Jon Salza1412922012-07-23 16:04:17 +0800748 if ((i.get_state().status == TestState.UNTESTED) or
749 (requirement.passed and i.get_state().status !=
750 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800751 # Found an untested test; move on to the next
752 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800753 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800754 break
755
756 if untested:
757 untested_paths = ', '.join(sorted([x.path for x in untested]))
758 if self.state_instance.get_shared_data('engineering_mode',
759 optional=True):
760 # In engineering mode, we'll let it go.
761 factory.console.warn('In engineering mode; running '
762 '%s even though required tests '
763 '[%s] have not completed',
764 test.path, untested_paths)
765 else:
766 # Not in engineering mode; mark it failed.
767 error_msg = ('Required tests [%s] have not been run yet'
768 % untested_paths)
769 factory.console.error('Not running %s: %s',
770 test.path, error_msg)
771 test.update_state(status=TestState.FAILED,
772 error_msg=error_msg)
773 continue
774
Jon Salz0697cbf2012-07-04 15:14:04 +0800775 if isinstance(test, factory.ShutdownStep):
776 if os.path.exists(NO_REBOOT_FILE):
777 test.update_state(
778 status=TestState.FAILED, increment_count=1,
779 error_msg=('Skipped shutdown since %s is present' %
Jon Salz304a75d2012-07-06 11:14:15 +0800780 NO_REBOOT_FILE))
Jon Salz0697cbf2012-07-04 15:14:04 +0800781 continue
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800782
Jon Salz0697cbf2012-07-04 15:14:04 +0800783 test.update_state(status=TestState.ACTIVE, increment_count=1,
784 error_msg='', shutdown_count=0)
785 if self._prompt_cancel_shutdown(test, 1):
786 self.event_log.Log('reboot_cancelled')
787 test.update_state(
788 status=TestState.FAILED, increment_count=1,
789 error_msg='Shutdown aborted by operator',
790 shutdown_count=0)
chungyiafe8f772012-08-15 19:36:29 +0800791 continue
Jon Salz2f757d42012-06-27 17:06:42 +0800792
Jon Salz0697cbf2012-07-04 15:14:04 +0800793 # Save pending test list in the state server
Jon Salzdbf398f2012-06-14 17:30:01 +0800794 self.state_instance.set_shared_data(
Jon Salz0697cbf2012-07-04 15:14:04 +0800795 'tests_after_shutdown',
796 [t.path for t in self.tests_to_run])
797 # Save shutdown time
798 self.state_instance.set_shared_data('shutdown_time',
799 time.time())
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800800
Jon Salz0697cbf2012-07-04 15:14:04 +0800801 with self.env.lock:
802 self.event_log.Log('shutdown', operation=test.operation)
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800803 if (test.enable_guest_mode and
804 not os.path.exists(
805 test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)):
806 # Create a temporary file GUEST_MODE_TAG_FILE to enable guest mode
807 # on next boot.
808 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
Jon Salz0697cbf2012-07-04 15:14:04 +0800809 shutdown_result = self.env.shutdown(test.operation)
810 if shutdown_result:
811 # That's all, folks!
812 self.run_queue.put(None)
813 return
814 else:
815 # Just pass (e.g., in the chroot).
816 test.update_state(status=TestState.PASSED)
817 self.state_instance.set_shared_data(
818 'tests_after_shutdown', None)
819 # Send event with no fields to indicate that there is no
820 # longer a pending shutdown.
821 self.event_client.post_event(Event(
822 Event.Type.PENDING_SHUTDOWN))
823 continue
Jon Salz258a40c2012-04-19 12:34:01 +0800824
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800825 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800826
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800827 def _run_test(self, test, iterations_left=None, retries_left=None):
Jon Salz1acc8742012-07-17 17:45:55 +0800828 invoc = TestInvocation(self, test, on_completion=self.run_next_test)
829 new_state = test.update_state(
830 status=TestState.ACTIVE, increment_count=1, error_msg='',
Jon Salzbd42ce12012-09-18 08:03:59 +0800831 invocation=invoc.uuid, iterations_left=iterations_left,
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800832 retries_left=retries_left,
Jon Salzbd42ce12012-09-18 08:03:59 +0800833 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800834 invoc.count = new_state.count
835
836 self.invocations[test] = invoc
837 if self.visible_test is None and test.has_ui:
838 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800839 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800840 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800841
Vic Yang311ddb82012-09-26 12:08:28 +0800842 def check_exclusive(self):
Jon Salzce6a7f82013-06-10 18:22:54 +0800843 # alias since this is really long
844 EXCL_OPT = factory.FactoryTest.EXCLUSIVE_OPTIONS
845
Vic Yang311ddb82012-09-26 12:08:28 +0800846 current_exclusive_items = set([
Jon Salzce6a7f82013-06-10 18:22:54 +0800847 item for item in EXCL_OPT
Vic Yang311ddb82012-09-26 12:08:28 +0800848 if any([test.is_exclusive(item) for test in self.invocations])])
849
850 new_exclusive_items = current_exclusive_items - self.exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800851 if EXCL_OPT.NETWORKING in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800852 logging.info('Disabling network')
853 self.connection_manager.DisableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800854 if EXCL_OPT.CHARGER in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800855 logging.info('Stop controlling charger')
856
857 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800858 if EXCL_OPT.NETWORKING in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800859 logging.info('Re-enabling network')
860 self.connection_manager.EnableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800861 if EXCL_OPT.CHARGER in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800862 logging.info('Start controlling charger')
863
Jon Salzce6a7f82013-06-10 18:22:54 +0800864 if self.cpufreq_manager:
865 enabled = EXCL_OPT.CPUFREQ not in current_exclusive_items
866 try:
867 self.cpufreq_manager.SetEnabled(enabled)
868 except: # pylint: disable=W0702
869 logging.exception('Unable to %s cpufreq services',
870 'enable' if enabled else 'disable')
871
Vic Yang311ddb82012-09-26 12:08:28 +0800872 # Only adjust charge state if not excluded
Jon Salzce6a7f82013-06-10 18:22:54 +0800873 if (EXCL_OPT.CHARGER not in current_exclusive_items and
874 not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800875 if self.charge_manager:
876 self.charge_manager.AdjustChargeState()
877 else:
878 try:
879 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
880 except BoardException:
881 logging.exception('Unable to set charge state on this board')
Vic Yang311ddb82012-09-26 12:08:28 +0800882
883 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800884
cychiang21886742012-07-05 15:16:32 +0800885 def check_for_updates(self):
886 '''
887 Schedules an asynchronous check for updates if necessary.
888 '''
889 if not self.test_list.options.update_period_secs:
890 # Not enabled.
891 return
892
893 now = time.time()
894 if self.last_update_check and (
895 now - self.last_update_check <
896 self.test_list.options.update_period_secs):
897 # Not yet time for another check.
898 return
899
900 self.last_update_check = now
901
902 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
903 if reached_shopfloor:
904 new_update_md5sum = md5sum if needs_update else None
905 if system.SystemInfo.update_md5sum != new_update_md5sum:
906 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
907 system.SystemInfo.update_md5sum = new_update_md5sum
908 self.run_queue.put(self.update_system_info)
909
910 updater.CheckForUpdateAsync(
911 handle_check_for_update,
912 self.test_list.options.shopfloor_timeout_secs)
913
Jon Salza6711d72012-07-18 14:33:03 +0800914 def cancel_pending_tests(self):
915 '''Cancels any tests in the run queue.'''
916 self.run_tests([])
917
Jon Salz0697cbf2012-07-04 15:14:04 +0800918 def run_tests(self, subtrees, untested_only=False):
919 '''
920 Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800921
Jon Salz0697cbf2012-07-04 15:14:04 +0800922 The tests are run in order unless one fails (then stops).
923 Backgroundable tests are run simultaneously; when a foreground test is
924 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800925
Jon Salz0697cbf2012-07-04 15:14:04 +0800926 @param subtrees: Node or nodes containing tests to run (may either be
927 a single test or a list). Duplicates will be ignored.
928 '''
929 if type(subtrees) != list:
930 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800931
Jon Salz0697cbf2012-07-04 15:14:04 +0800932 # Nodes we've seen so far, to avoid duplicates.
933 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800934
Jon Salz0697cbf2012-07-04 15:14:04 +0800935 self.tests_to_run = deque()
936 for subtree in subtrees:
937 for test in subtree.walk():
938 if test in seen:
939 continue
940 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800941
Jon Salz0697cbf2012-07-04 15:14:04 +0800942 if not test.is_leaf():
943 continue
944 if (untested_only and
945 test.get_state().status != TestState.UNTESTED):
946 continue
947 self.tests_to_run.append(test)
948 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800949
Jon Salz0697cbf2012-07-04 15:14:04 +0800950 def reap_completed_tests(self):
951 '''
952 Removes completed tests from the set of active tests.
953
954 Also updates the visible test if it was reaped.
955 '''
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800956 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800957 for t, v in dict(self.invocations).iteritems():
958 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800959 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800960 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800961 del self.invocations[t]
962
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800963 # Stop on failure if flag is true.
964 if (self.test_list.options.stop_on_failure and
965 new_state.status == TestState.FAILED):
966 # Clean all the tests to cause goofy to stop.
967 self.tests_to_run = []
968 factory.console.info("Stop on failure triggered. Empty the queue.")
969
Jon Salz1acc8742012-07-17 17:45:55 +0800970 if new_state.iterations_left and new_state.status == TestState.PASSED:
971 # Play it again, Sam!
972 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800973 # new_state.retries_left is obtained after update.
974 # For retries_left == 0, test can still be run for the last time.
975 elif (new_state.retries_left >= 0 and
976 new_state.status == TestState.FAILED):
977 # Still have to retry, Sam!
978 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +0800979
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800980 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +0800981 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800982
Jon Salz0697cbf2012-07-04 15:14:04 +0800983 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +0800984 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +0800985 self.set_visible_test(None)
986 # Make the first running test, if any, the visible test
987 for t in self.test_list.walk():
988 if t in self.invocations:
989 self.set_visible_test(t)
990 break
991
Jon Salz6dc031d2013-06-19 13:06:23 +0800992 def kill_active_tests(self, abort, root=None, reason=None):
Jon Salz0697cbf2012-07-04 15:14:04 +0800993 '''
994 Kills and waits for all active tests.
995
Jon Salz85a39882012-07-05 16:45:04 +0800996 Args:
997 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +0800998 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +0800999 root: If set, only kills tests with root as an ancestor.
Jon Salz0697cbf2012-07-04 15:14:04 +08001000 '''
1001 self.reap_completed_tests()
1002 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +08001003 if root and not test.has_ancestor(root):
1004 continue
1005
Jon Salz0697cbf2012-07-04 15:14:04 +08001006 factory.console.info('Killing active test %s...' % test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +08001007 invoc.abort_and_join(reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001008 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +08001009 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +08001010 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +08001011
Jon Salz0697cbf2012-07-04 15:14:04 +08001012 if not abort:
1013 test.update_state(status=TestState.UNTESTED)
1014 self.reap_completed_tests()
1015
Jon Salz6dc031d2013-06-19 13:06:23 +08001016 def stop(self, root=None, fail=False, reason=None):
1017 self.kill_active_tests(fail, root, reason)
Jon Salz85a39882012-07-05 16:45:04 +08001018 # Remove any tests in the run queue under the root.
1019 self.tests_to_run = deque([x for x in self.tests_to_run
1020 if root and not x.has_ancestor(root)])
1021 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +08001022
Jon Salz4712ac72013-02-07 17:12:05 +08001023 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +08001024 if root is None:
1025 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +08001026 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +08001027 for f in root.walk():
1028 if f.is_leaf():
1029 f.update_state(status=TestState.UNTESTED)
1030
Jon Salz6dc031d2013-06-19 13:06:23 +08001031 def abort_active_tests(self, reason=None):
1032 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001033
1034 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +08001035 syslog.openlog('goofy')
1036
Jon Salz0697cbf2012-07-04 15:14:04 +08001037 try:
Jon Salzd7550792013-07-12 05:49:27 +08001038 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +08001039 self.init()
1040 self.event_log.Log('goofy_init',
1041 success=True)
1042 except:
1043 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001044 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001045 self.event_log.Log('goofy_init',
1046 success=False,
1047 trace=traceback.format_exc())
1048 except: # pylint: disable=W0702
1049 pass
1050 raise
1051
Jon Salzd7550792013-07-12 05:49:27 +08001052 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +08001053 syslog.syslog('Goofy (factory test harness) starting')
Jon Salz0697cbf2012-07-04 15:14:04 +08001054 self.run()
1055
1056 def update_system_info(self):
1057 '''Updates system info.'''
1058 system_info = system.SystemInfo()
1059 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1060 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1061 system_info=system_info.__dict__))
1062 logging.info('System info: %r', system_info.__dict__)
1063
Jon Salzeb42f0d2012-07-27 19:14:04 +08001064 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
1065 '''Commences updating factory software.
1066
1067 Args:
1068 auto_run_on_restart: Auto-run when the machine comes back up.
1069 post_update_hook: Code to call after update but immediately before
1070 restart.
1071
1072 Returns:
1073 Never if the update was successful (we just reboot).
1074 False if the update was unnecessary (no update available).
1075 '''
Jon Salz6dc031d2013-06-19 13:06:23 +08001076 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +08001077 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001078
Jon Salz5c344f62012-07-13 14:31:16 +08001079 def pre_update_hook():
1080 if auto_run_on_restart:
1081 self.state_instance.set_shared_data('tests_after_shutdown',
1082 FORCE_AUTO_RUN)
1083 self.state_instance.close()
1084
Jon Salzeb42f0d2012-07-27 19:14:04 +08001085 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1086 if post_update_hook:
1087 post_update_hook()
1088 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001089
Jon Salzcef132a2012-08-30 04:58:08 +08001090 def handle_sigint(self, dummy_signum, dummy_frame):
Jon Salz77c151e2012-08-28 07:20:37 +08001091 logging.error('Received SIGINT')
1092 self.run_queue.put(None)
1093 raise KeyboardInterrupt()
1094
Jon Salze12c2b32013-06-25 16:24:34 +08001095 def find_kcrashes(self):
1096 """Finds kcrash files, logs them, and marks them as seen."""
1097 seen_crashes = set(
1098 self.state_instance.get_shared_data('seen_crashes', optional=True)
1099 or [])
1100
1101 for path in glob.glob('/var/spool/crash/*'):
1102 if not os.path.isfile(path):
1103 continue
1104 if path in seen_crashes:
1105 continue
1106 try:
1107 stat = os.stat(path)
1108 mtime = utils.TimeString(stat.st_mtime)
1109 logging.info(
1110 'Found new crash file %s (%d bytes at %s)',
1111 path, stat.st_size, mtime)
1112 extra_log_args = {}
1113
1114 try:
1115 _, ext = os.path.splitext(path)
1116 if ext in ['.kcrash', '.meta']:
1117 ext = ext.replace('.', '')
1118 with open(path) as f:
1119 data = f.read(MAX_CRASH_FILE_SIZE)
1120 tell = f.tell()
1121 logging.info(
1122 'Contents of %s%s:%s',
1123 path,
1124 ('' if tell == stat.st_size
1125 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
1126 ('\n' + data).replace('\n', '\n ' + ext + '> '))
1127 extra_log_args['data'] = data
1128
1129 # Copy to /var/factory/kcrash for posterity
1130 kcrash_dir = factory.get_factory_root('kcrash')
1131 utils.TryMakeDirs(kcrash_dir)
1132 shutil.copy(path, kcrash_dir)
1133 logging.info('Copied to %s',
1134 os.path.join(kcrash_dir, os.path.basename(path)))
1135 finally:
1136 # Even if something goes wrong with the above, still try to
1137 # log to event log
1138 self.event_log.Log('crash_file',
1139 path=path, size=stat.st_size, mtime=mtime,
1140 **extra_log_args)
1141 except: # pylint: disable=W0702
1142 logging.exception('Unable to handle crash files %s', path)
1143 seen_crashes.add(path)
1144
1145 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
1146
Jon Salz128b0932013-07-03 16:55:26 +08001147 def GetTestList(self, test_list_id):
1148 """Returns the test list with the given ID.
1149
1150 Raises:
1151 TestListError: The test list ID is not valid.
1152 """
1153 try:
1154 return self.test_lists[test_list_id]
1155 except KeyError:
1156 raise test_lists.TestListError(
1157 '%r is not a valid test list ID (available IDs are [%s])' % (
1158 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
1159
1160 def InitTestLists(self):
1161 """Reads in all test lists and sets the active test list."""
1162 self.test_lists = test_lists.BuildAllTestLists()
Jon Salzd7550792013-07-12 05:49:27 +08001163 logging.info('Loaded test lists: [%s]',
1164 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +08001165
1166 if not self.options.test_list:
1167 self.options.test_list = test_lists.GetActiveTestListId()
1168
1169 if os.sep in self.options.test_list:
1170 # It's a path pointing to an old-style test list; use it.
1171 self.test_list = factory.read_test_list(self.options.test_list)
1172 else:
1173 self.test_list = self.GetTestList(self.options.test_list)
1174
1175 logging.info('Active test list: %s', self.test_list.test_list_id)
1176
1177 if isinstance(self.test_list, test_lists.OldStyleTestList):
1178 # Actually load it in. (See OldStyleTestList for an explanation
1179 # of why this is necessary.)
1180 self.test_list = self.test_list.Load()
1181
1182 self.test_list.state_instance = self.state_instance
1183
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001184 def init_hooks(self):
1185 """Initializes hooks.
1186
1187 Must run after self.test_list ready.
1188 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001189 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
1190 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
1191 assert isinstance(self.hooks, factory.Hooks), (
1192 "hooks should be of type Hooks but is %r" % type(self.hooks))
1193 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001194 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001195
Jon Salz0697cbf2012-07-04 15:14:04 +08001196 def init(self, args=None, env=None):
1197 '''Initializes Goofy.
1198
1199 Args:
1200 args: A list of command-line arguments. Uses sys.argv if
1201 args is None.
1202 env: An Environment instance to use (or None to choose
1203 FakeChrootEnvironment or DUTEnvironment as appropriate).
1204 '''
Jon Salz77c151e2012-08-28 07:20:37 +08001205 signal.signal(signal.SIGINT, self.handle_sigint)
1206
Jon Salz0697cbf2012-07-04 15:14:04 +08001207 parser = OptionParser()
1208 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001209 action='store_true',
1210 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001211 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001212 metavar='FILE',
1213 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001214 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001215 action='store_true',
1216 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001217 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz8fa8e832012-07-13 19:04:09 +08001218 choices=['none', 'gtk', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001219 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001220 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001221 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001222 type='int', default=1,
1223 help=('Factor by which to scale UI '
1224 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001225 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001226 metavar='FILE',
1227 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001228 parser.add_option('--dummy_shopfloor', action='store_true',
1229 help='Use a dummy shopfloor server')
chungyiafe8f772012-08-15 19:36:29 +08001230 parser.add_option('--automation', dest='automation',
1231 action='store_true',
1232 help='Enable automation on running factory test')
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001233 parser.add_option('--guest_login', dest='guest_login', default=False,
Ricky Liangb2432362013-10-02 13:12:41 +08001234 action='store_true',
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001235 help='Log in as guest. This will not own the TPM.')
Jon Salz0697cbf2012-07-04 15:14:04 +08001236 (self.options, self.args) = parser.parse_args(args)
1237
Jon Salz46b89562012-07-05 11:49:22 +08001238 # Make sure factory directories exist.
1239 factory.get_log_root()
1240 factory.get_state_root()
1241 factory.get_test_data_root()
1242
Jon Salz0697cbf2012-07-04 15:14:04 +08001243 global _inited_logging # pylint: disable=W0603
1244 if not _inited_logging:
1245 factory.init_logging('goofy', verbose=self.options.verbose)
1246 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001247
Jon Salz0f996602012-10-03 15:26:48 +08001248 if self.options.print_test_list:
1249 print factory.read_test_list(
1250 self.options.print_test_list).__repr__(recursive=True)
1251 sys.exit(0)
1252
Jon Salzee85d522012-07-17 14:34:46 +08001253 event_log.IncrementBootSequence()
Jon Salzd15bbcf2013-05-21 17:33:57 +08001254 # Don't defer logging the initial event, so we can make sure
1255 # that device_id, reimage_id, etc. are all set up.
1256 self.event_log = EventLog('goofy', defer=False)
Jon Salz0697cbf2012-07-04 15:14:04 +08001257
1258 if (not suppress_chroot_warning and
1259 factory.in_chroot() and
1260 self.options.ui == 'gtk' and
1261 os.environ.get('DISPLAY') in [None, '', ':0', ':0.0']):
1262 # That's not going to work! Tell the user how to run
1263 # this way.
1264 logging.warn(GOOFY_IN_CHROOT_WARNING)
1265 time.sleep(1)
1266
1267 if env:
1268 self.env = env
1269 elif factory.in_chroot():
1270 self.env = test_environment.FakeChrootEnvironment()
1271 logging.warn(
1272 'Using chroot environment: will not actually run autotests')
1273 else:
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001274 if self.options.guest_login:
1275 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
1276 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001277 self.env.goofy = self
1278
1279 if self.options.restart:
1280 state.clear_state()
1281
Jon Salz0697cbf2012-07-04 15:14:04 +08001282 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1283 logging.warn(
1284 'In QEMU; ignoring ui_scale_factor argument')
1285 self.options.ui_scale_factor = 1
1286
1287 logging.info('Started')
1288
1289 self.start_state_server()
1290 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1291 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001292 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001293 self.last_shutdown_time = (
1294 self.state_instance.get_shared_data('shutdown_time', optional=True))
1295 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001296 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001297
Jon Salz128b0932013-07-03 16:55:26 +08001298 try:
1299 self.InitTestLists()
1300 except: # pylint: disable=W0702
1301 logging.exception('Unable to initialize test lists')
1302 self.state_instance.set_shared_data(
1303 'startup_error',
1304 'Unable to initialize test lists\n%s' % (
1305 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001306 if self.options.ui == 'chrome':
1307 # Create an empty test list with default options so that the rest of
1308 # startup can proceed.
1309 self.test_list = factory.FactoryTestList(
1310 [], self.state_instance, factory.Options())
1311 else:
1312 # Bail with an error; no point in starting up.
1313 sys.exit('No valid test list; exiting.')
1314
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001315 self.init_hooks()
1316
Jon Salz822838b2013-03-25 17:32:33 +08001317 if self.test_list.options.clear_state_on_start:
1318 self.state_instance.clear_test_state()
1319
Vic Yang3e1cf5d2013-06-05 18:50:24 +08001320 if system.SystemInfo().firmware_version is None and not utils.in_chroot():
Vic Yang9bd4f772013-06-04 17:34:00 +08001321 self.state_instance.set_shared_data('startup_error',
1322 'Netboot firmware detected\n'
1323 'Connect Ethernet and reboot to re-image.\n'
1324 u'侦测到网路开机固件\n'
1325 u'请连接乙太网并重启')
1326
Jon Salz0697cbf2012-07-04 15:14:04 +08001327 if not self.state_instance.has_shared_data('ui_lang'):
1328 self.state_instance.set_shared_data('ui_lang',
1329 self.test_list.options.ui_lang)
1330 self.state_instance.set_shared_data(
1331 'test_list_options',
1332 self.test_list.options.__dict__)
1333 self.state_instance.test_list = self.test_list
1334
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001335 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001336
Jon Salz23926422012-09-01 03:38:13 +08001337 if self.options.dummy_shopfloor:
1338 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1339 'http://localhost:%d/' % shopfloor.DEFAULT_SERVER_PORT)
1340 self.dummy_shopfloor = Spawn(
1341 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1342 '--dummy'])
1343 elif self.test_list.options.shopfloor_server_url:
1344 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001345 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001346
Jon Salz0f996602012-10-03 15:26:48 +08001347 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001348 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1349 base_time=time_sanitizer.GetBaseTimeFromFile(
1350 # lsb-factory is written by the factory install shim during
1351 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001352 # the mini-Omaha server. If it's not available, we'll use
1353 # /etc/lsb-factory (which will be much older, but reasonably
1354 # sane) and rely on a shopfloor sync to set a more accurate
1355 # time.
1356 '/usr/local/etc/lsb-factory',
1357 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001358 self.time_sanitizer.RunOnce()
1359
Vic Yangd8990da2013-06-27 16:57:43 +08001360 if self.test_list.options.check_cpu_usage_period_secs:
1361 self.cpu_usage_watcher = Spawn(['py/tools/cpu_usage_monitor.py',
1362 '-p', str(self.test_list.options.check_cpu_usage_period_secs)],
1363 cwd=factory.FACTORY_PATH)
1364
Jon Salz0697cbf2012-07-04 15:14:04 +08001365 self.init_states()
1366 self.start_event_server()
1367 self.connection_manager = self.env.create_connection_manager(
Tai-Hsu Lin371351a2012-08-27 14:17:14 +08001368 self.test_list.options.wlans,
1369 self.test_list.options.scan_wifi_period_secs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001370 # Note that we create a log watcher even if
1371 # sync_event_log_period_secs isn't set (no background
1372 # syncing), since we may use it to flush event logs as well.
1373 self.log_watcher = EventLogWatcher(
1374 self.test_list.options.sync_event_log_period_secs,
Jon Salzd15bbcf2013-05-21 17:33:57 +08001375 event_log_db_file=None,
Jon Salz16d10542012-07-23 12:18:45 +08001376 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001377 if self.test_list.options.sync_event_log_period_secs:
1378 self.log_watcher.StartWatchThread()
1379
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001380 # Note that we create a system log manager even if
1381 # sync_log_period_secs isn't set (no background
1382 # syncing), since we may kick it to sync logs in its
1383 # thread.
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001384 if self.test_list.options.enable_sync_log:
1385 self.system_log_manager = SystemLogManager(
1386 sync_log_paths=self.test_list.options.sync_log_paths,
1387 sync_period_sec=self.test_list.options.sync_log_period_secs,
1388 clear_log_paths=self.test_list.options.clear_log_paths)
1389 self.system_log_manager.StartSyncThread()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001390
Jon Salz0697cbf2012-07-04 15:14:04 +08001391 self.update_system_info()
1392
Vic Yang4953fc12012-07-26 16:19:53 +08001393 assert ((self.test_list.options.min_charge_pct is None) ==
1394 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001395 if utils.in_chroot():
1396 logging.info('In chroot, ignoring charge manager and charge state')
1397 elif self.test_list.options.min_charge_pct is not None:
Vic Yang4953fc12012-07-26 16:19:53 +08001398 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1399 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001400 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001401 else:
1402 # Goofy should set charger state to charge if charge_manager is disabled.
1403 try:
1404 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
1405 except BoardException:
1406 logging.exception('Unable to set charge state on this board')
Vic Yang4953fc12012-07-26 16:19:53 +08001407
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001408 self.core_dump_manager = CoreDumpManager(
1409 self.test_list.options.core_dump_watchlist)
1410
Jon Salz0697cbf2012-07-04 15:14:04 +08001411 os.environ['CROS_FACTORY'] = '1'
1412 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1413
1414 # Set CROS_UI since some behaviors in ui.py depend on the
1415 # particular UI in use. TODO(jsalz): Remove this (and all
1416 # places it is used) when the GTK UI is removed.
1417 os.environ['CROS_UI'] = self.options.ui
1418
Shuo-Peng Liao1ff502e2013-06-30 18:37:02 +08001419 if not utils.in_chroot() and self.test_list.options.use_cpufreq_manager:
Jon Salzddf0d052013-06-18 12:52:44 +08001420 self.cpufreq_manager = CpufreqManager(event_log=self.event_log)
Jon Salzce6a7f82013-06-10 18:22:54 +08001421
Justin Chuang31b02432013-06-27 15:16:51 +08001422 # Startup hooks may want to skip some tests.
1423 self.update_skipped_tests()
Jon Salz416f9cc2013-05-10 18:32:50 +08001424
Jon Salze12c2b32013-06-25 16:24:34 +08001425 self.find_kcrashes()
1426
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001427 # Should not move earlier.
1428 self.hooks.OnStartup()
1429
Jon Salz0697cbf2012-07-04 15:14:04 +08001430 if self.options.ui == 'chrome':
1431 self.env.launch_chrome()
1432 logging.info('Waiting for a web socket connection')
Cheng-Yi Chiangfd8ed392013-03-08 21:37:31 +08001433 self.web_socket_manager.wait()
Jon Salz0697cbf2012-07-04 15:14:04 +08001434
1435 # Wait for the test widget size to be set; this is done in
1436 # an asynchronous RPC so there is a small chance that the
1437 # web socket might be opened first.
1438 for _ in range(100): # 10 s
1439 try:
1440 if self.state_instance.get_shared_data('test_widget_size'):
1441 break
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001442 except KeyError:
Jon Salz0697cbf2012-07-04 15:14:04 +08001443 pass # Retry
1444 time.sleep(0.1) # 100 ms
1445 else:
1446 logging.warn('Never received test_widget_size from UI')
Jon Salz45297282013-05-18 14:31:47 +08001447
1448 # Send Chrome a Tab to get focus to the factory UI
1449 # (http://crosbug.com/p/19444). TODO(jsalz): remove this hack
1450 # and figure out the right way to get the focus to Chrome.
1451 if not utils.in_chroot():
Ricky Liangb97f3652013-08-20 17:30:28 +08001452 utils.SendKey('Tab')
Jon Salz0697cbf2012-07-04 15:14:04 +08001453 elif self.options.ui == 'gtk':
1454 self.start_ui()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001455
Ricky Liang650f6bf2012-09-28 13:22:54 +08001456 # Create download path for autotest beforehand or autotests run at
1457 # the same time might fail due to race condition.
1458 if not factory.in_chroot():
1459 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1460 'download'))
1461
Jon Salz0697cbf2012-07-04 15:14:04 +08001462 def state_change_callback(test, test_state):
1463 self.event_client.post_event(
1464 Event(Event.Type.STATE_CHANGE,
1465 path=test.path, state=test_state))
1466 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001467
Jon Salza6711d72012-07-18 14:33:03 +08001468 for handler in self.on_ui_startup:
1469 handler()
1470
1471 self.prespawner = Prespawner()
1472 self.prespawner.start()
1473
Jon Salz0697cbf2012-07-04 15:14:04 +08001474 try:
1475 tests_after_shutdown = self.state_instance.get_shared_data(
1476 'tests_after_shutdown')
1477 except KeyError:
1478 tests_after_shutdown = None
Jon Salz57717ca2012-04-04 16:47:25 +08001479
Jon Salz5c344f62012-07-13 14:31:16 +08001480 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1481 if not force_auto_run and tests_after_shutdown is not None:
Jon Salz0697cbf2012-07-04 15:14:04 +08001482 logging.info('Resuming tests after shutdown: %s',
1483 tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001484 self.tests_to_run.extend(
1485 self.test_list.lookup_path(t) for t in tests_after_shutdown)
1486 self.run_queue.put(self.run_next_test)
1487 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001488 if force_auto_run or self.test_list.options.auto_run_on_start:
Jon Salz0697cbf2012-07-04 15:14:04 +08001489 self.run_queue.put(
1490 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001491 self.state_instance.set_shared_data('tests_after_shutdown', None)
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001492
Dean Liao592e4d52013-01-10 20:06:39 +08001493 self.may_disable_cros_shortcut_keys()
1494
1495 def may_disable_cros_shortcut_keys(self):
1496 test_options = self.test_list.options
1497 if test_options.disable_cros_shortcut_keys:
1498 logging.info('Filter ChromeOS shortcut keys.')
1499 self.key_filter = KeyFilter(
1500 unmap_caps_lock=test_options.disable_caps_lock,
1501 caps_lock_keycode=test_options.caps_lock_keycode)
1502 self.key_filter.Start()
1503
Jon Salz0697cbf2012-07-04 15:14:04 +08001504 def run(self):
1505 '''Runs Goofy.'''
1506 # Process events forever.
1507 while self.run_once(True):
1508 pass
Jon Salz73e0fd02012-04-04 11:46:38 +08001509
Jon Salz0697cbf2012-07-04 15:14:04 +08001510 def run_once(self, block=False):
1511 '''Runs all items pending in the event loop.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001512
Jon Salz0697cbf2012-07-04 15:14:04 +08001513 Args:
1514 block: If true, block until at least one event is processed.
Jon Salz7c15e8b2012-06-19 17:10:37 +08001515
Jon Salz0697cbf2012-07-04 15:14:04 +08001516 Returns:
1517 True to keep going or False to shut down.
1518 '''
1519 events = utils.DrainQueue(self.run_queue)
cychiang21886742012-07-05 15:16:32 +08001520 while not events:
Jon Salz0697cbf2012-07-04 15:14:04 +08001521 # Nothing on the run queue.
1522 self._run_queue_idle()
1523 if block:
1524 # Block for at least one event...
cychiang21886742012-07-05 15:16:32 +08001525 try:
1526 events.append(self.run_queue.get(timeout=RUN_QUEUE_TIMEOUT_SECS))
1527 except Queue.Empty:
1528 # Keep going (calling _run_queue_idle() again at the top of
1529 # the loop)
1530 continue
Jon Salz0697cbf2012-07-04 15:14:04 +08001531 # ...and grab anything else that showed up at the same
1532 # time.
1533 events.extend(utils.DrainQueue(self.run_queue))
cychiang21886742012-07-05 15:16:32 +08001534 else:
1535 break
Jon Salz51528e12012-07-02 18:54:45 +08001536
Jon Salz0697cbf2012-07-04 15:14:04 +08001537 for event in events:
1538 if not event:
1539 # Shutdown request.
1540 self.run_queue.task_done()
1541 return False
Jon Salz51528e12012-07-02 18:54:45 +08001542
Jon Salz0697cbf2012-07-04 15:14:04 +08001543 try:
1544 event()
Jon Salz85a39882012-07-05 16:45:04 +08001545 except: # pylint: disable=W0702
1546 logging.exception('Error in event loop')
Jon Salz0697cbf2012-07-04 15:14:04 +08001547 self.record_exception(traceback.format_exception_only(
1548 *sys.exc_info()[:2]))
1549 # But keep going
1550 finally:
1551 self.run_queue.task_done()
1552 return True
Jon Salz0405ab52012-03-16 15:26:52 +08001553
Jon Salz0e6532d2012-10-25 16:30:11 +08001554 def _should_sync_time(self, foreground=False):
1555 '''Returns True if we should attempt syncing time with shopfloor.
1556
1557 Args:
1558 foreground: If True, synchronizes even if background syncing
1559 is disabled (e.g., in explicit sync requests from the
1560 SyncShopfloor test).
1561 '''
1562 return ((foreground or
1563 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001564 self.time_sanitizer and
1565 (not self.time_synced) and
1566 (not factory.in_chroot()))
1567
Jon Salz0e6532d2012-10-25 16:30:11 +08001568 def sync_time_with_shopfloor_server(self, foreground=False):
Jon Salz54882d02012-08-31 01:57:54 +08001569 '''Syncs time with shopfloor server, if not yet synced.
1570
Jon Salz0e6532d2012-10-25 16:30:11 +08001571 Args:
1572 foreground: If True, synchronizes even if background syncing
1573 is disabled (e.g., in explicit sync requests from the
1574 SyncShopfloor test).
1575
Jon Salz54882d02012-08-31 01:57:54 +08001576 Returns:
1577 False if no time sanitizer is available, or True if this sync (or a
1578 previous sync) succeeded.
1579
1580 Raises:
1581 Exception if unable to contact the shopfloor server.
1582 '''
Jon Salz0e6532d2012-10-25 16:30:11 +08001583 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001584 self.time_sanitizer.SyncWithShopfloor()
1585 self.time_synced = True
1586 return self.time_synced
1587
Jon Salzb92c5112012-09-21 15:40:11 +08001588 def log_disk_space_stats(self):
Jon Salz18e0e022013-06-11 17:13:39 +08001589 if (utils.in_chroot() or
1590 not self.test_list.options.log_disk_space_period_secs):
Jon Salzb92c5112012-09-21 15:40:11 +08001591 return
1592
1593 now = time.time()
1594 if (self.last_log_disk_space_time and
1595 now - self.last_log_disk_space_time <
1596 self.test_list.options.log_disk_space_period_secs):
1597 return
1598 self.last_log_disk_space_time = now
1599
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001600 # Upload event if stateful partition usage is above threshold.
1601 # Stateful partition is mounted on /usr/local, while
1602 # encrypted stateful partition is mounted on /var.
1603 # If there are too much logs in the factory process,
1604 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001605 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001606 vfs_infos = disk_space.GetAllVFSInfo()
1607 stateful_info, encrypted_info = None, None
1608 for vfs_info in vfs_infos.values():
1609 if '/usr/local' in vfs_info.mount_points:
1610 stateful_info = vfs_info
1611 if '/var' in vfs_info.mount_points:
1612 encrypted_info = vfs_info
1613
1614 stateful = disk_space.GetPartitionUsage(stateful_info)
1615 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1616
1617 above_threshold = (
1618 self.test_list.options.stateful_usage_threshold and
1619 max(stateful.bytes_used_pct,
1620 stateful.inodes_used_pct,
1621 encrypted.bytes_used_pct,
1622 encrypted.inodes_used_pct) >
1623 self.test_list.options.stateful_usage_threshold)
1624
1625 if above_threshold:
1626 self.event_log.Log('stateful_partition_usage',
1627 partitions={
1628 'stateful': {
1629 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1630 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1631 'encrypted_stateful': {
1632 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1633 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1634 })
1635 self.log_watcher.ScanEventLogs()
Cheng-Yi Chiang00798e72013-06-20 18:16:39 +08001636 if (not utils.in_chroot() and
1637 self.test_list.options.stateful_usage_above_threshold_action):
1638 Spawn(self.test_list.options.stateful_usage_above_threshold_action,
1639 call=True)
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001640
1641 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001642 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001643 if above_threshold:
1644 logging.warning(message)
1645 else:
1646 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001647 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001648 except: # pylint: disable=W0702
1649 logging.exception('Unable to get disk space used')
1650
Justin Chuang83813982013-05-13 01:26:32 +08001651 def check_battery(self):
1652 '''Checks the current battery status.
1653
1654 Logs current battery charging level and status to log. If the battery level
1655 is lower below warning_low_battery_pct, send warning event to shopfloor.
1656 If the battery level is lower below critical_low_battery_pct, flush disks.
1657 '''
1658 if not self.test_list.options.check_battery_period_secs:
1659 return
1660
1661 now = time.time()
1662 if (self.last_check_battery_time and
1663 now - self.last_check_battery_time <
1664 self.test_list.options.check_battery_period_secs):
1665 return
1666 self.last_check_battery_time = now
1667
1668 message = ''
1669 log_level = logging.INFO
1670 try:
1671 power = system.GetBoard().power
1672 if not power.CheckBatteryPresent():
1673 message = 'Battery is not present'
1674 else:
1675 ac_present = power.CheckACPresent()
1676 charge_pct = power.GetChargePct(get_float=True)
1677 message = ('Current battery level %.1f%%, AC charger is %s' %
1678 (charge_pct, 'connected' if ac_present else 'disconnected'))
1679
1680 if charge_pct > self.test_list.options.critical_low_battery_pct:
1681 critical_low_battery = False
1682 else:
1683 critical_low_battery = True
1684 # Only sync disks when battery level is still above minimum
1685 # value. This can be used for offline analysis when shopfloor cannot
1686 # be connected.
1687 if charge_pct > MIN_BATTERY_LEVEL_FOR_DISK_SYNC:
1688 logging.warning('disk syncing for critical low battery situation')
1689 os.system('sync; sync; sync')
1690 else:
1691 logging.warning('disk syncing is cancelled '
1692 'because battery level is lower than %.1f',
1693 MIN_BATTERY_LEVEL_FOR_DISK_SYNC)
1694
1695 # Notify shopfloor server
1696 if (critical_low_battery or
1697 (not ac_present and
1698 charge_pct <= self.test_list.options.warning_low_battery_pct)):
1699 log_level = logging.WARNING
1700
1701 self.event_log.Log('low_battery',
1702 battery_level=charge_pct,
1703 charger_connected=ac_present,
1704 critical=critical_low_battery)
1705 self.log_watcher.KickWatchThread()
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001706 if self.system_log_manager:
1707 self.system_log_manager.KickSyncThread()
Justin Chuang83813982013-05-13 01:26:32 +08001708 except: # pylint: disable=W0702
1709 logging.exception('Unable to check battery or notify shopfloor')
1710 finally:
1711 if message != self.last_check_battery_message:
1712 logging.log(log_level, message)
1713 self.last_check_battery_message = message
1714
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001715 def check_core_dump(self):
1716 '''Checks if there is any core dumped file.
1717
1718 Removes unwanted core dump files immediately.
1719 Syncs those files matching watch list to server with a delay between
1720 each sync. After the files have been synced to server, deletes the files.
1721 '''
1722 core_dump_files = self.core_dump_manager.ScanFiles()
1723 if core_dump_files:
1724 now = time.time()
1725 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1726 self.test_list.options.kick_sync_min_interval_secs):
1727 return
1728 self.last_kick_sync_time = now
1729
1730 # Sends event to server
1731 self.event_log.Log('core_dumped', files=core_dump_files)
1732 self.log_watcher.KickWatchThread()
1733
1734 # Syncs files to server
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001735 if self.system_log_manager:
1736 self.system_log_manager.KickSyncThread(
1737 core_dump_files, self.core_dump_manager.ClearFiles)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001738
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001739 def check_log_rotation(self):
1740 '''Checks log rotation file presence/absence according to test_list option.
1741
1742 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1743 is True, delete it otherwise. This must be done in idle loop because
1744 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1745 an autotest.
1746 '''
1747 if utils.in_chroot():
1748 return
1749 try:
1750 if self.test_list.options.disable_log_rotation:
1751 open(CLEANUP_LOGS_PAUSED, 'w').close()
1752 else:
1753 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1754 except: # pylint: disable=W0702
1755 # Oh well. Logs an error (but no trace)
1756 logging.info(
1757 'Unable to %s %s: %s',
1758 'touch' if self.test_list.options.disable_log_rotation else 'delete',
1759 CLEANUP_LOGS_PAUSED, utils.FormatExceptionOnly())
1760
Jon Salz8fa8e832012-07-13 19:04:09 +08001761 def sync_time_in_background(self):
Jon Salzb22d1172012-08-06 10:38:57 +08001762 '''Writes out current time and tries to sync with shopfloor server.'''
1763 if not self.time_sanitizer:
1764 return
1765
1766 # Write out the current time.
1767 self.time_sanitizer.SaveTime()
1768
Jon Salz54882d02012-08-31 01:57:54 +08001769 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001770 return
1771
1772 now = time.time()
1773 if self.last_sync_time and (
1774 now - self.last_sync_time <
1775 self.test_list.options.sync_time_period_secs):
1776 # Not yet time for another check.
1777 return
1778 self.last_sync_time = now
1779
1780 def target():
1781 try:
Jon Salz54882d02012-08-31 01:57:54 +08001782 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001783 except: # pylint: disable=W0702
1784 # Oh well. Log an error (but no trace)
1785 logging.info(
1786 'Unable to get time from shopfloor server: %s',
1787 utils.FormatExceptionOnly())
1788
1789 thread = threading.Thread(target=target)
1790 thread.daemon = True
1791 thread.start()
1792
Jon Salz0697cbf2012-07-04 15:14:04 +08001793 def _run_queue_idle(self):
Vic Yang4953fc12012-07-26 16:19:53 +08001794 '''Invoked when the run queue has no events.
1795
1796 This method must not raise exception.
1797 '''
Jon Salzb22d1172012-08-06 10:38:57 +08001798 now = time.time()
1799 if (self.last_idle and
1800 now < (self.last_idle + RUN_QUEUE_TIMEOUT_SECS - 1)):
1801 # Don't run more often than once every (RUN_QUEUE_TIMEOUT_SECS -
1802 # 1) seconds.
1803 return
1804
1805 self.last_idle = now
1806
Vic Yang311ddb82012-09-26 12:08:28 +08001807 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001808 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001809 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001810 self.log_disk_space_stats()
Justin Chuang83813982013-05-13 01:26:32 +08001811 self.check_battery()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001812 self.check_core_dump()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001813 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001814
Jon Salzd15bbcf2013-05-21 17:33:57 +08001815 def handle_event_logs(self, chunks):
Jon Salz0697cbf2012-07-04 15:14:04 +08001816 '''Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001817
Jon Salz0697cbf2012-07-04 15:14:04 +08001818 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001819
1820 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001821 chunks: A list of Chunk objects.
Jon Salz0697cbf2012-07-04 15:14:04 +08001822 '''
Vic Yang93027612013-05-06 02:42:49 +08001823 first_exception = None
1824 exception_count = 0
1825
Jon Salzd15bbcf2013-05-21 17:33:57 +08001826 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001827 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001828 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001829 start_time = time.time()
1830 shopfloor_client = shopfloor.get_instance(
1831 detect=True,
1832 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzd15bbcf2013-05-21 17:33:57 +08001833 shopfloor_client.UploadEvent(chunk.log_name + "." +
1834 event_log.GetReimageId(),
1835 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001836 logging.info(
1837 'Successfully synced %s in %.03f s',
1838 description, time.time() - start_time)
1839 except: # pylint: disable=W0702
Jon Salzd15bbcf2013-05-21 17:33:57 +08001840 first_exception = (first_exception or (chunk.log_name + ': ' +
Vic Yang93027612013-05-06 02:42:49 +08001841 utils.FormatExceptionOnly()))
1842 exception_count += 1
1843
1844 if exception_count:
1845 if exception_count == 1:
1846 msg = 'Log upload failed: %s' % first_exception
1847 else:
1848 msg = '%d log upload failed; first is: %s' % (
1849 exception_count, first_exception)
1850 raise Exception(msg)
1851
Jon Salz57717ca2012-04-04 16:47:25 +08001852
Jon Salz0697cbf2012-07-04 15:14:04 +08001853 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1854 root=None):
1855 '''Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001856
Jon Salz0697cbf2012-07-04 15:14:04 +08001857 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001858
Jon Salz0697cbf2012-07-04 15:14:04 +08001859 Args:
1860 starting_at: If provided, only auto-runs tests beginning with
1861 this test.
1862 '''
1863 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001864
Jon Salz0697cbf2012-07-04 15:14:04 +08001865 if starting_at:
1866 # Make sure they passed a test, not a string.
1867 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001868
Jon Salz0697cbf2012-07-04 15:14:04 +08001869 tests_to_reset = []
1870 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001871
Jon Salz0697cbf2012-07-04 15:14:04 +08001872 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001873
Jon Salz0697cbf2012-07-04 15:14:04 +08001874 for test in root.get_top_level_tests():
1875 if starting_at:
1876 if test == starting_at:
1877 # We've found starting_at; do auto-run on all
1878 # subsequent tests.
1879 found_starting_at = True
1880 if not found_starting_at:
1881 # Don't start this guy yet
1882 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001883
Jon Salz0697cbf2012-07-04 15:14:04 +08001884 status = test.get_state().status
1885 if status == TestState.ACTIVE or status in statuses_to_run:
1886 # Reset the test (later; we will need to abort
1887 # all active tests first).
1888 tests_to_reset.append(test)
1889 if status in statuses_to_run:
1890 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001891
Jon Salz6dc031d2013-06-19 13:06:23 +08001892 self.abort_active_tests('Operator requested run/re-run of certain tests')
Jon Salz258a40c2012-04-19 12:34:01 +08001893
Jon Salz0697cbf2012-07-04 15:14:04 +08001894 # Reset all statuses of the tests to run (in case any tests were active;
1895 # we want them to be run again).
1896 for test_to_reset in tests_to_reset:
1897 for test in test_to_reset.walk():
1898 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001899
Jon Salz0697cbf2012-07-04 15:14:04 +08001900 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001901
Jon Salz0697cbf2012-07-04 15:14:04 +08001902 def restart_tests(self, root=None):
1903 '''Restarts all tests.'''
1904 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001905
Jon Salz6dc031d2013-06-19 13:06:23 +08001906 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001907 for test in root.walk():
1908 test.update_state(status=TestState.UNTESTED)
1909 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001910
Jon Salz0697cbf2012-07-04 15:14:04 +08001911 def auto_run(self, starting_at=None, root=None):
1912 '''"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001913
Jon Salz0697cbf2012-07-04 15:14:04 +08001914 Args:
1915 starting_at: If provide, only auto-runs tests beginning with
1916 this test.
1917 '''
1918 root = root or self.test_list
1919 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1920 starting_at=starting_at,
1921 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001922
Jon Salz0697cbf2012-07-04 15:14:04 +08001923 def re_run_failed(self, root=None):
1924 '''Re-runs failed tests.'''
1925 root = root or self.test_list
1926 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001927
Jon Salz0697cbf2012-07-04 15:14:04 +08001928 def show_review_information(self):
1929 '''Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001930
Jon Salz0697cbf2012-07-04 15:14:04 +08001931 The information screene is rendered by main UI program (ui.py), so in
1932 goofy we only need to kill all active tests, set them as untested, and
1933 clear remaining tests.
1934 '''
1935 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001936 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001937
Jon Salz0697cbf2012-07-04 15:14:04 +08001938 def handle_switch_test(self, event):
1939 '''Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001940
Jon Salz0697cbf2012-07-04 15:14:04 +08001941 @param event: The SWITCH_TEST event.
1942 '''
1943 test = self.test_list.lookup_path(event.path)
1944 if not test:
1945 logging.error('Unknown test %r', event.key)
1946 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001947
Jon Salz0697cbf2012-07-04 15:14:04 +08001948 invoc = self.invocations.get(test)
1949 if invoc and test.backgroundable:
1950 # Already running: just bring to the front if it
1951 # has a UI.
1952 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001953 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001954 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001955
Jon Salz6dc031d2013-06-19 13:06:23 +08001956 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001957 for t in test.walk():
1958 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001959
Jon Salz0697cbf2012-07-04 15:14:04 +08001960 if self.test_list.options.auto_run_on_keypress:
1961 self.auto_run(starting_at=test)
1962 else:
1963 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001964
Jon Salz0697cbf2012-07-04 15:14:04 +08001965 def wait(self):
1966 '''Waits for all pending invocations.
1967
1968 Useful for testing.
1969 '''
Jon Salz1acc8742012-07-17 17:45:55 +08001970 while self.invocations:
1971 for k, v in self.invocations.iteritems():
1972 logging.info('Waiting for %s to complete...', k)
1973 v.thread.join()
1974 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001975
1976 def check_exceptions(self):
1977 '''Raises an error if any exceptions have occurred in
1978 invocation threads.'''
1979 if self.exceptions:
1980 raise RuntimeError('Exception in invocation thread: %r' %
1981 self.exceptions)
1982
1983 def record_exception(self, msg):
1984 '''Records an exception in an invocation thread.
1985
1986 An exception with the given message will be rethrown when
1987 Goofy is destroyed.'''
1988 self.exceptions.append(msg)
Jon Salz73e0fd02012-04-04 11:46:38 +08001989
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001990
1991if __name__ == '__main__':
Jon Salz77c151e2012-08-28 07:20:37 +08001992 goofy = Goofy()
1993 try:
1994 goofy.main()
Jon Salz0f996602012-10-03 15:26:48 +08001995 except SystemExit:
1996 # Propagate SystemExit without logging.
1997 raise
Jon Salz31373eb2012-09-21 16:19:49 +08001998 except:
Jon Salz0f996602012-10-03 15:26:48 +08001999 # Log the error before trying to shut down (unless it's a graceful
2000 # exit).
Jon Salz31373eb2012-09-21 16:19:49 +08002001 logging.exception('Error in main loop')
2002 raise
Jon Salz77c151e2012-08-28 07:20:37 +08002003 finally:
2004 goofy.destroy()