blob: 9441ce0bc72eef49d6f004ecad2a074b17c1b017 [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
Jon Salz83591782012-06-26 11:09:58 +080050from cros.factory.test.event import Event
51from cros.factory.test.event import EventClient
52from cros.factory.test.event import EventServer
jcliangcd688182012-08-20 21:01:26 +080053from cros.factory.test.factory import TestState
Jon Salzd7550792013-07-12 05:49:27 +080054from cros.factory.test.utils import Enum
Dean Liao592e4d52013-01-10 20:06:39 +080055from cros.factory.tools.key_filter import KeyFilter
Jon Salz2af235d2013-06-24 14:47:21 +080056from cros.factory.utils import file_utils
Jon Salz78c32392012-07-25 14:18:29 +080057from cros.factory.utils.process_utils import Spawn
Hung-Te Linf2f78f72012-02-08 19:27:11 +080058
59
Hung-Te Linf2f78f72012-02-08 19:27:11 +080060HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Chun-ta Lin279e7e92013-02-19 17:40:39 +080061CACHES_DIR = os.path.join(factory.get_state_root(), "caches")
Hung-Te Linf2f78f72012-02-08 19:27:11 +080062
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +080063CLEANUP_LOGS_PAUSED = '/var/lib/cleanup_logs_paused'
64
Jon Salz8796e362012-05-24 11:39:09 +080065# File that suppresses reboot if present (e.g., for development).
66NO_REBOOT_FILE = '/var/log/factory.noreboot'
67
Jon Salz5c344f62012-07-13 14:31:16 +080068# Value for tests_after_shutdown that forces auto-run (e.g., after
69# a factory update, when the available set of tests might change).
70FORCE_AUTO_RUN = 'force_auto_run'
71
cychiang21886742012-07-05 15:16:32 +080072RUN_QUEUE_TIMEOUT_SECS = 10
73
Justin Chuang83813982013-05-13 01:26:32 +080074# Sync disks when battery level is higher than this value.
75# Otherwise, power loss during disk sync operation may incur even worse outcome.
76MIN_BATTERY_LEVEL_FOR_DISK_SYNC = 1.0
77
Jon Salze12c2b32013-06-25 16:24:34 +080078MAX_CRASH_FILE_SIZE = 64*1024
79
Jon Salz758e6cc2012-04-03 15:47:07 +080080GOOFY_IN_CHROOT_WARNING = '\n' + ('*' * 70) + '''
81You are running Goofy inside the chroot. Autotests are not supported.
82
83To use Goofy in the chroot, first install an Xvnc server:
84
Jon Salz0697cbf2012-07-04 15:14:04 +080085 sudo apt-get install tightvncserver
Jon Salz758e6cc2012-04-03 15:47:07 +080086
87...and then start a VNC X server outside the chroot:
88
Jon Salz0697cbf2012-07-04 15:14:04 +080089 vncserver :10 &
90 vncviewer :10
Jon Salz758e6cc2012-04-03 15:47:07 +080091
92...and run Goofy as follows:
93
Jon Salz0697cbf2012-07-04 15:14:04 +080094 env --unset=XAUTHORITY DISPLAY=localhost:10 python goofy.py
Jon Salz758e6cc2012-04-03 15:47:07 +080095''' + ('*' * 70)
Jon Salz73e0fd02012-04-04 11:46:38 +080096suppress_chroot_warning = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +080097
Jon Salzd7550792013-07-12 05:49:27 +080098Status = Enum(['UNINITIALIZED', 'INITIALIZING', 'RUNNING',
99 'TERMINATING', 'TERMINATED'])
100
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800101def get_hwid_cfg():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800102 """Returns the HWID config tag, or an empty string if none can be found."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800103 if 'CROS_HWID' in os.environ:
104 return os.environ['CROS_HWID']
105 if os.path.exists(HWID_CFG_PATH):
106 with open(HWID_CFG_PATH, 'rt') as hwid_cfg_handle:
107 return hwid_cfg_handle.read().strip()
108 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800109
110
Jon Salz73e0fd02012-04-04 11:46:38 +0800111_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800112
113class Goofy(object):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800114 """The main factory flow.
Jon Salz0697cbf2012-07-04 15:14:04 +0800115
116 Note that all methods in this class must be invoked from the main
117 (event) thread. Other threads, such as callbacks and TestInvocation
118 methods, should instead post events on the run queue.
119
120 TODO: Unit tests. (chrome-os-partner:7409)
121
122 Properties:
123 uuid: A unique UUID for this invocation of Goofy.
124 state_instance: An instance of FactoryState.
125 state_server: The FactoryState XML/RPC server.
126 state_server_thread: A thread running state_server.
127 event_server: The EventServer socket server.
128 event_server_thread: A thread running event_server.
129 event_client: A client to the event server.
130 connection_manager: The connection_manager object.
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800131 system_log_manager: The SystemLogManager object.
132 core_dump_manager: The CoreDumpManager object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800133 ui_process: The factory ui process object.
134 run_queue: A queue of callbacks to invoke from the main thread.
135 invocations: A map from FactoryTest objects to the corresponding
136 TestInvocations objects representing active tests.
137 tests_to_run: A deque of tests that should be run when the current
138 test(s) complete.
139 options: Command-line options.
140 args: Command-line args.
141 test_list: The test list.
Jon Salz128b0932013-07-03 16:55:26 +0800142 test_lists: All new-style test lists.
Jon Salz0697cbf2012-07-04 15:14:04 +0800143 event_handlers: Map of Event.Type to the method used to handle that
144 event. If the method has an 'event' argument, the event is passed
145 to the handler.
146 exceptions: Exceptions encountered in invocation threads.
Jon Salz3c493bb2013-02-07 17:24:58 +0800147 last_log_disk_space_message: The last message we logged about disk space
148 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800149 last_kick_sync_time: The last time to kick system_log_manager to sync
150 because of core dump files (to avoid kicking too soon then abort the
151 sync.)
Jon Salz416f9cc2013-05-10 18:32:50 +0800152 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800153 status: The current Goofy status (a member of the Status enum).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800154 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800155 def __init__(self):
156 self.uuid = str(uuid.uuid4())
157 self.state_instance = None
158 self.state_server = None
159 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800160 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800161 self.event_server = None
162 self.event_server_thread = None
163 self.event_client = None
164 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800165 self.charge_manager = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800166 self.time_sanitizer = None
167 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800168 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800169 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800170 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800171 self.event_log = None
172 self.prespawner = None
173 self.ui_process = None
Jon Salzc79a9982012-08-30 04:42:01 +0800174 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800175 self.run_queue = Queue.Queue()
176 self.invocations = {}
177 self.tests_to_run = deque()
178 self.visible_test = None
179 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800180 self.hooks = None
Vic Yangd8990da2013-06-27 16:57:43 +0800181 self.cpu_usage_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800182
183 self.options = None
184 self.args = None
185 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800186 self.test_lists = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800187 self.on_ui_startup = []
188 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800189 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800190 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800191 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800192 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800193 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800194 self.last_log_disk_space_message = None
Justin Chuang83813982013-05-13 01:26:32 +0800195 self.last_check_battery_time = None
196 self.last_check_battery_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800197 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800198 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800199 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800200 self.key_filter = None
Jon Salzce6a7f82013-06-10 18:22:54 +0800201 self.cpufreq_manager = None
Jon Salzd7550792013-07-12 05:49:27 +0800202 self.status = Status.UNINITIALIZED
Jon Salz0697cbf2012-07-04 15:14:04 +0800203
Jon Salz85a39882012-07-05 16:45:04 +0800204 def test_or_root(event, parent_or_group=True):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800205 """Returns the test affected by a particular event.
Jon Salz85a39882012-07-05 16:45:04 +0800206
207 Args:
208 event: The event containing an optional 'path' attribute.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800209 parent_or_group: If True, returns the top-level parent for a test (the
Jon Salz85a39882012-07-05 16:45:04 +0800210 root node of the tests that need to be run together if the given test
211 path is to be run).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800212 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800213 try:
214 path = event.path
215 except AttributeError:
216 path = None
217
218 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800219 test = self.test_list.lookup_path(path)
220 if parent_or_group:
221 test = test.get_top_level_parent_or_group()
222 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800223 else:
224 return self.test_list
225
226 self.event_handlers = {
227 Event.Type.SWITCH_TEST: self.handle_switch_test,
228 Event.Type.SHOW_NEXT_ACTIVE_TEST:
229 lambda event: self.show_next_active_test(),
230 Event.Type.RESTART_TESTS:
231 lambda event: self.restart_tests(root=test_or_root(event)),
232 Event.Type.AUTO_RUN:
233 lambda event: self.auto_run(root=test_or_root(event)),
234 Event.Type.RE_RUN_FAILED:
235 lambda event: self.re_run_failed(root=test_or_root(event)),
236 Event.Type.RUN_TESTS_WITH_STATUS:
237 lambda event: self.run_tests_with_status(
238 event.status,
239 root=test_or_root(event)),
240 Event.Type.REVIEW:
241 lambda event: self.show_review_information(),
242 Event.Type.UPDATE_SYSTEM_INFO:
243 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800244 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800245 lambda event: self.stop(root=test_or_root(event, False),
Jon Salz6dc031d2013-06-19 13:06:23 +0800246 fail=getattr(event, 'fail', False),
247 reason=getattr(event, 'reason', None)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800248 Event.Type.SET_VISIBLE_TEST:
249 lambda event: self.set_visible_test(
250 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800251 Event.Type.CLEAR_STATE:
252 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800253 }
254
255 self.exceptions = []
256 self.web_socket_manager = None
257
258 def destroy(self):
Jon Salzd7550792013-07-12 05:49:27 +0800259 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800260 if self.chrome:
261 self.chrome.kill()
262 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800263 if self.dummy_shopfloor:
264 self.dummy_shopfloor.kill()
265 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800266 if self.ui_process:
267 utils.kill_process_tree(self.ui_process, 'ui')
268 self.ui_process = None
269 if self.web_socket_manager:
270 logging.info('Stopping web sockets')
271 self.web_socket_manager.close()
272 self.web_socket_manager = None
273 if self.state_server_thread:
274 logging.info('Stopping state server')
275 self.state_server.shutdown()
276 self.state_server_thread.join()
277 self.state_server.server_close()
278 self.state_server_thread = None
279 if self.state_instance:
280 self.state_instance.close()
281 if self.event_server_thread:
282 logging.info('Stopping event server')
283 self.event_server.shutdown() # pylint: disable=E1101
284 self.event_server_thread.join()
285 self.event_server.server_close()
286 self.event_server_thread = None
287 if self.log_watcher:
288 if self.log_watcher.IsThreadStarted():
289 self.log_watcher.StopWatchThread()
290 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800291 if self.system_log_manager:
292 if self.system_log_manager.IsThreadRunning():
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +0800293 self.system_log_manager.Stop()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800294 self.system_log_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800295 if self.prespawner:
296 logging.info('Stopping prespawner')
297 self.prespawner.stop()
298 self.prespawner = None
299 if self.event_client:
300 logging.info('Closing event client')
301 self.event_client.close()
302 self.event_client = None
Jon Salzddf0d052013-06-18 12:52:44 +0800303 if self.cpufreq_manager:
304 self.cpufreq_manager.Stop()
Jon Salz0697cbf2012-07-04 15:14:04 +0800305 if self.event_log:
306 self.event_log.Close()
307 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800308 if self.key_filter:
309 self.key_filter.Stop()
Vic Yangd8990da2013-06-27 16:57:43 +0800310 if self.cpu_usage_watcher:
311 self.cpu_usage_watcher.terminate()
Dean Liao592e4d52013-01-10 20:06:39 +0800312
Jon Salz0697cbf2012-07-04 15:14:04 +0800313 self.check_exceptions()
314 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800315 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800316
317 def start_state_server(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800318 # Before starting state server, remount stateful partitions with
319 # no commit flag. The default commit time (commit=600) makes corruption
320 # too likely.
321 file_utils.ResetCommitTime()
322
Jon Salz0697cbf2012-07-04 15:14:04 +0800323 self.state_instance, self.state_server = (
324 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800325 self.goofy_rpc = GoofyRPC(self)
326 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800327 logging.info('Starting state server')
328 self.state_server_thread = threading.Thread(
329 target=self.state_server.serve_forever,
330 name='StateServer')
331 self.state_server_thread.start()
332
333 def start_event_server(self):
334 self.event_server = EventServer()
335 logging.info('Starting factory event server')
336 self.event_server_thread = threading.Thread(
337 target=self.event_server.serve_forever,
338 name='EventServer') # pylint: disable=E1101
339 self.event_server_thread.start()
340
341 self.event_client = EventClient(
342 callback=self.handle_event, event_loop=self.run_queue)
343
344 self.web_socket_manager = WebSocketManager(self.uuid)
345 self.state_server.add_handler("/event",
346 self.web_socket_manager.handle_web_socket)
347
348 def start_ui(self):
349 ui_proc_args = [
350 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
351 self.options.test_list]
352 if self.options.verbose:
353 ui_proc_args.append('-v')
354 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800355 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800356 logging.info('Waiting for UI to come up...')
357 self.event_client.wait(
358 lambda event: event.type == Event.Type.UI_READY)
359 logging.info('UI has started')
360
361 def set_visible_test(self, test):
362 if self.visible_test == test:
363 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800364 if test and not test.has_ui:
365 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800366
367 if test:
368 test.update_state(visible=True)
369 if self.visible_test:
370 self.visible_test.update_state(visible=False)
371 self.visible_test = test
372
Jon Salzd4306c82012-11-30 15:16:36 +0800373 def _log_startup_messages(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800374 """Logs the tail of var/log/messages and mosys and EC console logs."""
Jon Salzd4306c82012-11-30 15:16:36 +0800375 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
376 # for factory-3004.B only. Consolidate and merge back to ToT.
377 if utils.in_chroot():
378 return
379
380 try:
381 var_log_messages = (
382 utils.var_log_messages_before_reboot())
383 logging.info(
384 'Tail of /var/log/messages before last reboot:\n'
385 '%s', ('\n'.join(
386 ' ' + x for x in var_log_messages)))
387 except: # pylint: disable=W0702
388 logging.exception('Unable to grok /var/log/messages')
389
390 try:
391 mosys_log = utils.Spawn(
392 ['mosys', 'eventlog', 'list'],
393 read_stdout=True, log_stderr_on_error=True).stdout_data
394 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
395 except: # pylint: disable=W0702
396 logging.exception('Unable to read mosys eventlog')
397
398 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800399 board = system.GetBoard()
400 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800401 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
402 except: # pylint: disable=W0702
403 logging.exception('Error retrieving EC console log')
404
Vic Yang079f9872013-07-01 11:32:00 +0800405 try:
406 board = system.GetBoard()
407 ec_panic_info = board.GetECPanicInfo()
408 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
409 except: # pylint: disable=W0702
410 logging.exception('Error retrieving EC panic info')
411
Jon Salz0697cbf2012-07-04 15:14:04 +0800412 def handle_shutdown_complete(self, test, test_state):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800413 """Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800414
Jon Salz0697cbf2012-07-04 15:14:04 +0800415 @param test: The ShutdownStep.
416 @param test_state: The test state.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800417 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800418 test_state = test.update_state(increment_shutdown_count=1)
419 logging.info('Detected shutdown (%d of %d)',
420 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800421
Jon Salz0697cbf2012-07-04 15:14:04 +0800422 def log_and_update_state(status, error_msg, **kw):
423 self.event_log.Log('rebooted',
424 status=status, error_msg=error_msg, **kw)
Jon Salzd4306c82012-11-30 15:16:36 +0800425 logging.info('Rebooted: status=%s, %s', status,
426 (('error_msg=%s' % error_msg) if error_msg else None))
Jon Salz0697cbf2012-07-04 15:14:04 +0800427 test.update_state(status=status, error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800428
Jon Salz0697cbf2012-07-04 15:14:04 +0800429 if not self.last_shutdown_time:
430 log_and_update_state(status=TestState.FAILED,
431 error_msg='Unable to read shutdown_time')
432 return
Jon Salz258a40c2012-04-19 12:34:01 +0800433
Jon Salz0697cbf2012-07-04 15:14:04 +0800434 now = time.time()
435 logging.info('%.03f s passed since reboot',
436 now - self.last_shutdown_time)
Jon Salz258a40c2012-04-19 12:34:01 +0800437
Jon Salz0697cbf2012-07-04 15:14:04 +0800438 if self.last_shutdown_time > now:
439 test.update_state(status=TestState.FAILED,
440 error_msg='Time moved backward during reboot')
441 elif (isinstance(test, factory.RebootStep) and
442 self.test_list.options.max_reboot_time_secs and
443 (now - self.last_shutdown_time >
444 self.test_list.options.max_reboot_time_secs)):
445 # A reboot took too long; fail. (We don't check this for
446 # HaltSteps, because the machine could be halted for a
447 # very long time, and even unplugged with battery backup,
448 # thus hosing the clock.)
449 log_and_update_state(
450 status=TestState.FAILED,
451 error_msg=('More than %d s elapsed during reboot '
452 '(%.03f s, from %s to %s)' % (
453 self.test_list.options.max_reboot_time_secs,
454 now - self.last_shutdown_time,
455 utils.TimeString(self.last_shutdown_time),
456 utils.TimeString(now))),
457 duration=(now-self.last_shutdown_time))
Jon Salzd4306c82012-11-30 15:16:36 +0800458 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800459 elif test_state.shutdown_count == test.iterations:
460 # Good!
461 log_and_update_state(status=TestState.PASSED,
462 duration=(now - self.last_shutdown_time),
463 error_msg='')
464 elif test_state.shutdown_count > test.iterations:
465 # Shut down too many times
466 log_and_update_state(status=TestState.FAILED,
467 error_msg='Too many shutdowns')
Jon Salzd4306c82012-11-30 15:16:36 +0800468 self._log_startup_messages()
Jon Salz0697cbf2012-07-04 15:14:04 +0800469 elif utils.are_shift_keys_depressed():
470 logging.info('Shift keys are depressed; cancelling restarts')
471 # Abort shutdown
472 log_and_update_state(
473 status=TestState.FAILED,
474 error_msg='Shutdown aborted with double shift keys')
Jon Salza6711d72012-07-18 14:33:03 +0800475 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800476 else:
477 def handler():
478 if self._prompt_cancel_shutdown(
479 test, test_state.shutdown_count + 1):
Jon Salza6711d72012-07-18 14:33:03 +0800480 factory.console.info('Shutdown aborted by operator')
Jon Salz0697cbf2012-07-04 15:14:04 +0800481 log_and_update_state(
482 status=TestState.FAILED,
483 error_msg='Shutdown aborted by operator')
Jon Salza6711d72012-07-18 14:33:03 +0800484 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800485 return
Jon Salz0405ab52012-03-16 15:26:52 +0800486
Jon Salz0697cbf2012-07-04 15:14:04 +0800487 # Time to shutdown again
488 log_and_update_state(
489 status=TestState.ACTIVE,
490 error_msg='',
491 iteration=test_state.shutdown_count)
Jon Salz73e0fd02012-04-04 11:46:38 +0800492
Jon Salz0697cbf2012-07-04 15:14:04 +0800493 self.event_log.Log('shutdown', operation='reboot')
494 self.state_instance.set_shared_data('shutdown_time',
495 time.time())
496 self.env.shutdown('reboot')
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800497
Jon Salz0697cbf2012-07-04 15:14:04 +0800498 self.on_ui_startup.append(handler)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800499
Jon Salz0697cbf2012-07-04 15:14:04 +0800500 def _prompt_cancel_shutdown(self, test, iteration):
501 if self.options.ui != 'chrome':
502 return False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800503
Jon Salz0697cbf2012-07-04 15:14:04 +0800504 pending_shutdown_data = {
505 'delay_secs': test.delay_secs,
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800506 'enable_guest_mode': test.enable_guest_mode,
Jon Salz0697cbf2012-07-04 15:14:04 +0800507 'time': time.time() + test.delay_secs,
508 'operation': test.operation,
509 'iteration': iteration,
510 'iterations': test.iterations,
511 }
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800512
Jon Salz0697cbf2012-07-04 15:14:04 +0800513 # Create a new (threaded) event client since we
514 # don't want to use the event loop for this.
515 with EventClient() as event_client:
516 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN,
517 **pending_shutdown_data))
518 aborted = event_client.wait(
519 lambda event: event.type == Event.Type.CANCEL_SHUTDOWN,
520 timeout=test.delay_secs) is not None
521 if aborted:
522 event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
523 return aborted
Jon Salz258a40c2012-04-19 12:34:01 +0800524
Jon Salz0697cbf2012-07-04 15:14:04 +0800525 def init_states(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800526 """Initializes all states on startup."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800527 for test in self.test_list.get_all_tests():
528 # Make sure the state server knows about all the tests,
529 # defaulting to an untested state.
530 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800531
Jon Salz0697cbf2012-07-04 15:14:04 +0800532 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800533 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800534 ec_console_log = None
Vic Yang079f9872013-07-01 11:32:00 +0800535 ec_panic_info = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800536
Jon Salz0697cbf2012-07-04 15:14:04 +0800537 # Any 'active' tests should be marked as failed now.
538 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800539 if not test.is_leaf():
540 # Don't bother with parents; they will be updated when their
541 # children are updated.
542 continue
543
Jon Salz0697cbf2012-07-04 15:14:04 +0800544 test_state = test.get_state()
545 if test_state.status != TestState.ACTIVE:
546 continue
547 if isinstance(test, factory.ShutdownStep):
548 # Shutdown while the test was active - that's good.
549 self.handle_shutdown_complete(test, test_state)
550 else:
551 # Unexpected shutdown. Grab /var/log/messages for context.
552 if var_log_messages is None:
553 try:
554 var_log_messages = (
555 utils.var_log_messages_before_reboot())
556 # Write it to the log, to make it easier to
557 # correlate with /var/log/messages.
558 logging.info(
559 'Unexpected shutdown. '
560 'Tail of /var/log/messages before last reboot:\n'
561 '%s', ('\n'.join(
562 ' ' + x for x in var_log_messages)))
563 except: # pylint: disable=W0702
564 logging.exception('Unable to grok /var/log/messages')
565 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800566
Jon Salz008f4ea2012-08-28 05:39:45 +0800567 if mosys_log is None and not utils.in_chroot():
568 try:
569 mosys_log = utils.Spawn(
570 ['mosys', 'eventlog', 'list'],
571 read_stdout=True, log_stderr_on_error=True).stdout_data
572 # Write it to the log also.
573 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
574 except: # pylint: disable=W0702
575 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800576
Vic Yange4c275d2012-08-28 01:50:20 +0800577 if ec_console_log is None:
578 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800579 board = system.GetBoard()
580 ec_console_log = board.GetECConsoleLog()
Vic Yange4c275d2012-08-28 01:50:20 +0800581 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Jon Salzfe1f6652012-09-07 05:40:14 +0800582 except: # pylint: disable=W0702
Vic Yange4c275d2012-08-28 01:50:20 +0800583 logging.exception('Error retrieving EC console log')
584
Vic Yang079f9872013-07-01 11:32:00 +0800585 if ec_panic_info is None:
586 try:
587 board = system.GetBoard()
588 ec_panic_info = board.GetECPanicInfo()
589 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
590 except: # pylint: disable=W0702
591 logging.exception('Error retrieving EC panic info')
592
Jon Salz0697cbf2012-07-04 15:14:04 +0800593 error_msg = 'Unexpected shutdown while test was running'
594 self.event_log.Log('end_test',
595 path=test.path,
596 status=TestState.FAILED,
597 invocation=test.get_state().invocation,
598 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800599 var_log_messages='\n'.join(var_log_messages),
600 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800601 test.update_state(
602 status=TestState.FAILED,
603 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800604
Jon Salz50efe942012-07-26 11:54:10 +0800605 if not test.never_fails:
606 # For "never_fails" tests (such as "Start"), don't cancel
607 # pending tests, since reboot is expected.
608 factory.console.info('Unexpected shutdown while test %s '
609 'running; cancelling any pending tests',
610 test.path)
611 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800612
Jon Salz008f4ea2012-08-28 05:39:45 +0800613 self.update_skipped_tests()
614
615 def update_skipped_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800616 """Updates skipped states based on run_if."""
Jon Salz885dcac2013-07-23 16:39:50 +0800617 env = TestArgEnv()
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800618 def _evaluate_skip_from_run_if(test):
619 """Returns the run_if evaluation of the test.
620
621 Args:
622 test: A FactoryTest object.
623
624 Returns:
625 The run_if evaluation result. Returns False if the test has no
626 run_if argument.
627 """
628 value = None
629 if test.run_if_expr:
630 try:
631 value = test.run_if_expr(env)
632 except: # pylint: disable=W0702
633 logging.exception('Unable to evaluate run_if expression for %s',
634 test.path)
635 # But keep going; we have no choice. This will end up
636 # always activating the test.
637 elif test.run_if_table_name:
638 try:
639 aux = shopfloor.get_selected_aux_data(test.run_if_table_name)
640 value = aux.get(test.run_if_col)
641 except ValueError:
642 # Not available; assume it shouldn't be skipped
643 pass
644
645 if value is None:
646 skip = False
647 else:
648 skip = (not value) ^ t.run_if_not
649 return skip
650
651 # Gets all run_if evaluation, and stores results in skip_map.
652 skip_map = dict()
Jon Salz008f4ea2012-08-28 05:39:45 +0800653 for t in self.test_list.walk():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800654 skip_map[t.path] = _evaluate_skip_from_run_if(t)
Jon Salz885dcac2013-07-23 16:39:50 +0800655
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800656 # Propagates the skip value from root of tree and updates skip_map.
657 def _update_skip_map_from_node(test, skip_from_parent):
658 """Updates skip_map from a given node.
Jon Salz885dcac2013-07-23 16:39:50 +0800659
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800660 Given a FactoryTest node and the skip value from parent, updates the
661 skip value of current node in the skip_map if skip value from parent is
662 True. If this node has children, recursively propagate this value to all
663 its children, that is, all its subtests.
664 Note that this function only updates value in skip_map, not the actual
665 test_list tree.
Jon Salz008f4ea2012-08-28 05:39:45 +0800666
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800667 Args:
668 test: The given FactoryTest object. It is a node in the test_list tree.
669 skip_from_parent: The skip value which propagates from the parent of
670 input node.
671 """
672 skip_this_tree = skip_from_parent or skip_map[test.path]
673 if skip_this_tree:
674 logging.info('Skip from node %r', test.path)
675 skip_map[test.path] = True
676 if test.is_leaf():
677 return
678 # Propagates skip value to its subtests
679 for subtest in test.subtests:
680 _update_skip_map_from_node(subtest, skip_this_tree)
681
682 _update_skip_map_from_node(self.test_list, False)
683
684 # Updates the skip value from skip_map to test_list tree. Also, updates test
685 # status if needed.
686 for t in self.test_list.walk():
687 skip = skip_map[t.path]
688 test_state = t.get_state()
689 if ((not skip) and
690 (test_state.status == TestState.PASSED) and
691 (test_state.error_msg == TestState.SKIPPED_MSG)):
692 # It was marked as skipped before, but now we need to run it.
693 # Mark as untested.
694 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
695 else:
696 t.update_state(skip=skip)
Jon Salz008f4ea2012-08-28 05:39:45 +0800697
Jon Salz0697cbf2012-07-04 15:14:04 +0800698 def show_next_active_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800699 """Rotates to the next visible active test."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800700 self.reap_completed_tests()
701 active_tests = [
702 t for t in self.test_list.walk()
703 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
704 if not active_tests:
705 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800706
Jon Salz0697cbf2012-07-04 15:14:04 +0800707 try:
708 next_test = active_tests[
709 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
710 except ValueError: # visible_test not present in active_tests
711 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800712
Jon Salz0697cbf2012-07-04 15:14:04 +0800713 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800714
Jon Salz0697cbf2012-07-04 15:14:04 +0800715 def handle_event(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800716 """Handles an event from the event server."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800717 handler = self.event_handlers.get(event.type)
718 if handler:
719 handler(event)
720 else:
721 # We don't register handlers for all event types - just ignore
722 # this event.
723 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800724
Vic Yangaabf9fd2013-04-09 18:56:13 +0800725 def check_critical_factory_note(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800726 """Returns True if the last factory note is critical."""
Vic Yangaabf9fd2013-04-09 18:56:13 +0800727 notes = self.state_instance.get_shared_data('factory_note', True)
728 return notes and notes[-1]['level'] == 'CRITICAL'
729
Jon Salz0697cbf2012-07-04 15:14:04 +0800730 def run_next_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800731 """Runs the next eligible test (or tests) in self.tests_to_run."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800732 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800733 if self.tests_to_run and self.check_critical_factory_note():
734 self.tests_to_run.clear()
735 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800736 while self.tests_to_run:
737 logging.debug('Tests to run: %s',
738 [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800739
Jon Salz0697cbf2012-07-04 15:14:04 +0800740 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800741
Jon Salz0697cbf2012-07-04 15:14:04 +0800742 if test in self.invocations:
743 logging.info('Next test %s is already running', test.path)
744 self.tests_to_run.popleft()
745 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800746
Jon Salza1412922012-07-23 16:04:17 +0800747 for requirement in test.require_run:
748 for i in requirement.test.walk():
749 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800750 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800751 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800752 return
753
Jon Salz0697cbf2012-07-04 15:14:04 +0800754 if self.invocations and not (test.backgroundable and all(
755 [x.backgroundable for x in self.invocations])):
756 logging.debug('Waiting for non-backgroundable tests to '
757 'complete before running %s', test.path)
758 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800759
Jon Salz3e6f5202012-10-15 15:08:29 +0800760 if test.get_state().skip:
761 factory.console.info('Skipping test %s', test.path)
762 test.update_state(status=TestState.PASSED,
763 error_msg=TestState.SKIPPED_MSG)
764 self.tests_to_run.popleft()
765 continue
766
Jon Salz0697cbf2012-07-04 15:14:04 +0800767 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800768
Jon Salz304a75d2012-07-06 11:14:15 +0800769 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800770 for requirement in test.require_run:
771 for i in requirement.test.walk():
772 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800773 # We've hit this test itself; stop checking
774 break
Jon Salza1412922012-07-23 16:04:17 +0800775 if ((i.get_state().status == TestState.UNTESTED) or
776 (requirement.passed and i.get_state().status !=
777 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800778 # Found an untested test; move on to the next
779 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800780 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800781 break
782
783 if untested:
784 untested_paths = ', '.join(sorted([x.path for x in untested]))
785 if self.state_instance.get_shared_data('engineering_mode',
786 optional=True):
787 # In engineering mode, we'll let it go.
788 factory.console.warn('In engineering mode; running '
789 '%s even though required tests '
790 '[%s] have not completed',
791 test.path, untested_paths)
792 else:
793 # Not in engineering mode; mark it failed.
794 error_msg = ('Required tests [%s] have not been run yet'
795 % untested_paths)
796 factory.console.error('Not running %s: %s',
797 test.path, error_msg)
798 test.update_state(status=TestState.FAILED,
799 error_msg=error_msg)
800 continue
801
Jon Salz0697cbf2012-07-04 15:14:04 +0800802 if isinstance(test, factory.ShutdownStep):
803 if os.path.exists(NO_REBOOT_FILE):
804 test.update_state(
805 status=TestState.FAILED, increment_count=1,
806 error_msg=('Skipped shutdown since %s is present' %
Jon Salz304a75d2012-07-06 11:14:15 +0800807 NO_REBOOT_FILE))
Jon Salz0697cbf2012-07-04 15:14:04 +0800808 continue
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800809
Jon Salz0697cbf2012-07-04 15:14:04 +0800810 test.update_state(status=TestState.ACTIVE, increment_count=1,
811 error_msg='', shutdown_count=0)
812 if self._prompt_cancel_shutdown(test, 1):
813 self.event_log.Log('reboot_cancelled')
814 test.update_state(
815 status=TestState.FAILED, increment_count=1,
816 error_msg='Shutdown aborted by operator',
817 shutdown_count=0)
chungyiafe8f772012-08-15 19:36:29 +0800818 continue
Jon Salz2f757d42012-06-27 17:06:42 +0800819
Jon Salz0697cbf2012-07-04 15:14:04 +0800820 # Save pending test list in the state server
Jon Salzdbf398f2012-06-14 17:30:01 +0800821 self.state_instance.set_shared_data(
Jon Salz0697cbf2012-07-04 15:14:04 +0800822 'tests_after_shutdown',
823 [t.path for t in self.tests_to_run])
824 # Save shutdown time
825 self.state_instance.set_shared_data('shutdown_time',
826 time.time())
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800827
Jon Salz0697cbf2012-07-04 15:14:04 +0800828 with self.env.lock:
829 self.event_log.Log('shutdown', operation=test.operation)
Ricky Liang8c2c6c32013-11-02 23:02:44 +0800830 if (test.enable_guest_mode and
831 not os.path.exists(
832 test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)):
833 # Create a temporary file GUEST_MODE_TAG_FILE to enable guest mode
834 # on next boot.
835 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
Jon Salz0697cbf2012-07-04 15:14:04 +0800836 shutdown_result = self.env.shutdown(test.operation)
837 if shutdown_result:
838 # That's all, folks!
839 self.run_queue.put(None)
840 return
841 else:
842 # Just pass (e.g., in the chroot).
843 test.update_state(status=TestState.PASSED)
844 self.state_instance.set_shared_data(
845 'tests_after_shutdown', None)
846 # Send event with no fields to indicate that there is no
847 # longer a pending shutdown.
848 self.event_client.post_event(Event(
849 Event.Type.PENDING_SHUTDOWN))
850 continue
Jon Salz258a40c2012-04-19 12:34:01 +0800851
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800852 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800853
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800854 def _run_test(self, test, iterations_left=None, retries_left=None):
Jon Salz1acc8742012-07-17 17:45:55 +0800855 invoc = TestInvocation(self, test, on_completion=self.run_next_test)
856 new_state = test.update_state(
857 status=TestState.ACTIVE, increment_count=1, error_msg='',
Jon Salzbd42ce12012-09-18 08:03:59 +0800858 invocation=invoc.uuid, iterations_left=iterations_left,
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800859 retries_left=retries_left,
Jon Salzbd42ce12012-09-18 08:03:59 +0800860 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800861 invoc.count = new_state.count
862
863 self.invocations[test] = invoc
864 if self.visible_test is None and test.has_ui:
865 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800866 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800867 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800868
Vic Yang311ddb82012-09-26 12:08:28 +0800869 def check_exclusive(self):
Jon Salzce6a7f82013-06-10 18:22:54 +0800870 # alias since this is really long
871 EXCL_OPT = factory.FactoryTest.EXCLUSIVE_OPTIONS
872
Vic Yang311ddb82012-09-26 12:08:28 +0800873 current_exclusive_items = set([
Jon Salzce6a7f82013-06-10 18:22:54 +0800874 item for item in EXCL_OPT
Vic Yang311ddb82012-09-26 12:08:28 +0800875 if any([test.is_exclusive(item) for test in self.invocations])])
876
877 new_exclusive_items = current_exclusive_items - self.exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800878 if EXCL_OPT.NETWORKING in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800879 logging.info('Disabling network')
880 self.connection_manager.DisableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800881 if EXCL_OPT.CHARGER in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800882 logging.info('Stop controlling charger')
883
884 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800885 if EXCL_OPT.NETWORKING in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800886 logging.info('Re-enabling network')
887 self.connection_manager.EnableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800888 if EXCL_OPT.CHARGER in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800889 logging.info('Start controlling charger')
890
Jon Salzce6a7f82013-06-10 18:22:54 +0800891 if self.cpufreq_manager:
892 enabled = EXCL_OPT.CPUFREQ not in current_exclusive_items
893 try:
894 self.cpufreq_manager.SetEnabled(enabled)
895 except: # pylint: disable=W0702
896 logging.exception('Unable to %s cpufreq services',
897 'enable' if enabled else 'disable')
898
Vic Yang311ddb82012-09-26 12:08:28 +0800899 # Only adjust charge state if not excluded
Jon Salzce6a7f82013-06-10 18:22:54 +0800900 if (EXCL_OPT.CHARGER not in current_exclusive_items and
901 not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800902 if self.charge_manager:
903 self.charge_manager.AdjustChargeState()
904 else:
905 try:
906 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
907 except BoardException:
908 logging.exception('Unable to set charge state on this board')
Vic Yang311ddb82012-09-26 12:08:28 +0800909
910 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800911
cychiang21886742012-07-05 15:16:32 +0800912 def check_for_updates(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800913 """Schedules an asynchronous check for updates if necessary."""
cychiang21886742012-07-05 15:16:32 +0800914 if not self.test_list.options.update_period_secs:
915 # Not enabled.
916 return
917
918 now = time.time()
919 if self.last_update_check and (
920 now - self.last_update_check <
921 self.test_list.options.update_period_secs):
922 # Not yet time for another check.
923 return
924
925 self.last_update_check = now
926
927 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
928 if reached_shopfloor:
929 new_update_md5sum = md5sum if needs_update else None
930 if system.SystemInfo.update_md5sum != new_update_md5sum:
931 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
932 system.SystemInfo.update_md5sum = new_update_md5sum
933 self.run_queue.put(self.update_system_info)
934
935 updater.CheckForUpdateAsync(
936 handle_check_for_update,
937 self.test_list.options.shopfloor_timeout_secs)
938
Jon Salza6711d72012-07-18 14:33:03 +0800939 def cancel_pending_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800940 """Cancels any tests in the run queue."""
Jon Salza6711d72012-07-18 14:33:03 +0800941 self.run_tests([])
942
Jon Salz0697cbf2012-07-04 15:14:04 +0800943 def run_tests(self, subtrees, untested_only=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800944 """Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800945
Jon Salz0697cbf2012-07-04 15:14:04 +0800946 The tests are run in order unless one fails (then stops).
947 Backgroundable tests are run simultaneously; when a foreground test is
948 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800949
Jon Salz0697cbf2012-07-04 15:14:04 +0800950 @param subtrees: Node or nodes containing tests to run (may either be
951 a single test or a list). Duplicates will be ignored.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800952 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800953 if type(subtrees) != list:
954 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800955
Jon Salz0697cbf2012-07-04 15:14:04 +0800956 # Nodes we've seen so far, to avoid duplicates.
957 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800958
Jon Salz0697cbf2012-07-04 15:14:04 +0800959 self.tests_to_run = deque()
960 for subtree in subtrees:
961 for test in subtree.walk():
962 if test in seen:
963 continue
964 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800965
Jon Salz0697cbf2012-07-04 15:14:04 +0800966 if not test.is_leaf():
967 continue
968 if (untested_only and
969 test.get_state().status != TestState.UNTESTED):
970 continue
971 self.tests_to_run.append(test)
972 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800973
Jon Salz0697cbf2012-07-04 15:14:04 +0800974 def reap_completed_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800975 """Removes completed tests from the set of active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800976
977 Also updates the visible test if it was reaped.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800978 """
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800979 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800980 for t, v in dict(self.invocations).iteritems():
981 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800982 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800983 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800984 del self.invocations[t]
985
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800986 # Stop on failure if flag is true.
987 if (self.test_list.options.stop_on_failure and
988 new_state.status == TestState.FAILED):
989 # Clean all the tests to cause goofy to stop.
990 self.tests_to_run = []
991 factory.console.info("Stop on failure triggered. Empty the queue.")
992
Jon Salz1acc8742012-07-17 17:45:55 +0800993 if new_state.iterations_left and new_state.status == TestState.PASSED:
994 # Play it again, Sam!
995 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800996 # new_state.retries_left is obtained after update.
997 # For retries_left == 0, test can still be run for the last time.
998 elif (new_state.retries_left >= 0 and
999 new_state.status == TestState.FAILED):
1000 # Still have to retry, Sam!
1001 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +08001002
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001003 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +08001004 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001005
Jon Salz0697cbf2012-07-04 15:14:04 +08001006 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +08001007 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +08001008 self.set_visible_test(None)
1009 # Make the first running test, if any, the visible test
1010 for t in self.test_list.walk():
1011 if t in self.invocations:
1012 self.set_visible_test(t)
1013 break
1014
Jon Salz6dc031d2013-06-19 13:06:23 +08001015 def kill_active_tests(self, abort, root=None, reason=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001016 """Kills and waits for all active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +08001017
Jon Salz85a39882012-07-05 16:45:04 +08001018 Args:
1019 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +08001020 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +08001021 root: If set, only kills tests with root as an ancestor.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001022 reason: If set, the abort reason.
1023 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001024 self.reap_completed_tests()
1025 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +08001026 if root and not test.has_ancestor(root):
1027 continue
1028
Jon Salz0697cbf2012-07-04 15:14:04 +08001029 factory.console.info('Killing active test %s...' % test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +08001030 invoc.abort_and_join(reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001031 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +08001032 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +08001033 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +08001034
Jon Salz0697cbf2012-07-04 15:14:04 +08001035 if not abort:
1036 test.update_state(status=TestState.UNTESTED)
1037 self.reap_completed_tests()
1038
Jon Salz6dc031d2013-06-19 13:06:23 +08001039 def stop(self, root=None, fail=False, reason=None):
1040 self.kill_active_tests(fail, root, reason)
Jon Salz85a39882012-07-05 16:45:04 +08001041 # Remove any tests in the run queue under the root.
1042 self.tests_to_run = deque([x for x in self.tests_to_run
1043 if root and not x.has_ancestor(root)])
1044 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +08001045
Jon Salz4712ac72013-02-07 17:12:05 +08001046 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +08001047 if root is None:
1048 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +08001049 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +08001050 for f in root.walk():
1051 if f.is_leaf():
1052 f.update_state(status=TestState.UNTESTED)
1053
Jon Salz6dc031d2013-06-19 13:06:23 +08001054 def abort_active_tests(self, reason=None):
1055 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001056
1057 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +08001058 syslog.openlog('goofy')
1059
Jon Salz0697cbf2012-07-04 15:14:04 +08001060 try:
Jon Salzd7550792013-07-12 05:49:27 +08001061 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +08001062 self.init()
1063 self.event_log.Log('goofy_init',
1064 success=True)
1065 except:
1066 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001067 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001068 self.event_log.Log('goofy_init',
1069 success=False,
1070 trace=traceback.format_exc())
1071 except: # pylint: disable=W0702
1072 pass
1073 raise
1074
Jon Salzd7550792013-07-12 05:49:27 +08001075 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +08001076 syslog.syslog('Goofy (factory test harness) starting')
Jon Salz0697cbf2012-07-04 15:14:04 +08001077 self.run()
1078
1079 def update_system_info(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001080 """Updates system info."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001081 system_info = system.SystemInfo()
1082 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1083 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1084 system_info=system_info.__dict__))
1085 logging.info('System info: %r', system_info.__dict__)
1086
Jon Salzeb42f0d2012-07-27 19:14:04 +08001087 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001088 """Commences updating factory software.
Jon Salzeb42f0d2012-07-27 19:14:04 +08001089
1090 Args:
1091 auto_run_on_restart: Auto-run when the machine comes back up.
1092 post_update_hook: Code to call after update but immediately before
1093 restart.
1094
1095 Returns:
1096 Never if the update was successful (we just reboot).
1097 False if the update was unnecessary (no update available).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001098 """
Jon Salz6dc031d2013-06-19 13:06:23 +08001099 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +08001100 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001101
Jon Salz5c344f62012-07-13 14:31:16 +08001102 def pre_update_hook():
1103 if auto_run_on_restart:
1104 self.state_instance.set_shared_data('tests_after_shutdown',
1105 FORCE_AUTO_RUN)
1106 self.state_instance.close()
1107
Jon Salzeb42f0d2012-07-27 19:14:04 +08001108 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1109 if post_update_hook:
1110 post_update_hook()
1111 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001112
Jon Salzcef132a2012-08-30 04:58:08 +08001113 def handle_sigint(self, dummy_signum, dummy_frame):
Jon Salz77c151e2012-08-28 07:20:37 +08001114 logging.error('Received SIGINT')
1115 self.run_queue.put(None)
1116 raise KeyboardInterrupt()
1117
Jon Salze12c2b32013-06-25 16:24:34 +08001118 def find_kcrashes(self):
1119 """Finds kcrash files, logs them, and marks them as seen."""
1120 seen_crashes = set(
1121 self.state_instance.get_shared_data('seen_crashes', optional=True)
1122 or [])
1123
1124 for path in glob.glob('/var/spool/crash/*'):
1125 if not os.path.isfile(path):
1126 continue
1127 if path in seen_crashes:
1128 continue
1129 try:
1130 stat = os.stat(path)
1131 mtime = utils.TimeString(stat.st_mtime)
1132 logging.info(
1133 'Found new crash file %s (%d bytes at %s)',
1134 path, stat.st_size, mtime)
1135 extra_log_args = {}
1136
1137 try:
1138 _, ext = os.path.splitext(path)
1139 if ext in ['.kcrash', '.meta']:
1140 ext = ext.replace('.', '')
1141 with open(path) as f:
1142 data = f.read(MAX_CRASH_FILE_SIZE)
1143 tell = f.tell()
1144 logging.info(
1145 'Contents of %s%s:%s',
1146 path,
1147 ('' if tell == stat.st_size
1148 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
1149 ('\n' + data).replace('\n', '\n ' + ext + '> '))
1150 extra_log_args['data'] = data
1151
1152 # Copy to /var/factory/kcrash for posterity
1153 kcrash_dir = factory.get_factory_root('kcrash')
1154 utils.TryMakeDirs(kcrash_dir)
1155 shutil.copy(path, kcrash_dir)
1156 logging.info('Copied to %s',
1157 os.path.join(kcrash_dir, os.path.basename(path)))
1158 finally:
1159 # Even if something goes wrong with the above, still try to
1160 # log to event log
1161 self.event_log.Log('crash_file',
1162 path=path, size=stat.st_size, mtime=mtime,
1163 **extra_log_args)
1164 except: # pylint: disable=W0702
1165 logging.exception('Unable to handle crash files %s', path)
1166 seen_crashes.add(path)
1167
1168 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
1169
Jon Salz128b0932013-07-03 16:55:26 +08001170 def GetTestList(self, test_list_id):
1171 """Returns the test list with the given ID.
1172
1173 Raises:
1174 TestListError: The test list ID is not valid.
1175 """
1176 try:
1177 return self.test_lists[test_list_id]
1178 except KeyError:
1179 raise test_lists.TestListError(
1180 '%r is not a valid test list ID (available IDs are [%s])' % (
1181 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
1182
1183 def InitTestLists(self):
1184 """Reads in all test lists and sets the active test list."""
1185 self.test_lists = test_lists.BuildAllTestLists()
Jon Salzd7550792013-07-12 05:49:27 +08001186 logging.info('Loaded test lists: [%s]',
1187 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +08001188
1189 if not self.options.test_list:
1190 self.options.test_list = test_lists.GetActiveTestListId()
1191
1192 if os.sep in self.options.test_list:
1193 # It's a path pointing to an old-style test list; use it.
1194 self.test_list = factory.read_test_list(self.options.test_list)
1195 else:
1196 self.test_list = self.GetTestList(self.options.test_list)
1197
1198 logging.info('Active test list: %s', self.test_list.test_list_id)
1199
1200 if isinstance(self.test_list, test_lists.OldStyleTestList):
1201 # Actually load it in. (See OldStyleTestList for an explanation
1202 # of why this is necessary.)
1203 self.test_list = self.test_list.Load()
1204
1205 self.test_list.state_instance = self.state_instance
1206
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001207 def init_hooks(self):
1208 """Initializes hooks.
1209
1210 Must run after self.test_list ready.
1211 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001212 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
1213 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
1214 assert isinstance(self.hooks, factory.Hooks), (
1215 "hooks should be of type Hooks but is %r" % type(self.hooks))
1216 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001217 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001218
Jon Salz0697cbf2012-07-04 15:14:04 +08001219 def init(self, args=None, env=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001220 """Initializes Goofy.
Jon Salz0697cbf2012-07-04 15:14:04 +08001221
1222 Args:
1223 args: A list of command-line arguments. Uses sys.argv if
1224 args is None.
1225 env: An Environment instance to use (or None to choose
1226 FakeChrootEnvironment or DUTEnvironment as appropriate).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001227 """
Jon Salz77c151e2012-08-28 07:20:37 +08001228 signal.signal(signal.SIGINT, self.handle_sigint)
1229
Jon Salz0697cbf2012-07-04 15:14:04 +08001230 parser = OptionParser()
1231 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001232 action='store_true',
1233 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001234 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001235 metavar='FILE',
1236 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001237 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001238 action='store_true',
1239 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001240 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz8fa8e832012-07-13 19:04:09 +08001241 choices=['none', 'gtk', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001242 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001243 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001244 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001245 type='int', default=1,
1246 help=('Factor by which to scale UI '
1247 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001248 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001249 metavar='FILE',
1250 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001251 parser.add_option('--dummy_shopfloor', action='store_true',
1252 help='Use a dummy shopfloor server')
chungyiafe8f772012-08-15 19:36:29 +08001253 parser.add_option('--automation', dest='automation',
1254 action='store_true',
1255 help='Enable automation on running factory test')
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001256 parser.add_option('--guest_login', dest='guest_login', default=False,
Ricky Liangb2432362013-10-02 13:12:41 +08001257 action='store_true',
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001258 help='Log in as guest. This will not own the TPM.')
Jon Salz0697cbf2012-07-04 15:14:04 +08001259 (self.options, self.args) = parser.parse_args(args)
1260
Jon Salz46b89562012-07-05 11:49:22 +08001261 # Make sure factory directories exist.
1262 factory.get_log_root()
1263 factory.get_state_root()
1264 factory.get_test_data_root()
1265
Jon Salz0697cbf2012-07-04 15:14:04 +08001266 global _inited_logging # pylint: disable=W0603
1267 if not _inited_logging:
1268 factory.init_logging('goofy', verbose=self.options.verbose)
1269 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001270
Jon Salz0f996602012-10-03 15:26:48 +08001271 if self.options.print_test_list:
1272 print factory.read_test_list(
1273 self.options.print_test_list).__repr__(recursive=True)
1274 sys.exit(0)
1275
Jon Salzee85d522012-07-17 14:34:46 +08001276 event_log.IncrementBootSequence()
Jon Salzd15bbcf2013-05-21 17:33:57 +08001277 # Don't defer logging the initial event, so we can make sure
1278 # that device_id, reimage_id, etc. are all set up.
1279 self.event_log = EventLog('goofy', defer=False)
Jon Salz0697cbf2012-07-04 15:14:04 +08001280
1281 if (not suppress_chroot_warning and
1282 factory.in_chroot() and
1283 self.options.ui == 'gtk' and
1284 os.environ.get('DISPLAY') in [None, '', ':0', ':0.0']):
1285 # That's not going to work! Tell the user how to run
1286 # this way.
1287 logging.warn(GOOFY_IN_CHROOT_WARNING)
1288 time.sleep(1)
1289
1290 if env:
1291 self.env = env
1292 elif factory.in_chroot():
1293 self.env = test_environment.FakeChrootEnvironment()
1294 logging.warn(
1295 'Using chroot environment: will not actually run autotests')
1296 else:
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001297 if self.options.guest_login:
1298 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
1299 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001300 self.env.goofy = self
1301
1302 if self.options.restart:
1303 state.clear_state()
1304
Jon Salz0697cbf2012-07-04 15:14:04 +08001305 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1306 logging.warn(
1307 'In QEMU; ignoring ui_scale_factor argument')
1308 self.options.ui_scale_factor = 1
1309
1310 logging.info('Started')
1311
1312 self.start_state_server()
1313 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1314 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001315 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001316 self.last_shutdown_time = (
1317 self.state_instance.get_shared_data('shutdown_time', optional=True))
1318 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001319 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001320
Jon Salz128b0932013-07-03 16:55:26 +08001321 try:
1322 self.InitTestLists()
1323 except: # pylint: disable=W0702
1324 logging.exception('Unable to initialize test lists')
1325 self.state_instance.set_shared_data(
1326 'startup_error',
1327 'Unable to initialize test lists\n%s' % (
1328 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001329 if self.options.ui == 'chrome':
1330 # Create an empty test list with default options so that the rest of
1331 # startup can proceed.
1332 self.test_list = factory.FactoryTestList(
1333 [], self.state_instance, factory.Options())
1334 else:
1335 # Bail with an error; no point in starting up.
1336 sys.exit('No valid test list; exiting.')
1337
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001338 self.init_hooks()
1339
Jon Salz822838b2013-03-25 17:32:33 +08001340 if self.test_list.options.clear_state_on_start:
1341 self.state_instance.clear_test_state()
1342
Vic Yang3e1cf5d2013-06-05 18:50:24 +08001343 if system.SystemInfo().firmware_version is None and not utils.in_chroot():
Vic Yang9bd4f772013-06-04 17:34:00 +08001344 self.state_instance.set_shared_data('startup_error',
1345 'Netboot firmware detected\n'
1346 'Connect Ethernet and reboot to re-image.\n'
1347 u'侦测到网路开机固件\n'
1348 u'请连接乙太网并重启')
1349
Jon Salz0697cbf2012-07-04 15:14:04 +08001350 if not self.state_instance.has_shared_data('ui_lang'):
1351 self.state_instance.set_shared_data('ui_lang',
1352 self.test_list.options.ui_lang)
1353 self.state_instance.set_shared_data(
1354 'test_list_options',
1355 self.test_list.options.__dict__)
1356 self.state_instance.test_list = self.test_list
1357
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001358 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001359
Jon Salz23926422012-09-01 03:38:13 +08001360 if self.options.dummy_shopfloor:
1361 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1362 'http://localhost:%d/' % shopfloor.DEFAULT_SERVER_PORT)
1363 self.dummy_shopfloor = Spawn(
1364 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1365 '--dummy'])
1366 elif self.test_list.options.shopfloor_server_url:
1367 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001368 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001369
Jon Salz0f996602012-10-03 15:26:48 +08001370 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001371 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1372 base_time=time_sanitizer.GetBaseTimeFromFile(
1373 # lsb-factory is written by the factory install shim during
1374 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001375 # the mini-Omaha server. If it's not available, we'll use
1376 # /etc/lsb-factory (which will be much older, but reasonably
1377 # sane) and rely on a shopfloor sync to set a more accurate
1378 # time.
1379 '/usr/local/etc/lsb-factory',
1380 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001381 self.time_sanitizer.RunOnce()
1382
Vic Yangd8990da2013-06-27 16:57:43 +08001383 if self.test_list.options.check_cpu_usage_period_secs:
1384 self.cpu_usage_watcher = Spawn(['py/tools/cpu_usage_monitor.py',
1385 '-p', str(self.test_list.options.check_cpu_usage_period_secs)],
1386 cwd=factory.FACTORY_PATH)
1387
Jon Salz0697cbf2012-07-04 15:14:04 +08001388 self.init_states()
1389 self.start_event_server()
1390 self.connection_manager = self.env.create_connection_manager(
Tai-Hsu Lin371351a2012-08-27 14:17:14 +08001391 self.test_list.options.wlans,
1392 self.test_list.options.scan_wifi_period_secs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001393 # Note that we create a log watcher even if
1394 # sync_event_log_period_secs isn't set (no background
1395 # syncing), since we may use it to flush event logs as well.
1396 self.log_watcher = EventLogWatcher(
1397 self.test_list.options.sync_event_log_period_secs,
Jon Salzd15bbcf2013-05-21 17:33:57 +08001398 event_log_db_file=None,
Jon Salz16d10542012-07-23 12:18:45 +08001399 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001400 if self.test_list.options.sync_event_log_period_secs:
1401 self.log_watcher.StartWatchThread()
1402
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001403 # Creates a system log manager to scan logs periocially.
1404 # A scan includes clearing logs and optionally syncing logs if
1405 # enable_syng_log is True. We kick it to sync logs.
1406 self.system_log_manager = SystemLogManager(
1407 sync_log_paths=self.test_list.options.sync_log_paths,
1408 sync_log_period_secs=self.test_list.options.sync_log_period_secs,
1409 scan_log_period_secs=self.test_list.options.scan_log_period_secs,
Cheng-Yi Chiangb8a491c2014-01-20 14:37:57 +08001410 clear_log_paths=self.test_list.options.clear_log_paths,
1411 clear_log_excluded_paths=self.test_list.options.clear_log_excluded_paths)
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001412 self.system_log_manager.Start()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001413
Jon Salz0697cbf2012-07-04 15:14:04 +08001414 self.update_system_info()
1415
Vic Yang4953fc12012-07-26 16:19:53 +08001416 assert ((self.test_list.options.min_charge_pct is None) ==
1417 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001418 if utils.in_chroot():
1419 logging.info('In chroot, ignoring charge manager and charge state')
1420 elif self.test_list.options.min_charge_pct is not None:
Vic Yang4953fc12012-07-26 16:19:53 +08001421 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1422 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001423 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001424 else:
1425 # Goofy should set charger state to charge if charge_manager is disabled.
1426 try:
1427 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
1428 except BoardException:
1429 logging.exception('Unable to set charge state on this board')
Vic Yang4953fc12012-07-26 16:19:53 +08001430
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001431 self.core_dump_manager = CoreDumpManager(
1432 self.test_list.options.core_dump_watchlist)
1433
Jon Salz0697cbf2012-07-04 15:14:04 +08001434 os.environ['CROS_FACTORY'] = '1'
1435 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1436
1437 # Set CROS_UI since some behaviors in ui.py depend on the
1438 # particular UI in use. TODO(jsalz): Remove this (and all
1439 # places it is used) when the GTK UI is removed.
1440 os.environ['CROS_UI'] = self.options.ui
1441
Shuo-Peng Liao1ff502e2013-06-30 18:37:02 +08001442 if not utils.in_chroot() and self.test_list.options.use_cpufreq_manager:
Jon Salzddf0d052013-06-18 12:52:44 +08001443 self.cpufreq_manager = CpufreqManager(event_log=self.event_log)
Jon Salzce6a7f82013-06-10 18:22:54 +08001444
Justin Chuang31b02432013-06-27 15:16:51 +08001445 # Startup hooks may want to skip some tests.
1446 self.update_skipped_tests()
Jon Salz416f9cc2013-05-10 18:32:50 +08001447
Jon Salze12c2b32013-06-25 16:24:34 +08001448 self.find_kcrashes()
1449
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001450 # Should not move earlier.
1451 self.hooks.OnStartup()
1452
Jon Salz0697cbf2012-07-04 15:14:04 +08001453 if self.options.ui == 'chrome':
1454 self.env.launch_chrome()
1455 logging.info('Waiting for a web socket connection')
Cheng-Yi Chiangfd8ed392013-03-08 21:37:31 +08001456 self.web_socket_manager.wait()
Jon Salz0697cbf2012-07-04 15:14:04 +08001457
1458 # Wait for the test widget size to be set; this is done in
1459 # an asynchronous RPC so there is a small chance that the
1460 # web socket might be opened first.
1461 for _ in range(100): # 10 s
1462 try:
1463 if self.state_instance.get_shared_data('test_widget_size'):
1464 break
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001465 except KeyError:
Jon Salz0697cbf2012-07-04 15:14:04 +08001466 pass # Retry
1467 time.sleep(0.1) # 100 ms
1468 else:
1469 logging.warn('Never received test_widget_size from UI')
Jon Salz45297282013-05-18 14:31:47 +08001470
1471 # Send Chrome a Tab to get focus to the factory UI
1472 # (http://crosbug.com/p/19444). TODO(jsalz): remove this hack
1473 # and figure out the right way to get the focus to Chrome.
1474 if not utils.in_chroot():
Ricky Liangb97f3652013-08-20 17:30:28 +08001475 utils.SendKey('Tab')
Jon Salz0697cbf2012-07-04 15:14:04 +08001476 elif self.options.ui == 'gtk':
1477 self.start_ui()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001478
Ricky Liang650f6bf2012-09-28 13:22:54 +08001479 # Create download path for autotest beforehand or autotests run at
1480 # the same time might fail due to race condition.
1481 if not factory.in_chroot():
1482 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1483 'download'))
1484
Jon Salz0697cbf2012-07-04 15:14:04 +08001485 def state_change_callback(test, test_state):
1486 self.event_client.post_event(
1487 Event(Event.Type.STATE_CHANGE,
1488 path=test.path, state=test_state))
1489 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001490
Jon Salza6711d72012-07-18 14:33:03 +08001491 for handler in self.on_ui_startup:
1492 handler()
1493
1494 self.prespawner = Prespawner()
1495 self.prespawner.start()
1496
Jon Salz0697cbf2012-07-04 15:14:04 +08001497 try:
1498 tests_after_shutdown = self.state_instance.get_shared_data(
1499 'tests_after_shutdown')
1500 except KeyError:
1501 tests_after_shutdown = None
Jon Salz57717ca2012-04-04 16:47:25 +08001502
Jon Salz5c344f62012-07-13 14:31:16 +08001503 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1504 if not force_auto_run and tests_after_shutdown is not None:
Jon Salz0697cbf2012-07-04 15:14:04 +08001505 logging.info('Resuming tests after shutdown: %s',
1506 tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001507 self.tests_to_run.extend(
1508 self.test_list.lookup_path(t) for t in tests_after_shutdown)
1509 self.run_queue.put(self.run_next_test)
1510 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001511 if force_auto_run or self.test_list.options.auto_run_on_start:
Jon Salz0697cbf2012-07-04 15:14:04 +08001512 self.run_queue.put(
1513 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001514 self.state_instance.set_shared_data('tests_after_shutdown', None)
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001515
Dean Liao592e4d52013-01-10 20:06:39 +08001516 self.may_disable_cros_shortcut_keys()
1517
1518 def may_disable_cros_shortcut_keys(self):
1519 test_options = self.test_list.options
1520 if test_options.disable_cros_shortcut_keys:
1521 logging.info('Filter ChromeOS shortcut keys.')
1522 self.key_filter = KeyFilter(
1523 unmap_caps_lock=test_options.disable_caps_lock,
1524 caps_lock_keycode=test_options.caps_lock_keycode)
1525 self.key_filter.Start()
1526
Jon Salz0697cbf2012-07-04 15:14:04 +08001527 def run(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001528 """Runs Goofy."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001529 # Process events forever.
1530 while self.run_once(True):
1531 pass
Jon Salz73e0fd02012-04-04 11:46:38 +08001532
Jon Salz0697cbf2012-07-04 15:14:04 +08001533 def run_once(self, block=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001534 """Runs all items pending in the event loop.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001535
Jon Salz0697cbf2012-07-04 15:14:04 +08001536 Args:
1537 block: If true, block until at least one event is processed.
Jon Salz7c15e8b2012-06-19 17:10:37 +08001538
Jon Salz0697cbf2012-07-04 15:14:04 +08001539 Returns:
1540 True to keep going or False to shut down.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001541 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001542 events = utils.DrainQueue(self.run_queue)
cychiang21886742012-07-05 15:16:32 +08001543 while not events:
Jon Salz0697cbf2012-07-04 15:14:04 +08001544 # Nothing on the run queue.
1545 self._run_queue_idle()
1546 if block:
1547 # Block for at least one event...
cychiang21886742012-07-05 15:16:32 +08001548 try:
1549 events.append(self.run_queue.get(timeout=RUN_QUEUE_TIMEOUT_SECS))
1550 except Queue.Empty:
1551 # Keep going (calling _run_queue_idle() again at the top of
1552 # the loop)
1553 continue
Jon Salz0697cbf2012-07-04 15:14:04 +08001554 # ...and grab anything else that showed up at the same
1555 # time.
1556 events.extend(utils.DrainQueue(self.run_queue))
cychiang21886742012-07-05 15:16:32 +08001557 else:
1558 break
Jon Salz51528e12012-07-02 18:54:45 +08001559
Jon Salz0697cbf2012-07-04 15:14:04 +08001560 for event in events:
1561 if not event:
1562 # Shutdown request.
1563 self.run_queue.task_done()
1564 return False
Jon Salz51528e12012-07-02 18:54:45 +08001565
Jon Salz0697cbf2012-07-04 15:14:04 +08001566 try:
1567 event()
Jon Salz85a39882012-07-05 16:45:04 +08001568 except: # pylint: disable=W0702
1569 logging.exception('Error in event loop')
Jon Salz0697cbf2012-07-04 15:14:04 +08001570 self.record_exception(traceback.format_exception_only(
1571 *sys.exc_info()[:2]))
1572 # But keep going
1573 finally:
1574 self.run_queue.task_done()
1575 return True
Jon Salz0405ab52012-03-16 15:26:52 +08001576
Jon Salz0e6532d2012-10-25 16:30:11 +08001577 def _should_sync_time(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001578 """Returns True if we should attempt syncing time with shopfloor.
Jon Salz0e6532d2012-10-25 16:30:11 +08001579
1580 Args:
1581 foreground: If True, synchronizes even if background syncing
1582 is disabled (e.g., in explicit sync requests from the
1583 SyncShopfloor test).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001584 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001585 return ((foreground or
1586 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001587 self.time_sanitizer and
1588 (not self.time_synced) and
1589 (not factory.in_chroot()))
1590
Jon Salz0e6532d2012-10-25 16:30:11 +08001591 def sync_time_with_shopfloor_server(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001592 """Syncs time with shopfloor server, if not yet synced.
Jon Salz54882d02012-08-31 01:57:54 +08001593
Jon Salz0e6532d2012-10-25 16:30:11 +08001594 Args:
1595 foreground: If True, synchronizes even if background syncing
1596 is disabled (e.g., in explicit sync requests from the
1597 SyncShopfloor test).
1598
Jon Salz54882d02012-08-31 01:57:54 +08001599 Returns:
1600 False if no time sanitizer is available, or True if this sync (or a
1601 previous sync) succeeded.
1602
1603 Raises:
1604 Exception if unable to contact the shopfloor server.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001605 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001606 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001607 self.time_sanitizer.SyncWithShopfloor()
1608 self.time_synced = True
1609 return self.time_synced
1610
Jon Salzb92c5112012-09-21 15:40:11 +08001611 def log_disk_space_stats(self):
Jon Salz18e0e022013-06-11 17:13:39 +08001612 if (utils.in_chroot() or
1613 not self.test_list.options.log_disk_space_period_secs):
Jon Salzb92c5112012-09-21 15:40:11 +08001614 return
1615
1616 now = time.time()
1617 if (self.last_log_disk_space_time and
1618 now - self.last_log_disk_space_time <
1619 self.test_list.options.log_disk_space_period_secs):
1620 return
1621 self.last_log_disk_space_time = now
1622
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001623 # Upload event if stateful partition usage is above threshold.
1624 # Stateful partition is mounted on /usr/local, while
1625 # encrypted stateful partition is mounted on /var.
1626 # If there are too much logs in the factory process,
1627 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001628 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001629 vfs_infos = disk_space.GetAllVFSInfo()
1630 stateful_info, encrypted_info = None, None
1631 for vfs_info in vfs_infos.values():
1632 if '/usr/local' in vfs_info.mount_points:
1633 stateful_info = vfs_info
1634 if '/var' in vfs_info.mount_points:
1635 encrypted_info = vfs_info
1636
1637 stateful = disk_space.GetPartitionUsage(stateful_info)
1638 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1639
1640 above_threshold = (
1641 self.test_list.options.stateful_usage_threshold and
1642 max(stateful.bytes_used_pct,
1643 stateful.inodes_used_pct,
1644 encrypted.bytes_used_pct,
1645 encrypted.inodes_used_pct) >
1646 self.test_list.options.stateful_usage_threshold)
1647
1648 if above_threshold:
1649 self.event_log.Log('stateful_partition_usage',
1650 partitions={
1651 'stateful': {
1652 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1653 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1654 'encrypted_stateful': {
1655 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1656 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1657 })
1658 self.log_watcher.ScanEventLogs()
Cheng-Yi Chiang00798e72013-06-20 18:16:39 +08001659 if (not utils.in_chroot() and
1660 self.test_list.options.stateful_usage_above_threshold_action):
1661 Spawn(self.test_list.options.stateful_usage_above_threshold_action,
1662 call=True)
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001663
1664 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001665 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001666 if above_threshold:
1667 logging.warning(message)
1668 else:
1669 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001670 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001671 except: # pylint: disable=W0702
1672 logging.exception('Unable to get disk space used')
1673
Justin Chuang83813982013-05-13 01:26:32 +08001674 def check_battery(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001675 """Checks the current battery status.
Justin Chuang83813982013-05-13 01:26:32 +08001676
1677 Logs current battery charging level and status to log. If the battery level
1678 is lower below warning_low_battery_pct, send warning event to shopfloor.
1679 If the battery level is lower below critical_low_battery_pct, flush disks.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001680 """
Justin Chuang83813982013-05-13 01:26:32 +08001681 if not self.test_list.options.check_battery_period_secs:
1682 return
1683
1684 now = time.time()
1685 if (self.last_check_battery_time and
1686 now - self.last_check_battery_time <
1687 self.test_list.options.check_battery_period_secs):
1688 return
1689 self.last_check_battery_time = now
1690
1691 message = ''
1692 log_level = logging.INFO
1693 try:
1694 power = system.GetBoard().power
1695 if not power.CheckBatteryPresent():
1696 message = 'Battery is not present'
1697 else:
1698 ac_present = power.CheckACPresent()
1699 charge_pct = power.GetChargePct(get_float=True)
1700 message = ('Current battery level %.1f%%, AC charger is %s' %
1701 (charge_pct, 'connected' if ac_present else 'disconnected'))
1702
1703 if charge_pct > self.test_list.options.critical_low_battery_pct:
1704 critical_low_battery = False
1705 else:
1706 critical_low_battery = True
1707 # Only sync disks when battery level is still above minimum
1708 # value. This can be used for offline analysis when shopfloor cannot
1709 # be connected.
1710 if charge_pct > MIN_BATTERY_LEVEL_FOR_DISK_SYNC:
1711 logging.warning('disk syncing for critical low battery situation')
1712 os.system('sync; sync; sync')
1713 else:
1714 logging.warning('disk syncing is cancelled '
1715 'because battery level is lower than %.1f',
1716 MIN_BATTERY_LEVEL_FOR_DISK_SYNC)
1717
1718 # Notify shopfloor server
1719 if (critical_low_battery or
1720 (not ac_present and
1721 charge_pct <= self.test_list.options.warning_low_battery_pct)):
1722 log_level = logging.WARNING
1723
1724 self.event_log.Log('low_battery',
1725 battery_level=charge_pct,
1726 charger_connected=ac_present,
1727 critical=critical_low_battery)
1728 self.log_watcher.KickWatchThread()
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001729 if self.test_list.options.enable_sync_log:
1730 self.system_log_manager.KickToSync()
Justin Chuang83813982013-05-13 01:26:32 +08001731 except: # pylint: disable=W0702
1732 logging.exception('Unable to check battery or notify shopfloor')
1733 finally:
1734 if message != self.last_check_battery_message:
1735 logging.log(log_level, message)
1736 self.last_check_battery_message = message
1737
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001738 def check_core_dump(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001739 """Checks if there is any core dumped file.
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001740
1741 Removes unwanted core dump files immediately.
1742 Syncs those files matching watch list to server with a delay between
1743 each sync. After the files have been synced to server, deletes the files.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001744 """
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001745 core_dump_files = self.core_dump_manager.ScanFiles()
1746 if core_dump_files:
1747 now = time.time()
1748 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1749 self.test_list.options.kick_sync_min_interval_secs):
1750 return
1751 self.last_kick_sync_time = now
1752
1753 # Sends event to server
1754 self.event_log.Log('core_dumped', files=core_dump_files)
1755 self.log_watcher.KickWatchThread()
1756
1757 # Syncs files to server
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001758 if self.test_list.options.enable_sync_log:
1759 self.system_log_manager.KickToSync(
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001760 core_dump_files, self.core_dump_manager.ClearFiles)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001761
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001762 def check_log_rotation(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001763 """Checks log rotation file presence/absence according to test_list option.
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001764
1765 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1766 is True, delete it otherwise. This must be done in idle loop because
1767 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1768 an autotest.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001769 """
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001770 if utils.in_chroot():
1771 return
1772 try:
1773 if self.test_list.options.disable_log_rotation:
1774 open(CLEANUP_LOGS_PAUSED, 'w').close()
1775 else:
1776 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1777 except: # pylint: disable=W0702
1778 # Oh well. Logs an error (but no trace)
1779 logging.info(
1780 'Unable to %s %s: %s',
1781 'touch' if self.test_list.options.disable_log_rotation else 'delete',
1782 CLEANUP_LOGS_PAUSED, utils.FormatExceptionOnly())
1783
Jon Salz8fa8e832012-07-13 19:04:09 +08001784 def sync_time_in_background(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001785 """Writes out current time and tries to sync with shopfloor server."""
Jon Salzb22d1172012-08-06 10:38:57 +08001786 if not self.time_sanitizer:
1787 return
1788
1789 # Write out the current time.
1790 self.time_sanitizer.SaveTime()
1791
Jon Salz54882d02012-08-31 01:57:54 +08001792 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001793 return
1794
1795 now = time.time()
1796 if self.last_sync_time and (
1797 now - self.last_sync_time <
1798 self.test_list.options.sync_time_period_secs):
1799 # Not yet time for another check.
1800 return
1801 self.last_sync_time = now
1802
1803 def target():
1804 try:
Jon Salz54882d02012-08-31 01:57:54 +08001805 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001806 except: # pylint: disable=W0702
1807 # Oh well. Log an error (but no trace)
1808 logging.info(
1809 'Unable to get time from shopfloor server: %s',
1810 utils.FormatExceptionOnly())
1811
1812 thread = threading.Thread(target=target)
1813 thread.daemon = True
1814 thread.start()
1815
Jon Salz0697cbf2012-07-04 15:14:04 +08001816 def _run_queue_idle(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001817 """Invoked when the run queue has no events.
Vic Yang4953fc12012-07-26 16:19:53 +08001818
1819 This method must not raise exception.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001820 """
Jon Salzb22d1172012-08-06 10:38:57 +08001821 now = time.time()
1822 if (self.last_idle and
1823 now < (self.last_idle + RUN_QUEUE_TIMEOUT_SECS - 1)):
1824 # Don't run more often than once every (RUN_QUEUE_TIMEOUT_SECS -
1825 # 1) seconds.
1826 return
1827
1828 self.last_idle = now
1829
Vic Yang311ddb82012-09-26 12:08:28 +08001830 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001831 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001832 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001833 self.log_disk_space_stats()
Justin Chuang83813982013-05-13 01:26:32 +08001834 self.check_battery()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001835 self.check_core_dump()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001836 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001837
Jon Salzd15bbcf2013-05-21 17:33:57 +08001838 def handle_event_logs(self, chunks):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001839 """Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001840
Jon Salz0697cbf2012-07-04 15:14:04 +08001841 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001842
1843 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001844 chunks: A list of Chunk objects.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001845 """
Vic Yang93027612013-05-06 02:42:49 +08001846 first_exception = None
1847 exception_count = 0
1848
Jon Salzd15bbcf2013-05-21 17:33:57 +08001849 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001850 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001851 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001852 start_time = time.time()
1853 shopfloor_client = shopfloor.get_instance(
1854 detect=True,
1855 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzd15bbcf2013-05-21 17:33:57 +08001856 shopfloor_client.UploadEvent(chunk.log_name + "." +
1857 event_log.GetReimageId(),
1858 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001859 logging.info(
1860 'Successfully synced %s in %.03f s',
1861 description, time.time() - start_time)
1862 except: # pylint: disable=W0702
Jon Salzd15bbcf2013-05-21 17:33:57 +08001863 first_exception = (first_exception or (chunk.log_name + ': ' +
Vic Yang93027612013-05-06 02:42:49 +08001864 utils.FormatExceptionOnly()))
1865 exception_count += 1
1866
1867 if exception_count:
1868 if exception_count == 1:
1869 msg = 'Log upload failed: %s' % first_exception
1870 else:
1871 msg = '%d log upload failed; first is: %s' % (
1872 exception_count, first_exception)
1873 raise Exception(msg)
1874
Jon Salz57717ca2012-04-04 16:47:25 +08001875
Jon Salz0697cbf2012-07-04 15:14:04 +08001876 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1877 root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001878 """Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001879
Jon Salz0697cbf2012-07-04 15:14:04 +08001880 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001881
Jon Salz0697cbf2012-07-04 15:14:04 +08001882 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001883 statuses_to_run: The particular status that caller wants to run.
Jon Salz0697cbf2012-07-04 15:14:04 +08001884 starting_at: If provided, only auto-runs tests beginning with
1885 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001886 root: The root of tests to run. If not provided, it will be
1887 the root of all tests.
1888 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001889 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001890
Jon Salz0697cbf2012-07-04 15:14:04 +08001891 if starting_at:
1892 # Make sure they passed a test, not a string.
1893 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001894
Jon Salz0697cbf2012-07-04 15:14:04 +08001895 tests_to_reset = []
1896 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001897
Jon Salz0697cbf2012-07-04 15:14:04 +08001898 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001899
Jon Salz0697cbf2012-07-04 15:14:04 +08001900 for test in root.get_top_level_tests():
1901 if starting_at:
1902 if test == starting_at:
1903 # We've found starting_at; do auto-run on all
1904 # subsequent tests.
1905 found_starting_at = True
1906 if not found_starting_at:
1907 # Don't start this guy yet
1908 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001909
Jon Salz0697cbf2012-07-04 15:14:04 +08001910 status = test.get_state().status
1911 if status == TestState.ACTIVE or status in statuses_to_run:
1912 # Reset the test (later; we will need to abort
1913 # all active tests first).
1914 tests_to_reset.append(test)
1915 if status in statuses_to_run:
1916 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001917
Jon Salz6dc031d2013-06-19 13:06:23 +08001918 self.abort_active_tests('Operator requested run/re-run of certain tests')
Jon Salz258a40c2012-04-19 12:34:01 +08001919
Jon Salz0697cbf2012-07-04 15:14:04 +08001920 # Reset all statuses of the tests to run (in case any tests were active;
1921 # we want them to be run again).
1922 for test_to_reset in tests_to_reset:
1923 for test in test_to_reset.walk():
1924 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001925
Jon Salz0697cbf2012-07-04 15:14:04 +08001926 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001927
Jon Salz0697cbf2012-07-04 15:14:04 +08001928 def restart_tests(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001929 """Restarts all tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001930 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001931
Jon Salz6dc031d2013-06-19 13:06:23 +08001932 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001933 for test in root.walk():
1934 test.update_state(status=TestState.UNTESTED)
1935 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001936
Jon Salz0697cbf2012-07-04 15:14:04 +08001937 def auto_run(self, starting_at=None, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001938 """"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001939
Jon Salz0697cbf2012-07-04 15:14:04 +08001940 Args:
1941 starting_at: If provide, only auto-runs tests beginning with
1942 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001943 root: If provided, the root of tests to run. If not provided, the root
1944 will be test_list (root of all tests).
1945 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001946 root = root or self.test_list
1947 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1948 starting_at=starting_at,
1949 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001950
Jon Salz0697cbf2012-07-04 15:14:04 +08001951 def re_run_failed(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001952 """Re-runs failed tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001953 root = root or self.test_list
1954 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001955
Jon Salz0697cbf2012-07-04 15:14:04 +08001956 def show_review_information(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001957 """Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001958
Jon Salz0697cbf2012-07-04 15:14:04 +08001959 The information screene is rendered by main UI program (ui.py), so in
1960 goofy we only need to kill all active tests, set them as untested, and
1961 clear remaining tests.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001962 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001963 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001964 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001965
Jon Salz0697cbf2012-07-04 15:14:04 +08001966 def handle_switch_test(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001967 """Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001968
Jon Salz0697cbf2012-07-04 15:14:04 +08001969 @param event: The SWITCH_TEST event.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001970 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001971 test = self.test_list.lookup_path(event.path)
1972 if not test:
1973 logging.error('Unknown test %r', event.key)
1974 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001975
Jon Salz0697cbf2012-07-04 15:14:04 +08001976 invoc = self.invocations.get(test)
1977 if invoc and test.backgroundable:
1978 # Already running: just bring to the front if it
1979 # has a UI.
1980 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001981 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001982 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001983
Jon Salz6dc031d2013-06-19 13:06:23 +08001984 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001985 for t in test.walk():
1986 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001987
Jon Salz0697cbf2012-07-04 15:14:04 +08001988 if self.test_list.options.auto_run_on_keypress:
1989 self.auto_run(starting_at=test)
1990 else:
1991 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001992
Jon Salz0697cbf2012-07-04 15:14:04 +08001993 def wait(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001994 """Waits for all pending invocations.
Jon Salz0697cbf2012-07-04 15:14:04 +08001995
1996 Useful for testing.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001997 """
Jon Salz1acc8742012-07-17 17:45:55 +08001998 while self.invocations:
1999 for k, v in self.invocations.iteritems():
2000 logging.info('Waiting for %s to complete...', k)
2001 v.thread.join()
2002 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08002003
2004 def check_exceptions(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002005 """Raises an error if any exceptions have occurred in
2006 invocation threads.
2007 """
Jon Salz0697cbf2012-07-04 15:14:04 +08002008 if self.exceptions:
2009 raise RuntimeError('Exception in invocation thread: %r' %
2010 self.exceptions)
2011
2012 def record_exception(self, msg):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002013 """Records an exception in an invocation thread.
Jon Salz0697cbf2012-07-04 15:14:04 +08002014
2015 An exception with the given message will be rethrown when
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08002016 Goofy is destroyed.
2017 """
Jon Salz0697cbf2012-07-04 15:14:04 +08002018 self.exceptions.append(msg)
Jon Salz73e0fd02012-04-04 11:46:38 +08002019
Hung-Te Linf2f78f72012-02-08 19:27:11 +08002020
2021if __name__ == '__main__':
Jon Salz77c151e2012-08-28 07:20:37 +08002022 goofy = Goofy()
2023 try:
2024 goofy.main()
Jon Salz0f996602012-10-03 15:26:48 +08002025 except SystemExit:
2026 # Propagate SystemExit without logging.
2027 raise
Jon Salz31373eb2012-09-21 16:19:49 +08002028 except:
Jon Salz0f996602012-10-03 15:26:48 +08002029 # Log the error before trying to shut down (unless it's a graceful
2030 # exit).
Jon Salz31373eb2012-09-21 16:19:49 +08002031 logging.exception('Error in main loop')
2032 raise
Jon Salz77c151e2012-08-28 07:20:37 +08002033 finally:
2034 goofy.destroy()