blob: 1b0dbdf247027727d8bdb35f14af264d8c29b2d5 [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
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08008"""The main factory flow that runs the factory test and finalizes a device."""
Hung-Te Linf2f78f72012-02-08 19:27:11 +08009
Jon Salze12c2b32013-06-25 16:24:34 +080010import glob
Jon Salz0405ab52012-03-16 15:26:52 +080011import logging
12import os
Jon Salz73e0fd02012-04-04 11:46:38 +080013import Queue
Jon Salze12c2b32013-06-25 16:24:34 +080014import shutil
Jon Salz77c151e2012-08-28 07:20:37 +080015import signal
Jon Salz0405ab52012-03-16 15:26:52 +080016import sys
Jon Salzeff94182013-06-19 15:06:28 +080017import syslog
Jon Salz0405ab52012-03-16 15:26:52 +080018import threading
19import time
20import traceback
Jon Salz258a40c2012-04-19 12:34:01 +080021import uuid
Jon Salzb10cf512012-08-09 17:29:21 +080022from xmlrpclib import Binary
Hung-Te Linf2f78f72012-02-08 19:27:11 +080023from collections import deque
24from optparse import OptionParser
Hung-Te Linf2f78f72012-02-08 19:27:11 +080025
Jon Salz0697cbf2012-07-04 15:14:04 +080026import factory_common # pylint: disable=W0611
jcliangcd688182012-08-20 21:01:26 +080027from cros.factory import event_log
28from cros.factory import system
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +080029from cros.factory.event_log import EventLog, FloatDigit
Tom Wai-Hong Tamd33723e2013-04-10 21:14:37 +080030from cros.factory.event_log_watcher import EventLogWatcher
jcliangcd688182012-08-20 21:01:26 +080031from cros.factory.goofy import test_environment
32from cros.factory.goofy import time_sanitizer
Jon Salz83591782012-06-26 11:09:58 +080033from cros.factory.goofy import updater
jcliangcd688182012-08-20 21:01:26 +080034from cros.factory.goofy.goofy_rpc import GoofyRPC
Jon Salz885dcac2013-07-23 16:39:50 +080035from cros.factory.goofy.invocation import TestArgEnv
jcliangcd688182012-08-20 21:01:26 +080036from cros.factory.goofy.invocation import TestInvocation
37from cros.factory.goofy.prespawner import Prespawner
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +080038from cros.factory.goofy.system_log_manager import SystemLogManager
jcliangcd688182012-08-20 21:01:26 +080039from cros.factory.goofy.web_socket_manager import WebSocketManager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +080040from cros.factory.system.board import Board, BoardException
jcliangcd688182012-08-20 21:01:26 +080041from cros.factory.system.charge_manager import ChargeManager
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +080042from cros.factory.system.core_dump_manager import CoreDumpManager
Jon Salzce6a7f82013-06-10 18:22:54 +080043from cros.factory.system.cpufreq_manager import CpufreqManager
Jon Salzb92c5112012-09-21 15:40:11 +080044from cros.factory.system import disk_space
jcliangcd688182012-08-20 21:01:26 +080045from cros.factory.test import factory
46from cros.factory.test import state
Jon Salz51528e12012-07-02 18:54:45 +080047from cros.factory.test import shopfloor
Jon Salz83591782012-06-26 11:09:58 +080048from cros.factory.test import utils
Jon Salz128b0932013-07-03 16:55:26 +080049from cros.factory.test.test_lists import test_lists
Ricky Liang6fe218c2013-12-27 15:17:17 +080050from cros.factory.test.e2e_test.common import (
51 AutomationMode, AutomationModePrompt, ParseAutomationMode)
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():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800104 """Returns the HWID config tag, or an empty string if none can be found."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800105 if 'CROS_HWID' in os.environ:
106 return os.environ['CROS_HWID']
107 if os.path.exists(HWID_CFG_PATH):
108 with open(HWID_CFG_PATH, 'rt') as hwid_cfg_handle:
109 return hwid_cfg_handle.read().strip()
110 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800111
112
Jon Salz73e0fd02012-04-04 11:46:38 +0800113_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800114
115class Goofy(object):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800116 """The main factory flow.
Jon Salz0697cbf2012-07-04 15:14:04 +0800117
118 Note that all methods in this class must be invoked from the main
119 (event) thread. Other threads, such as callbacks and TestInvocation
120 methods, should instead post events on the run queue.
121
122 TODO: Unit tests. (chrome-os-partner:7409)
123
124 Properties:
125 uuid: A unique UUID for this invocation of Goofy.
126 state_instance: An instance of FactoryState.
127 state_server: The FactoryState XML/RPC server.
128 state_server_thread: A thread running state_server.
129 event_server: The EventServer socket server.
130 event_server_thread: A thread running event_server.
131 event_client: A client to the event server.
132 connection_manager: The connection_manager object.
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800133 system_log_manager: The SystemLogManager object.
134 core_dump_manager: The CoreDumpManager object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800135 ui_process: The factory ui process object.
136 run_queue: A queue of callbacks to invoke from the main thread.
137 invocations: A map from FactoryTest objects to the corresponding
138 TestInvocations objects representing active tests.
139 tests_to_run: A deque of tests that should be run when the current
140 test(s) complete.
141 options: Command-line options.
142 args: Command-line args.
143 test_list: The test list.
Jon Salz128b0932013-07-03 16:55:26 +0800144 test_lists: All new-style test lists.
Jon Salz0697cbf2012-07-04 15:14:04 +0800145 event_handlers: Map of Event.Type to the method used to handle that
146 event. If the method has an 'event' argument, the event is passed
147 to the handler.
148 exceptions: Exceptions encountered in invocation threads.
Jon Salz3c493bb2013-02-07 17:24:58 +0800149 last_log_disk_space_message: The last message we logged about disk space
150 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800151 last_kick_sync_time: The last time to kick system_log_manager to sync
152 because of core dump files (to avoid kicking too soon then abort the
153 sync.)
Jon Salz416f9cc2013-05-10 18:32:50 +0800154 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800155 status: The current Goofy status (a member of the Status enum).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800156 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800157 def __init__(self):
158 self.uuid = str(uuid.uuid4())
159 self.state_instance = None
160 self.state_server = None
161 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800162 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800163 self.event_server = None
164 self.event_server_thread = None
165 self.event_client = None
166 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800167 self.charge_manager = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800168 self.time_sanitizer = None
169 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800170 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800171 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800172 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800173 self.event_log = None
174 self.prespawner = None
175 self.ui_process = None
Jon Salzc79a9982012-08-30 04:42:01 +0800176 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800177 self.run_queue = Queue.Queue()
178 self.invocations = {}
179 self.tests_to_run = deque()
180 self.visible_test = None
181 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800182 self.hooks = None
Vic Yangd8990da2013-06-27 16:57:43 +0800183 self.cpu_usage_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800184
185 self.options = None
186 self.args = None
187 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800188 self.test_lists = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800189 self.on_ui_startup = []
190 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800191 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800192 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800193 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800194 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800195 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800196 self.last_log_disk_space_message = None
Justin Chuang83813982013-05-13 01:26:32 +0800197 self.last_check_battery_time = None
198 self.last_check_battery_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800199 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800200 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800201 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800202 self.key_filter = None
Jon Salzce6a7f82013-06-10 18:22:54 +0800203 self.cpufreq_manager = None
Jon Salzd7550792013-07-12 05:49:27 +0800204 self.status = Status.UNINITIALIZED
Jon Salz0697cbf2012-07-04 15:14:04 +0800205
Jon Salz85a39882012-07-05 16:45:04 +0800206 def test_or_root(event, parent_or_group=True):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800207 """Returns the test affected by a particular event.
Jon Salz85a39882012-07-05 16:45:04 +0800208
209 Args:
210 event: The event containing an optional 'path' attribute.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800211 parent_or_group: If True, returns the top-level parent for a test (the
Jon Salz85a39882012-07-05 16:45:04 +0800212 root node of the tests that need to be run together if the given test
213 path is to be run).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800214 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800215 try:
216 path = event.path
217 except AttributeError:
218 path = None
219
220 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800221 test = self.test_list.lookup_path(path)
222 if parent_or_group:
223 test = test.get_top_level_parent_or_group()
224 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800225 else:
226 return self.test_list
227
228 self.event_handlers = {
229 Event.Type.SWITCH_TEST: self.handle_switch_test,
230 Event.Type.SHOW_NEXT_ACTIVE_TEST:
231 lambda event: self.show_next_active_test(),
232 Event.Type.RESTART_TESTS:
233 lambda event: self.restart_tests(root=test_or_root(event)),
234 Event.Type.AUTO_RUN:
235 lambda event: self.auto_run(root=test_or_root(event)),
236 Event.Type.RE_RUN_FAILED:
237 lambda event: self.re_run_failed(root=test_or_root(event)),
238 Event.Type.RUN_TESTS_WITH_STATUS:
239 lambda event: self.run_tests_with_status(
240 event.status,
241 root=test_or_root(event)),
242 Event.Type.REVIEW:
243 lambda event: self.show_review_information(),
244 Event.Type.UPDATE_SYSTEM_INFO:
245 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800246 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800247 lambda event: self.stop(root=test_or_root(event, False),
Jon Salz6dc031d2013-06-19 13:06:23 +0800248 fail=getattr(event, 'fail', False),
249 reason=getattr(event, 'reason', None)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800250 Event.Type.SET_VISIBLE_TEST:
251 lambda event: self.set_visible_test(
252 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800253 Event.Type.CLEAR_STATE:
254 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800255 }
256
257 self.exceptions = []
258 self.web_socket_manager = None
259
260 def destroy(self):
Jon Salzd7550792013-07-12 05:49:27 +0800261 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800262 if self.chrome:
263 self.chrome.kill()
264 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800265 if self.dummy_shopfloor:
266 self.dummy_shopfloor.kill()
267 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800268 if self.ui_process:
269 utils.kill_process_tree(self.ui_process, 'ui')
270 self.ui_process = None
271 if self.web_socket_manager:
272 logging.info('Stopping web sockets')
273 self.web_socket_manager.close()
274 self.web_socket_manager = None
275 if self.state_server_thread:
276 logging.info('Stopping state server')
277 self.state_server.shutdown()
278 self.state_server_thread.join()
279 self.state_server.server_close()
280 self.state_server_thread = None
281 if self.state_instance:
282 self.state_instance.close()
283 if self.event_server_thread:
284 logging.info('Stopping event server')
285 self.event_server.shutdown() # pylint: disable=E1101
286 self.event_server_thread.join()
287 self.event_server.server_close()
288 self.event_server_thread = None
289 if self.log_watcher:
290 if self.log_watcher.IsThreadStarted():
291 self.log_watcher.StopWatchThread()
292 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800293 if self.system_log_manager:
294 if self.system_log_manager.IsThreadRunning():
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +0800295 self.system_log_manager.Stop()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800296 self.system_log_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800297 if self.prespawner:
298 logging.info('Stopping prespawner')
299 self.prespawner.stop()
300 self.prespawner = None
301 if self.event_client:
302 logging.info('Closing event client')
303 self.event_client.close()
304 self.event_client = None
Jon Salzddf0d052013-06-18 12:52:44 +0800305 if self.cpufreq_manager:
306 self.cpufreq_manager.Stop()
Jon Salz0697cbf2012-07-04 15:14:04 +0800307 if self.event_log:
308 self.event_log.Close()
309 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800310 if self.key_filter:
311 self.key_filter.Stop()
Vic Yangd8990da2013-06-27 16:57:43 +0800312 if self.cpu_usage_watcher:
313 self.cpu_usage_watcher.terminate()
Dean Liao592e4d52013-01-10 20:06:39 +0800314
Jon Salz0697cbf2012-07-04 15:14:04 +0800315 self.check_exceptions()
316 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800317 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800318
319 def start_state_server(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800320 # Before starting state server, remount stateful partitions with
321 # no commit flag. The default commit time (commit=600) makes corruption
322 # too likely.
323 file_utils.ResetCommitTime()
324
Jon Salz0697cbf2012-07-04 15:14:04 +0800325 self.state_instance, self.state_server = (
326 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800327 self.goofy_rpc = GoofyRPC(self)
328 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800329 logging.info('Starting state server')
330 self.state_server_thread = threading.Thread(
331 target=self.state_server.serve_forever,
332 name='StateServer')
333 self.state_server_thread.start()
334
335 def start_event_server(self):
336 self.event_server = EventServer()
337 logging.info('Starting factory event server')
338 self.event_server_thread = threading.Thread(
339 target=self.event_server.serve_forever,
340 name='EventServer') # pylint: disable=E1101
341 self.event_server_thread.start()
342
343 self.event_client = EventClient(
344 callback=self.handle_event, event_loop=self.run_queue)
345
346 self.web_socket_manager = WebSocketManager(self.uuid)
347 self.state_server.add_handler("/event",
348 self.web_socket_manager.handle_web_socket)
349
350 def start_ui(self):
351 ui_proc_args = [
352 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
353 self.options.test_list]
354 if self.options.verbose:
355 ui_proc_args.append('-v')
356 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800357 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800358 logging.info('Waiting for UI to come up...')
359 self.event_client.wait(
360 lambda event: event.type == Event.Type.UI_READY)
361 logging.info('UI has started')
362
363 def set_visible_test(self, test):
364 if self.visible_test == test:
365 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800366 if test and not test.has_ui:
367 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800368
369 if test:
370 test.update_state(visible=True)
371 if self.visible_test:
372 self.visible_test.update_state(visible=False)
373 self.visible_test = test
374
Jon Salzd4306c82012-11-30 15:16:36 +0800375 def _log_startup_messages(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800376 """Logs the tail of var/log/messages and mosys and EC console logs."""
Jon Salzd4306c82012-11-30 15:16:36 +0800377 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
378 # for factory-3004.B only. Consolidate and merge back to ToT.
379 if utils.in_chroot():
380 return
381
382 try:
383 var_log_messages = (
384 utils.var_log_messages_before_reboot())
385 logging.info(
386 'Tail of /var/log/messages before last reboot:\n'
387 '%s', ('\n'.join(
388 ' ' + x for x in var_log_messages)))
389 except: # pylint: disable=W0702
390 logging.exception('Unable to grok /var/log/messages')
391
392 try:
393 mosys_log = utils.Spawn(
394 ['mosys', 'eventlog', 'list'],
395 read_stdout=True, log_stderr_on_error=True).stdout_data
396 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
397 except: # pylint: disable=W0702
398 logging.exception('Unable to read mosys eventlog')
399
400 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800401 board = system.GetBoard()
402 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800403 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
404 except: # pylint: disable=W0702
405 logging.exception('Error retrieving EC console log')
406
Vic Yang079f9872013-07-01 11:32:00 +0800407 try:
408 board = system.GetBoard()
409 ec_panic_info = board.GetECPanicInfo()
410 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
411 except: # pylint: disable=W0702
412 logging.exception('Error retrieving EC panic info')
413
Jon Salz0697cbf2012-07-04 15:14:04 +0800414 def handle_shutdown_complete(self, test, test_state):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800415 """Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800416
Ricky Liang6fe218c2013-12-27 15:17:17 +0800417 Args:
418 test: The ShutdownStep.
419 test_state: The test state.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800420 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800421 test_state = test.update_state(increment_shutdown_count=1)
422 logging.info('Detected shutdown (%d of %d)',
423 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800424
Jon Salz0697cbf2012-07-04 15:14:04 +0800425 def log_and_update_state(status, error_msg, **kw):
426 self.event_log.Log('rebooted',
427 status=status, error_msg=error_msg, **kw)
Jon Salzd4306c82012-11-30 15:16:36 +0800428 logging.info('Rebooted: status=%s, %s', status,
429 (('error_msg=%s' % error_msg) if error_msg else None))
Jon Salz0697cbf2012-07-04 15:14:04 +0800430 test.update_state(status=status, error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800431
Jon Salz0697cbf2012-07-04 15:14:04 +0800432 if not self.last_shutdown_time:
433 log_and_update_state(status=TestState.FAILED,
434 error_msg='Unable to read shutdown_time')
435 return
Jon Salz258a40c2012-04-19 12:34:01 +0800436
Jon Salz0697cbf2012-07-04 15:14:04 +0800437 now = time.time()
438 logging.info('%.03f s passed since reboot',
439 now - self.last_shutdown_time)
Jon Salz258a40c2012-04-19 12:34:01 +0800440
Jon Salz0697cbf2012-07-04 15:14:04 +0800441 if self.last_shutdown_time > now:
442 test.update_state(status=TestState.FAILED,
443 error_msg='Time moved backward during reboot')
444 elif (isinstance(test, factory.RebootStep) and
445 self.test_list.options.max_reboot_time_secs and
446 (now - self.last_shutdown_time >
447 self.test_list.options.max_reboot_time_secs)):
448 # A reboot took too long; fail. (We don't check this for
449 # HaltSteps, because the machine could be halted for a
450 # very long time, and even unplugged with battery backup,
451 # thus hosing the clock.)
452 log_and_update_state(
453 status=TestState.FAILED,
454 error_msg=('More than %d s elapsed during reboot '
455 '(%.03f s, from %s to %s)' % (
456 self.test_list.options.max_reboot_time_secs,
457 now - self.last_shutdown_time,
458 utils.TimeString(self.last_shutdown_time),
459 utils.TimeString(now))),
460 duration=(now-self.last_shutdown_time))
Jon Salzd4306c82012-11-30 15:16:36 +0800461 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800462 elif test_state.shutdown_count == test.iterations:
463 # Good!
464 log_and_update_state(status=TestState.PASSED,
465 duration=(now - self.last_shutdown_time),
466 error_msg='')
467 elif test_state.shutdown_count > test.iterations:
468 # Shut down too many times
469 log_and_update_state(status=TestState.FAILED,
470 error_msg='Too many shutdowns')
Jon Salzd4306c82012-11-30 15:16:36 +0800471 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800472 elif utils.are_shift_keys_depressed():
473 logging.info('Shift keys are depressed; cancelling restarts')
474 # Abort shutdown
475 log_and_update_state(
476 status=TestState.FAILED,
477 error_msg='Shutdown aborted with double shift keys')
Jon Salza6711d72012-07-18 14:33:03 +0800478 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800479 else:
480 def handler():
481 if self._prompt_cancel_shutdown(
482 test, test_state.shutdown_count + 1):
Jon Salza6711d72012-07-18 14:33:03 +0800483 factory.console.info('Shutdown aborted by operator')
Jon Salz0697cbf2012-07-04 15:14:04 +0800484 log_and_update_state(
485 status=TestState.FAILED,
486 error_msg='Shutdown aborted by operator')
Jon Salza6711d72012-07-18 14:33:03 +0800487 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800488 return
Jon Salz0405ab52012-03-16 15:26:52 +0800489
Jon Salz0697cbf2012-07-04 15:14:04 +0800490 # Time to shutdown again
491 log_and_update_state(
492 status=TestState.ACTIVE,
493 error_msg='',
494 iteration=test_state.shutdown_count)
Jon Salz73e0fd02012-04-04 11:46:38 +0800495
Jon Salz0697cbf2012-07-04 15:14:04 +0800496 self.event_log.Log('shutdown', operation='reboot')
497 self.state_instance.set_shared_data('shutdown_time',
498 time.time())
499 self.env.shutdown('reboot')
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800500
Jon Salz0697cbf2012-07-04 15:14:04 +0800501 self.on_ui_startup.append(handler)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800502
Jon Salz0697cbf2012-07-04 15:14:04 +0800503 def _prompt_cancel_shutdown(self, test, iteration):
504 if self.options.ui != 'chrome':
505 return False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800506
Jon Salz0697cbf2012-07-04 15:14:04 +0800507 pending_shutdown_data = {
508 'delay_secs': test.delay_secs,
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800509 'enable_guest_mode': test.enable_guest_mode,
Jon Salz0697cbf2012-07-04 15:14:04 +0800510 'time': time.time() + test.delay_secs,
511 'operation': test.operation,
512 'iteration': iteration,
513 'iterations': test.iterations,
514 }
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800515
Jon Salz0697cbf2012-07-04 15:14:04 +0800516 # Create a new (threaded) event client since we
517 # don't want to use the event loop for this.
518 with EventClient() as event_client:
519 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN,
520 **pending_shutdown_data))
521 aborted = event_client.wait(
522 lambda event: event.type == Event.Type.CANCEL_SHUTDOWN,
523 timeout=test.delay_secs) is not None
524 if aborted:
525 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
526 return aborted
Jon Salz258a40c2012-04-19 12:34:01 +0800527
Jon Salz0697cbf2012-07-04 15:14:04 +0800528 def init_states(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800529 """Initializes all states on startup."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800530 for test in self.test_list.get_all_tests():
531 # Make sure the state server knows about all the tests,
532 # defaulting to an untested state.
533 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800534
Jon Salz0697cbf2012-07-04 15:14:04 +0800535 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800536 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800537 ec_console_log = None
Vic Yang079f9872013-07-01 11:32:00 +0800538 ec_panic_info = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800539
Jon Salz0697cbf2012-07-04 15:14:04 +0800540 # Any 'active' tests should be marked as failed now.
541 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800542 if not test.is_leaf():
543 # Don't bother with parents; they will be updated when their
544 # children are updated.
545 continue
546
Jon Salz0697cbf2012-07-04 15:14:04 +0800547 test_state = test.get_state()
548 if test_state.status != TestState.ACTIVE:
549 continue
550 if isinstance(test, factory.ShutdownStep):
551 # Shutdown while the test was active - that's good.
552 self.handle_shutdown_complete(test, test_state)
553 else:
554 # Unexpected shutdown. Grab /var/log/messages for context.
555 if var_log_messages is None:
556 try:
557 var_log_messages = (
558 utils.var_log_messages_before_reboot())
559 # Write it to the log, to make it easier to
560 # correlate with /var/log/messages.
561 logging.info(
562 'Unexpected shutdown. '
563 'Tail of /var/log/messages before last reboot:\n'
564 '%s', ('\n'.join(
565 ' ' + x for x in var_log_messages)))
566 except: # pylint: disable=W0702
567 logging.exception('Unable to grok /var/log/messages')
568 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800569
Jon Salz008f4ea2012-08-28 05:39:45 +0800570 if mosys_log is None and not utils.in_chroot():
571 try:
572 mosys_log = utils.Spawn(
573 ['mosys', 'eventlog', 'list'],
574 read_stdout=True, log_stderr_on_error=True).stdout_data
575 # Write it to the log also.
576 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
577 except: # pylint: disable=W0702
578 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800579
Vic Yange4c275d2012-08-28 01:50:20 +0800580 if ec_console_log is None:
581 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800582 board = system.GetBoard()
583 ec_console_log = board.GetECConsoleLog()
Vic Yange4c275d2012-08-28 01:50:20 +0800584 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Jon Salzfe1f6652012-09-07 05:40:14 +0800585 except: # pylint: disable=W0702
Vic Yange4c275d2012-08-28 01:50:20 +0800586 logging.exception('Error retrieving EC console log')
587
Vic Yang079f9872013-07-01 11:32:00 +0800588 if ec_panic_info is None:
589 try:
590 board = system.GetBoard()
591 ec_panic_info = board.GetECPanicInfo()
592 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
593 except: # pylint: disable=W0702
594 logging.exception('Error retrieving EC panic info')
595
Jon Salz0697cbf2012-07-04 15:14:04 +0800596 error_msg = 'Unexpected shutdown while test was running'
597 self.event_log.Log('end_test',
598 path=test.path,
599 status=TestState.FAILED,
600 invocation=test.get_state().invocation,
601 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800602 var_log_messages='\n'.join(var_log_messages),
603 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800604 test.update_state(
605 status=TestState.FAILED,
606 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800607
Jon Salz50efe942012-07-26 11:54:10 +0800608 if not test.never_fails:
609 # For "never_fails" tests (such as "Start"), don't cancel
610 # pending tests, since reboot is expected.
611 factory.console.info('Unexpected shutdown while test %s '
612 'running; cancelling any pending tests',
613 test.path)
614 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800615
Jon Salz008f4ea2012-08-28 05:39:45 +0800616 self.update_skipped_tests()
617
618 def update_skipped_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800619 """Updates skipped states based on run_if."""
Jon Salz885dcac2013-07-23 16:39:50 +0800620 env = TestArgEnv()
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800621 def _evaluate_skip_from_run_if(test):
622 """Returns the run_if evaluation of the test.
623
624 Args:
625 test: A FactoryTest object.
626
627 Returns:
628 The run_if evaluation result. Returns False if the test has no
629 run_if argument.
630 """
631 value = None
632 if test.run_if_expr:
633 try:
634 value = test.run_if_expr(env)
635 except: # pylint: disable=W0702
636 logging.exception('Unable to evaluate run_if expression for %s',
637 test.path)
638 # But keep going; we have no choice. This will end up
639 # always activating the test.
640 elif test.run_if_table_name:
641 try:
642 aux = shopfloor.get_selected_aux_data(test.run_if_table_name)
643 value = aux.get(test.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
652 return skip
653
654 # Gets all run_if evaluation, and stores results in skip_map.
655 skip_map = dict()
Jon Salz008f4ea2012-08-28 05:39:45 +0800656 for t in self.test_list.walk():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800657 skip_map[t.path] = _evaluate_skip_from_run_if(t)
Jon Salz885dcac2013-07-23 16:39:50 +0800658
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800659 # Propagates the skip value from root of tree and updates skip_map.
660 def _update_skip_map_from_node(test, skip_from_parent):
661 """Updates skip_map from a given node.
Jon Salz885dcac2013-07-23 16:39:50 +0800662
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800663 Given a FactoryTest node and the skip value from parent, updates the
664 skip value of current node in the skip_map if skip value from parent is
665 True. If this node has children, recursively propagate this value to all
666 its children, that is, all its subtests.
667 Note that this function only updates value in skip_map, not the actual
668 test_list tree.
Jon Salz008f4ea2012-08-28 05:39:45 +0800669
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800670 Args:
671 test: The given FactoryTest object. It is a node in the test_list tree.
672 skip_from_parent: The skip value which propagates from the parent of
673 input node.
674 """
675 skip_this_tree = skip_from_parent or skip_map[test.path]
676 if skip_this_tree:
677 logging.info('Skip from node %r', test.path)
678 skip_map[test.path] = True
679 if test.is_leaf():
680 return
681 # Propagates skip value to its subtests
682 for subtest in test.subtests:
683 _update_skip_map_from_node(subtest, skip_this_tree)
684
685 _update_skip_map_from_node(self.test_list, False)
686
687 # Updates the skip value from skip_map to test_list tree. Also, updates test
688 # status if needed.
689 for t in self.test_list.walk():
690 skip = skip_map[t.path]
691 test_state = t.get_state()
692 if ((not skip) and
693 (test_state.status == TestState.PASSED) and
694 (test_state.error_msg == TestState.SKIPPED_MSG)):
695 # It was marked as skipped before, but now we need to run it.
696 # Mark as untested.
697 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
698 else:
699 t.update_state(skip=skip)
Jon Salz008f4ea2012-08-28 05:39:45 +0800700
Jon Salz0697cbf2012-07-04 15:14:04 +0800701 def show_next_active_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800702 """Rotates to the next visible active test."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800703 self.reap_completed_tests()
704 active_tests = [
705 t for t in self.test_list.walk()
706 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
707 if not active_tests:
708 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800709
Jon Salz0697cbf2012-07-04 15:14:04 +0800710 try:
711 next_test = active_tests[
712 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
713 except ValueError: # visible_test not present in active_tests
714 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800715
Jon Salz0697cbf2012-07-04 15:14:04 +0800716 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800717
Jon Salz0697cbf2012-07-04 15:14:04 +0800718 def handle_event(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800719 """Handles an event from the event server."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800720 handler = self.event_handlers.get(event.type)
721 if handler:
722 handler(event)
723 else:
724 # We don't register handlers for all event types - just ignore
725 # this event.
726 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800727
Vic Yangaabf9fd2013-04-09 18:56:13 +0800728 def check_critical_factory_note(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800729 """Returns True if the last factory note is critical."""
Vic Yangaabf9fd2013-04-09 18:56:13 +0800730 notes = self.state_instance.get_shared_data('factory_note', True)
731 return notes and notes[-1]['level'] == 'CRITICAL'
732
Jon Salz0697cbf2012-07-04 15:14:04 +0800733 def run_next_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800734 """Runs the next eligible test (or tests) in self.tests_to_run."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800735 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800736 if self.tests_to_run and self.check_critical_factory_note():
737 self.tests_to_run.clear()
738 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800739 while self.tests_to_run:
Ricky Liang6fe218c2013-12-27 15:17:17 +0800740 logging.debug('Tests to run: %s', [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800741
Jon Salz0697cbf2012-07-04 15:14:04 +0800742 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800743
Jon Salz0697cbf2012-07-04 15:14:04 +0800744 if test in self.invocations:
745 logging.info('Next test %s is already running', test.path)
746 self.tests_to_run.popleft()
747 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800748
Jon Salza1412922012-07-23 16:04:17 +0800749 for requirement in test.require_run:
750 for i in requirement.test.walk():
751 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800752 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800753 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800754 return
755
Jon Salz0697cbf2012-07-04 15:14:04 +0800756 if self.invocations and not (test.backgroundable and all(
757 [x.backgroundable for x in self.invocations])):
758 logging.debug('Waiting for non-backgroundable tests to '
Ricky Liang6fe218c2013-12-27 15:17:17 +0800759 'complete before running %s', test.path)
Jon Salz0697cbf2012-07-04 15:14:04 +0800760 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800761
Jon Salz3e6f5202012-10-15 15:08:29 +0800762 if test.get_state().skip:
763 factory.console.info('Skipping test %s', test.path)
764 test.update_state(status=TestState.PASSED,
765 error_msg=TestState.SKIPPED_MSG)
766 self.tests_to_run.popleft()
767 continue
768
Jon Salz0697cbf2012-07-04 15:14:04 +0800769 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800770
Jon Salz304a75d2012-07-06 11:14:15 +0800771 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800772 for requirement in test.require_run:
773 for i in requirement.test.walk():
774 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800775 # We've hit this test itself; stop checking
776 break
Jon Salza1412922012-07-23 16:04:17 +0800777 if ((i.get_state().status == TestState.UNTESTED) or
778 (requirement.passed and i.get_state().status !=
779 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800780 # Found an untested test; move on to the next
781 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800782 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800783 break
784
785 if untested:
786 untested_paths = ', '.join(sorted([x.path for x in untested]))
787 if self.state_instance.get_shared_data('engineering_mode',
788 optional=True):
789 # In engineering mode, we'll let it go.
790 factory.console.warn('In engineering mode; running '
791 '%s even though required tests '
792 '[%s] have not completed',
793 test.path, untested_paths)
794 else:
795 # Not in engineering mode; mark it failed.
796 error_msg = ('Required tests [%s] have not been run yet'
797 % untested_paths)
798 factory.console.error('Not running %s: %s',
799 test.path, error_msg)
800 test.update_state(status=TestState.FAILED,
801 error_msg=error_msg)
802 continue
803
Jon Salz0697cbf2012-07-04 15:14:04 +0800804 if isinstance(test, factory.ShutdownStep):
805 if os.path.exists(NO_REBOOT_FILE):
806 test.update_state(
Ricky Liang6fe218c2013-12-27 15:17:17 +0800807 status=TestState.FAILED, increment_count=1,
808 error_msg=('Skipped shutdown since %s is present' %
809 NO_REBOOT_FILE))
810 continue
811
812 if (test.operation == factory.ShutdownStep.HALT and
813 self.options.automation_mode == AutomationMode.FULL):
814 logging.info('Skip halt in full automation mode.')
815 test.update_state(status=TestState.PASSED)
Jon Salz0697cbf2012-07-04 15:14:04 +0800816 continue
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800817
Jon Salz0697cbf2012-07-04 15:14:04 +0800818 test.update_state(status=TestState.ACTIVE, increment_count=1,
Ricky Liang6fe218c2013-12-27 15:17:17 +0800819 error_msg='', shutdown_count=0)
Jon Salz0697cbf2012-07-04 15:14:04 +0800820 if self._prompt_cancel_shutdown(test, 1):
821 self.event_log.Log('reboot_cancelled')
822 test.update_state(
Ricky Liang6fe218c2013-12-27 15:17:17 +0800823 status=TestState.FAILED, increment_count=1,
824 error_msg='Shutdown aborted by operator',
825 shutdown_count=0)
chungyiafe8f772012-08-15 19:36:29 +0800826 continue
Jon Salz2f757d42012-06-27 17:06:42 +0800827
Jon Salz0697cbf2012-07-04 15:14:04 +0800828 # Save pending test list in the state server
Jon Salzdbf398f2012-06-14 17:30:01 +0800829 self.state_instance.set_shared_data(
Ricky Liang6fe218c2013-12-27 15:17:17 +0800830 'tests_after_shutdown',
831 [t.path for t in self.tests_to_run])
Jon Salz0697cbf2012-07-04 15:14:04 +0800832 # Save shutdown time
Ricky Liang6fe218c2013-12-27 15:17:17 +0800833 self.state_instance.set_shared_data('shutdown_time', time.time())
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800834
Jon Salz0697cbf2012-07-04 15:14:04 +0800835 with self.env.lock:
836 self.event_log.Log('shutdown', operation=test.operation)
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800837 if (test.enable_guest_mode and
838 not os.path.exists(
839 test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)):
840 # Create a temporary file GUEST_MODE_TAG_FILE to enable guest mode
841 # on next boot.
842 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
Jon Salz0697cbf2012-07-04 15:14:04 +0800843 shutdown_result = self.env.shutdown(test.operation)
844 if shutdown_result:
845 # That's all, folks!
846 self.run_queue.put(None)
847 return
848 else:
849 # Just pass (e.g., in the chroot).
850 test.update_state(status=TestState.PASSED)
Ricky Liang6fe218c2013-12-27 15:17:17 +0800851 self.state_instance.set_shared_data('tests_after_shutdown', None)
Jon Salz0697cbf2012-07-04 15:14:04 +0800852 # Send event with no fields to indicate that there is no
853 # longer a pending shutdown.
Ricky Liang6fe218c2013-12-27 15:17:17 +0800854 self.event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
Jon Salz0697cbf2012-07-04 15:14:04 +0800855 continue
Jon Salz258a40c2012-04-19 12:34:01 +0800856
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800857 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800858
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800859 def _run_test(self, test, iterations_left=None, retries_left=None):
Jon Salz1acc8742012-07-17 17:45:55 +0800860 invoc = TestInvocation(self, test, on_completion=self.run_next_test)
861 new_state = test.update_state(
862 status=TestState.ACTIVE, increment_count=1, error_msg='',
Jon Salzbd42ce12012-09-18 08:03:59 +0800863 invocation=invoc.uuid, iterations_left=iterations_left,
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800864 retries_left=retries_left,
Jon Salzbd42ce12012-09-18 08:03:59 +0800865 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800866 invoc.count = new_state.count
867
868 self.invocations[test] = invoc
869 if self.visible_test is None and test.has_ui:
870 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800871 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800872 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800873
Vic Yang311ddb82012-09-26 12:08:28 +0800874 def check_exclusive(self):
Jon Salzce6a7f82013-06-10 18:22:54 +0800875 # alias since this is really long
876 EXCL_OPT = factory.FactoryTest.EXCLUSIVE_OPTIONS
877
Vic Yang311ddb82012-09-26 12:08:28 +0800878 current_exclusive_items = set([
Jon Salzce6a7f82013-06-10 18:22:54 +0800879 item for item in EXCL_OPT
Vic Yang311ddb82012-09-26 12:08:28 +0800880 if any([test.is_exclusive(item) for test in self.invocations])])
881
882 new_exclusive_items = current_exclusive_items - self.exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800883 if EXCL_OPT.NETWORKING in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800884 logging.info('Disabling network')
885 self.connection_manager.DisableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800886 if EXCL_OPT.CHARGER in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800887 logging.info('Stop controlling charger')
888
889 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800890 if EXCL_OPT.NETWORKING in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800891 logging.info('Re-enabling network')
892 self.connection_manager.EnableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800893 if EXCL_OPT.CHARGER in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800894 logging.info('Start controlling charger')
895
Jon Salzce6a7f82013-06-10 18:22:54 +0800896 if self.cpufreq_manager:
897 enabled = EXCL_OPT.CPUFREQ not in current_exclusive_items
898 try:
899 self.cpufreq_manager.SetEnabled(enabled)
900 except: # pylint: disable=W0702
901 logging.exception('Unable to %s cpufreq services',
902 'enable' if enabled else 'disable')
903
Vic Yang311ddb82012-09-26 12:08:28 +0800904 # Only adjust charge state if not excluded
Jon Salzce6a7f82013-06-10 18:22:54 +0800905 if (EXCL_OPT.CHARGER not in current_exclusive_items and
906 not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800907 if self.charge_manager:
908 self.charge_manager.AdjustChargeState()
909 else:
910 try:
911 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
912 except BoardException:
913 logging.exception('Unable to set charge state on this board')
Vic Yang311ddb82012-09-26 12:08:28 +0800914
915 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800916
cychiang21886742012-07-05 15:16:32 +0800917 def check_for_updates(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800918 """Schedules an asynchronous check for updates if necessary."""
cychiang21886742012-07-05 15:16:32 +0800919 if not self.test_list.options.update_period_secs:
920 # Not enabled.
921 return
922
923 now = time.time()
924 if self.last_update_check and (
925 now - self.last_update_check <
926 self.test_list.options.update_period_secs):
927 # Not yet time for another check.
928 return
929
930 self.last_update_check = now
931
932 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
933 if reached_shopfloor:
934 new_update_md5sum = md5sum if needs_update else None
935 if system.SystemInfo.update_md5sum != new_update_md5sum:
936 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
937 system.SystemInfo.update_md5sum = new_update_md5sum
938 self.run_queue.put(self.update_system_info)
939
940 updater.CheckForUpdateAsync(
941 handle_check_for_update,
942 self.test_list.options.shopfloor_timeout_secs)
943
Jon Salza6711d72012-07-18 14:33:03 +0800944 def cancel_pending_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800945 """Cancels any tests in the run queue."""
Jon Salza6711d72012-07-18 14:33:03 +0800946 self.run_tests([])
947
Jon Salz0697cbf2012-07-04 15:14:04 +0800948 def run_tests(self, subtrees, untested_only=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800949 """Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800950
Jon Salz0697cbf2012-07-04 15:14:04 +0800951 The tests are run in order unless one fails (then stops).
952 Backgroundable tests are run simultaneously; when a foreground test is
953 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800954
Ricky Liang6fe218c2013-12-27 15:17:17 +0800955 Args:
956 subtrees: Node or nodes containing tests to run (may either be
957 a single test or a list). Duplicates will be ignored.
958 untested_only: True to run untested tests only.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800959 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800960 if type(subtrees) != list:
961 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800962
Jon Salz0697cbf2012-07-04 15:14:04 +0800963 # Nodes we've seen so far, to avoid duplicates.
964 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800965
Jon Salz0697cbf2012-07-04 15:14:04 +0800966 self.tests_to_run = deque()
967 for subtree in subtrees:
968 for test in subtree.walk():
969 if test in seen:
970 continue
971 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800972
Jon Salz0697cbf2012-07-04 15:14:04 +0800973 if not test.is_leaf():
974 continue
975 if (untested_only and
976 test.get_state().status != TestState.UNTESTED):
977 continue
978 self.tests_to_run.append(test)
979 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800980
Jon Salz0697cbf2012-07-04 15:14:04 +0800981 def reap_completed_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800982 """Removes completed tests from the set of active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800983
984 Also updates the visible test if it was reaped.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800985 """
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800986 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800987 for t, v in dict(self.invocations).iteritems():
988 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800989 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800990 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800991 del self.invocations[t]
992
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800993 # Stop on failure if flag is true.
994 if (self.test_list.options.stop_on_failure and
995 new_state.status == TestState.FAILED):
996 # Clean all the tests to cause goofy to stop.
997 self.tests_to_run = []
998 factory.console.info("Stop on failure triggered. Empty the queue.")
999
Jon Salz1acc8742012-07-17 17:45:55 +08001000 if new_state.iterations_left and new_state.status == TestState.PASSED:
1001 # Play it again, Sam!
1002 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +08001003 # new_state.retries_left is obtained after update.
1004 # For retries_left == 0, test can still be run for the last time.
1005 elif (new_state.retries_left >= 0 and
1006 new_state.status == TestState.FAILED):
1007 # Still have to retry, Sam!
1008 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +08001009
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001010 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +08001011 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001012
Jon Salz0697cbf2012-07-04 15:14:04 +08001013 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +08001014 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +08001015 self.set_visible_test(None)
1016 # Make the first running test, if any, the visible test
1017 for t in self.test_list.walk():
1018 if t in self.invocations:
1019 self.set_visible_test(t)
1020 break
1021
Jon Salz6dc031d2013-06-19 13:06:23 +08001022 def kill_active_tests(self, abort, root=None, reason=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001023 """Kills and waits for all active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +08001024
Jon Salz85a39882012-07-05 16:45:04 +08001025 Args:
1026 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +08001027 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +08001028 root: If set, only kills tests with root as an ancestor.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001029 reason: If set, the abort reason.
1030 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001031 self.reap_completed_tests()
1032 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +08001033 if root and not test.has_ancestor(root):
1034 continue
1035
Jon Salz0697cbf2012-07-04 15:14:04 +08001036 factory.console.info('Killing active test %s...' % test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +08001037 invoc.abort_and_join(reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001038 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +08001039 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +08001040 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +08001041
Jon Salz0697cbf2012-07-04 15:14:04 +08001042 if not abort:
1043 test.update_state(status=TestState.UNTESTED)
1044 self.reap_completed_tests()
1045
Jon Salz6dc031d2013-06-19 13:06:23 +08001046 def stop(self, root=None, fail=False, reason=None):
1047 self.kill_active_tests(fail, root, reason)
Jon Salz85a39882012-07-05 16:45:04 +08001048 # Remove any tests in the run queue under the root.
1049 self.tests_to_run = deque([x for x in self.tests_to_run
1050 if root and not x.has_ancestor(root)])
1051 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +08001052
Jon Salz4712ac72013-02-07 17:12:05 +08001053 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +08001054 if root is None:
1055 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +08001056 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +08001057 for f in root.walk():
1058 if f.is_leaf():
1059 f.update_state(status=TestState.UNTESTED)
1060
Jon Salz6dc031d2013-06-19 13:06:23 +08001061 def abort_active_tests(self, reason=None):
1062 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001063
1064 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +08001065 syslog.openlog('goofy')
1066
Jon Salz0697cbf2012-07-04 15:14:04 +08001067 try:
Jon Salzd7550792013-07-12 05:49:27 +08001068 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +08001069 self.init()
1070 self.event_log.Log('goofy_init',
1071 success=True)
1072 except:
1073 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001074 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001075 self.event_log.Log('goofy_init',
1076 success=False,
1077 trace=traceback.format_exc())
1078 except: # pylint: disable=W0702
1079 pass
1080 raise
1081
Jon Salzd7550792013-07-12 05:49:27 +08001082 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +08001083 syslog.syslog('Goofy (factory test harness) starting')
Jon Salz0697cbf2012-07-04 15:14:04 +08001084 self.run()
1085
1086 def update_system_info(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001087 """Updates system info."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001088 system_info = system.SystemInfo()
1089 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1090 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1091 system_info=system_info.__dict__))
1092 logging.info('System info: %r', system_info.__dict__)
1093
Jon Salzeb42f0d2012-07-27 19:14:04 +08001094 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001095 """Commences updating factory software.
Jon Salzeb42f0d2012-07-27 19:14:04 +08001096
1097 Args:
1098 auto_run_on_restart: Auto-run when the machine comes back up.
1099 post_update_hook: Code to call after update but immediately before
1100 restart.
1101
1102 Returns:
1103 Never if the update was successful (we just reboot).
1104 False if the update was unnecessary (no update available).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001105 """
Jon Salz6dc031d2013-06-19 13:06:23 +08001106 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +08001107 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001108
Jon Salz5c344f62012-07-13 14:31:16 +08001109 def pre_update_hook():
1110 if auto_run_on_restart:
1111 self.state_instance.set_shared_data('tests_after_shutdown',
1112 FORCE_AUTO_RUN)
1113 self.state_instance.close()
1114
Jon Salzeb42f0d2012-07-27 19:14:04 +08001115 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1116 if post_update_hook:
1117 post_update_hook()
1118 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001119
Jon Salzcef132a2012-08-30 04:58:08 +08001120 def handle_sigint(self, dummy_signum, dummy_frame):
Jon Salz77c151e2012-08-28 07:20:37 +08001121 logging.error('Received SIGINT')
1122 self.run_queue.put(None)
1123 raise KeyboardInterrupt()
1124
Jon Salze12c2b32013-06-25 16:24:34 +08001125 def find_kcrashes(self):
1126 """Finds kcrash files, logs them, and marks them as seen."""
1127 seen_crashes = set(
1128 self.state_instance.get_shared_data('seen_crashes', optional=True)
1129 or [])
1130
1131 for path in glob.glob('/var/spool/crash/*'):
1132 if not os.path.isfile(path):
1133 continue
1134 if path in seen_crashes:
1135 continue
1136 try:
1137 stat = os.stat(path)
1138 mtime = utils.TimeString(stat.st_mtime)
1139 logging.info(
1140 'Found new crash file %s (%d bytes at %s)',
1141 path, stat.st_size, mtime)
1142 extra_log_args = {}
1143
1144 try:
1145 _, ext = os.path.splitext(path)
1146 if ext in ['.kcrash', '.meta']:
1147 ext = ext.replace('.', '')
1148 with open(path) as f:
1149 data = f.read(MAX_CRASH_FILE_SIZE)
1150 tell = f.tell()
1151 logging.info(
1152 'Contents of %s%s:%s',
1153 path,
1154 ('' if tell == stat.st_size
1155 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
1156 ('\n' + data).replace('\n', '\n ' + ext + '> '))
1157 extra_log_args['data'] = data
1158
1159 # Copy to /var/factory/kcrash for posterity
1160 kcrash_dir = factory.get_factory_root('kcrash')
1161 utils.TryMakeDirs(kcrash_dir)
1162 shutil.copy(path, kcrash_dir)
1163 logging.info('Copied to %s',
1164 os.path.join(kcrash_dir, os.path.basename(path)))
1165 finally:
1166 # Even if something goes wrong with the above, still try to
1167 # log to event log
1168 self.event_log.Log('crash_file',
1169 path=path, size=stat.st_size, mtime=mtime,
1170 **extra_log_args)
1171 except: # pylint: disable=W0702
1172 logging.exception('Unable to handle crash files %s', path)
1173 seen_crashes.add(path)
1174
1175 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
1176
Jon Salz128b0932013-07-03 16:55:26 +08001177 def GetTestList(self, test_list_id):
1178 """Returns the test list with the given ID.
1179
1180 Raises:
1181 TestListError: The test list ID is not valid.
1182 """
1183 try:
1184 return self.test_lists[test_list_id]
1185 except KeyError:
1186 raise test_lists.TestListError(
1187 '%r is not a valid test list ID (available IDs are [%s])' % (
1188 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
1189
1190 def InitTestLists(self):
1191 """Reads in all test lists and sets the active test list."""
1192 self.test_lists = test_lists.BuildAllTestLists()
Jon Salzd7550792013-07-12 05:49:27 +08001193 logging.info('Loaded test lists: [%s]',
1194 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +08001195
1196 if not self.options.test_list:
1197 self.options.test_list = test_lists.GetActiveTestListId()
1198
1199 if os.sep in self.options.test_list:
1200 # It's a path pointing to an old-style test list; use it.
1201 self.test_list = factory.read_test_list(self.options.test_list)
1202 else:
1203 self.test_list = self.GetTestList(self.options.test_list)
1204
1205 logging.info('Active test list: %s', self.test_list.test_list_id)
1206
1207 if isinstance(self.test_list, test_lists.OldStyleTestList):
1208 # Actually load it in. (See OldStyleTestList for an explanation
1209 # of why this is necessary.)
1210 self.test_list = self.test_list.Load()
1211
1212 self.test_list.state_instance = self.state_instance
1213
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001214 def init_hooks(self):
1215 """Initializes hooks.
1216
1217 Must run after self.test_list ready.
1218 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001219 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
1220 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
1221 assert isinstance(self.hooks, factory.Hooks), (
1222 "hooks should be of type Hooks but is %r" % type(self.hooks))
1223 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001224 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001225
Jon Salz0697cbf2012-07-04 15:14:04 +08001226 def init(self, args=None, env=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001227 """Initializes Goofy.
Jon Salz0697cbf2012-07-04 15:14:04 +08001228
1229 Args:
1230 args: A list of command-line arguments. Uses sys.argv if
1231 args is None.
1232 env: An Environment instance to use (or None to choose
1233 FakeChrootEnvironment or DUTEnvironment as appropriate).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001234 """
Jon Salz77c151e2012-08-28 07:20:37 +08001235 signal.signal(signal.SIGINT, self.handle_sigint)
1236
Jon Salz0697cbf2012-07-04 15:14:04 +08001237 parser = OptionParser()
1238 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001239 action='store_true',
1240 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001241 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001242 metavar='FILE',
1243 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001244 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001245 action='store_true',
1246 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001247 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz8fa8e832012-07-13 19:04:09 +08001248 choices=['none', 'gtk', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001249 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001250 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001251 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001252 type='int', default=1,
1253 help=('Factor by which to scale UI '
1254 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001255 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001256 metavar='FILE',
1257 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001258 parser.add_option('--dummy_shopfloor', action='store_true',
1259 help='Use a dummy shopfloor server')
Ricky Liang6fe218c2013-12-27 15:17:17 +08001260 parser.add_option('--automation-mode',
1261 choices=[m.lower() for m in AutomationMode],
1262 default='none', help="Factory test automation mode.")
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001263 parser.add_option('--guest_login', dest='guest_login', default=False,
Ricky Liangb2432362013-10-02 13:12:41 +08001264 action='store_true',
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001265 help='Log in as guest. This will not own the TPM.')
Jon Salz0697cbf2012-07-04 15:14:04 +08001266 (self.options, self.args) = parser.parse_args(args)
1267
Jon Salz46b89562012-07-05 11:49:22 +08001268 # Make sure factory directories exist.
1269 factory.get_log_root()
1270 factory.get_state_root()
1271 factory.get_test_data_root()
1272
Jon Salz0697cbf2012-07-04 15:14:04 +08001273 global _inited_logging # pylint: disable=W0603
1274 if not _inited_logging:
1275 factory.init_logging('goofy', verbose=self.options.verbose)
1276 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001277
Jon Salz0f996602012-10-03 15:26:48 +08001278 if self.options.print_test_list:
1279 print factory.read_test_list(
1280 self.options.print_test_list).__repr__(recursive=True)
1281 sys.exit(0)
1282
Jon Salzee85d522012-07-17 14:34:46 +08001283 event_log.IncrementBootSequence()
Jon Salzd15bbcf2013-05-21 17:33:57 +08001284 # Don't defer logging the initial event, so we can make sure
1285 # that device_id, reimage_id, etc. are all set up.
1286 self.event_log = EventLog('goofy', defer=False)
Jon Salz0697cbf2012-07-04 15:14:04 +08001287
1288 if (not suppress_chroot_warning and
1289 factory.in_chroot() and
1290 self.options.ui == 'gtk' and
1291 os.environ.get('DISPLAY') in [None, '', ':0', ':0.0']):
1292 # That's not going to work! Tell the user how to run
1293 # this way.
1294 logging.warn(GOOFY_IN_CHROOT_WARNING)
1295 time.sleep(1)
1296
1297 if env:
1298 self.env = env
1299 elif factory.in_chroot():
1300 self.env = test_environment.FakeChrootEnvironment()
1301 logging.warn(
1302 'Using chroot environment: will not actually run autotests')
1303 else:
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001304 if self.options.guest_login:
1305 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
1306 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001307 self.env.goofy = self
1308
1309 if self.options.restart:
1310 state.clear_state()
1311
Jon Salz0697cbf2012-07-04 15:14:04 +08001312 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1313 logging.warn(
1314 'In QEMU; ignoring ui_scale_factor argument')
1315 self.options.ui_scale_factor = 1
1316
1317 logging.info('Started')
1318
1319 self.start_state_server()
1320 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1321 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001322 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001323 self.last_shutdown_time = (
1324 self.state_instance.get_shared_data('shutdown_time', optional=True))
1325 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001326 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001327
Ricky Liang6fe218c2013-12-27 15:17:17 +08001328 self.options.automation_mode = ParseAutomationMode(
1329 self.options.automation_mode)
1330 self.state_instance.set_shared_data('automation_mode',
1331 self.options.automation_mode)
1332 self.state_instance.set_shared_data(
1333 'automation_mode_prompt',
1334 AutomationModePrompt[self.options.automation_mode])
1335
Jon Salz128b0932013-07-03 16:55:26 +08001336 try:
1337 self.InitTestLists()
1338 except: # pylint: disable=W0702
1339 logging.exception('Unable to initialize test lists')
1340 self.state_instance.set_shared_data(
1341 'startup_error',
1342 'Unable to initialize test lists\n%s' % (
1343 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001344 if self.options.ui == 'chrome':
1345 # Create an empty test list with default options so that the rest of
1346 # startup can proceed.
1347 self.test_list = factory.FactoryTestList(
1348 [], self.state_instance, factory.Options())
1349 else:
1350 # Bail with an error; no point in starting up.
1351 sys.exit('No valid test list; exiting.')
1352
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001353 self.init_hooks()
1354
Jon Salz822838b2013-03-25 17:32:33 +08001355 if self.test_list.options.clear_state_on_start:
1356 self.state_instance.clear_test_state()
1357
Vic Yang3e1cf5d2013-06-05 18:50:24 +08001358 if system.SystemInfo().firmware_version is None and not utils.in_chroot():
Vic Yang9bd4f772013-06-04 17:34:00 +08001359 self.state_instance.set_shared_data('startup_error',
1360 'Netboot firmware detected\n'
1361 'Connect Ethernet and reboot to re-image.\n'
1362 u'侦测到网路开机固件\n'
1363 u'请连接乙太网并重启')
1364
Jon Salz0697cbf2012-07-04 15:14:04 +08001365 if not self.state_instance.has_shared_data('ui_lang'):
1366 self.state_instance.set_shared_data('ui_lang',
1367 self.test_list.options.ui_lang)
1368 self.state_instance.set_shared_data(
1369 'test_list_options',
1370 self.test_list.options.__dict__)
1371 self.state_instance.test_list = self.test_list
1372
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001373 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001374
Jon Salz23926422012-09-01 03:38:13 +08001375 if self.options.dummy_shopfloor:
1376 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1377 'http://localhost:%d/' % shopfloor.DEFAULT_SERVER_PORT)
1378 self.dummy_shopfloor = Spawn(
1379 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1380 '--dummy'])
1381 elif self.test_list.options.shopfloor_server_url:
1382 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001383 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001384
Jon Salz0f996602012-10-03 15:26:48 +08001385 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001386 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1387 base_time=time_sanitizer.GetBaseTimeFromFile(
1388 # lsb-factory is written by the factory install shim during
1389 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001390 # the mini-Omaha server. If it's not available, we'll use
1391 # /etc/lsb-factory (which will be much older, but reasonably
1392 # sane) and rely on a shopfloor sync to set a more accurate
1393 # time.
1394 '/usr/local/etc/lsb-factory',
1395 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001396 self.time_sanitizer.RunOnce()
1397
Vic Yangd8990da2013-06-27 16:57:43 +08001398 if self.test_list.options.check_cpu_usage_period_secs:
1399 self.cpu_usage_watcher = Spawn(['py/tools/cpu_usage_monitor.py',
1400 '-p', str(self.test_list.options.check_cpu_usage_period_secs)],
1401 cwd=factory.FACTORY_PATH)
1402
Jon Salz0697cbf2012-07-04 15:14:04 +08001403 self.init_states()
1404 self.start_event_server()
1405 self.connection_manager = self.env.create_connection_manager(
Tai-Hsu Lin371351a2012-08-27 14:17:14 +08001406 self.test_list.options.wlans,
1407 self.test_list.options.scan_wifi_period_secs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001408 # Note that we create a log watcher even if
1409 # sync_event_log_period_secs isn't set (no background
1410 # syncing), since we may use it to flush event logs as well.
1411 self.log_watcher = EventLogWatcher(
1412 self.test_list.options.sync_event_log_period_secs,
Jon Salzd15bbcf2013-05-21 17:33:57 +08001413 event_log_db_file=None,
Jon Salz16d10542012-07-23 12:18:45 +08001414 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001415 if self.test_list.options.sync_event_log_period_secs:
1416 self.log_watcher.StartWatchThread()
1417
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001418 # Creates a system log manager to scan logs periocially.
1419 # A scan includes clearing logs and optionally syncing logs if
1420 # enable_syng_log is True. We kick it to sync logs.
1421 self.system_log_manager = SystemLogManager(
1422 sync_log_paths=self.test_list.options.sync_log_paths,
1423 sync_log_period_secs=self.test_list.options.sync_log_period_secs,
1424 scan_log_period_secs=self.test_list.options.scan_log_period_secs,
Cheng-Yi Chiangb8a491c2014-01-20 14:37:57 +08001425 clear_log_paths=self.test_list.options.clear_log_paths,
1426 clear_log_excluded_paths=self.test_list.options.clear_log_excluded_paths)
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001427 self.system_log_manager.Start()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001428
Jon Salz0697cbf2012-07-04 15:14:04 +08001429 self.update_system_info()
1430
Vic Yang4953fc12012-07-26 16:19:53 +08001431 assert ((self.test_list.options.min_charge_pct is None) ==
1432 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001433 if utils.in_chroot():
1434 logging.info('In chroot, ignoring charge manager and charge state')
1435 elif self.test_list.options.min_charge_pct is not None:
Vic Yang4953fc12012-07-26 16:19:53 +08001436 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1437 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001438 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001439 else:
1440 # Goofy should set charger state to charge if charge_manager is disabled.
1441 try:
1442 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
1443 except BoardException:
1444 logging.exception('Unable to set charge state on this board')
Vic Yang4953fc12012-07-26 16:19:53 +08001445
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001446 self.core_dump_manager = CoreDumpManager(
1447 self.test_list.options.core_dump_watchlist)
1448
Jon Salz0697cbf2012-07-04 15:14:04 +08001449 os.environ['CROS_FACTORY'] = '1'
1450 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1451
1452 # Set CROS_UI since some behaviors in ui.py depend on the
1453 # particular UI in use. TODO(jsalz): Remove this (and all
1454 # places it is used) when the GTK UI is removed.
1455 os.environ['CROS_UI'] = self.options.ui
1456
Shuo-Peng Liao1ff502e2013-06-30 18:37:02 +08001457 if not utils.in_chroot() and self.test_list.options.use_cpufreq_manager:
Jon Salzddf0d052013-06-18 12:52:44 +08001458 self.cpufreq_manager = CpufreqManager(event_log=self.event_log)
Jon Salzce6a7f82013-06-10 18:22:54 +08001459
Justin Chuang31b02432013-06-27 15:16:51 +08001460 # Startup hooks may want to skip some tests.
1461 self.update_skipped_tests()
Jon Salz416f9cc2013-05-10 18:32:50 +08001462
Jon Salze12c2b32013-06-25 16:24:34 +08001463 self.find_kcrashes()
1464
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001465 # Should not move earlier.
1466 self.hooks.OnStartup()
1467
Jon Salz0697cbf2012-07-04 15:14:04 +08001468 if self.options.ui == 'chrome':
1469 self.env.launch_chrome()
1470 logging.info('Waiting for a web socket connection')
Cheng-Yi Chiangfd8ed392013-03-08 21:37:31 +08001471 self.web_socket_manager.wait()
Jon Salz0697cbf2012-07-04 15:14:04 +08001472
1473 # Wait for the test widget size to be set; this is done in
1474 # an asynchronous RPC so there is a small chance that the
1475 # web socket might be opened first.
1476 for _ in range(100): # 10 s
1477 try:
1478 if self.state_instance.get_shared_data('test_widget_size'):
1479 break
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001480 except KeyError:
Jon Salz0697cbf2012-07-04 15:14:04 +08001481 pass # Retry
1482 time.sleep(0.1) # 100 ms
1483 else:
1484 logging.warn('Never received test_widget_size from UI')
Jon Salz45297282013-05-18 14:31:47 +08001485
1486 # Send Chrome a Tab to get focus to the factory UI
1487 # (http://crosbug.com/p/19444). TODO(jsalz): remove this hack
1488 # and figure out the right way to get the focus to Chrome.
1489 if not utils.in_chroot():
Ricky Liangb97f3652013-08-20 17:30:28 +08001490 utils.SendKey('Tab')
Jon Salz0697cbf2012-07-04 15:14:04 +08001491 elif self.options.ui == 'gtk':
1492 self.start_ui()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001493
Ricky Liang650f6bf2012-09-28 13:22:54 +08001494 # Create download path for autotest beforehand or autotests run at
1495 # the same time might fail due to race condition.
1496 if not factory.in_chroot():
1497 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1498 'download'))
1499
Jon Salz0697cbf2012-07-04 15:14:04 +08001500 def state_change_callback(test, test_state):
1501 self.event_client.post_event(
1502 Event(Event.Type.STATE_CHANGE,
1503 path=test.path, state=test_state))
1504 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001505
Jon Salza6711d72012-07-18 14:33:03 +08001506 for handler in self.on_ui_startup:
1507 handler()
1508
1509 self.prespawner = Prespawner()
1510 self.prespawner.start()
1511
Jon Salz0697cbf2012-07-04 15:14:04 +08001512 try:
1513 tests_after_shutdown = self.state_instance.get_shared_data(
1514 'tests_after_shutdown')
1515 except KeyError:
1516 tests_after_shutdown = None
Jon Salz57717ca2012-04-04 16:47:25 +08001517
Jon Salz5c344f62012-07-13 14:31:16 +08001518 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1519 if not force_auto_run and tests_after_shutdown is not None:
Jon Salz0697cbf2012-07-04 15:14:04 +08001520 logging.info('Resuming tests after shutdown: %s',
1521 tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001522 self.tests_to_run.extend(
1523 self.test_list.lookup_path(t) for t in tests_after_shutdown)
1524 self.run_queue.put(self.run_next_test)
1525 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001526 if force_auto_run or self.test_list.options.auto_run_on_start:
Jon Salz0697cbf2012-07-04 15:14:04 +08001527 self.run_queue.put(
1528 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001529 self.state_instance.set_shared_data('tests_after_shutdown', None)
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001530
Dean Liao592e4d52013-01-10 20:06:39 +08001531 self.may_disable_cros_shortcut_keys()
1532
1533 def may_disable_cros_shortcut_keys(self):
1534 test_options = self.test_list.options
1535 if test_options.disable_cros_shortcut_keys:
1536 logging.info('Filter ChromeOS shortcut keys.')
1537 self.key_filter = KeyFilter(
1538 unmap_caps_lock=test_options.disable_caps_lock,
1539 caps_lock_keycode=test_options.caps_lock_keycode)
1540 self.key_filter.Start()
1541
Jon Salz0697cbf2012-07-04 15:14:04 +08001542 def run(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001543 """Runs Goofy."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001544 # Process events forever.
1545 while self.run_once(True):
1546 pass
Jon Salz73e0fd02012-04-04 11:46:38 +08001547
Jon Salz0697cbf2012-07-04 15:14:04 +08001548 def run_once(self, block=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001549 """Runs all items pending in the event loop.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001550
Jon Salz0697cbf2012-07-04 15:14:04 +08001551 Args:
1552 block: If true, block until at least one event is processed.
Jon Salz7c15e8b2012-06-19 17:10:37 +08001553
Jon Salz0697cbf2012-07-04 15:14:04 +08001554 Returns:
1555 True to keep going or False to shut down.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001556 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001557 events = utils.DrainQueue(self.run_queue)
cychiang21886742012-07-05 15:16:32 +08001558 while not events:
Jon Salz0697cbf2012-07-04 15:14:04 +08001559 # Nothing on the run queue.
1560 self._run_queue_idle()
1561 if block:
1562 # Block for at least one event...
cychiang21886742012-07-05 15:16:32 +08001563 try:
1564 events.append(self.run_queue.get(timeout=RUN_QUEUE_TIMEOUT_SECS))
1565 except Queue.Empty:
1566 # Keep going (calling _run_queue_idle() again at the top of
1567 # the loop)
1568 continue
Jon Salz0697cbf2012-07-04 15:14:04 +08001569 # ...and grab anything else that showed up at the same
1570 # time.
1571 events.extend(utils.DrainQueue(self.run_queue))
cychiang21886742012-07-05 15:16:32 +08001572 else:
1573 break
Jon Salz51528e12012-07-02 18:54:45 +08001574
Jon Salz0697cbf2012-07-04 15:14:04 +08001575 for event in events:
1576 if not event:
1577 # Shutdown request.
1578 self.run_queue.task_done()
1579 return False
Jon Salz51528e12012-07-02 18:54:45 +08001580
Jon Salz0697cbf2012-07-04 15:14:04 +08001581 try:
1582 event()
Jon Salz85a39882012-07-05 16:45:04 +08001583 except: # pylint: disable=W0702
1584 logging.exception('Error in event loop')
Jon Salz0697cbf2012-07-04 15:14:04 +08001585 self.record_exception(traceback.format_exception_only(
1586 *sys.exc_info()[:2]))
1587 # But keep going
1588 finally:
1589 self.run_queue.task_done()
1590 return True
Jon Salz0405ab52012-03-16 15:26:52 +08001591
Jon Salz0e6532d2012-10-25 16:30:11 +08001592 def _should_sync_time(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001593 """Returns True if we should attempt syncing time with shopfloor.
Jon Salz0e6532d2012-10-25 16:30:11 +08001594
1595 Args:
1596 foreground: If True, synchronizes even if background syncing
1597 is disabled (e.g., in explicit sync requests from the
1598 SyncShopfloor test).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001599 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001600 return ((foreground or
1601 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001602 self.time_sanitizer and
1603 (not self.time_synced) and
1604 (not factory.in_chroot()))
1605
Jon Salz0e6532d2012-10-25 16:30:11 +08001606 def sync_time_with_shopfloor_server(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001607 """Syncs time with shopfloor server, if not yet synced.
Jon Salz54882d02012-08-31 01:57:54 +08001608
Jon Salz0e6532d2012-10-25 16:30:11 +08001609 Args:
1610 foreground: If True, synchronizes even if background syncing
1611 is disabled (e.g., in explicit sync requests from the
1612 SyncShopfloor test).
1613
Jon Salz54882d02012-08-31 01:57:54 +08001614 Returns:
1615 False if no time sanitizer is available, or True if this sync (or a
1616 previous sync) succeeded.
1617
1618 Raises:
1619 Exception if unable to contact the shopfloor server.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001620 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001621 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001622 self.time_sanitizer.SyncWithShopfloor()
1623 self.time_synced = True
1624 return self.time_synced
1625
Jon Salzb92c5112012-09-21 15:40:11 +08001626 def log_disk_space_stats(self):
Jon Salz18e0e022013-06-11 17:13:39 +08001627 if (utils.in_chroot() or
1628 not self.test_list.options.log_disk_space_period_secs):
Jon Salzb92c5112012-09-21 15:40:11 +08001629 return
1630
1631 now = time.time()
1632 if (self.last_log_disk_space_time and
1633 now - self.last_log_disk_space_time <
1634 self.test_list.options.log_disk_space_period_secs):
1635 return
1636 self.last_log_disk_space_time = now
1637
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001638 # Upload event if stateful partition usage is above threshold.
1639 # Stateful partition is mounted on /usr/local, while
1640 # encrypted stateful partition is mounted on /var.
1641 # If there are too much logs in the factory process,
1642 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001643 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001644 vfs_infos = disk_space.GetAllVFSInfo()
1645 stateful_info, encrypted_info = None, None
1646 for vfs_info in vfs_infos.values():
1647 if '/usr/local' in vfs_info.mount_points:
1648 stateful_info = vfs_info
1649 if '/var' in vfs_info.mount_points:
1650 encrypted_info = vfs_info
1651
1652 stateful = disk_space.GetPartitionUsage(stateful_info)
1653 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1654
1655 above_threshold = (
1656 self.test_list.options.stateful_usage_threshold and
1657 max(stateful.bytes_used_pct,
1658 stateful.inodes_used_pct,
1659 encrypted.bytes_used_pct,
1660 encrypted.inodes_used_pct) >
1661 self.test_list.options.stateful_usage_threshold)
1662
1663 if above_threshold:
1664 self.event_log.Log('stateful_partition_usage',
1665 partitions={
1666 'stateful': {
1667 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1668 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1669 'encrypted_stateful': {
1670 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1671 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1672 })
1673 self.log_watcher.ScanEventLogs()
Cheng-Yi Chiang00798e72013-06-20 18:16:39 +08001674 if (not utils.in_chroot() and
1675 self.test_list.options.stateful_usage_above_threshold_action):
1676 Spawn(self.test_list.options.stateful_usage_above_threshold_action,
1677 call=True)
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001678
1679 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001680 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001681 if above_threshold:
1682 logging.warning(message)
1683 else:
1684 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001685 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001686 except: # pylint: disable=W0702
1687 logging.exception('Unable to get disk space used')
1688
Justin Chuang83813982013-05-13 01:26:32 +08001689 def check_battery(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001690 """Checks the current battery status.
Justin Chuang83813982013-05-13 01:26:32 +08001691
1692 Logs current battery charging level and status to log. If the battery level
1693 is lower below warning_low_battery_pct, send warning event to shopfloor.
1694 If the battery level is lower below critical_low_battery_pct, flush disks.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001695 """
Justin Chuang83813982013-05-13 01:26:32 +08001696 if not self.test_list.options.check_battery_period_secs:
1697 return
1698
1699 now = time.time()
1700 if (self.last_check_battery_time and
1701 now - self.last_check_battery_time <
1702 self.test_list.options.check_battery_period_secs):
1703 return
1704 self.last_check_battery_time = now
1705
1706 message = ''
1707 log_level = logging.INFO
1708 try:
1709 power = system.GetBoard().power
1710 if not power.CheckBatteryPresent():
1711 message = 'Battery is not present'
1712 else:
1713 ac_present = power.CheckACPresent()
1714 charge_pct = power.GetChargePct(get_float=True)
1715 message = ('Current battery level %.1f%%, AC charger is %s' %
1716 (charge_pct, 'connected' if ac_present else 'disconnected'))
1717
1718 if charge_pct > self.test_list.options.critical_low_battery_pct:
1719 critical_low_battery = False
1720 else:
1721 critical_low_battery = True
1722 # Only sync disks when battery level is still above minimum
1723 # value. This can be used for offline analysis when shopfloor cannot
1724 # be connected.
1725 if charge_pct > MIN_BATTERY_LEVEL_FOR_DISK_SYNC:
1726 logging.warning('disk syncing for critical low battery situation')
1727 os.system('sync; sync; sync')
1728 else:
1729 logging.warning('disk syncing is cancelled '
1730 'because battery level is lower than %.1f',
1731 MIN_BATTERY_LEVEL_FOR_DISK_SYNC)
1732
1733 # Notify shopfloor server
1734 if (critical_low_battery or
1735 (not ac_present and
1736 charge_pct <= self.test_list.options.warning_low_battery_pct)):
1737 log_level = logging.WARNING
1738
1739 self.event_log.Log('low_battery',
1740 battery_level=charge_pct,
1741 charger_connected=ac_present,
1742 critical=critical_low_battery)
1743 self.log_watcher.KickWatchThread()
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001744 if self.test_list.options.enable_sync_log:
1745 self.system_log_manager.KickToSync()
Justin Chuang83813982013-05-13 01:26:32 +08001746 except: # pylint: disable=W0702
1747 logging.exception('Unable to check battery or notify shopfloor')
1748 finally:
1749 if message != self.last_check_battery_message:
1750 logging.log(log_level, message)
1751 self.last_check_battery_message = message
1752
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001753 def check_core_dump(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001754 """Checks if there is any core dumped file.
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001755
1756 Removes unwanted core dump files immediately.
1757 Syncs those files matching watch list to server with a delay between
1758 each sync. After the files have been synced to server, deletes the files.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001759 """
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001760 core_dump_files = self.core_dump_manager.ScanFiles()
1761 if core_dump_files:
1762 now = time.time()
1763 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1764 self.test_list.options.kick_sync_min_interval_secs):
1765 return
1766 self.last_kick_sync_time = now
1767
1768 # Sends event to server
1769 self.event_log.Log('core_dumped', files=core_dump_files)
1770 self.log_watcher.KickWatchThread()
1771
1772 # Syncs files to server
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001773 if self.test_list.options.enable_sync_log:
1774 self.system_log_manager.KickToSync(
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001775 core_dump_files, self.core_dump_manager.ClearFiles)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001776
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001777 def check_log_rotation(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001778 """Checks log rotation file presence/absence according to test_list option.
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001779
1780 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1781 is True, delete it otherwise. This must be done in idle loop because
1782 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1783 an autotest.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001784 """
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001785 if utils.in_chroot():
1786 return
1787 try:
1788 if self.test_list.options.disable_log_rotation:
1789 open(CLEANUP_LOGS_PAUSED, 'w').close()
1790 else:
1791 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1792 except: # pylint: disable=W0702
1793 # Oh well. Logs an error (but no trace)
1794 logging.info(
1795 'Unable to %s %s: %s',
1796 'touch' if self.test_list.options.disable_log_rotation else 'delete',
1797 CLEANUP_LOGS_PAUSED, utils.FormatExceptionOnly())
1798
Jon Salz8fa8e832012-07-13 19:04:09 +08001799 def sync_time_in_background(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001800 """Writes out current time and tries to sync with shopfloor server."""
Jon Salzb22d1172012-08-06 10:38:57 +08001801 if not self.time_sanitizer:
1802 return
1803
1804 # Write out the current time.
1805 self.time_sanitizer.SaveTime()
1806
Jon Salz54882d02012-08-31 01:57:54 +08001807 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001808 return
1809
1810 now = time.time()
1811 if self.last_sync_time and (
1812 now - self.last_sync_time <
1813 self.test_list.options.sync_time_period_secs):
1814 # Not yet time for another check.
1815 return
1816 self.last_sync_time = now
1817
1818 def target():
1819 try:
Jon Salz54882d02012-08-31 01:57:54 +08001820 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001821 except: # pylint: disable=W0702
1822 # Oh well. Log an error (but no trace)
1823 logging.info(
1824 'Unable to get time from shopfloor server: %s',
1825 utils.FormatExceptionOnly())
1826
1827 thread = threading.Thread(target=target)
1828 thread.daemon = True
1829 thread.start()
1830
Jon Salz0697cbf2012-07-04 15:14:04 +08001831 def _run_queue_idle(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001832 """Invoked when the run queue has no events.
Vic Yang4953fc12012-07-26 16:19:53 +08001833
1834 This method must not raise exception.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001835 """
Jon Salzb22d1172012-08-06 10:38:57 +08001836 now = time.time()
1837 if (self.last_idle and
1838 now < (self.last_idle + RUN_QUEUE_TIMEOUT_SECS - 1)):
1839 # Don't run more often than once every (RUN_QUEUE_TIMEOUT_SECS -
1840 # 1) seconds.
1841 return
1842
1843 self.last_idle = now
1844
Vic Yang311ddb82012-09-26 12:08:28 +08001845 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001846 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001847 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001848 self.log_disk_space_stats()
Justin Chuang83813982013-05-13 01:26:32 +08001849 self.check_battery()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001850 self.check_core_dump()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001851 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001852
Jon Salzd15bbcf2013-05-21 17:33:57 +08001853 def handle_event_logs(self, chunks):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001854 """Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001855
Jon Salz0697cbf2012-07-04 15:14:04 +08001856 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001857
1858 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001859 chunks: A list of Chunk objects.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001860 """
Vic Yang93027612013-05-06 02:42:49 +08001861 first_exception = None
1862 exception_count = 0
1863
Jon Salzd15bbcf2013-05-21 17:33:57 +08001864 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001865 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001866 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001867 start_time = time.time()
1868 shopfloor_client = shopfloor.get_instance(
1869 detect=True,
1870 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzd15bbcf2013-05-21 17:33:57 +08001871 shopfloor_client.UploadEvent(chunk.log_name + "." +
1872 event_log.GetReimageId(),
1873 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001874 logging.info(
1875 'Successfully synced %s in %.03f s',
1876 description, time.time() - start_time)
1877 except: # pylint: disable=W0702
Jon Salzd15bbcf2013-05-21 17:33:57 +08001878 first_exception = (first_exception or (chunk.log_name + ': ' +
Vic Yang93027612013-05-06 02:42:49 +08001879 utils.FormatExceptionOnly()))
1880 exception_count += 1
1881
1882 if exception_count:
1883 if exception_count == 1:
1884 msg = 'Log upload failed: %s' % first_exception
1885 else:
1886 msg = '%d log upload failed; first is: %s' % (
1887 exception_count, first_exception)
1888 raise Exception(msg)
1889
Jon Salz57717ca2012-04-04 16:47:25 +08001890
Jon Salz0697cbf2012-07-04 15:14:04 +08001891 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1892 root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001893 """Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001894
Jon Salz0697cbf2012-07-04 15:14:04 +08001895 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001896
Jon Salz0697cbf2012-07-04 15:14:04 +08001897 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001898 statuses_to_run: The particular status that caller wants to run.
Jon Salz0697cbf2012-07-04 15:14:04 +08001899 starting_at: If provided, only auto-runs tests beginning with
1900 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001901 root: The root of tests to run. If not provided, it will be
1902 the root of all tests.
1903 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001904 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001905
Jon Salz0697cbf2012-07-04 15:14:04 +08001906 if starting_at:
1907 # Make sure they passed a test, not a string.
1908 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001909
Jon Salz0697cbf2012-07-04 15:14:04 +08001910 tests_to_reset = []
1911 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001912
Jon Salz0697cbf2012-07-04 15:14:04 +08001913 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001914
Jon Salz0697cbf2012-07-04 15:14:04 +08001915 for test in root.get_top_level_tests():
1916 if starting_at:
1917 if test == starting_at:
1918 # We've found starting_at; do auto-run on all
1919 # subsequent tests.
1920 found_starting_at = True
1921 if not found_starting_at:
1922 # Don't start this guy yet
1923 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001924
Jon Salz0697cbf2012-07-04 15:14:04 +08001925 status = test.get_state().status
1926 if status == TestState.ACTIVE or status in statuses_to_run:
1927 # Reset the test (later; we will need to abort
1928 # all active tests first).
1929 tests_to_reset.append(test)
1930 if status in statuses_to_run:
1931 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001932
Jon Salz6dc031d2013-06-19 13:06:23 +08001933 self.abort_active_tests('Operator requested run/re-run of certain tests')
Jon Salz258a40c2012-04-19 12:34:01 +08001934
Jon Salz0697cbf2012-07-04 15:14:04 +08001935 # Reset all statuses of the tests to run (in case any tests were active;
1936 # we want them to be run again).
1937 for test_to_reset in tests_to_reset:
1938 for test in test_to_reset.walk():
1939 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001940
Jon Salz0697cbf2012-07-04 15:14:04 +08001941 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001942
Jon Salz0697cbf2012-07-04 15:14:04 +08001943 def restart_tests(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001944 """Restarts all tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001945 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001946
Jon Salz6dc031d2013-06-19 13:06:23 +08001947 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001948 for test in root.walk():
1949 test.update_state(status=TestState.UNTESTED)
1950 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001951
Jon Salz0697cbf2012-07-04 15:14:04 +08001952 def auto_run(self, starting_at=None, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001953 """"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001954
Jon Salz0697cbf2012-07-04 15:14:04 +08001955 Args:
1956 starting_at: If provide, only auto-runs tests beginning with
1957 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001958 root: If provided, the root of tests to run. If not provided, the root
1959 will be test_list (root of all tests).
1960 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001961 root = root or self.test_list
1962 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1963 starting_at=starting_at,
1964 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001965
Jon Salz0697cbf2012-07-04 15:14:04 +08001966 def re_run_failed(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001967 """Re-runs failed tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001968 root = root or self.test_list
1969 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001970
Jon Salz0697cbf2012-07-04 15:14:04 +08001971 def show_review_information(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001972 """Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001973
Jon Salz0697cbf2012-07-04 15:14:04 +08001974 The information screene is rendered by main UI program (ui.py), so in
1975 goofy we only need to kill all active tests, set them as untested, and
1976 clear remaining tests.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001977 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001978 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001979 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001980
Jon Salz0697cbf2012-07-04 15:14:04 +08001981 def handle_switch_test(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001982 """Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001983
Ricky Liang6fe218c2013-12-27 15:17:17 +08001984 Args:
1985 event: The SWITCH_TEST event.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001986 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001987 test = self.test_list.lookup_path(event.path)
1988 if not test:
1989 logging.error('Unknown test %r', event.key)
1990 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001991
Jon Salz0697cbf2012-07-04 15:14:04 +08001992 invoc = self.invocations.get(test)
1993 if invoc and test.backgroundable:
1994 # Already running: just bring to the front if it
1995 # has a UI.
1996 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001997 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001998 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001999
Jon Salz6dc031d2013-06-19 13:06:23 +08002000 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08002001 for t in test.walk():
2002 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08002003
Jon Salz0697cbf2012-07-04 15:14:04 +08002004 if self.test_list.options.auto_run_on_keypress:
2005 self.auto_run(starting_at=test)
2006 else:
2007 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08002008
Jon Salz0697cbf2012-07-04 15:14:04 +08002009 def wait(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002010 """Waits for all pending invocations.
Jon Salz0697cbf2012-07-04 15:14:04 +08002011
2012 Useful for testing.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002013 """
Jon Salz1acc8742012-07-17 17:45:55 +08002014 while self.invocations:
2015 for k, v in self.invocations.iteritems():
2016 logging.info('Waiting for %s to complete...', k)
2017 v.thread.join()
2018 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08002019
2020 def check_exceptions(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002021 """Raises an error if any exceptions have occurred in
2022 invocation threads.
2023 """
Jon Salz0697cbf2012-07-04 15:14:04 +08002024 if self.exceptions:
2025 raise RuntimeError('Exception in invocation thread: %r' %
2026 self.exceptions)
2027
2028 def record_exception(self, msg):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002029 """Records an exception in an invocation thread.
Jon Salz0697cbf2012-07-04 15:14:04 +08002030
2031 An exception with the given message will be rethrown when
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002032 Goofy is destroyed.
2033 """
Jon Salz0697cbf2012-07-04 15:14:04 +08002034 self.exceptions.append(msg)
Jon Salz73e0fd02012-04-04 11:46:38 +08002035
Hung-Te Linf2f78f72012-02-08 19:27:11 +08002036
2037if __name__ == '__main__':
Jon Salz77c151e2012-08-28 07:20:37 +08002038 goofy = Goofy()
2039 try:
2040 goofy.main()
Jon Salz0f996602012-10-03 15:26:48 +08002041 except SystemExit:
2042 # Propagate SystemExit without logging.
2043 raise
Jon Salz31373eb2012-09-21 16:19:49 +08002044 except:
Jon Salz0f996602012-10-03 15:26:48 +08002045 # Log the error before trying to shut down (unless it's a graceful
2046 # exit).
Jon Salz31373eb2012-09-21 16:19:49 +08002047 logging.exception('Error in main loop')
2048 raise
Jon Salz77c151e2012-08-28 07:20:37 +08002049 finally:
2050 goofy.destroy()