blob: 46425e1d616c5d698359d9547175688cb0d1f99d [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
Jon Salz670ce062014-05-16 15:53:50 +080046from cros.factory.test import phase
jcliangcd688182012-08-20 21:01:26 +080047from cros.factory.test import state
Jon Salz51528e12012-07-02 18:54:45 +080048from cros.factory.test import shopfloor
Jon Salz83591782012-06-26 11:09:58 +080049from cros.factory.test import utils
Jon Salz128b0932013-07-03 16:55:26 +080050from cros.factory.test.test_lists import test_lists
Ricky Liang6fe218c2013-12-27 15:17:17 +080051from cros.factory.test.e2e_test.common import (
52 AutomationMode, AutomationModePrompt, ParseAutomationMode)
Jon Salz83591782012-06-26 11:09:58 +080053from cros.factory.test.event import Event
54from cros.factory.test.event import EventClient
55from cros.factory.test.event import EventServer
jcliangcd688182012-08-20 21:01:26 +080056from cros.factory.test.factory import TestState
Jon Salzd7550792013-07-12 05:49:27 +080057from cros.factory.test.utils import Enum
Dean Liao592e4d52013-01-10 20:06:39 +080058from cros.factory.tools.key_filter import KeyFilter
Jon Salz2af235d2013-06-24 14:47:21 +080059from cros.factory.utils import file_utils
Jon Salz78c32392012-07-25 14:18:29 +080060from cros.factory.utils.process_utils import Spawn
Hung-Te Linf2f78f72012-02-08 19:27:11 +080061
62
Hung-Te Linf2f78f72012-02-08 19:27:11 +080063HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Chun-ta Lin279e7e92013-02-19 17:40:39 +080064CACHES_DIR = os.path.join(factory.get_state_root(), "caches")
Hung-Te Linf2f78f72012-02-08 19:27:11 +080065
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +080066CLEANUP_LOGS_PAUSED = '/var/lib/cleanup_logs_paused'
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.
Ricky Liang4bff3e32014-02-20 18:46:11 +0800143 run_id: The identifier for latest test run.
144 scheduled_run_tests: The list of tests scheduled for latest test run.
Jon Salz0697cbf2012-07-04 15:14:04 +0800145 event_handlers: Map of Event.Type to the method used to handle that
146 event. If the method has an 'event' argument, the event is passed
147 to the handler.
148 exceptions: Exceptions encountered in invocation threads.
Jon Salz3c493bb2013-02-07 17:24:58 +0800149 last_log_disk_space_message: The last message we logged about disk space
150 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800151 last_kick_sync_time: The last time to kick system_log_manager to sync
152 because of core dump files (to avoid kicking too soon then abort the
153 sync.)
Jon Salz416f9cc2013-05-10 18:32:50 +0800154 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800155 status: The current Goofy status (a member of the Status enum).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800156 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800157 def __init__(self):
158 self.uuid = str(uuid.uuid4())
159 self.state_instance = None
160 self.state_server = None
161 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800162 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800163 self.event_server = None
164 self.event_server_thread = None
165 self.event_client = None
166 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800167 self.charge_manager = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800168 self.time_sanitizer = None
169 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800170 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800171 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800172 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800173 self.event_log = None
174 self.prespawner = None
175 self.ui_process = None
Jon Salzc79a9982012-08-30 04:42:01 +0800176 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800177 self.run_queue = Queue.Queue()
178 self.invocations = {}
179 self.tests_to_run = deque()
180 self.visible_test = None
181 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800182 self.hooks = None
Vic Yangd8990da2013-06-27 16:57:43 +0800183 self.cpu_usage_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800184
185 self.options = None
186 self.args = None
187 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800188 self.test_lists = None
Ricky Liang4bff3e32014-02-20 18:46:11 +0800189 self.run_id = None
190 self.scheduled_run_tests = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800191 self.on_ui_startup = []
192 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800193 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800194 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800195 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800196 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800197 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800198 self.last_log_disk_space_message = None
Justin Chuang83813982013-05-13 01:26:32 +0800199 self.last_check_battery_time = None
200 self.last_check_battery_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800201 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800202 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800203 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800204 self.key_filter = None
Jon Salzce6a7f82013-06-10 18:22:54 +0800205 self.cpufreq_manager = None
Jon Salzd7550792013-07-12 05:49:27 +0800206 self.status = Status.UNINITIALIZED
Jon Salz0697cbf2012-07-04 15:14:04 +0800207
Jon Salz85a39882012-07-05 16:45:04 +0800208 def test_or_root(event, parent_or_group=True):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800209 """Returns the test affected by a particular event.
Jon Salz85a39882012-07-05 16:45:04 +0800210
211 Args:
212 event: The event containing an optional 'path' attribute.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800213 parent_or_group: If True, returns the top-level parent for a test (the
Jon Salz85a39882012-07-05 16:45:04 +0800214 root node of the tests that need to be run together if the given test
215 path is to be run).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800216 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800217 try:
218 path = event.path
219 except AttributeError:
220 path = None
221
222 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800223 test = self.test_list.lookup_path(path)
224 if parent_or_group:
225 test = test.get_top_level_parent_or_group()
226 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800227 else:
228 return self.test_list
229
230 self.event_handlers = {
231 Event.Type.SWITCH_TEST: self.handle_switch_test,
232 Event.Type.SHOW_NEXT_ACTIVE_TEST:
233 lambda event: self.show_next_active_test(),
234 Event.Type.RESTART_TESTS:
235 lambda event: self.restart_tests(root=test_or_root(event)),
236 Event.Type.AUTO_RUN:
237 lambda event: self.auto_run(root=test_or_root(event)),
238 Event.Type.RE_RUN_FAILED:
239 lambda event: self.re_run_failed(root=test_or_root(event)),
240 Event.Type.RUN_TESTS_WITH_STATUS:
241 lambda event: self.run_tests_with_status(
242 event.status,
243 root=test_or_root(event)),
244 Event.Type.REVIEW:
245 lambda event: self.show_review_information(),
246 Event.Type.UPDATE_SYSTEM_INFO:
247 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800248 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800249 lambda event: self.stop(root=test_or_root(event, False),
Jon Salz6dc031d2013-06-19 13:06:23 +0800250 fail=getattr(event, 'fail', False),
251 reason=getattr(event, 'reason', None)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800252 Event.Type.SET_VISIBLE_TEST:
253 lambda event: self.set_visible_test(
254 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800255 Event.Type.CLEAR_STATE:
256 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800257 }
258
259 self.exceptions = []
260 self.web_socket_manager = None
261
262 def destroy(self):
Jon Salzd7550792013-07-12 05:49:27 +0800263 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800264 if self.chrome:
265 self.chrome.kill()
266 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800267 if self.dummy_shopfloor:
268 self.dummy_shopfloor.kill()
269 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800270 if self.ui_process:
271 utils.kill_process_tree(self.ui_process, 'ui')
272 self.ui_process = None
273 if self.web_socket_manager:
274 logging.info('Stopping web sockets')
275 self.web_socket_manager.close()
276 self.web_socket_manager = None
277 if self.state_server_thread:
278 logging.info('Stopping state server')
279 self.state_server.shutdown()
280 self.state_server_thread.join()
281 self.state_server.server_close()
282 self.state_server_thread = None
283 if self.state_instance:
284 self.state_instance.close()
285 if self.event_server_thread:
286 logging.info('Stopping event server')
287 self.event_server.shutdown() # pylint: disable=E1101
288 self.event_server_thread.join()
289 self.event_server.server_close()
290 self.event_server_thread = None
291 if self.log_watcher:
292 if self.log_watcher.IsThreadStarted():
293 self.log_watcher.StopWatchThread()
294 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800295 if self.system_log_manager:
296 if self.system_log_manager.IsThreadRunning():
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +0800297 self.system_log_manager.Stop()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800298 self.system_log_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800299 if self.prespawner:
300 logging.info('Stopping prespawner')
301 self.prespawner.stop()
302 self.prespawner = None
303 if self.event_client:
304 logging.info('Closing event client')
305 self.event_client.close()
306 self.event_client = None
Jon Salzddf0d052013-06-18 12:52:44 +0800307 if self.cpufreq_manager:
308 self.cpufreq_manager.Stop()
Jon Salz0697cbf2012-07-04 15:14:04 +0800309 if self.event_log:
310 self.event_log.Close()
311 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800312 if self.key_filter:
313 self.key_filter.Stop()
Vic Yangd8990da2013-06-27 16:57:43 +0800314 if self.cpu_usage_watcher:
315 self.cpu_usage_watcher.terminate()
Dean Liao592e4d52013-01-10 20:06:39 +0800316
Jon Salz0697cbf2012-07-04 15:14:04 +0800317 self.check_exceptions()
318 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800319 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800320
321 def start_state_server(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800322 # Before starting state server, remount stateful partitions with
323 # no commit flag. The default commit time (commit=600) makes corruption
324 # too likely.
325 file_utils.ResetCommitTime()
326
Jon Salz0697cbf2012-07-04 15:14:04 +0800327 self.state_instance, self.state_server = (
328 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800329 self.goofy_rpc = GoofyRPC(self)
330 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800331 logging.info('Starting state server')
332 self.state_server_thread = threading.Thread(
333 target=self.state_server.serve_forever,
334 name='StateServer')
335 self.state_server_thread.start()
336
337 def start_event_server(self):
338 self.event_server = EventServer()
339 logging.info('Starting factory event server')
340 self.event_server_thread = threading.Thread(
341 target=self.event_server.serve_forever,
342 name='EventServer') # pylint: disable=E1101
343 self.event_server_thread.start()
344
345 self.event_client = EventClient(
346 callback=self.handle_event, event_loop=self.run_queue)
347
348 self.web_socket_manager = WebSocketManager(self.uuid)
349 self.state_server.add_handler("/event",
350 self.web_socket_manager.handle_web_socket)
351
352 def start_ui(self):
353 ui_proc_args = [
354 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
355 self.options.test_list]
356 if self.options.verbose:
357 ui_proc_args.append('-v')
358 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800359 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800360 logging.info('Waiting for UI to come up...')
361 self.event_client.wait(
362 lambda event: event.type == Event.Type.UI_READY)
363 logging.info('UI has started')
364
365 def set_visible_test(self, test):
366 if self.visible_test == test:
367 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800368 if test and not test.has_ui:
369 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800370
371 if test:
372 test.update_state(visible=True)
373 if self.visible_test:
374 self.visible_test.update_state(visible=False)
375 self.visible_test = test
376
Ricky Liang48e47f92014-02-26 19:31:51 +0800377 def log_startup_messages(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800378 """Logs the tail of var/log/messages and mosys and EC console logs."""
Jon Salzd4306c82012-11-30 15:16:36 +0800379 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
380 # for factory-3004.B only. Consolidate and merge back to ToT.
381 if utils.in_chroot():
382 return
383
384 try:
385 var_log_messages = (
386 utils.var_log_messages_before_reboot())
387 logging.info(
388 'Tail of /var/log/messages before last reboot:\n'
389 '%s', ('\n'.join(
390 ' ' + x for x in var_log_messages)))
391 except: # pylint: disable=W0702
392 logging.exception('Unable to grok /var/log/messages')
393
394 try:
Ricky Liang117484a2014-04-14 11:14:41 +0800395 mosys_log = Spawn(
Jon Salzd4306c82012-11-30 15:16:36 +0800396 ['mosys', 'eventlog', 'list'],
397 read_stdout=True, log_stderr_on_error=True).stdout_data
398 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
399 except: # pylint: disable=W0702
400 logging.exception('Unable to read mosys eventlog')
401
402 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800403 board = system.GetBoard()
404 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800405 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
406 except: # pylint: disable=W0702
407 logging.exception('Error retrieving EC console log')
408
Vic Yang079f9872013-07-01 11:32:00 +0800409 try:
410 board = system.GetBoard()
411 ec_panic_info = board.GetECPanicInfo()
412 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
413 except: # pylint: disable=W0702
414 logging.exception('Error retrieving EC panic info')
415
Ricky Liang48e47f92014-02-26 19:31:51 +0800416 def shutdown(self, operation):
417 """Starts shutdown procedure.
418
419 Args:
Vic (Chun-Ju) Yang05b0d952014-04-28 17:39:09 +0800420 operation: The shutdown operation (reboot, full_reboot, or halt).
Ricky Liang48e47f92014-02-26 19:31:51 +0800421 """
422 active_tests = []
423 for test in self.test_list.walk():
424 if not test.is_leaf():
425 continue
426
427 test_state = test.get_state()
428 if test_state.status == TestState.ACTIVE:
429 active_tests.append(test)
430
431
432 if not (len(active_tests) == 1 and
433 isinstance(active_tests[0], factory.ShutdownStep)):
434 logging.error(
435 'Calling Goofy shutdown outside of the shutdown factory test')
436 return
437
438 logging.info('Start Goofy shutdown (%s)', operation)
439 # Save pending test list in the state server
440 self.state_instance.set_shared_data(
441 'tests_after_shutdown',
442 [t.path for t in self.tests_to_run])
443 # Save shutdown time
444 self.state_instance.set_shared_data('shutdown_time', time.time())
445
446 with self.env.lock:
447 self.event_log.Log('shutdown', operation=operation)
448 shutdown_result = self.env.shutdown(operation)
449 if shutdown_result:
450 # That's all, folks!
451 self.run_queue.put(None)
452 else:
453 # Just pass (e.g., in the chroot).
454 self.state_instance.set_shared_data('tests_after_shutdown', None)
455 # Send event with no fields to indicate that there is no
456 # longer a pending shutdown.
457 self.event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
458
459 def handle_shutdown_complete(self, test):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800460 """Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800461
Ricky Liang6fe218c2013-12-27 15:17:17 +0800462 Args:
463 test: The ShutdownStep.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800464 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800465 test_state = test.update_state(increment_shutdown_count=1)
466 logging.info('Detected shutdown (%d of %d)',
Ricky Liang48e47f92014-02-26 19:31:51 +0800467 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800468
Ricky Liang48e47f92014-02-26 19:31:51 +0800469 # Insert current shutdown test at the front of the list of tests to run
470 # after shutdown. This is to continue on post-shutdown verification in the
471 # shutdown step.
472 tests_after_shutdown = self.state_instance.get_shared_data(
473 'tests_after_shutdown', optional=True)
474 if not tests_after_shutdown:
475 self.state_instance.set_shared_data('tests_after_shutdown', [test.path])
476 elif isinstance(tests_after_shutdown, list):
477 self.state_instance.set_shared_data(
478 'tests_after_shutdown', [test.path] + tests_after_shutdown)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800479
Ricky Liang48e47f92014-02-26 19:31:51 +0800480 # Set 'post_shutdown' to inform shutdown test that a shutdown just occurred.
481 self.state_instance.set_shared_data('post_shutdown', True)
Jon Salz258a40c2012-04-19 12:34:01 +0800482
Jon Salz0697cbf2012-07-04 15:14:04 +0800483 def init_states(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800484 """Initializes all states on startup."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800485 for test in self.test_list.get_all_tests():
486 # Make sure the state server knows about all the tests,
487 # defaulting to an untested state.
488 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800489
Jon Salz0697cbf2012-07-04 15:14:04 +0800490 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800491 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800492 ec_console_log = None
Vic Yang079f9872013-07-01 11:32:00 +0800493 ec_panic_info = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800494
Jon Salz0697cbf2012-07-04 15:14:04 +0800495 # Any 'active' tests should be marked as failed now.
496 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800497 if not test.is_leaf():
498 # Don't bother with parents; they will be updated when their
499 # children are updated.
500 continue
501
Jon Salz0697cbf2012-07-04 15:14:04 +0800502 test_state = test.get_state()
503 if test_state.status != TestState.ACTIVE:
504 continue
505 if isinstance(test, factory.ShutdownStep):
506 # Shutdown while the test was active - that's good.
Ricky Liang48e47f92014-02-26 19:31:51 +0800507 self.handle_shutdown_complete(test)
Jon Salz0697cbf2012-07-04 15:14:04 +0800508 else:
509 # Unexpected shutdown. Grab /var/log/messages for context.
510 if var_log_messages is None:
511 try:
512 var_log_messages = (
513 utils.var_log_messages_before_reboot())
514 # Write it to the log, to make it easier to
515 # correlate with /var/log/messages.
516 logging.info(
517 'Unexpected shutdown. '
518 'Tail of /var/log/messages before last reboot:\n'
519 '%s', ('\n'.join(
520 ' ' + x for x in var_log_messages)))
521 except: # pylint: disable=W0702
522 logging.exception('Unable to grok /var/log/messages')
523 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800524
Jon Salz008f4ea2012-08-28 05:39:45 +0800525 if mosys_log is None and not utils.in_chroot():
526 try:
Ricky Liang117484a2014-04-14 11:14:41 +0800527 mosys_log = Spawn(
Jon Salz008f4ea2012-08-28 05:39:45 +0800528 ['mosys', 'eventlog', 'list'],
529 read_stdout=True, log_stderr_on_error=True).stdout_data
530 # Write it to the log also.
531 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
532 except: # pylint: disable=W0702
533 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800534
Vic Yange4c275d2012-08-28 01:50:20 +0800535 if ec_console_log is None:
536 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800537 board = system.GetBoard()
538 ec_console_log = board.GetECConsoleLog()
Vic Yange4c275d2012-08-28 01:50:20 +0800539 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Jon Salzfe1f6652012-09-07 05:40:14 +0800540 except: # pylint: disable=W0702
Vic Yange4c275d2012-08-28 01:50:20 +0800541 logging.exception('Error retrieving EC console log')
542
Vic Yang079f9872013-07-01 11:32:00 +0800543 if ec_panic_info is None:
544 try:
545 board = system.GetBoard()
546 ec_panic_info = board.GetECPanicInfo()
547 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
548 except: # pylint: disable=W0702
549 logging.exception('Error retrieving EC panic info')
550
Jon Salz0697cbf2012-07-04 15:14:04 +0800551 error_msg = 'Unexpected shutdown while test was running'
552 self.event_log.Log('end_test',
553 path=test.path,
554 status=TestState.FAILED,
555 invocation=test.get_state().invocation,
556 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800557 var_log_messages='\n'.join(var_log_messages),
558 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800559 test.update_state(
560 status=TestState.FAILED,
561 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800562
Jon Salz50efe942012-07-26 11:54:10 +0800563 if not test.never_fails:
564 # For "never_fails" tests (such as "Start"), don't cancel
565 # pending tests, since reboot is expected.
566 factory.console.info('Unexpected shutdown while test %s '
567 'running; cancelling any pending tests',
568 test.path)
569 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800570
Jon Salz008f4ea2012-08-28 05:39:45 +0800571 self.update_skipped_tests()
572
573 def update_skipped_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800574 """Updates skipped states based on run_if."""
Jon Salz885dcac2013-07-23 16:39:50 +0800575 env = TestArgEnv()
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800576 def _evaluate_skip_from_run_if(test):
577 """Returns the run_if evaluation of the test.
578
579 Args:
580 test: A FactoryTest object.
581
582 Returns:
583 The run_if evaluation result. Returns False if the test has no
584 run_if argument.
585 """
586 value = None
587 if test.run_if_expr:
588 try:
589 value = test.run_if_expr(env)
590 except: # pylint: disable=W0702
591 logging.exception('Unable to evaluate run_if expression for %s',
592 test.path)
593 # But keep going; we have no choice. This will end up
594 # always activating the test.
595 elif test.run_if_table_name:
596 try:
597 aux = shopfloor.get_selected_aux_data(test.run_if_table_name)
598 value = aux.get(test.run_if_col)
599 except ValueError:
600 # Not available; assume it shouldn't be skipped
601 pass
602
603 if value is None:
604 skip = False
605 else:
606 skip = (not value) ^ t.run_if_not
607 return skip
608
609 # Gets all run_if evaluation, and stores results in skip_map.
610 skip_map = dict()
Jon Salz008f4ea2012-08-28 05:39:45 +0800611 for t in self.test_list.walk():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800612 skip_map[t.path] = _evaluate_skip_from_run_if(t)
Jon Salz885dcac2013-07-23 16:39:50 +0800613
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800614 # Propagates the skip value from root of tree and updates skip_map.
615 def _update_skip_map_from_node(test, skip_from_parent):
616 """Updates skip_map from a given node.
Jon Salz885dcac2013-07-23 16:39:50 +0800617
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800618 Given a FactoryTest node and the skip value from parent, updates the
619 skip value of current node in the skip_map if skip value from parent is
620 True. If this node has children, recursively propagate this value to all
621 its children, that is, all its subtests.
622 Note that this function only updates value in skip_map, not the actual
623 test_list tree.
Jon Salz008f4ea2012-08-28 05:39:45 +0800624
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800625 Args:
626 test: The given FactoryTest object. It is a node in the test_list tree.
627 skip_from_parent: The skip value which propagates from the parent of
628 input node.
629 """
630 skip_this_tree = skip_from_parent or skip_map[test.path]
631 if skip_this_tree:
632 logging.info('Skip from node %r', test.path)
633 skip_map[test.path] = True
634 if test.is_leaf():
635 return
636 # Propagates skip value to its subtests
637 for subtest in test.subtests:
638 _update_skip_map_from_node(subtest, skip_this_tree)
639
640 _update_skip_map_from_node(self.test_list, False)
641
642 # Updates the skip value from skip_map to test_list tree. Also, updates test
643 # status if needed.
644 for t in self.test_list.walk():
645 skip = skip_map[t.path]
646 test_state = t.get_state()
647 if ((not skip) and
648 (test_state.status == TestState.PASSED) and
649 (test_state.error_msg == TestState.SKIPPED_MSG)):
650 # It was marked as skipped before, but now we need to run it.
651 # Mark as untested.
652 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
653 else:
654 t.update_state(skip=skip)
Jon Salz008f4ea2012-08-28 05:39:45 +0800655
Jon Salz0697cbf2012-07-04 15:14:04 +0800656 def show_next_active_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800657 """Rotates to the next visible active test."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800658 self.reap_completed_tests()
659 active_tests = [
660 t for t in self.test_list.walk()
661 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
662 if not active_tests:
663 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800664
Jon Salz0697cbf2012-07-04 15:14:04 +0800665 try:
666 next_test = active_tests[
667 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
668 except ValueError: # visible_test not present in active_tests
669 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800670
Jon Salz0697cbf2012-07-04 15:14:04 +0800671 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800672
Jon Salz0697cbf2012-07-04 15:14:04 +0800673 def handle_event(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800674 """Handles an event from the event server."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800675 handler = self.event_handlers.get(event.type)
676 if handler:
677 handler(event)
678 else:
679 # We don't register handlers for all event types - just ignore
680 # this event.
681 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800682
Vic Yangaabf9fd2013-04-09 18:56:13 +0800683 def check_critical_factory_note(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800684 """Returns True if the last factory note is critical."""
Vic Yangaabf9fd2013-04-09 18:56:13 +0800685 notes = self.state_instance.get_shared_data('factory_note', True)
686 return notes and notes[-1]['level'] == 'CRITICAL'
687
Jon Salz0697cbf2012-07-04 15:14:04 +0800688 def run_next_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800689 """Runs the next eligible test (or tests) in self.tests_to_run."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800690 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800691 if self.tests_to_run and self.check_critical_factory_note():
692 self.tests_to_run.clear()
693 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800694 while self.tests_to_run:
Ricky Liang6fe218c2013-12-27 15:17:17 +0800695 logging.debug('Tests to run: %s', [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800696
Jon Salz0697cbf2012-07-04 15:14:04 +0800697 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800698
Jon Salz0697cbf2012-07-04 15:14:04 +0800699 if test in self.invocations:
700 logging.info('Next test %s is already running', test.path)
701 self.tests_to_run.popleft()
702 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800703
Jon Salza1412922012-07-23 16:04:17 +0800704 for requirement in test.require_run:
705 for i in requirement.test.walk():
706 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800707 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800708 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800709 return
710
Jon Salz0697cbf2012-07-04 15:14:04 +0800711 if self.invocations and not (test.backgroundable and all(
712 [x.backgroundable for x in self.invocations])):
713 logging.debug('Waiting for non-backgroundable tests to '
Ricky Liang6fe218c2013-12-27 15:17:17 +0800714 'complete before running %s', test.path)
Jon Salz0697cbf2012-07-04 15:14:04 +0800715 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800716
Jon Salz3e6f5202012-10-15 15:08:29 +0800717 if test.get_state().skip:
718 factory.console.info('Skipping test %s', test.path)
719 test.update_state(status=TestState.PASSED,
720 error_msg=TestState.SKIPPED_MSG)
721 self.tests_to_run.popleft()
722 continue
723
Jon Salz0697cbf2012-07-04 15:14:04 +0800724 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800725
Jon Salz304a75d2012-07-06 11:14:15 +0800726 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800727 for requirement in test.require_run:
728 for i in requirement.test.walk():
729 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800730 # We've hit this test itself; stop checking
731 break
Jon Salza1412922012-07-23 16:04:17 +0800732 if ((i.get_state().status == TestState.UNTESTED) or
733 (requirement.passed and i.get_state().status !=
734 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800735 # Found an untested test; move on to the next
736 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800737 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800738 break
739
740 if untested:
741 untested_paths = ', '.join(sorted([x.path for x in untested]))
742 if self.state_instance.get_shared_data('engineering_mode',
743 optional=True):
744 # In engineering mode, we'll let it go.
745 factory.console.warn('In engineering mode; running '
746 '%s even though required tests '
747 '[%s] have not completed',
748 test.path, untested_paths)
749 else:
750 # Not in engineering mode; mark it failed.
751 error_msg = ('Required tests [%s] have not been run yet'
752 % untested_paths)
753 factory.console.error('Not running %s: %s',
754 test.path, error_msg)
755 test.update_state(status=TestState.FAILED,
756 error_msg=error_msg)
757 continue
758
Ricky Liang48e47f92014-02-26 19:31:51 +0800759 if (isinstance(test, factory.ShutdownStep) and
760 self.state_instance.get_shared_data('post_shutdown', optional=True)):
761 # Invoking post shutdown method of shutdown test. We should retain the
762 # iterations_left and retries_left of the original test state.
763 test_state = self.state_instance.get_test_state(test.path)
764 self._run_test(test, test_state.iterations_left,
765 test_state.retries_left)
766 else:
767 # Starts a new test run; reset iterations and retries.
768 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800769
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800770 def _run_test(self, test, iterations_left=None, retries_left=None):
Jon Salz1acc8742012-07-17 17:45:55 +0800771 invoc = TestInvocation(self, test, on_completion=self.run_next_test)
772 new_state = test.update_state(
Ricky Liang48e47f92014-02-26 19:31:51 +0800773 status=TestState.ACTIVE, increment_count=1, error_msg='',
774 invocation=invoc.uuid, iterations_left=iterations_left,
775 retries_left=retries_left,
776 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800777 invoc.count = new_state.count
778
779 self.invocations[test] = invoc
780 if self.visible_test is None and test.has_ui:
781 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800782 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800783 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800784
Vic Yang311ddb82012-09-26 12:08:28 +0800785 def check_exclusive(self):
Jon Salzce6a7f82013-06-10 18:22:54 +0800786 # alias since this is really long
787 EXCL_OPT = factory.FactoryTest.EXCLUSIVE_OPTIONS
788
Vic Yang311ddb82012-09-26 12:08:28 +0800789 current_exclusive_items = set([
Jon Salzce6a7f82013-06-10 18:22:54 +0800790 item for item in EXCL_OPT
Vic Yang311ddb82012-09-26 12:08:28 +0800791 if any([test.is_exclusive(item) for test in self.invocations])])
792
793 new_exclusive_items = current_exclusive_items - self.exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800794 if EXCL_OPT.NETWORKING in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800795 logging.info('Disabling network')
796 self.connection_manager.DisableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800797 if EXCL_OPT.CHARGER in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800798 logging.info('Stop controlling charger')
799
800 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800801 if EXCL_OPT.NETWORKING in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800802 logging.info('Re-enabling network')
803 self.connection_manager.EnableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800804 if EXCL_OPT.CHARGER in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800805 logging.info('Start controlling charger')
806
Jon Salzce6a7f82013-06-10 18:22:54 +0800807 if self.cpufreq_manager:
808 enabled = EXCL_OPT.CPUFREQ not in current_exclusive_items
809 try:
810 self.cpufreq_manager.SetEnabled(enabled)
811 except: # pylint: disable=W0702
812 logging.exception('Unable to %s cpufreq services',
813 'enable' if enabled else 'disable')
814
Vic Yang311ddb82012-09-26 12:08:28 +0800815 # Only adjust charge state if not excluded
Jon Salzce6a7f82013-06-10 18:22:54 +0800816 if (EXCL_OPT.CHARGER not in current_exclusive_items and
817 not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800818 if self.charge_manager:
819 self.charge_manager.AdjustChargeState()
820 else:
821 try:
822 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
823 except BoardException:
824 logging.exception('Unable to set charge state on this board')
Vic Yang311ddb82012-09-26 12:08:28 +0800825
826 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800827
cychiang21886742012-07-05 15:16:32 +0800828 def check_for_updates(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800829 """Schedules an asynchronous check for updates if necessary."""
cychiang21886742012-07-05 15:16:32 +0800830 if not self.test_list.options.update_period_secs:
831 # Not enabled.
832 return
833
834 now = time.time()
835 if self.last_update_check and (
836 now - self.last_update_check <
837 self.test_list.options.update_period_secs):
838 # Not yet time for another check.
839 return
840
841 self.last_update_check = now
842
843 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
844 if reached_shopfloor:
845 new_update_md5sum = md5sum if needs_update else None
846 if system.SystemInfo.update_md5sum != new_update_md5sum:
847 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
848 system.SystemInfo.update_md5sum = new_update_md5sum
849 self.run_queue.put(self.update_system_info)
850
851 updater.CheckForUpdateAsync(
852 handle_check_for_update,
853 self.test_list.options.shopfloor_timeout_secs)
854
Jon Salza6711d72012-07-18 14:33:03 +0800855 def cancel_pending_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800856 """Cancels any tests in the run queue."""
Jon Salza6711d72012-07-18 14:33:03 +0800857 self.run_tests([])
858
Ricky Liang4bff3e32014-02-20 18:46:11 +0800859 def restore_active_run_state(self):
860 """Restores active run id and the list of scheduled tests."""
861 self.run_id = self.state_instance.get_shared_data('run_id', optional=True)
862 self.scheduled_run_tests = self.state_instance.get_shared_data(
863 'scheduled_run_tests', optional=True)
864
865 def set_active_run_state(self):
866 """Sets active run id and the list of scheduled tests."""
867 self.run_id = str(uuid.uuid4())
868 self.scheduled_run_tests = [test.path for test in self.tests_to_run]
869 self.state_instance.set_shared_data('run_id', self.run_id)
870 self.state_instance.set_shared_data('scheduled_run_tests',
871 self.scheduled_run_tests)
872
Jon Salz0697cbf2012-07-04 15:14:04 +0800873 def run_tests(self, subtrees, untested_only=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800874 """Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800875
Jon Salz0697cbf2012-07-04 15:14:04 +0800876 The tests are run in order unless one fails (then stops).
877 Backgroundable tests are run simultaneously; when a foreground test is
878 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800879
Ricky Liang6fe218c2013-12-27 15:17:17 +0800880 Args:
881 subtrees: Node or nodes containing tests to run (may either be
882 a single test or a list). Duplicates will be ignored.
883 untested_only: True to run untested tests only.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800884 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800885 if type(subtrees) != list:
886 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800887
Jon Salz0697cbf2012-07-04 15:14:04 +0800888 # Nodes we've seen so far, to avoid duplicates.
889 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800890
Jon Salz0697cbf2012-07-04 15:14:04 +0800891 self.tests_to_run = deque()
892 for subtree in subtrees:
893 for test in subtree.walk():
894 if test in seen:
895 continue
896 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800897
Jon Salz0697cbf2012-07-04 15:14:04 +0800898 if not test.is_leaf():
899 continue
Ricky Liang4bff3e32014-02-20 18:46:11 +0800900 if (untested_only and test.get_state().status != TestState.UNTESTED):
Jon Salz0697cbf2012-07-04 15:14:04 +0800901 continue
902 self.tests_to_run.append(test)
Ricky Liang4bff3e32014-02-20 18:46:11 +0800903 if subtrees:
904 self.set_active_run_state()
Jon Salz0697cbf2012-07-04 15:14:04 +0800905 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800906
Jon Salz0697cbf2012-07-04 15:14:04 +0800907 def reap_completed_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800908 """Removes completed tests from the set of active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800909
910 Also updates the visible test if it was reaped.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800911 """
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800912 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800913 for t, v in dict(self.invocations).iteritems():
914 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800915 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800916 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800917 del self.invocations[t]
918
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800919 # Stop on failure if flag is true.
920 if (self.test_list.options.stop_on_failure and
921 new_state.status == TestState.FAILED):
922 # Clean all the tests to cause goofy to stop.
923 self.tests_to_run = []
924 factory.console.info("Stop on failure triggered. Empty the queue.")
925
Jon Salz1acc8742012-07-17 17:45:55 +0800926 if new_state.iterations_left and new_state.status == TestState.PASSED:
927 # Play it again, Sam!
928 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800929 # new_state.retries_left is obtained after update.
930 # For retries_left == 0, test can still be run for the last time.
931 elif (new_state.retries_left >= 0 and
932 new_state.status == TestState.FAILED):
933 # Still have to retry, Sam!
934 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +0800935
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800936 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +0800937 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800938
Jon Salz0697cbf2012-07-04 15:14:04 +0800939 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +0800940 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +0800941 self.set_visible_test(None)
942 # Make the first running test, if any, the visible test
943 for t in self.test_list.walk():
944 if t in self.invocations:
945 self.set_visible_test(t)
946 break
947
Jon Salz6dc031d2013-06-19 13:06:23 +0800948 def kill_active_tests(self, abort, root=None, reason=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800949 """Kills and waits for all active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800950
Jon Salz85a39882012-07-05 16:45:04 +0800951 Args:
952 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +0800953 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +0800954 root: If set, only kills tests with root as an ancestor.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800955 reason: If set, the abort reason.
956 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800957 self.reap_completed_tests()
958 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +0800959 if root and not test.has_ancestor(root):
960 continue
961
Jon Salz0697cbf2012-07-04 15:14:04 +0800962 factory.console.info('Killing active test %s...' % test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +0800963 invoc.abort_and_join(reason)
Jon Salz0697cbf2012-07-04 15:14:04 +0800964 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +0800965 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800966 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +0800967
Jon Salz0697cbf2012-07-04 15:14:04 +0800968 if not abort:
969 test.update_state(status=TestState.UNTESTED)
970 self.reap_completed_tests()
971
Jon Salz6dc031d2013-06-19 13:06:23 +0800972 def stop(self, root=None, fail=False, reason=None):
973 self.kill_active_tests(fail, root, reason)
Jon Salz85a39882012-07-05 16:45:04 +0800974 # Remove any tests in the run queue under the root.
975 self.tests_to_run = deque([x for x in self.tests_to_run
976 if root and not x.has_ancestor(root)])
977 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +0800978
Jon Salz4712ac72013-02-07 17:12:05 +0800979 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +0800980 if root is None:
981 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +0800982 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +0800983 for f in root.walk():
984 if f.is_leaf():
985 f.update_state(status=TestState.UNTESTED)
986
Jon Salz6dc031d2013-06-19 13:06:23 +0800987 def abort_active_tests(self, reason=None):
988 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +0800989
990 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +0800991 syslog.openlog('goofy')
992
Jon Salz0697cbf2012-07-04 15:14:04 +0800993 try:
Jon Salzd7550792013-07-12 05:49:27 +0800994 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +0800995 self.init()
996 self.event_log.Log('goofy_init',
997 success=True)
998 except:
999 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001000 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001001 self.event_log.Log('goofy_init',
1002 success=False,
1003 trace=traceback.format_exc())
1004 except: # pylint: disable=W0702
1005 pass
1006 raise
1007
Jon Salzd7550792013-07-12 05:49:27 +08001008 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +08001009 syslog.syslog('Goofy (factory test harness) starting')
Jon Salz0697cbf2012-07-04 15:14:04 +08001010 self.run()
1011
1012 def update_system_info(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001013 """Updates system info."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001014 system_info = system.SystemInfo()
1015 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1016 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1017 system_info=system_info.__dict__))
1018 logging.info('System info: %r', system_info.__dict__)
1019
Jon Salzeb42f0d2012-07-27 19:14:04 +08001020 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001021 """Commences updating factory software.
Jon Salzeb42f0d2012-07-27 19:14:04 +08001022
1023 Args:
1024 auto_run_on_restart: Auto-run when the machine comes back up.
1025 post_update_hook: Code to call after update but immediately before
1026 restart.
1027
1028 Returns:
1029 Never if the update was successful (we just reboot).
1030 False if the update was unnecessary (no update available).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001031 """
Jon Salz6dc031d2013-06-19 13:06:23 +08001032 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +08001033 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001034
Jon Salz5c344f62012-07-13 14:31:16 +08001035 def pre_update_hook():
1036 if auto_run_on_restart:
1037 self.state_instance.set_shared_data('tests_after_shutdown',
1038 FORCE_AUTO_RUN)
1039 self.state_instance.close()
1040
Jon Salzeb42f0d2012-07-27 19:14:04 +08001041 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1042 if post_update_hook:
1043 post_update_hook()
1044 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001045
Ricky Liang8fecf412014-05-22 10:56:14 +08001046 def handle_sigint(self, dummy_signum, dummy_frame): # pylint: disable=W0613
Jon Salz77c151e2012-08-28 07:20:37 +08001047 logging.error('Received SIGINT')
1048 self.run_queue.put(None)
1049 raise KeyboardInterrupt()
1050
Ricky Liang8fecf412014-05-22 10:56:14 +08001051 def handle_sigterm(self, dummy_signum, dummy_frame): # pylint: disable=W0613
1052 logging.error('Received SIGTERM')
1053 if not utils.in_chroot():
1054 self.goofy_rpc.CloseGoofyTab()
1055 self.run_queue.put(None)
1056 raise RuntimeError('Received SIGTERM')
1057
Jon Salze12c2b32013-06-25 16:24:34 +08001058 def find_kcrashes(self):
1059 """Finds kcrash files, logs them, and marks them as seen."""
1060 seen_crashes = set(
1061 self.state_instance.get_shared_data('seen_crashes', optional=True)
1062 or [])
1063
1064 for path in glob.glob('/var/spool/crash/*'):
1065 if not os.path.isfile(path):
1066 continue
1067 if path in seen_crashes:
1068 continue
1069 try:
1070 stat = os.stat(path)
1071 mtime = utils.TimeString(stat.st_mtime)
1072 logging.info(
1073 'Found new crash file %s (%d bytes at %s)',
1074 path, stat.st_size, mtime)
1075 extra_log_args = {}
1076
1077 try:
1078 _, ext = os.path.splitext(path)
1079 if ext in ['.kcrash', '.meta']:
1080 ext = ext.replace('.', '')
1081 with open(path) as f:
1082 data = f.read(MAX_CRASH_FILE_SIZE)
1083 tell = f.tell()
1084 logging.info(
1085 'Contents of %s%s:%s',
1086 path,
1087 ('' if tell == stat.st_size
1088 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
1089 ('\n' + data).replace('\n', '\n ' + ext + '> '))
1090 extra_log_args['data'] = data
1091
1092 # Copy to /var/factory/kcrash for posterity
1093 kcrash_dir = factory.get_factory_root('kcrash')
1094 utils.TryMakeDirs(kcrash_dir)
1095 shutil.copy(path, kcrash_dir)
1096 logging.info('Copied to %s',
1097 os.path.join(kcrash_dir, os.path.basename(path)))
1098 finally:
1099 # Even if something goes wrong with the above, still try to
1100 # log to event log
1101 self.event_log.Log('crash_file',
1102 path=path, size=stat.st_size, mtime=mtime,
1103 **extra_log_args)
1104 except: # pylint: disable=W0702
1105 logging.exception('Unable to handle crash files %s', path)
1106 seen_crashes.add(path)
1107
1108 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
1109
Jon Salz128b0932013-07-03 16:55:26 +08001110 def GetTestList(self, test_list_id):
1111 """Returns the test list with the given ID.
1112
1113 Raises:
1114 TestListError: The test list ID is not valid.
1115 """
1116 try:
1117 return self.test_lists[test_list_id]
1118 except KeyError:
1119 raise test_lists.TestListError(
1120 '%r is not a valid test list ID (available IDs are [%s])' % (
1121 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
1122
1123 def InitTestLists(self):
1124 """Reads in all test lists and sets the active test list."""
Ricky Liang27051552014-05-04 14:22:26 +08001125 self.test_lists = test_lists.BuildAllTestLists(
1126 force_generic=(self.options.automation_mode is not None))
Jon Salzd7550792013-07-12 05:49:27 +08001127 logging.info('Loaded test lists: [%s]',
1128 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +08001129
1130 if not self.options.test_list:
1131 self.options.test_list = test_lists.GetActiveTestListId()
1132
1133 if os.sep in self.options.test_list:
1134 # It's a path pointing to an old-style test list; use it.
1135 self.test_list = factory.read_test_list(self.options.test_list)
1136 else:
1137 self.test_list = self.GetTestList(self.options.test_list)
1138
1139 logging.info('Active test list: %s', self.test_list.test_list_id)
1140
1141 if isinstance(self.test_list, test_lists.OldStyleTestList):
1142 # Actually load it in. (See OldStyleTestList for an explanation
1143 # of why this is necessary.)
1144 self.test_list = self.test_list.Load()
1145
1146 self.test_list.state_instance = self.state_instance
1147
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001148 def init_hooks(self):
1149 """Initializes hooks.
1150
1151 Must run after self.test_list ready.
1152 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001153 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
1154 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
1155 assert isinstance(self.hooks, factory.Hooks), (
1156 "hooks should be of type Hooks but is %r" % type(self.hooks))
1157 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001158 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001159
Jon Salz0697cbf2012-07-04 15:14:04 +08001160 def init(self, args=None, env=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001161 """Initializes Goofy.
Jon Salz0697cbf2012-07-04 15:14:04 +08001162
1163 Args:
1164 args: A list of command-line arguments. Uses sys.argv if
1165 args is None.
1166 env: An Environment instance to use (or None to choose
1167 FakeChrootEnvironment or DUTEnvironment as appropriate).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001168 """
Jon Salz77c151e2012-08-28 07:20:37 +08001169 signal.signal(signal.SIGINT, self.handle_sigint)
Ricky Liang8fecf412014-05-22 10:56:14 +08001170 signal.signal(signal.SIGTERM, self.handle_sigterm)
Jon Salz77c151e2012-08-28 07:20:37 +08001171
Jon Salz0697cbf2012-07-04 15:14:04 +08001172 parser = OptionParser()
1173 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001174 action='store_true',
1175 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001176 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001177 metavar='FILE',
1178 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001179 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001180 action='store_true',
1181 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001182 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz8fa8e832012-07-13 19:04:09 +08001183 choices=['none', 'gtk', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001184 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001185 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001186 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001187 type='int', default=1,
1188 help=('Factor by which to scale UI '
1189 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001190 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001191 metavar='FILE',
1192 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001193 parser.add_option('--dummy_shopfloor', action='store_true',
1194 help='Use a dummy shopfloor server')
Ricky Liang6fe218c2013-12-27 15:17:17 +08001195 parser.add_option('--automation-mode',
1196 choices=[m.lower() for m in AutomationMode],
1197 default='none', help="Factory test automation mode.")
Ricky Liang117484a2014-04-14 11:14:41 +08001198 parser.add_option('--no-auto-run-on-start', dest='auto_run_on_start',
1199 action='store_false', default=True,
1200 help=('do not automatically run the test list on goofy '
1201 'start; this is only valid when factory test '
1202 'automation is enabled'))
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001203 parser.add_option('--guest_login', dest='guest_login', default=False,
Ricky Liangb2432362013-10-02 13:12:41 +08001204 action='store_true',
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001205 help='Log in as guest. This will not own the TPM.')
Jon Salz0697cbf2012-07-04 15:14:04 +08001206 (self.options, self.args) = parser.parse_args(args)
1207
Jon Salz46b89562012-07-05 11:49:22 +08001208 # Make sure factory directories exist.
1209 factory.get_log_root()
1210 factory.get_state_root()
1211 factory.get_test_data_root()
1212
Jon Salz0697cbf2012-07-04 15:14:04 +08001213 global _inited_logging # pylint: disable=W0603
1214 if not _inited_logging:
1215 factory.init_logging('goofy', verbose=self.options.verbose)
1216 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001217
Jon Salz0f996602012-10-03 15:26:48 +08001218 if self.options.print_test_list:
1219 print factory.read_test_list(
1220 self.options.print_test_list).__repr__(recursive=True)
1221 sys.exit(0)
1222
Jon Salzee85d522012-07-17 14:34:46 +08001223 event_log.IncrementBootSequence()
Jon Salzd15bbcf2013-05-21 17:33:57 +08001224 # Don't defer logging the initial event, so we can make sure
1225 # that device_id, reimage_id, etc. are all set up.
1226 self.event_log = EventLog('goofy', defer=False)
Jon Salz0697cbf2012-07-04 15:14:04 +08001227
1228 if (not suppress_chroot_warning and
1229 factory.in_chroot() and
1230 self.options.ui == 'gtk' and
1231 os.environ.get('DISPLAY') in [None, '', ':0', ':0.0']):
1232 # That's not going to work! Tell the user how to run
1233 # this way.
1234 logging.warn(GOOFY_IN_CHROOT_WARNING)
1235 time.sleep(1)
1236
1237 if env:
1238 self.env = env
1239 elif factory.in_chroot():
1240 self.env = test_environment.FakeChrootEnvironment()
1241 logging.warn(
1242 'Using chroot environment: will not actually run autotests')
1243 else:
Ricky Liang8c2c6c32013-11-02 23:02:44 +08001244 if self.options.guest_login:
1245 os.mknod(test_environment.DUTEnvironment.GUEST_MODE_TAG_FILE)
1246 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001247 self.env.goofy = self
1248
1249 if self.options.restart:
1250 state.clear_state()
1251
Jon Salz0697cbf2012-07-04 15:14:04 +08001252 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1253 logging.warn(
1254 'In QEMU; ignoring ui_scale_factor argument')
1255 self.options.ui_scale_factor = 1
1256
1257 logging.info('Started')
1258
1259 self.start_state_server()
1260 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1261 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001262 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001263 self.last_shutdown_time = (
1264 self.state_instance.get_shared_data('shutdown_time', optional=True))
1265 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001266 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001267
Ricky Liang6fe218c2013-12-27 15:17:17 +08001268 self.options.automation_mode = ParseAutomationMode(
1269 self.options.automation_mode)
1270 self.state_instance.set_shared_data('automation_mode',
1271 self.options.automation_mode)
1272 self.state_instance.set_shared_data(
1273 'automation_mode_prompt',
1274 AutomationModePrompt[self.options.automation_mode])
1275
Jon Salz128b0932013-07-03 16:55:26 +08001276 try:
1277 self.InitTestLists()
1278 except: # pylint: disable=W0702
1279 logging.exception('Unable to initialize test lists')
1280 self.state_instance.set_shared_data(
1281 'startup_error',
1282 'Unable to initialize test lists\n%s' % (
1283 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001284 if self.options.ui == 'chrome':
1285 # Create an empty test list with default options so that the rest of
1286 # startup can proceed.
1287 self.test_list = factory.FactoryTestList(
1288 [], self.state_instance, factory.Options())
1289 else:
1290 # Bail with an error; no point in starting up.
1291 sys.exit('No valid test list; exiting.')
1292
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001293 self.init_hooks()
1294
Jon Salz822838b2013-03-25 17:32:33 +08001295 if self.test_list.options.clear_state_on_start:
1296 self.state_instance.clear_test_state()
1297
Jon Salz670ce062014-05-16 15:53:50 +08001298 # If the phase is invalid, this will raise a ValueError.
1299 phase.SetPersistentPhase(self.test_list.options.phase)
1300
Vic Yang3e1cf5d2013-06-05 18:50:24 +08001301 if system.SystemInfo().firmware_version is None and not utils.in_chroot():
Vic Yang9bd4f772013-06-04 17:34:00 +08001302 self.state_instance.set_shared_data('startup_error',
1303 'Netboot firmware detected\n'
1304 'Connect Ethernet and reboot to re-image.\n'
1305 u'侦测到网路开机固件\n'
1306 u'请连接乙太网并重启')
1307
Jon Salz0697cbf2012-07-04 15:14:04 +08001308 if not self.state_instance.has_shared_data('ui_lang'):
1309 self.state_instance.set_shared_data('ui_lang',
1310 self.test_list.options.ui_lang)
1311 self.state_instance.set_shared_data(
1312 'test_list_options',
1313 self.test_list.options.__dict__)
1314 self.state_instance.test_list = self.test_list
1315
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001316 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001317
Jon Salz23926422012-09-01 03:38:13 +08001318 if self.options.dummy_shopfloor:
1319 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1320 'http://localhost:%d/' % shopfloor.DEFAULT_SERVER_PORT)
1321 self.dummy_shopfloor = Spawn(
1322 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1323 '--dummy'])
1324 elif self.test_list.options.shopfloor_server_url:
1325 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001326 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001327
Jon Salz0f996602012-10-03 15:26:48 +08001328 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001329 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1330 base_time=time_sanitizer.GetBaseTimeFromFile(
1331 # lsb-factory is written by the factory install shim during
1332 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001333 # the mini-Omaha server. If it's not available, we'll use
1334 # /etc/lsb-factory (which will be much older, but reasonably
1335 # sane) and rely on a shopfloor sync to set a more accurate
1336 # time.
1337 '/usr/local/etc/lsb-factory',
1338 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001339 self.time_sanitizer.RunOnce()
1340
Vic Yangd8990da2013-06-27 16:57:43 +08001341 if self.test_list.options.check_cpu_usage_period_secs:
1342 self.cpu_usage_watcher = Spawn(['py/tools/cpu_usage_monitor.py',
1343 '-p', str(self.test_list.options.check_cpu_usage_period_secs)],
1344 cwd=factory.FACTORY_PATH)
1345
Jon Salz0697cbf2012-07-04 15:14:04 +08001346 self.init_states()
1347 self.start_event_server()
1348 self.connection_manager = self.env.create_connection_manager(
Tai-Hsu Lin371351a2012-08-27 14:17:14 +08001349 self.test_list.options.wlans,
1350 self.test_list.options.scan_wifi_period_secs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001351 # Note that we create a log watcher even if
1352 # sync_event_log_period_secs isn't set (no background
1353 # syncing), since we may use it to flush event logs as well.
1354 self.log_watcher = EventLogWatcher(
1355 self.test_list.options.sync_event_log_period_secs,
Jon Salzd15bbcf2013-05-21 17:33:57 +08001356 event_log_db_file=None,
Jon Salz16d10542012-07-23 12:18:45 +08001357 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001358 if self.test_list.options.sync_event_log_period_secs:
1359 self.log_watcher.StartWatchThread()
1360
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001361 # Creates a system log manager to scan logs periocially.
1362 # A scan includes clearing logs and optionally syncing logs if
1363 # enable_syng_log is True. We kick it to sync logs.
1364 self.system_log_manager = SystemLogManager(
1365 sync_log_paths=self.test_list.options.sync_log_paths,
1366 sync_log_period_secs=self.test_list.options.sync_log_period_secs,
1367 scan_log_period_secs=self.test_list.options.scan_log_period_secs,
Cheng-Yi Chiangb8a491c2014-01-20 14:37:57 +08001368 clear_log_paths=self.test_list.options.clear_log_paths,
1369 clear_log_excluded_paths=self.test_list.options.clear_log_excluded_paths)
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001370 self.system_log_manager.Start()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001371
Jon Salz0697cbf2012-07-04 15:14:04 +08001372 self.update_system_info()
1373
Vic Yang4953fc12012-07-26 16:19:53 +08001374 assert ((self.test_list.options.min_charge_pct is None) ==
1375 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001376 if utils.in_chroot():
1377 logging.info('In chroot, ignoring charge manager and charge state')
1378 elif self.test_list.options.min_charge_pct is not None:
Vic Yang4953fc12012-07-26 16:19:53 +08001379 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1380 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001381 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001382 else:
1383 # Goofy should set charger state to charge if charge_manager is disabled.
1384 try:
1385 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
1386 except BoardException:
1387 logging.exception('Unable to set charge state on this board')
Vic Yang4953fc12012-07-26 16:19:53 +08001388
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001389 self.core_dump_manager = CoreDumpManager(
1390 self.test_list.options.core_dump_watchlist)
1391
Jon Salz0697cbf2012-07-04 15:14:04 +08001392 os.environ['CROS_FACTORY'] = '1'
1393 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1394
1395 # Set CROS_UI since some behaviors in ui.py depend on the
1396 # particular UI in use. TODO(jsalz): Remove this (and all
1397 # places it is used) when the GTK UI is removed.
1398 os.environ['CROS_UI'] = self.options.ui
1399
Shuo-Peng Liao1ff502e2013-06-30 18:37:02 +08001400 if not utils.in_chroot() and self.test_list.options.use_cpufreq_manager:
Jon Salzddf0d052013-06-18 12:52:44 +08001401 self.cpufreq_manager = CpufreqManager(event_log=self.event_log)
Jon Salzce6a7f82013-06-10 18:22:54 +08001402
Justin Chuang31b02432013-06-27 15:16:51 +08001403 # Startup hooks may want to skip some tests.
1404 self.update_skipped_tests()
Jon Salz416f9cc2013-05-10 18:32:50 +08001405
Jon Salze12c2b32013-06-25 16:24:34 +08001406 self.find_kcrashes()
1407
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001408 # Should not move earlier.
1409 self.hooks.OnStartup()
1410
Jon Salz0697cbf2012-07-04 15:14:04 +08001411 if self.options.ui == 'chrome':
1412 self.env.launch_chrome()
1413 logging.info('Waiting for a web socket connection')
Cheng-Yi Chiangfd8ed392013-03-08 21:37:31 +08001414 self.web_socket_manager.wait()
Jon Salz0697cbf2012-07-04 15:14:04 +08001415
1416 # Wait for the test widget size to be set; this is done in
1417 # an asynchronous RPC so there is a small chance that the
1418 # web socket might be opened first.
1419 for _ in range(100): # 10 s
1420 try:
1421 if self.state_instance.get_shared_data('test_widget_size'):
1422 break
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001423 except KeyError:
Jon Salz0697cbf2012-07-04 15:14:04 +08001424 pass # Retry
1425 time.sleep(0.1) # 100 ms
1426 else:
1427 logging.warn('Never received test_widget_size from UI')
Jon Salz45297282013-05-18 14:31:47 +08001428
1429 # Send Chrome a Tab to get focus to the factory UI
1430 # (http://crosbug.com/p/19444). TODO(jsalz): remove this hack
1431 # and figure out the right way to get the focus to Chrome.
1432 if not utils.in_chroot():
Ricky Liangb97f3652013-08-20 17:30:28 +08001433 utils.SendKey('Tab')
Jon Salz0697cbf2012-07-04 15:14:04 +08001434 elif self.options.ui == 'gtk':
1435 self.start_ui()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001436
Ricky Liang650f6bf2012-09-28 13:22:54 +08001437 # Create download path for autotest beforehand or autotests run at
1438 # the same time might fail due to race condition.
1439 if not factory.in_chroot():
1440 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1441 'download'))
1442
Jon Salz0697cbf2012-07-04 15:14:04 +08001443 def state_change_callback(test, test_state):
1444 self.event_client.post_event(
Ricky Liang4bff3e32014-02-20 18:46:11 +08001445 Event(Event.Type.STATE_CHANGE, path=test.path, state=test_state))
Jon Salz0697cbf2012-07-04 15:14:04 +08001446 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001447
Jon Salza6711d72012-07-18 14:33:03 +08001448 for handler in self.on_ui_startup:
1449 handler()
1450
1451 self.prespawner = Prespawner()
1452 self.prespawner.start()
1453
Ricky Liang48e47f92014-02-26 19:31:51 +08001454 tests_after_shutdown = self.state_instance.get_shared_data(
1455 'tests_after_shutdown', optional=True)
Jon Salz57717ca2012-04-04 16:47:25 +08001456
Jon Salz5c344f62012-07-13 14:31:16 +08001457 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1458 if not force_auto_run and tests_after_shutdown is not None:
Ricky Liang48e47f92014-02-26 19:31:51 +08001459 logging.info('Resuming tests after shutdown: %s', tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001460 self.tests_to_run.extend(
Ricky Liang4bff3e32014-02-20 18:46:11 +08001461 self.test_list.lookup_path(t) for t in tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001462 self.run_queue.put(self.run_next_test)
1463 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001464 if force_auto_run or self.test_list.options.auto_run_on_start:
Ricky Liang117484a2014-04-14 11:14:41 +08001465 # If automation mode is enabled, allow suppress auto_run_on_start.
1466 if (self.options.automation_mode == 'NONE' or
1467 self.options.auto_run_on_start):
1468 self.run_queue.put(
1469 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001470 self.state_instance.set_shared_data('tests_after_shutdown', None)
Ricky Liang4bff3e32014-02-20 18:46:11 +08001471 self.restore_active_run_state()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001472
Dean Liao592e4d52013-01-10 20:06:39 +08001473 self.may_disable_cros_shortcut_keys()
1474
1475 def may_disable_cros_shortcut_keys(self):
1476 test_options = self.test_list.options
1477 if test_options.disable_cros_shortcut_keys:
1478 logging.info('Filter ChromeOS shortcut keys.')
1479 self.key_filter = KeyFilter(
1480 unmap_caps_lock=test_options.disable_caps_lock,
1481 caps_lock_keycode=test_options.caps_lock_keycode)
1482 self.key_filter.Start()
1483
Jon Salz0697cbf2012-07-04 15:14:04 +08001484 def run(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001485 """Runs Goofy."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001486 # Process events forever.
1487 while self.run_once(True):
1488 pass
Jon Salz73e0fd02012-04-04 11:46:38 +08001489
Jon Salz0697cbf2012-07-04 15:14:04 +08001490 def run_once(self, block=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001491 """Runs all items pending in the event loop.
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001492
Jon Salz0697cbf2012-07-04 15:14:04 +08001493 Args:
1494 block: If true, block until at least one event is processed.
Jon Salz7c15e8b2012-06-19 17:10:37 +08001495
Jon Salz0697cbf2012-07-04 15:14:04 +08001496 Returns:
1497 True to keep going or False to shut down.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001498 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001499 events = utils.DrainQueue(self.run_queue)
cychiang21886742012-07-05 15:16:32 +08001500 while not events:
Jon Salz0697cbf2012-07-04 15:14:04 +08001501 # Nothing on the run queue.
1502 self._run_queue_idle()
1503 if block:
1504 # Block for at least one event...
cychiang21886742012-07-05 15:16:32 +08001505 try:
1506 events.append(self.run_queue.get(timeout=RUN_QUEUE_TIMEOUT_SECS))
1507 except Queue.Empty:
1508 # Keep going (calling _run_queue_idle() again at the top of
1509 # the loop)
1510 continue
Jon Salz0697cbf2012-07-04 15:14:04 +08001511 # ...and grab anything else that showed up at the same
1512 # time.
1513 events.extend(utils.DrainQueue(self.run_queue))
cychiang21886742012-07-05 15:16:32 +08001514 else:
1515 break
Jon Salz51528e12012-07-02 18:54:45 +08001516
Jon Salz0697cbf2012-07-04 15:14:04 +08001517 for event in events:
1518 if not event:
1519 # Shutdown request.
1520 self.run_queue.task_done()
1521 return False
Jon Salz51528e12012-07-02 18:54:45 +08001522
Jon Salz0697cbf2012-07-04 15:14:04 +08001523 try:
1524 event()
Jon Salz85a39882012-07-05 16:45:04 +08001525 except: # pylint: disable=W0702
1526 logging.exception('Error in event loop')
Jon Salz0697cbf2012-07-04 15:14:04 +08001527 self.record_exception(traceback.format_exception_only(
1528 *sys.exc_info()[:2]))
1529 # But keep going
1530 finally:
1531 self.run_queue.task_done()
1532 return True
Jon Salz0405ab52012-03-16 15:26:52 +08001533
Jon Salz0e6532d2012-10-25 16:30:11 +08001534 def _should_sync_time(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001535 """Returns True if we should attempt syncing time with shopfloor.
Jon Salz0e6532d2012-10-25 16:30:11 +08001536
1537 Args:
1538 foreground: If True, synchronizes even if background syncing
1539 is disabled (e.g., in explicit sync requests from the
1540 SyncShopfloor test).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001541 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001542 return ((foreground or
1543 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001544 self.time_sanitizer and
1545 (not self.time_synced) and
1546 (not factory.in_chroot()))
1547
Jon Salz0e6532d2012-10-25 16:30:11 +08001548 def sync_time_with_shopfloor_server(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001549 """Syncs time with shopfloor server, if not yet synced.
Jon Salz54882d02012-08-31 01:57:54 +08001550
Jon Salz0e6532d2012-10-25 16:30:11 +08001551 Args:
1552 foreground: If True, synchronizes even if background syncing
1553 is disabled (e.g., in explicit sync requests from the
1554 SyncShopfloor test).
1555
Jon Salz54882d02012-08-31 01:57:54 +08001556 Returns:
1557 False if no time sanitizer is available, or True if this sync (or a
1558 previous sync) succeeded.
1559
1560 Raises:
1561 Exception if unable to contact the shopfloor server.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001562 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001563 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001564 self.time_sanitizer.SyncWithShopfloor()
1565 self.time_synced = True
1566 return self.time_synced
1567
Jon Salzb92c5112012-09-21 15:40:11 +08001568 def log_disk_space_stats(self):
Jon Salz18e0e022013-06-11 17:13:39 +08001569 if (utils.in_chroot() or
1570 not self.test_list.options.log_disk_space_period_secs):
Jon Salzb92c5112012-09-21 15:40:11 +08001571 return
1572
1573 now = time.time()
1574 if (self.last_log_disk_space_time and
1575 now - self.last_log_disk_space_time <
1576 self.test_list.options.log_disk_space_period_secs):
1577 return
1578 self.last_log_disk_space_time = now
1579
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001580 # Upload event if stateful partition usage is above threshold.
1581 # Stateful partition is mounted on /usr/local, while
1582 # encrypted stateful partition is mounted on /var.
1583 # If there are too much logs in the factory process,
1584 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001585 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001586 vfs_infos = disk_space.GetAllVFSInfo()
1587 stateful_info, encrypted_info = None, None
1588 for vfs_info in vfs_infos.values():
1589 if '/usr/local' in vfs_info.mount_points:
1590 stateful_info = vfs_info
1591 if '/var' in vfs_info.mount_points:
1592 encrypted_info = vfs_info
1593
1594 stateful = disk_space.GetPartitionUsage(stateful_info)
1595 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1596
1597 above_threshold = (
1598 self.test_list.options.stateful_usage_threshold and
1599 max(stateful.bytes_used_pct,
1600 stateful.inodes_used_pct,
1601 encrypted.bytes_used_pct,
1602 encrypted.inodes_used_pct) >
1603 self.test_list.options.stateful_usage_threshold)
1604
1605 if above_threshold:
1606 self.event_log.Log('stateful_partition_usage',
1607 partitions={
1608 'stateful': {
1609 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1610 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1611 'encrypted_stateful': {
1612 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1613 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1614 })
1615 self.log_watcher.ScanEventLogs()
Cheng-Yi Chiang00798e72013-06-20 18:16:39 +08001616 if (not utils.in_chroot() and
1617 self.test_list.options.stateful_usage_above_threshold_action):
1618 Spawn(self.test_list.options.stateful_usage_above_threshold_action,
1619 call=True)
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001620
1621 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001622 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001623 if above_threshold:
1624 logging.warning(message)
1625 else:
1626 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001627 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001628 except: # pylint: disable=W0702
1629 logging.exception('Unable to get disk space used')
1630
Justin Chuang83813982013-05-13 01:26:32 +08001631 def check_battery(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001632 """Checks the current battery status.
Justin Chuang83813982013-05-13 01:26:32 +08001633
1634 Logs current battery charging level and status to log. If the battery level
1635 is lower below warning_low_battery_pct, send warning event to shopfloor.
1636 If the battery level is lower below critical_low_battery_pct, flush disks.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001637 """
Justin Chuang83813982013-05-13 01:26:32 +08001638 if not self.test_list.options.check_battery_period_secs:
1639 return
1640
1641 now = time.time()
1642 if (self.last_check_battery_time and
1643 now - self.last_check_battery_time <
1644 self.test_list.options.check_battery_period_secs):
1645 return
1646 self.last_check_battery_time = now
1647
1648 message = ''
1649 log_level = logging.INFO
1650 try:
1651 power = system.GetBoard().power
1652 if not power.CheckBatteryPresent():
1653 message = 'Battery is not present'
1654 else:
1655 ac_present = power.CheckACPresent()
1656 charge_pct = power.GetChargePct(get_float=True)
1657 message = ('Current battery level %.1f%%, AC charger is %s' %
1658 (charge_pct, 'connected' if ac_present else 'disconnected'))
1659
1660 if charge_pct > self.test_list.options.critical_low_battery_pct:
1661 critical_low_battery = False
1662 else:
1663 critical_low_battery = True
1664 # Only sync disks when battery level is still above minimum
1665 # value. This can be used for offline analysis when shopfloor cannot
1666 # be connected.
1667 if charge_pct > MIN_BATTERY_LEVEL_FOR_DISK_SYNC:
1668 logging.warning('disk syncing for critical low battery situation')
1669 os.system('sync; sync; sync')
1670 else:
1671 logging.warning('disk syncing is cancelled '
1672 'because battery level is lower than %.1f',
1673 MIN_BATTERY_LEVEL_FOR_DISK_SYNC)
1674
1675 # Notify shopfloor server
1676 if (critical_low_battery or
1677 (not ac_present and
1678 charge_pct <= self.test_list.options.warning_low_battery_pct)):
1679 log_level = logging.WARNING
1680
1681 self.event_log.Log('low_battery',
1682 battery_level=charge_pct,
1683 charger_connected=ac_present,
1684 critical=critical_low_battery)
1685 self.log_watcher.KickWatchThread()
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001686 if self.test_list.options.enable_sync_log:
1687 self.system_log_manager.KickToSync()
Justin Chuang83813982013-05-13 01:26:32 +08001688 except: # pylint: disable=W0702
1689 logging.exception('Unable to check battery or notify shopfloor')
1690 finally:
1691 if message != self.last_check_battery_message:
1692 logging.log(log_level, message)
1693 self.last_check_battery_message = message
1694
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001695 def check_core_dump(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001696 """Checks if there is any core dumped file.
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001697
1698 Removes unwanted core dump files immediately.
1699 Syncs those files matching watch list to server with a delay between
1700 each sync. After the files have been synced to server, deletes the files.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001701 """
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001702 core_dump_files = self.core_dump_manager.ScanFiles()
1703 if core_dump_files:
1704 now = time.time()
1705 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1706 self.test_list.options.kick_sync_min_interval_secs):
1707 return
1708 self.last_kick_sync_time = now
1709
1710 # Sends event to server
1711 self.event_log.Log('core_dumped', files=core_dump_files)
1712 self.log_watcher.KickWatchThread()
1713
1714 # Syncs files to server
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001715 if self.test_list.options.enable_sync_log:
1716 self.system_log_manager.KickToSync(
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001717 core_dump_files, self.core_dump_manager.ClearFiles)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001718
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001719 def check_log_rotation(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001720 """Checks log rotation file presence/absence according to test_list option.
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001721
1722 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1723 is True, delete it otherwise. This must be done in idle loop because
1724 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1725 an autotest.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001726 """
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001727 if utils.in_chroot():
1728 return
1729 try:
1730 if self.test_list.options.disable_log_rotation:
1731 open(CLEANUP_LOGS_PAUSED, 'w').close()
1732 else:
1733 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1734 except: # pylint: disable=W0702
1735 # Oh well. Logs an error (but no trace)
1736 logging.info(
1737 'Unable to %s %s: %s',
1738 'touch' if self.test_list.options.disable_log_rotation else 'delete',
1739 CLEANUP_LOGS_PAUSED, utils.FormatExceptionOnly())
1740
Jon Salz8fa8e832012-07-13 19:04:09 +08001741 def sync_time_in_background(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001742 """Writes out current time and tries to sync with shopfloor server."""
Jon Salzb22d1172012-08-06 10:38:57 +08001743 if not self.time_sanitizer:
1744 return
1745
1746 # Write out the current time.
1747 self.time_sanitizer.SaveTime()
1748
Jon Salz54882d02012-08-31 01:57:54 +08001749 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001750 return
1751
1752 now = time.time()
1753 if self.last_sync_time and (
1754 now - self.last_sync_time <
1755 self.test_list.options.sync_time_period_secs):
1756 # Not yet time for another check.
1757 return
1758 self.last_sync_time = now
1759
1760 def target():
1761 try:
Jon Salz54882d02012-08-31 01:57:54 +08001762 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001763 except: # pylint: disable=W0702
1764 # Oh well. Log an error (but no trace)
1765 logging.info(
1766 'Unable to get time from shopfloor server: %s',
1767 utils.FormatExceptionOnly())
1768
1769 thread = threading.Thread(target=target)
1770 thread.daemon = True
1771 thread.start()
1772
Jon Salz0697cbf2012-07-04 15:14:04 +08001773 def _run_queue_idle(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001774 """Invoked when the run queue has no events.
Vic Yang4953fc12012-07-26 16:19:53 +08001775
1776 This method must not raise exception.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001777 """
Jon Salzb22d1172012-08-06 10:38:57 +08001778 now = time.time()
1779 if (self.last_idle and
1780 now < (self.last_idle + RUN_QUEUE_TIMEOUT_SECS - 1)):
1781 # Don't run more often than once every (RUN_QUEUE_TIMEOUT_SECS -
1782 # 1) seconds.
1783 return
1784
1785 self.last_idle = now
1786
Vic Yang311ddb82012-09-26 12:08:28 +08001787 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001788 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001789 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001790 self.log_disk_space_stats()
Justin Chuang83813982013-05-13 01:26:32 +08001791 self.check_battery()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001792 self.check_core_dump()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001793 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001794
Jon Salzd15bbcf2013-05-21 17:33:57 +08001795 def handle_event_logs(self, chunks):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001796 """Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001797
Jon Salz0697cbf2012-07-04 15:14:04 +08001798 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001799
1800 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001801 chunks: A list of Chunk objects.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001802 """
Vic Yang93027612013-05-06 02:42:49 +08001803 first_exception = None
1804 exception_count = 0
1805
Jon Salzd15bbcf2013-05-21 17:33:57 +08001806 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001807 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001808 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001809 start_time = time.time()
1810 shopfloor_client = shopfloor.get_instance(
1811 detect=True,
1812 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzd15bbcf2013-05-21 17:33:57 +08001813 shopfloor_client.UploadEvent(chunk.log_name + "." +
1814 event_log.GetReimageId(),
1815 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001816 logging.info(
1817 'Successfully synced %s in %.03f s',
1818 description, time.time() - start_time)
1819 except: # pylint: disable=W0702
Jon Salzd15bbcf2013-05-21 17:33:57 +08001820 first_exception = (first_exception or (chunk.log_name + ': ' +
Vic Yang93027612013-05-06 02:42:49 +08001821 utils.FormatExceptionOnly()))
1822 exception_count += 1
1823
1824 if exception_count:
1825 if exception_count == 1:
1826 msg = 'Log upload failed: %s' % first_exception
1827 else:
1828 msg = '%d log upload failed; first is: %s' % (
1829 exception_count, first_exception)
1830 raise Exception(msg)
1831
Jon Salz57717ca2012-04-04 16:47:25 +08001832
Jon Salz0697cbf2012-07-04 15:14:04 +08001833 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1834 root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001835 """Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001836
Jon Salz0697cbf2012-07-04 15:14:04 +08001837 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001838
Jon Salz0697cbf2012-07-04 15:14:04 +08001839 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001840 statuses_to_run: The particular status that caller wants to run.
Jon Salz0697cbf2012-07-04 15:14:04 +08001841 starting_at: If provided, only auto-runs tests beginning with
1842 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001843 root: The root of tests to run. If not provided, it will be
1844 the root of all tests.
1845 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001846 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001847
Jon Salz0697cbf2012-07-04 15:14:04 +08001848 if starting_at:
1849 # Make sure they passed a test, not a string.
1850 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001851
Jon Salz0697cbf2012-07-04 15:14:04 +08001852 tests_to_reset = []
1853 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001854
Jon Salz0697cbf2012-07-04 15:14:04 +08001855 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001856
Jon Salz0697cbf2012-07-04 15:14:04 +08001857 for test in root.get_top_level_tests():
1858 if starting_at:
1859 if test == starting_at:
1860 # We've found starting_at; do auto-run on all
1861 # subsequent tests.
1862 found_starting_at = True
1863 if not found_starting_at:
1864 # Don't start this guy yet
1865 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001866
Jon Salz0697cbf2012-07-04 15:14:04 +08001867 status = test.get_state().status
1868 if status == TestState.ACTIVE or status in statuses_to_run:
1869 # Reset the test (later; we will need to abort
1870 # all active tests first).
1871 tests_to_reset.append(test)
1872 if status in statuses_to_run:
1873 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001874
Jon Salz6dc031d2013-06-19 13:06:23 +08001875 self.abort_active_tests('Operator requested run/re-run of certain tests')
Jon Salz258a40c2012-04-19 12:34:01 +08001876
Jon Salz0697cbf2012-07-04 15:14:04 +08001877 # Reset all statuses of the tests to run (in case any tests were active;
1878 # we want them to be run again).
1879 for test_to_reset in tests_to_reset:
1880 for test in test_to_reset.walk():
1881 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001882
Jon Salz0697cbf2012-07-04 15:14:04 +08001883 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001884
Jon Salz0697cbf2012-07-04 15:14:04 +08001885 def restart_tests(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001886 """Restarts all tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001887 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001888
Jon Salz6dc031d2013-06-19 13:06:23 +08001889 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001890 for test in root.walk():
Ricky Liang48e47f92014-02-26 19:31:51 +08001891 test.update_state(status=TestState.UNTESTED, shutdown_count=0)
Jon Salz0697cbf2012-07-04 15:14:04 +08001892 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001893
Jon Salz0697cbf2012-07-04 15:14:04 +08001894 def auto_run(self, starting_at=None, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001895 """"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001896
Jon Salz0697cbf2012-07-04 15:14:04 +08001897 Args:
1898 starting_at: If provide, only auto-runs tests beginning with
1899 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001900 root: If provided, the root of tests to run. If not provided, the root
1901 will be test_list (root of all tests).
1902 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001903 root = root or self.test_list
1904 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1905 starting_at=starting_at,
1906 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001907
Jon Salz0697cbf2012-07-04 15:14:04 +08001908 def re_run_failed(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001909 """Re-runs failed tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001910 root = root or self.test_list
1911 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001912
Jon Salz0697cbf2012-07-04 15:14:04 +08001913 def show_review_information(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001914 """Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001915
Jon Salz0697cbf2012-07-04 15:14:04 +08001916 The information screene is rendered by main UI program (ui.py), so in
1917 goofy we only need to kill all active tests, set them as untested, and
1918 clear remaining tests.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001919 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001920 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001921 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001922
Jon Salz0697cbf2012-07-04 15:14:04 +08001923 def handle_switch_test(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001924 """Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001925
Ricky Liang6fe218c2013-12-27 15:17:17 +08001926 Args:
1927 event: The SWITCH_TEST event.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001928 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001929 test = self.test_list.lookup_path(event.path)
1930 if not test:
1931 logging.error('Unknown test %r', event.key)
1932 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001933
Jon Salz0697cbf2012-07-04 15:14:04 +08001934 invoc = self.invocations.get(test)
1935 if invoc and test.backgroundable:
1936 # Already running: just bring to the front if it
1937 # has a UI.
1938 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001939 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001940 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001941
Jon Salz6dc031d2013-06-19 13:06:23 +08001942 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001943 for t in test.walk():
1944 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001945
Jon Salz0697cbf2012-07-04 15:14:04 +08001946 if self.test_list.options.auto_run_on_keypress:
1947 self.auto_run(starting_at=test)
1948 else:
1949 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001950
Jon Salz0697cbf2012-07-04 15:14:04 +08001951 def wait(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001952 """Waits for all pending invocations.
Jon Salz0697cbf2012-07-04 15:14:04 +08001953
1954 Useful for testing.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001955 """
Jon Salz1acc8742012-07-17 17:45:55 +08001956 while self.invocations:
1957 for k, v in self.invocations.iteritems():
1958 logging.info('Waiting for %s to complete...', k)
1959 v.thread.join()
1960 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001961
1962 def check_exceptions(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001963 """Raises an error if any exceptions have occurred in
1964 invocation threads.
1965 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001966 if self.exceptions:
1967 raise RuntimeError('Exception in invocation thread: %r' %
1968 self.exceptions)
1969
1970 def record_exception(self, msg):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001971 """Records an exception in an invocation thread.
Jon Salz0697cbf2012-07-04 15:14:04 +08001972
1973 An exception with the given message will be rethrown when
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001974 Goofy is destroyed.
1975 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001976 self.exceptions.append(msg)
Jon Salz73e0fd02012-04-04 11:46:38 +08001977
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001978
1979if __name__ == '__main__':
Jon Salz77c151e2012-08-28 07:20:37 +08001980 goofy = Goofy()
1981 try:
1982 goofy.main()
Jon Salz0f996602012-10-03 15:26:48 +08001983 except SystemExit:
1984 # Propagate SystemExit without logging.
1985 raise
Jon Salz31373eb2012-09-21 16:19:49 +08001986 except:
Jon Salz0f996602012-10-03 15:26:48 +08001987 # Log the error before trying to shut down (unless it's a graceful
1988 # exit).
Jon Salz31373eb2012-09-21 16:19:49 +08001989 logging.exception('Error in main loop')
1990 raise
Jon Salz77c151e2012-08-28 07:20:37 +08001991 finally:
1992 goofy.destroy()