blob: f16abe918b4a9b3b016dfbaf4f6225bbe0cc7212 [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
Joel Kitchingb85ed7f2014-10-08 18:24:39 +080010from __future__ import print_function
11
Jon Salze12c2b32013-06-25 16:24:34 +080012import glob
Jon Salz0405ab52012-03-16 15:26:52 +080013import logging
14import os
Jon Salze12c2b32013-06-25 16:24:34 +080015import shutil
Jon Salz77c151e2012-08-28 07:20:37 +080016import signal
Jon Salz0405ab52012-03-16 15:26:52 +080017import sys
Jon Salzeff94182013-06-19 15:06:28 +080018import syslog
Jon Salz0405ab52012-03-16 15:26:52 +080019import threading
20import time
21import traceback
Jon Salz258a40c2012-04-19 12:34:01 +080022import uuid
Jon Salzb10cf512012-08-09 17:29:21 +080023from xmlrpclib import Binary
Hung-Te Linf2f78f72012-02-08 19:27:11 +080024from collections import deque
25from optparse import OptionParser
Hung-Te Linf2f78f72012-02-08 19:27:11 +080026
Jon Salz0697cbf2012-07-04 15:14:04 +080027import factory_common # pylint: disable=W0611
jcliangcd688182012-08-20 21:01:26 +080028from cros.factory import event_log
29from cros.factory import system
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +080030from cros.factory.event_log import EventLog, FloatDigit
Tom Wai-Hong Tamd33723e2013-04-10 21:14:37 +080031from cros.factory.event_log_watcher import EventLogWatcher
Hung-Te Lincc41d2a2014-10-29 13:35:20 +080032from cros.factory.goofy import connection_manager
Vic Yangd80ea752014-09-24 16:07:14 +080033from cros.factory.goofy import test_environment
34from cros.factory.goofy import time_sanitizer
35from cros.factory.goofy import updater
36from cros.factory.goofy.goofy_base import GoofyBase
37from cros.factory.goofy.goofy_rpc import GoofyRPC
38from cros.factory.goofy.invocation import TestArgEnv
39from cros.factory.goofy.invocation import TestInvocation
40from cros.factory.goofy.link_manager import PresenterLinkManager
Vic Yange2c76a82014-10-30 12:48:19 -070041from cros.factory.goofy import prespawner
Vic Yangd80ea752014-09-24 16:07:14 +080042from cros.factory.goofy.system_log_manager import SystemLogManager
43from cros.factory.goofy.web_socket_manager import WebSocketManager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +080044from cros.factory.system.board import Board, BoardException
jcliangcd688182012-08-20 21:01:26 +080045from cros.factory.system.charge_manager import ChargeManager
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +080046from cros.factory.system.core_dump_manager import CoreDumpManager
Jon Salzce6a7f82013-06-10 18:22:54 +080047from cros.factory.system.cpufreq_manager import CpufreqManager
Jon Salzb92c5112012-09-21 15:40:11 +080048from cros.factory.system import disk_space
jcliangcd688182012-08-20 21:01:26 +080049from cros.factory.test import factory
Jon Salz670ce062014-05-16 15:53:50 +080050from cros.factory.test import phase
jcliangcd688182012-08-20 21:01:26 +080051from cros.factory.test import state
Jon Salz51528e12012-07-02 18:54:45 +080052from cros.factory.test import shopfloor
Jon Salz83591782012-06-26 11:09:58 +080053from cros.factory.test import utils
Jon Salz128b0932013-07-03 16:55:26 +080054from cros.factory.test.test_lists import test_lists
Ricky Liang6fe218c2013-12-27 15:17:17 +080055from cros.factory.test.e2e_test.common import (
56 AutomationMode, AutomationModePrompt, ParseAutomationMode)
Jon Salz83591782012-06-26 11:09:58 +080057from cros.factory.test.event import Event
58from cros.factory.test.event import EventClient
59from cros.factory.test.event import EventServer
jcliangcd688182012-08-20 21:01:26 +080060from cros.factory.test.factory import TestState
Jon Salzd7550792013-07-12 05:49:27 +080061from cros.factory.test.utils import Enum
Dean Liao592e4d52013-01-10 20:06:39 +080062from cros.factory.tools.key_filter import KeyFilter
Jon Salz2af235d2013-06-24 14:47:21 +080063from cros.factory.utils import file_utils
Joel Kitchingb85ed7f2014-10-08 18:24:39 +080064from cros.factory.utils import net_utils
Jon Salz78c32392012-07-25 14:18:29 +080065from cros.factory.utils.process_utils import Spawn
Hung-Te Linf2f78f72012-02-08 19:27:11 +080066
67
Hung-Te Linf2f78f72012-02-08 19:27:11 +080068HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Chun-ta Lin279e7e92013-02-19 17:40:39 +080069CACHES_DIR = os.path.join(factory.get_state_root(), "caches")
Hung-Te Linf2f78f72012-02-08 19:27:11 +080070
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +080071CLEANUP_LOGS_PAUSED = '/var/lib/cleanup_logs_paused'
72
Jon Salz5c344f62012-07-13 14:31:16 +080073# Value for tests_after_shutdown that forces auto-run (e.g., after
74# a factory update, when the available set of tests might change).
75FORCE_AUTO_RUN = 'force_auto_run'
76
Justin Chuang83813982013-05-13 01:26:32 +080077# Sync disks when battery level is higher than this value.
78# Otherwise, power loss during disk sync operation may incur even worse outcome.
79MIN_BATTERY_LEVEL_FOR_DISK_SYNC = 1.0
80
Jon Salze12c2b32013-06-25 16:24:34 +080081MAX_CRASH_FILE_SIZE = 64*1024
82
Jon Salzd7550792013-07-12 05:49:27 +080083Status = Enum(['UNINITIALIZED', 'INITIALIZING', 'RUNNING',
84 'TERMINATING', 'TERMINATED'])
85
Hung-Te Linf2f78f72012-02-08 19:27:11 +080086def get_hwid_cfg():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +080087 """Returns the HWID config tag, or an empty string if none can be found."""
Jon Salz0697cbf2012-07-04 15:14:04 +080088 if 'CROS_HWID' in os.environ:
89 return os.environ['CROS_HWID']
90 if os.path.exists(HWID_CFG_PATH):
91 with open(HWID_CFG_PATH, 'rt') as hwid_cfg_handle:
92 return hwid_cfg_handle.read().strip()
93 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +080094
95
Jon Salz73e0fd02012-04-04 11:46:38 +080096_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +080097
Peter Ammon1e1ec572014-06-26 17:56:32 -070098class Goofy(GoofyBase):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +080099 """The main factory flow.
Jon Salz0697cbf2012-07-04 15:14:04 +0800100
101 Note that all methods in this class must be invoked from the main
102 (event) thread. Other threads, such as callbacks and TestInvocation
103 methods, should instead post events on the run queue.
104
105 TODO: Unit tests. (chrome-os-partner:7409)
106
107 Properties:
108 uuid: A unique UUID for this invocation of Goofy.
109 state_instance: An instance of FactoryState.
110 state_server: The FactoryState XML/RPC server.
111 state_server_thread: A thread running state_server.
112 event_server: The EventServer socket server.
113 event_server_thread: A thread running event_server.
114 event_client: A client to the event server.
115 connection_manager: The connection_manager object.
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800116 system_log_manager: The SystemLogManager object.
117 core_dump_manager: The CoreDumpManager object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800118 ui_process: The factory ui process object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800119 invocations: A map from FactoryTest objects to the corresponding
120 TestInvocations objects representing active tests.
121 tests_to_run: A deque of tests that should be run when the current
122 test(s) complete.
123 options: Command-line options.
124 args: Command-line args.
125 test_list: The test list.
Jon Salz128b0932013-07-03 16:55:26 +0800126 test_lists: All new-style test lists.
Ricky Liang4bff3e32014-02-20 18:46:11 +0800127 run_id: The identifier for latest test run.
128 scheduled_run_tests: The list of tests scheduled for latest test run.
Jon Salz0697cbf2012-07-04 15:14:04 +0800129 event_handlers: Map of Event.Type to the method used to handle that
130 event. If the method has an 'event' argument, the event is passed
131 to the handler.
Jon Salz3c493bb2013-02-07 17:24:58 +0800132 last_log_disk_space_message: The last message we logged about disk space
133 (to avoid duplication).
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800134 last_kick_sync_time: The last time to kick system_log_manager to sync
135 because of core dump files (to avoid kicking too soon then abort the
136 sync.)
Jon Salz416f9cc2013-05-10 18:32:50 +0800137 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800138 status: The current Goofy status (a member of the Status enum).
Peter Ammon948b7172014-07-15 12:43:06 -0700139 link_manager: Instance of PresenterLinkManager for communicating
140 with GoofyPresenter
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800141 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800142 def __init__(self):
Peter Ammon1e1ec572014-06-26 17:56:32 -0700143 super(Goofy, self).__init__()
Jon Salz0697cbf2012-07-04 15:14:04 +0800144 self.uuid = str(uuid.uuid4())
145 self.state_instance = None
146 self.state_server = None
147 self.state_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800148 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800149 self.event_server = None
150 self.event_server_thread = None
151 self.event_client = None
152 self.connection_manager = None
Vic Yang4953fc12012-07-26 16:19:53 +0800153 self.charge_manager = None
Dean Liao88b93192014-10-23 19:37:41 +0800154 self._can_charge = True
Jon Salz8fa8e832012-07-13 19:04:09 +0800155 self.time_sanitizer = None
156 self.time_synced = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800157 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800158 self.system_log_manager = None
Cheng-Yi Chiang835f2682013-05-06 22:15:48 +0800159 self.core_dump_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800160 self.event_log = None
Vic Yange2c76a82014-10-30 12:48:19 -0700161 self.autotest_prespawner = None
162 self.pytest_prespawner = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800163 self.ui_process = None
Vic Yanga3cecf82014-12-26 00:44:21 -0800164 self._ui_initialized = False
Jon Salzc79a9982012-08-30 04:42:01 +0800165 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800166 self.invocations = {}
167 self.tests_to_run = deque()
168 self.visible_test = None
169 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800170 self.hooks = None
Vic Yangd8990da2013-06-27 16:57:43 +0800171 self.cpu_usage_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800172
173 self.options = None
174 self.args = None
175 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800176 self.test_lists = None
Ricky Liang4bff3e32014-02-20 18:46:11 +0800177 self.run_id = None
178 self.scheduled_run_tests = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800179 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800180 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800181 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800182 self.last_update_check = None
Jon Salz8fa8e832012-07-13 19:04:09 +0800183 self.last_sync_time = None
Jon Salzb92c5112012-09-21 15:40:11 +0800184 self.last_log_disk_space_time = None
Jon Salz3c493bb2013-02-07 17:24:58 +0800185 self.last_log_disk_space_message = None
Justin Chuang83813982013-05-13 01:26:32 +0800186 self.last_check_battery_time = None
187 self.last_check_battery_message = None
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +0800188 self.last_kick_sync_time = None
Vic Yang311ddb82012-09-26 12:08:28 +0800189 self.exclusive_items = set()
Jon Salz0f996602012-10-03 15:26:48 +0800190 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800191 self.key_filter = None
Jon Salzce6a7f82013-06-10 18:22:54 +0800192 self.cpufreq_manager = None
Jon Salzd7550792013-07-12 05:49:27 +0800193 self.status = Status.UNINITIALIZED
Ricky Liang36512a32014-07-25 11:47:04 +0800194 self.ready_for_ui_connection = False
Peter Ammon1e1ec572014-06-26 17:56:32 -0700195 self.link_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800196
Jon Salz85a39882012-07-05 16:45:04 +0800197 def test_or_root(event, parent_or_group=True):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800198 """Returns the test affected by a particular event.
Jon Salz85a39882012-07-05 16:45:04 +0800199
200 Args:
201 event: The event containing an optional 'path' attribute.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800202 parent_or_group: If True, returns the top-level parent for a test (the
Jon Salz85a39882012-07-05 16:45:04 +0800203 root node of the tests that need to be run together if the given test
204 path is to be run).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800205 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800206 try:
207 path = event.path
208 except AttributeError:
209 path = None
210
211 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800212 test = self.test_list.lookup_path(path)
213 if parent_or_group:
214 test = test.get_top_level_parent_or_group()
215 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800216 else:
217 return self.test_list
218
219 self.event_handlers = {
220 Event.Type.SWITCH_TEST: self.handle_switch_test,
221 Event.Type.SHOW_NEXT_ACTIVE_TEST:
222 lambda event: self.show_next_active_test(),
223 Event.Type.RESTART_TESTS:
224 lambda event: self.restart_tests(root=test_or_root(event)),
225 Event.Type.AUTO_RUN:
226 lambda event: self.auto_run(root=test_or_root(event)),
227 Event.Type.RE_RUN_FAILED:
228 lambda event: self.re_run_failed(root=test_or_root(event)),
229 Event.Type.RUN_TESTS_WITH_STATUS:
230 lambda event: self.run_tests_with_status(
231 event.status,
232 root=test_or_root(event)),
233 Event.Type.REVIEW:
234 lambda event: self.show_review_information(),
235 Event.Type.UPDATE_SYSTEM_INFO:
236 lambda event: self.update_system_info(),
Jon Salz0697cbf2012-07-04 15:14:04 +0800237 Event.Type.STOP:
Jon Salz85a39882012-07-05 16:45:04 +0800238 lambda event: self.stop(root=test_or_root(event, False),
Jon Salz6dc031d2013-06-19 13:06:23 +0800239 fail=getattr(event, 'fail', False),
240 reason=getattr(event, 'reason', None)),
Jon Salz36fbbb52012-07-05 13:45:06 +0800241 Event.Type.SET_VISIBLE_TEST:
242 lambda event: self.set_visible_test(
243 self.test_list.lookup_path(event.path)),
Jon Salz4712ac72013-02-07 17:12:05 +0800244 Event.Type.CLEAR_STATE:
245 lambda event: self.clear_state(self.test_list.lookup_path(event.path)),
Jon Salz0697cbf2012-07-04 15:14:04 +0800246 }
247
Jon Salz0697cbf2012-07-04 15:14:04 +0800248 self.web_socket_manager = None
249
250 def destroy(self):
Ricky Liang74237a02014-09-18 15:11:23 +0800251 """Performs any shutdown tasks. Overrides base class method."""
Jon Salzd7550792013-07-12 05:49:27 +0800252 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800253 if self.chrome:
254 self.chrome.kill()
255 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800256 if self.dummy_shopfloor:
257 self.dummy_shopfloor.kill()
258 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800259 if self.ui_process:
260 utils.kill_process_tree(self.ui_process, 'ui')
261 self.ui_process = None
262 if self.web_socket_manager:
263 logging.info('Stopping web sockets')
264 self.web_socket_manager.close()
265 self.web_socket_manager = None
266 if self.state_server_thread:
267 logging.info('Stopping state server')
268 self.state_server.shutdown()
269 self.state_server_thread.join()
270 self.state_server.server_close()
271 self.state_server_thread = None
272 if self.state_instance:
273 self.state_instance.close()
274 if self.event_server_thread:
275 logging.info('Stopping event server')
276 self.event_server.shutdown() # pylint: disable=E1101
277 self.event_server_thread.join()
278 self.event_server.server_close()
279 self.event_server_thread = None
280 if self.log_watcher:
281 if self.log_watcher.IsThreadStarted():
282 self.log_watcher.StopWatchThread()
283 self.log_watcher = None
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800284 if self.system_log_manager:
285 if self.system_log_manager.IsThreadRunning():
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +0800286 self.system_log_manager.Stop()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +0800287 self.system_log_manager = None
Vic Yange2c76a82014-10-30 12:48:19 -0700288 if self.autotest_prespawner:
289 logging.info('Stopping autotest prespawner')
290 self.autotest_prespawner.stop()
291 self.autotest_prespawner = None
292 if self.pytest_prespawner:
293 logging.info('Stopping pytest prespawner')
294 self.pytest_prespawner.stop()
295 self.pytest_prespawner = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800296 if self.event_client:
297 logging.info('Closing event client')
298 self.event_client.close()
299 self.event_client = None
Jon Salzddf0d052013-06-18 12:52:44 +0800300 if self.cpufreq_manager:
301 self.cpufreq_manager.Stop()
Jon Salz0697cbf2012-07-04 15:14:04 +0800302 if self.event_log:
303 self.event_log.Close()
304 self.event_log = None
Dean Liao592e4d52013-01-10 20:06:39 +0800305 if self.key_filter:
306 self.key_filter.Stop()
Vic Yangd8990da2013-06-27 16:57:43 +0800307 if self.cpu_usage_watcher:
308 self.cpu_usage_watcher.terminate()
Peter Ammon1e1ec572014-06-26 17:56:32 -0700309 if self.link_manager:
310 self.link_manager.Stop()
311 self.link_manager = None
Dean Liao592e4d52013-01-10 20:06:39 +0800312
Peter Ammon1e1ec572014-06-26 17:56:32 -0700313 super(Goofy, self).destroy()
Jon Salz0697cbf2012-07-04 15:14:04 +0800314 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800315 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800316
317 def start_state_server(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800318 # Before starting state server, remount stateful partitions with
319 # no commit flag. The default commit time (commit=600) makes corruption
320 # too likely.
Hung-Te Lind59dbfa2014-08-27 12:27:53 +0800321 utils.ResetCommitTime()
Jon Salz2af235d2013-06-24 14:47:21 +0800322
Jon Salz0697cbf2012-07-04 15:14:04 +0800323 self.state_instance, self.state_server = (
324 state.create_server(bind_address='0.0.0.0'))
Jon Salz16d10542012-07-23 12:18:45 +0800325 self.goofy_rpc = GoofyRPC(self)
326 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800327 logging.info('Starting state server')
328 self.state_server_thread = threading.Thread(
329 target=self.state_server.serve_forever,
330 name='StateServer')
331 self.state_server_thread.start()
332
333 def start_event_server(self):
334 self.event_server = EventServer()
335 logging.info('Starting factory event server')
336 self.event_server_thread = threading.Thread(
337 target=self.event_server.serve_forever,
338 name='EventServer') # pylint: disable=E1101
339 self.event_server_thread.start()
340
341 self.event_client = EventClient(
342 callback=self.handle_event, event_loop=self.run_queue)
343
344 self.web_socket_manager = WebSocketManager(self.uuid)
345 self.state_server.add_handler("/event",
346 self.web_socket_manager.handle_web_socket)
347
348 def start_ui(self):
349 ui_proc_args = [
350 os.path.join(factory.FACTORY_PACKAGE_PATH, 'test', 'ui.py'),
351 self.options.test_list]
352 if self.options.verbose:
353 ui_proc_args.append('-v')
354 logging.info('Starting ui %s', ui_proc_args)
Jon Salz78c32392012-07-25 14:18:29 +0800355 self.ui_process = Spawn(ui_proc_args)
Jon Salz0697cbf2012-07-04 15:14:04 +0800356 logging.info('Waiting for UI to come up...')
357 self.event_client.wait(
358 lambda event: event.type == Event.Type.UI_READY)
359 logging.info('UI has started')
360
361 def set_visible_test(self, test):
362 if self.visible_test == test:
363 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800364 if test and not test.has_ui:
365 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800366
367 if test:
368 test.update_state(visible=True)
369 if self.visible_test:
370 self.visible_test.update_state(visible=False)
371 self.visible_test = test
372
Ricky Liang48e47f92014-02-26 19:31:51 +0800373 def log_startup_messages(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800374 """Logs the tail of var/log/messages and mosys and EC console logs."""
Jon Salzd4306c82012-11-30 15:16:36 +0800375 # TODO(jsalz): This is mostly a copy-and-paste of code in init_states,
376 # for factory-3004.B only. Consolidate and merge back to ToT.
377 if utils.in_chroot():
378 return
379
380 try:
381 var_log_messages = (
382 utils.var_log_messages_before_reboot())
383 logging.info(
384 'Tail of /var/log/messages before last reboot:\n'
385 '%s', ('\n'.join(
386 ' ' + x for x in var_log_messages)))
387 except: # pylint: disable=W0702
388 logging.exception('Unable to grok /var/log/messages')
389
390 try:
Ricky Liang117484a2014-04-14 11:14:41 +0800391 mosys_log = Spawn(
Jon Salzd4306c82012-11-30 15:16:36 +0800392 ['mosys', 'eventlog', 'list'],
393 read_stdout=True, log_stderr_on_error=True).stdout_data
394 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
395 except: # pylint: disable=W0702
396 logging.exception('Unable to read mosys eventlog')
397
Dean Liao88b93192014-10-23 19:37:41 +0800398 self.log_ec_console()
399 self.log_ec_panic_info()
400
401 @staticmethod
402 def log_ec_console():
403 """Logs EC console log into logging.info.
404
405 It logs an error message in logging.exception if an exception is raised
406 when getting EC console log.
407 For unsupported device, it logs unsupport message in logging.info
408
409 Returns:
410 EC console log string.
411 """
Jon Salzd4306c82012-11-30 15:16:36 +0800412 try:
Vic Yang8341dde2013-01-29 16:48:52 +0800413 board = system.GetBoard()
414 ec_console_log = board.GetECConsoleLog()
Jon Salzd4306c82012-11-30 15:16:36 +0800415 logging.info('EC console log after reboot:\n%s\n', ec_console_log)
Dean Liao88b93192014-10-23 19:37:41 +0800416 return ec_console_log
417 except NotImplementedError:
418 logging.info('EC console log not supported')
Jon Salzd4306c82012-11-30 15:16:36 +0800419 except: # pylint: disable=W0702
420 logging.exception('Error retrieving EC console log')
421
Dean Liao88b93192014-10-23 19:37:41 +0800422 @staticmethod
423 def log_ec_panic_info():
424 """Logs EC panic info into logging.info.
425
426 It logs an error message in logging.exception if an exception is raised
427 when getting EC panic info.
428 For unsupported device, it logs unsupport message in logging.info
429
430 Returns:
431 EC panic info string.
432 """
Vic Yang079f9872013-07-01 11:32:00 +0800433 try:
434 board = system.GetBoard()
435 ec_panic_info = board.GetECPanicInfo()
436 logging.info('EC panic info after reboot:\n%s\n', ec_panic_info)
Dean Liao88b93192014-10-23 19:37:41 +0800437 return ec_panic_info
438 except NotImplementedError:
439 logging.info('EC panic info is not supported')
Vic Yang079f9872013-07-01 11:32:00 +0800440 except: # pylint: disable=W0702
441 logging.exception('Error retrieving EC panic info')
442
Ricky Liang48e47f92014-02-26 19:31:51 +0800443 def shutdown(self, operation):
444 """Starts shutdown procedure.
445
446 Args:
Vic (Chun-Ju) Yang05b0d952014-04-28 17:39:09 +0800447 operation: The shutdown operation (reboot, full_reboot, or halt).
Ricky Liang48e47f92014-02-26 19:31:51 +0800448 """
449 active_tests = []
450 for test in self.test_list.walk():
451 if not test.is_leaf():
452 continue
453
454 test_state = test.get_state()
455 if test_state.status == TestState.ACTIVE:
456 active_tests.append(test)
457
458
459 if not (len(active_tests) == 1 and
460 isinstance(active_tests[0], factory.ShutdownStep)):
461 logging.error(
462 'Calling Goofy shutdown outside of the shutdown factory test')
463 return
464
465 logging.info('Start Goofy shutdown (%s)', operation)
466 # Save pending test list in the state server
467 self.state_instance.set_shared_data(
468 'tests_after_shutdown',
469 [t.path for t in self.tests_to_run])
470 # Save shutdown time
471 self.state_instance.set_shared_data('shutdown_time', time.time())
472
473 with self.env.lock:
474 self.event_log.Log('shutdown', operation=operation)
475 shutdown_result = self.env.shutdown(operation)
476 if shutdown_result:
477 # That's all, folks!
Peter Ammon1e1ec572014-06-26 17:56:32 -0700478 self.run_enqueue(None)
Ricky Liang48e47f92014-02-26 19:31:51 +0800479 else:
480 # Just pass (e.g., in the chroot).
481 self.state_instance.set_shared_data('tests_after_shutdown', None)
482 # Send event with no fields to indicate that there is no
483 # longer a pending shutdown.
484 self.event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
485
486 def handle_shutdown_complete(self, test):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800487 """Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800488
Ricky Liang6fe218c2013-12-27 15:17:17 +0800489 Args:
490 test: The ShutdownStep.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800491 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800492 test_state = test.update_state(increment_shutdown_count=1)
493 logging.info('Detected shutdown (%d of %d)',
Ricky Liang48e47f92014-02-26 19:31:51 +0800494 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800495
Ricky Liang48e47f92014-02-26 19:31:51 +0800496 # Insert current shutdown test at the front of the list of tests to run
497 # after shutdown. This is to continue on post-shutdown verification in the
498 # shutdown step.
499 tests_after_shutdown = self.state_instance.get_shared_data(
500 'tests_after_shutdown', optional=True)
501 if not tests_after_shutdown:
502 self.state_instance.set_shared_data('tests_after_shutdown', [test.path])
503 elif isinstance(tests_after_shutdown, list):
504 self.state_instance.set_shared_data(
505 'tests_after_shutdown', [test.path] + tests_after_shutdown)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800506
Ricky Liang48e47f92014-02-26 19:31:51 +0800507 # Set 'post_shutdown' to inform shutdown test that a shutdown just occurred.
Ricky Liangb7eb8772014-09-15 18:05:22 +0800508 self.state_instance.set_shared_data(
509 state.POST_SHUTDOWN_TAG % test.path,
510 self.state_instance.get_test_state(test.path).invocation)
Jon Salz258a40c2012-04-19 12:34:01 +0800511
Jon Salz0697cbf2012-07-04 15:14:04 +0800512 def init_states(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800513 """Initializes all states on startup."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800514 for test in self.test_list.get_all_tests():
515 # Make sure the state server knows about all the tests,
516 # defaulting to an untested state.
517 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800518
Jon Salz0697cbf2012-07-04 15:14:04 +0800519 var_log_messages = None
Vic Yanga9c32212012-08-16 20:07:54 +0800520 mosys_log = None
Vic Yange4c275d2012-08-28 01:50:20 +0800521 ec_console_log = None
Vic Yang079f9872013-07-01 11:32:00 +0800522 ec_panic_info = None
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800523
Jon Salz0697cbf2012-07-04 15:14:04 +0800524 # Any 'active' tests should be marked as failed now.
525 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800526 if not test.is_leaf():
527 # Don't bother with parents; they will be updated when their
528 # children are updated.
529 continue
530
Jon Salz0697cbf2012-07-04 15:14:04 +0800531 test_state = test.get_state()
532 if test_state.status != TestState.ACTIVE:
533 continue
534 if isinstance(test, factory.ShutdownStep):
535 # Shutdown while the test was active - that's good.
Ricky Liang48e47f92014-02-26 19:31:51 +0800536 self.handle_shutdown_complete(test)
Jon Salz0697cbf2012-07-04 15:14:04 +0800537 else:
538 # Unexpected shutdown. Grab /var/log/messages for context.
539 if var_log_messages is None:
540 try:
541 var_log_messages = (
542 utils.var_log_messages_before_reboot())
543 # Write it to the log, to make it easier to
544 # correlate with /var/log/messages.
545 logging.info(
546 'Unexpected shutdown. '
547 'Tail of /var/log/messages before last reboot:\n'
548 '%s', ('\n'.join(
549 ' ' + x for x in var_log_messages)))
550 except: # pylint: disable=W0702
551 logging.exception('Unable to grok /var/log/messages')
552 var_log_messages = []
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800553
Jon Salz008f4ea2012-08-28 05:39:45 +0800554 if mosys_log is None and not utils.in_chroot():
555 try:
Ricky Liang117484a2014-04-14 11:14:41 +0800556 mosys_log = Spawn(
Jon Salz008f4ea2012-08-28 05:39:45 +0800557 ['mosys', 'eventlog', 'list'],
558 read_stdout=True, log_stderr_on_error=True).stdout_data
559 # Write it to the log also.
560 logging.info('System eventlog from mosys:\n%s\n', mosys_log)
561 except: # pylint: disable=W0702
562 logging.exception('Unable to read mosys eventlog')
Vic Yanga9c32212012-08-16 20:07:54 +0800563
Vic Yange4c275d2012-08-28 01:50:20 +0800564 if ec_console_log is None:
Dean Liao88b93192014-10-23 19:37:41 +0800565 ec_console_log = self.log_ec_console()
Vic Yange4c275d2012-08-28 01:50:20 +0800566
Vic Yang079f9872013-07-01 11:32:00 +0800567 if ec_panic_info is None:
Dean Liao88b93192014-10-23 19:37:41 +0800568 ec_panic_info = self.log_ec_panic_info()
Vic Yang079f9872013-07-01 11:32:00 +0800569
Jon Salz0697cbf2012-07-04 15:14:04 +0800570 error_msg = 'Unexpected shutdown while test was running'
571 self.event_log.Log('end_test',
572 path=test.path,
573 status=TestState.FAILED,
574 invocation=test.get_state().invocation,
575 error_msg=error_msg,
Vic Yanga9c32212012-08-16 20:07:54 +0800576 var_log_messages='\n'.join(var_log_messages),
577 mosys_log=mosys_log)
Jon Salz0697cbf2012-07-04 15:14:04 +0800578 test.update_state(
579 status=TestState.FAILED,
580 error_msg=error_msg)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800581
Jon Salz50efe942012-07-26 11:54:10 +0800582 if not test.never_fails:
583 # For "never_fails" tests (such as "Start"), don't cancel
584 # pending tests, since reboot is expected.
585 factory.console.info('Unexpected shutdown while test %s '
586 'running; cancelling any pending tests',
587 test.path)
588 self.state_instance.set_shared_data('tests_after_shutdown', [])
Jon Salz69806bb2012-07-20 18:05:02 +0800589
Jon Salz008f4ea2012-08-28 05:39:45 +0800590 self.update_skipped_tests()
591
592 def update_skipped_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800593 """Updates skipped states based on run_if."""
Jon Salz885dcac2013-07-23 16:39:50 +0800594 env = TestArgEnv()
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800595 def _evaluate_skip_from_run_if(test):
596 """Returns the run_if evaluation of the test.
597
598 Args:
599 test: A FactoryTest object.
600
601 Returns:
602 The run_if evaluation result. Returns False if the test has no
603 run_if argument.
604 """
605 value = None
606 if test.run_if_expr:
607 try:
608 value = test.run_if_expr(env)
609 except: # pylint: disable=W0702
610 logging.exception('Unable to evaluate run_if expression for %s',
611 test.path)
612 # But keep going; we have no choice. This will end up
613 # always activating the test.
614 elif test.run_if_table_name:
615 try:
616 aux = shopfloor.get_selected_aux_data(test.run_if_table_name)
617 value = aux.get(test.run_if_col)
618 except ValueError:
619 # Not available; assume it shouldn't be skipped
620 pass
621
622 if value is None:
623 skip = False
624 else:
625 skip = (not value) ^ t.run_if_not
626 return skip
627
628 # Gets all run_if evaluation, and stores results in skip_map.
629 skip_map = dict()
Jon Salz008f4ea2012-08-28 05:39:45 +0800630 for t in self.test_list.walk():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800631 skip_map[t.path] = _evaluate_skip_from_run_if(t)
Jon Salz885dcac2013-07-23 16:39:50 +0800632
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800633 # Propagates the skip value from root of tree and updates skip_map.
634 def _update_skip_map_from_node(test, skip_from_parent):
635 """Updates skip_map from a given node.
Jon Salz885dcac2013-07-23 16:39:50 +0800636
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800637 Given a FactoryTest node and the skip value from parent, updates the
638 skip value of current node in the skip_map if skip value from parent is
639 True. If this node has children, recursively propagate this value to all
640 its children, that is, all its subtests.
641 Note that this function only updates value in skip_map, not the actual
642 test_list tree.
Jon Salz008f4ea2012-08-28 05:39:45 +0800643
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800644 Args:
645 test: The given FactoryTest object. It is a node in the test_list tree.
646 skip_from_parent: The skip value which propagates from the parent of
647 input node.
648 """
649 skip_this_tree = skip_from_parent or skip_map[test.path]
650 if skip_this_tree:
651 logging.info('Skip from node %r', test.path)
652 skip_map[test.path] = True
653 if test.is_leaf():
654 return
655 # Propagates skip value to its subtests
656 for subtest in test.subtests:
657 _update_skip_map_from_node(subtest, skip_this_tree)
658
659 _update_skip_map_from_node(self.test_list, False)
660
661 # Updates the skip value from skip_map to test_list tree. Also, updates test
662 # status if needed.
663 for t in self.test_list.walk():
664 skip = skip_map[t.path]
665 test_state = t.get_state()
666 if ((not skip) and
667 (test_state.status == TestState.PASSED) and
668 (test_state.error_msg == TestState.SKIPPED_MSG)):
669 # It was marked as skipped before, but now we need to run it.
670 # Mark as untested.
671 t.update_state(skip=skip, status=TestState.UNTESTED, error_msg='')
672 else:
673 t.update_state(skip=skip)
Jon Salz008f4ea2012-08-28 05:39:45 +0800674
Jon Salz0697cbf2012-07-04 15:14:04 +0800675 def show_next_active_test(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800676 """Rotates to the next visible active test."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800677 self.reap_completed_tests()
678 active_tests = [
679 t for t in self.test_list.walk()
680 if t.is_leaf() and t.get_state().status == TestState.ACTIVE]
681 if not active_tests:
682 return
Jon Salz4f6c7172012-06-11 20:45:36 +0800683
Jon Salz0697cbf2012-07-04 15:14:04 +0800684 try:
685 next_test = active_tests[
686 (active_tests.index(self.visible_test) + 1) % len(active_tests)]
687 except ValueError: # visible_test not present in active_tests
688 next_test = active_tests[0]
Jon Salz4f6c7172012-06-11 20:45:36 +0800689
Jon Salz0697cbf2012-07-04 15:14:04 +0800690 self.set_visible_test(next_test)
Jon Salz4f6c7172012-06-11 20:45:36 +0800691
Jon Salz0697cbf2012-07-04 15:14:04 +0800692 def handle_event(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800693 """Handles an event from the event server."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800694 handler = self.event_handlers.get(event.type)
695 if handler:
696 handler(event)
697 else:
698 # We don't register handlers for all event types - just ignore
699 # this event.
700 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800701
Vic Yangaabf9fd2013-04-09 18:56:13 +0800702 def check_critical_factory_note(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800703 """Returns True if the last factory note is critical."""
Vic Yangaabf9fd2013-04-09 18:56:13 +0800704 notes = self.state_instance.get_shared_data('factory_note', True)
705 return notes and notes[-1]['level'] == 'CRITICAL'
706
Jon Salz0697cbf2012-07-04 15:14:04 +0800707 def run_next_test(self):
henryhsu4cc6b022014-04-22 17:12:42 +0800708 """Runs the next eligible test (or tests) in self.tests_to_run.
709
710 We have three kinds of the next eligible test:
711 1. normal
712 2. backgroundable
713 3. force_background
714
715 And we have four situations of the ongoing invocations:
716 a. only a running normal test
717 b. all running tests are backgroundable
718 c. all running tests are force_background
719 d. all running tests are any combination of backgroundable and
720 force_background
721
722 When a test would like to be run, it must follow the rules:
723 [1] cannot run with [abd]
724 [2] cannot run with [a]
725 All the other combinations are allowed
726 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800727 self.reap_completed_tests()
Vic Yangaabf9fd2013-04-09 18:56:13 +0800728 if self.tests_to_run and self.check_critical_factory_note():
729 self.tests_to_run.clear()
730 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800731 while self.tests_to_run:
Ricky Liang6fe218c2013-12-27 15:17:17 +0800732 logging.debug('Tests to run: %s', [x.path for x in self.tests_to_run])
Jon Salz94eb56f2012-06-12 18:01:12 +0800733
Jon Salz0697cbf2012-07-04 15:14:04 +0800734 test = self.tests_to_run[0]
Jon Salz94eb56f2012-06-12 18:01:12 +0800735
Jon Salz0697cbf2012-07-04 15:14:04 +0800736 if test in self.invocations:
737 logging.info('Next test %s is already running', test.path)
738 self.tests_to_run.popleft()
739 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800740
Jon Salza1412922012-07-23 16:04:17 +0800741 for requirement in test.require_run:
742 for i in requirement.test.walk():
743 if i.get_state().status == TestState.ACTIVE:
Jon Salz304a75d2012-07-06 11:14:15 +0800744 logging.info('Waiting for active test %s to complete '
Jon Salza1412922012-07-23 16:04:17 +0800745 'before running %s', i.path, test.path)
Jon Salz304a75d2012-07-06 11:14:15 +0800746 return
747
henryhsu4cc6b022014-04-22 17:12:42 +0800748 def is_normal_test(test):
749 return not (test.backgroundable or test.force_background)
750
751 # [1] cannot run with [abd].
752 if self.invocations and is_normal_test(test) and any(
753 [not x.force_background for x in self.invocations]):
754 logging.info('Waiting for non-force_background tests to '
755 'complete before running %s', test.path)
756 return
757
758 # [2] cannot run with [a].
759 if self.invocations and test.backgroundable and any(
760 [is_normal_test(x) for x in self.invocations]):
761 logging.info('Waiting for normal tests to '
762 'complete before running %s', test.path)
Jon Salz0697cbf2012-07-04 15:14:04 +0800763 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800764
Jon Salz3e6f5202012-10-15 15:08:29 +0800765 if test.get_state().skip:
766 factory.console.info('Skipping test %s', test.path)
767 test.update_state(status=TestState.PASSED,
768 error_msg=TestState.SKIPPED_MSG)
769 self.tests_to_run.popleft()
770 continue
771
Jon Salz0697cbf2012-07-04 15:14:04 +0800772 self.tests_to_run.popleft()
Jon Salz94eb56f2012-06-12 18:01:12 +0800773
Jon Salz304a75d2012-07-06 11:14:15 +0800774 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800775 for requirement in test.require_run:
776 for i in requirement.test.walk():
777 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800778 # We've hit this test itself; stop checking
779 break
Jon Salza1412922012-07-23 16:04:17 +0800780 if ((i.get_state().status == TestState.UNTESTED) or
781 (requirement.passed and i.get_state().status !=
782 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800783 # Found an untested test; move on to the next
784 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800785 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800786 break
787
788 if untested:
789 untested_paths = ', '.join(sorted([x.path for x in untested]))
790 if self.state_instance.get_shared_data('engineering_mode',
791 optional=True):
792 # In engineering mode, we'll let it go.
793 factory.console.warn('In engineering mode; running '
794 '%s even though required tests '
795 '[%s] have not completed',
796 test.path, untested_paths)
797 else:
798 # Not in engineering mode; mark it failed.
799 error_msg = ('Required tests [%s] have not been run yet'
800 % untested_paths)
801 factory.console.error('Not running %s: %s',
802 test.path, error_msg)
803 test.update_state(status=TestState.FAILED,
804 error_msg=error_msg)
805 continue
806
Ricky Liang48e47f92014-02-26 19:31:51 +0800807 if (isinstance(test, factory.ShutdownStep) and
Ricky Liangb7eb8772014-09-15 18:05:22 +0800808 self.state_instance.get_shared_data(
809 state.POST_SHUTDOWN_TAG % test.path, optional=True)):
Ricky Liang48e47f92014-02-26 19:31:51 +0800810 # Invoking post shutdown method of shutdown test. We should retain the
811 # iterations_left and retries_left of the original test state.
812 test_state = self.state_instance.get_test_state(test.path)
813 self._run_test(test, test_state.iterations_left,
814 test_state.retries_left)
815 else:
816 # Starts a new test run; reset iterations and retries.
817 self._run_test(test, test.iterations, test.retries)
Jon Salz1acc8742012-07-17 17:45:55 +0800818
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800819 def _run_test(self, test, iterations_left=None, retries_left=None):
Vic Yanga3cecf82014-12-26 00:44:21 -0800820 if not self._ui_initialized and not test.is_no_host():
821 self.init_ui()
Vic Yang08505c72015-01-06 17:01:53 -0800822 invoc = TestInvocation(
823 self, test, on_completion=self.run_next_test,
824 on_test_failure=system.GetBoard().OnTestFailure)
Jon Salz1acc8742012-07-17 17:45:55 +0800825 new_state = test.update_state(
Ricky Liang48e47f92014-02-26 19:31:51 +0800826 status=TestState.ACTIVE, increment_count=1, error_msg='',
827 invocation=invoc.uuid, iterations_left=iterations_left,
828 retries_left=retries_left,
829 visible=(self.visible_test == test))
Jon Salz1acc8742012-07-17 17:45:55 +0800830 invoc.count = new_state.count
831
832 self.invocations[test] = invoc
833 if self.visible_test is None and test.has_ui:
834 self.set_visible_test(test)
Vic Yang311ddb82012-09-26 12:08:28 +0800835 self.check_exclusive()
Jon Salz1acc8742012-07-17 17:45:55 +0800836 invoc.start()
Jon Salz5f2a0672012-05-22 17:14:06 +0800837
Vic Yang311ddb82012-09-26 12:08:28 +0800838 def check_exclusive(self):
Jon Salzce6a7f82013-06-10 18:22:54 +0800839 # alias since this is really long
840 EXCL_OPT = factory.FactoryTest.EXCLUSIVE_OPTIONS
841
Vic Yang311ddb82012-09-26 12:08:28 +0800842 current_exclusive_items = set([
Jon Salzce6a7f82013-06-10 18:22:54 +0800843 item for item in EXCL_OPT
Vic Yang311ddb82012-09-26 12:08:28 +0800844 if any([test.is_exclusive(item) for test in self.invocations])])
845
846 new_exclusive_items = current_exclusive_items - self.exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800847 if EXCL_OPT.NETWORKING in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800848 logging.info('Disabling network')
849 self.connection_manager.DisableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800850 if EXCL_OPT.CHARGER in new_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800851 logging.info('Stop controlling charger')
852
853 new_non_exclusive_items = self.exclusive_items - current_exclusive_items
Jon Salzce6a7f82013-06-10 18:22:54 +0800854 if EXCL_OPT.NETWORKING in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800855 logging.info('Re-enabling network')
856 self.connection_manager.EnableNetworking()
Jon Salzce6a7f82013-06-10 18:22:54 +0800857 if EXCL_OPT.CHARGER in new_non_exclusive_items:
Vic Yang311ddb82012-09-26 12:08:28 +0800858 logging.info('Start controlling charger')
859
Jon Salzce6a7f82013-06-10 18:22:54 +0800860 if self.cpufreq_manager:
861 enabled = EXCL_OPT.CPUFREQ not in current_exclusive_items
862 try:
863 self.cpufreq_manager.SetEnabled(enabled)
864 except: # pylint: disable=W0702
865 logging.exception('Unable to %s cpufreq services',
866 'enable' if enabled else 'disable')
867
Vic Yang311ddb82012-09-26 12:08:28 +0800868 # Only adjust charge state if not excluded
Jon Salzce6a7f82013-06-10 18:22:54 +0800869 if (EXCL_OPT.CHARGER not in current_exclusive_items and
870 not utils.in_chroot()):
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +0800871 if self.charge_manager:
872 self.charge_manager.AdjustChargeState()
873 else:
Dean Liao88b93192014-10-23 19:37:41 +0800874 self.charge()
Vic Yang311ddb82012-09-26 12:08:28 +0800875
876 self.exclusive_items = current_exclusive_items
Jon Salz5da61e62012-05-31 13:06:22 +0800877
Dean Liao88b93192014-10-23 19:37:41 +0800878 def charge(self):
879 """Charges the board.
880
881 It won't try again if last time SetChargeState raised an exception.
882 """
883 if not self._can_charge:
884 return
885
886 try:
887 system.GetBoard().SetChargeState(Board.ChargeState.CHARGE)
888 except NotImplementedError:
889 logging.info('Charging is not supported')
890 self._can_charge = False
891 except BoardException:
892 logging.exception('Unable to set charge state on this board')
893 self._can_charge = False
894
cychiang21886742012-07-05 15:16:32 +0800895 def check_for_updates(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800896 """Schedules an asynchronous check for updates if necessary."""
cychiang21886742012-07-05 15:16:32 +0800897 if not self.test_list.options.update_period_secs:
898 # Not enabled.
899 return
900
901 now = time.time()
902 if self.last_update_check and (
903 now - self.last_update_check <
904 self.test_list.options.update_period_secs):
905 # Not yet time for another check.
906 return
907
908 self.last_update_check = now
909
910 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
911 if reached_shopfloor:
912 new_update_md5sum = md5sum if needs_update else None
913 if system.SystemInfo.update_md5sum != new_update_md5sum:
914 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
915 system.SystemInfo.update_md5sum = new_update_md5sum
Peter Ammon1e1ec572014-06-26 17:56:32 -0700916 self.run_enqueue(self.update_system_info)
cychiang21886742012-07-05 15:16:32 +0800917
918 updater.CheckForUpdateAsync(
919 handle_check_for_update,
920 self.test_list.options.shopfloor_timeout_secs)
921
Jon Salza6711d72012-07-18 14:33:03 +0800922 def cancel_pending_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800923 """Cancels any tests in the run queue."""
Jon Salza6711d72012-07-18 14:33:03 +0800924 self.run_tests([])
925
Ricky Liang4bff3e32014-02-20 18:46:11 +0800926 def restore_active_run_state(self):
927 """Restores active run id and the list of scheduled tests."""
928 self.run_id = self.state_instance.get_shared_data('run_id', optional=True)
929 self.scheduled_run_tests = self.state_instance.get_shared_data(
930 'scheduled_run_tests', optional=True)
931
932 def set_active_run_state(self):
933 """Sets active run id and the list of scheduled tests."""
934 self.run_id = str(uuid.uuid4())
935 self.scheduled_run_tests = [test.path for test in self.tests_to_run]
936 self.state_instance.set_shared_data('run_id', self.run_id)
937 self.state_instance.set_shared_data('scheduled_run_tests',
938 self.scheduled_run_tests)
939
Jon Salz0697cbf2012-07-04 15:14:04 +0800940 def run_tests(self, subtrees, untested_only=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800941 """Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800942
Jon Salz0697cbf2012-07-04 15:14:04 +0800943 The tests are run in order unless one fails (then stops).
944 Backgroundable tests are run simultaneously; when a foreground test is
945 encountered, we wait for all active tests to finish before continuing.
Jon Salzb1b39092012-05-03 02:05:09 +0800946
Ricky Liang6fe218c2013-12-27 15:17:17 +0800947 Args:
948 subtrees: Node or nodes containing tests to run (may either be
949 a single test or a list). Duplicates will be ignored.
950 untested_only: True to run untested tests only.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800951 """
Vic Yang08505c72015-01-06 17:01:53 -0800952 system.GetBoard().OnTestStart()
953
Jon Salz0697cbf2012-07-04 15:14:04 +0800954 if type(subtrees) != list:
955 subtrees = [subtrees]
Jon Salz258a40c2012-04-19 12:34:01 +0800956
Jon Salz0697cbf2012-07-04 15:14:04 +0800957 # Nodes we've seen so far, to avoid duplicates.
958 seen = set()
Jon Salz94eb56f2012-06-12 18:01:12 +0800959
Jon Salz0697cbf2012-07-04 15:14:04 +0800960 self.tests_to_run = deque()
961 for subtree in subtrees:
962 for test in subtree.walk():
963 if test in seen:
964 continue
965 seen.add(test)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800966
Jon Salz0697cbf2012-07-04 15:14:04 +0800967 if not test.is_leaf():
968 continue
Ricky Liang4bff3e32014-02-20 18:46:11 +0800969 if (untested_only and test.get_state().status != TestState.UNTESTED):
Jon Salz0697cbf2012-07-04 15:14:04 +0800970 continue
971 self.tests_to_run.append(test)
Ricky Liang4bff3e32014-02-20 18:46:11 +0800972 if subtrees:
973 self.set_active_run_state()
Jon Salz0697cbf2012-07-04 15:14:04 +0800974 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800975
Jon Salz0697cbf2012-07-04 15:14:04 +0800976 def reap_completed_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800977 """Removes completed tests from the set of active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800978
979 Also updates the visible test if it was reaped.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800980 """
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800981 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800982 for t, v in dict(self.invocations).iteritems():
983 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800984 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800985 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800986 del self.invocations[t]
987
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800988 # Stop on failure if flag is true.
989 if (self.test_list.options.stop_on_failure and
990 new_state.status == TestState.FAILED):
991 # Clean all the tests to cause goofy to stop.
992 self.tests_to_run = []
993 factory.console.info("Stop on failure triggered. Empty the queue.")
994
Jon Salz1acc8742012-07-17 17:45:55 +0800995 if new_state.iterations_left and new_state.status == TestState.PASSED:
996 # Play it again, Sam!
997 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800998 # new_state.retries_left is obtained after update.
999 # For retries_left == 0, test can still be run for the last time.
1000 elif (new_state.retries_left >= 0 and
1001 new_state.status == TestState.FAILED):
1002 # Still have to retry, Sam!
1003 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +08001004
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001005 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +08001006 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +08001007
Jon Salz0697cbf2012-07-04 15:14:04 +08001008 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +08001009 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +08001010 self.set_visible_test(None)
1011 # Make the first running test, if any, the visible test
1012 for t in self.test_list.walk():
1013 if t in self.invocations:
1014 self.set_visible_test(t)
1015 break
1016
Jon Salz6dc031d2013-06-19 13:06:23 +08001017 def kill_active_tests(self, abort, root=None, reason=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001018 """Kills and waits for all active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +08001019
Jon Salz85a39882012-07-05 16:45:04 +08001020 Args:
1021 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +08001022 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +08001023 root: If set, only kills tests with root as an ancestor.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001024 reason: If set, the abort reason.
1025 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001026 self.reap_completed_tests()
1027 for test, invoc in self.invocations.items():
Jon Salz85a39882012-07-05 16:45:04 +08001028 if root and not test.has_ancestor(root):
1029 continue
1030
Jon Salz0697cbf2012-07-04 15:14:04 +08001031 factory.console.info('Killing active test %s...' % test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +08001032 invoc.abort_and_join(reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001033 factory.console.info('Killed %s' % test.path)
Jon Salz1acc8742012-07-17 17:45:55 +08001034 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +08001035 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +08001036
Jon Salz0697cbf2012-07-04 15:14:04 +08001037 if not abort:
1038 test.update_state(status=TestState.UNTESTED)
1039 self.reap_completed_tests()
1040
Jon Salz6dc031d2013-06-19 13:06:23 +08001041 def stop(self, root=None, fail=False, reason=None):
1042 self.kill_active_tests(fail, root, reason)
Jon Salz85a39882012-07-05 16:45:04 +08001043 # Remove any tests in the run queue under the root.
1044 self.tests_to_run = deque([x for x in self.tests_to_run
1045 if root and not x.has_ancestor(root)])
1046 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +08001047
Jon Salz4712ac72013-02-07 17:12:05 +08001048 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +08001049 if root is None:
1050 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +08001051 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +08001052 for f in root.walk():
1053 if f.is_leaf():
1054 f.update_state(status=TestState.UNTESTED)
1055
Jon Salz6dc031d2013-06-19 13:06:23 +08001056 def abort_active_tests(self, reason=None):
1057 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +08001058
1059 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +08001060 syslog.openlog('goofy')
1061
Jon Salz0697cbf2012-07-04 15:14:04 +08001062 try:
Jon Salzd7550792013-07-12 05:49:27 +08001063 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +08001064 self.init()
1065 self.event_log.Log('goofy_init',
1066 success=True)
1067 except:
1068 if self.event_log:
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001069 try:
Jon Salz0697cbf2012-07-04 15:14:04 +08001070 self.event_log.Log('goofy_init',
1071 success=False,
1072 trace=traceback.format_exc())
1073 except: # pylint: disable=W0702
1074 pass
1075 raise
1076
Jon Salzd7550792013-07-12 05:49:27 +08001077 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +08001078 syslog.syslog('Goofy (factory test harness) starting')
Jon Salz0697cbf2012-07-04 15:14:04 +08001079 self.run()
1080
1081 def update_system_info(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001082 """Updates system info."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001083 system_info = system.SystemInfo()
1084 self.state_instance.set_shared_data('system_info', system_info.__dict__)
1085 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
1086 system_info=system_info.__dict__))
1087 logging.info('System info: %r', system_info.__dict__)
1088
Jon Salzeb42f0d2012-07-27 19:14:04 +08001089 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001090 """Commences updating factory software.
Jon Salzeb42f0d2012-07-27 19:14:04 +08001091
1092 Args:
1093 auto_run_on_restart: Auto-run when the machine comes back up.
1094 post_update_hook: Code to call after update but immediately before
1095 restart.
1096
1097 Returns:
1098 Never if the update was successful (we just reboot).
1099 False if the update was unnecessary (no update available).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001100 """
Jon Salz6dc031d2013-06-19 13:06:23 +08001101 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +08001102 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001103
Jon Salz5c344f62012-07-13 14:31:16 +08001104 def pre_update_hook():
1105 if auto_run_on_restart:
1106 self.state_instance.set_shared_data('tests_after_shutdown',
1107 FORCE_AUTO_RUN)
1108 self.state_instance.close()
1109
Jon Salzeb42f0d2012-07-27 19:14:04 +08001110 if updater.TryUpdate(pre_update_hook=pre_update_hook):
1111 if post_update_hook:
1112 post_update_hook()
1113 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +08001114
Ricky Liang8fecf412014-05-22 10:56:14 +08001115 def handle_sigint(self, dummy_signum, dummy_frame): # pylint: disable=W0613
Jon Salz77c151e2012-08-28 07:20:37 +08001116 logging.error('Received SIGINT')
Peter Ammon1e1ec572014-06-26 17:56:32 -07001117 self.run_enqueue(None)
Jon Salz77c151e2012-08-28 07:20:37 +08001118 raise KeyboardInterrupt()
1119
Ricky Liang8fecf412014-05-22 10:56:14 +08001120 def handle_sigterm(self, dummy_signum, dummy_frame): # pylint: disable=W0613
1121 logging.error('Received SIGTERM')
Hung-Te Lin94ca4742014-07-09 20:13:50 +08001122 self.env.terminate()
1123 self.run_queue.put(None)
Ricky Liang8fecf412014-05-22 10:56:14 +08001124 raise RuntimeError('Received SIGTERM')
1125
Jon Salze12c2b32013-06-25 16:24:34 +08001126 def find_kcrashes(self):
1127 """Finds kcrash files, logs them, and marks them as seen."""
1128 seen_crashes = set(
1129 self.state_instance.get_shared_data('seen_crashes', optional=True)
1130 or [])
1131
1132 for path in glob.glob('/var/spool/crash/*'):
1133 if not os.path.isfile(path):
1134 continue
1135 if path in seen_crashes:
1136 continue
1137 try:
1138 stat = os.stat(path)
1139 mtime = utils.TimeString(stat.st_mtime)
1140 logging.info(
1141 'Found new crash file %s (%d bytes at %s)',
1142 path, stat.st_size, mtime)
1143 extra_log_args = {}
1144
1145 try:
1146 _, ext = os.path.splitext(path)
1147 if ext in ['.kcrash', '.meta']:
1148 ext = ext.replace('.', '')
1149 with open(path) as f:
1150 data = f.read(MAX_CRASH_FILE_SIZE)
1151 tell = f.tell()
1152 logging.info(
1153 'Contents of %s%s:%s',
1154 path,
1155 ('' if tell == stat.st_size
1156 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
1157 ('\n' + data).replace('\n', '\n ' + ext + '> '))
1158 extra_log_args['data'] = data
1159
1160 # Copy to /var/factory/kcrash for posterity
1161 kcrash_dir = factory.get_factory_root('kcrash')
1162 utils.TryMakeDirs(kcrash_dir)
1163 shutil.copy(path, kcrash_dir)
1164 logging.info('Copied to %s',
1165 os.path.join(kcrash_dir, os.path.basename(path)))
1166 finally:
1167 # Even if something goes wrong with the above, still try to
1168 # log to event log
1169 self.event_log.Log('crash_file',
1170 path=path, size=stat.st_size, mtime=mtime,
1171 **extra_log_args)
1172 except: # pylint: disable=W0702
1173 logging.exception('Unable to handle crash files %s', path)
1174 seen_crashes.add(path)
1175
1176 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
1177
Jon Salz128b0932013-07-03 16:55:26 +08001178 def GetTestList(self, test_list_id):
1179 """Returns the test list with the given ID.
1180
1181 Raises:
1182 TestListError: The test list ID is not valid.
1183 """
1184 try:
1185 return self.test_lists[test_list_id]
1186 except KeyError:
1187 raise test_lists.TestListError(
1188 '%r is not a valid test list ID (available IDs are [%s])' % (
1189 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
1190
1191 def InitTestLists(self):
1192 """Reads in all test lists and sets the active test list."""
Ricky Liang27051552014-05-04 14:22:26 +08001193 self.test_lists = test_lists.BuildAllTestLists(
1194 force_generic=(self.options.automation_mode is not None))
Jon Salzd7550792013-07-12 05:49:27 +08001195 logging.info('Loaded test lists: [%s]',
1196 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +08001197
1198 if not self.options.test_list:
1199 self.options.test_list = test_lists.GetActiveTestListId()
1200
1201 if os.sep in self.options.test_list:
1202 # It's a path pointing to an old-style test list; use it.
1203 self.test_list = factory.read_test_list(self.options.test_list)
1204 else:
1205 self.test_list = self.GetTestList(self.options.test_list)
1206
1207 logging.info('Active test list: %s', self.test_list.test_list_id)
1208
1209 if isinstance(self.test_list, test_lists.OldStyleTestList):
1210 # Actually load it in. (See OldStyleTestList for an explanation
1211 # of why this is necessary.)
1212 self.test_list = self.test_list.Load()
1213
1214 self.test_list.state_instance = self.state_instance
1215
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001216 def init_hooks(self):
1217 """Initializes hooks.
1218
1219 Must run after self.test_list ready.
1220 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001221 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
1222 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
1223 assert isinstance(self.hooks, factory.Hooks), (
1224 "hooks should be of type Hooks but is %r" % type(self.hooks))
1225 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001226 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +08001227
Vic Yanga3cecf82014-12-26 00:44:21 -08001228 def init_ui(self):
1229 """Initialize UI."""
1230 self._ui_initialized = True
1231 if self.options.ui == 'chrome':
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001232 if self.options.monolithic:
Hung-Te Lin7bd55312014-12-30 16:43:36 +08001233 self.env.launch_chrome()
1234 else:
1235 # The presenter is responsible for launching Chrome. Let's just
1236 # wait here.
1237 self.env.controller_ready_for_ui()
Vic Yanga3cecf82014-12-26 00:44:21 -08001238 logging.info('Waiting for a web socket connection')
1239 self.web_socket_manager.wait()
1240
1241 # Wait for the test widget size to be set; this is done in
1242 # an asynchronous RPC so there is a small chance that the
1243 # web socket might be opened first.
1244 for _ in range(100): # 10 s
1245 try:
1246 if self.state_instance.get_shared_data('test_widget_size'):
1247 break
1248 except KeyError:
1249 pass # Retry
1250 time.sleep(0.1) # 100 ms
1251 else:
1252 logging.warn('Never received test_widget_size from UI')
1253
Hung-Te Lin7bd55312014-12-30 16:43:36 +08001254 logging.info('Waiting for a web socket connection')
1255 self.web_socket_manager.wait()
1256
Jon Salz0697cbf2012-07-04 15:14:04 +08001257 def init(self, args=None, env=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001258 """Initializes Goofy.
Jon Salz0697cbf2012-07-04 15:14:04 +08001259
1260 Args:
1261 args: A list of command-line arguments. Uses sys.argv if
1262 args is None.
1263 env: An Environment instance to use (or None to choose
1264 FakeChrootEnvironment or DUTEnvironment as appropriate).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001265 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001266 parser = OptionParser()
1267 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001268 action='store_true',
1269 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001270 parser.add_option('--print_test_list', dest='print_test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001271 metavar='FILE',
1272 help='Read and print test list FILE, and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001273 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001274 action='store_true',
1275 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001276 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz7b5482e2014-08-04 17:48:41 +08001277 choices=['none', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001278 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001279 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001280 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001281 type='int', default=1,
1282 help=('Factor by which to scale UI '
1283 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001284 parser.add_option('--test_list', dest='test_list',
Jon Salz8fa8e832012-07-13 19:04:09 +08001285 metavar='FILE',
1286 help='Use FILE as test list')
Jon Salzc79a9982012-08-30 04:42:01 +08001287 parser.add_option('--dummy_shopfloor', action='store_true',
1288 help='Use a dummy shopfloor server')
Hung-Te Lincc41d2a2014-10-29 13:35:20 +08001289 parser.add_option('--dummy_connection_manager', action='store_true',
1290 help='Use a dummy connection manager')
Ricky Liang6fe218c2013-12-27 15:17:17 +08001291 parser.add_option('--automation-mode',
1292 choices=[m.lower() for m in AutomationMode],
1293 default='none', help="Factory test automation mode.")
Ricky Liang117484a2014-04-14 11:14:41 +08001294 parser.add_option('--no-auto-run-on-start', dest='auto_run_on_start',
1295 action='store_false', default=True,
1296 help=('do not automatically run the test list on goofy '
1297 'start; this is only valid when factory test '
1298 'automation is enabled'))
Chun-Ta Lina8dd3172014-11-26 16:15:13 +08001299 parser.add_option('--handshake_timeout', dest='handshake_timeout',
1300 type='float', default=0.3,
1301 help=('RPC timeout when doing handshake between device '
1302 'and presenter.'))
Vic Yang7d693c42014-09-14 09:52:39 +08001303 parser.add_option('--standalone', dest='standalone',
1304 action='store_true', default=False,
1305 help=('Assume the presenter is running on the same '
1306 'machines.'))
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001307 parser.add_option('--monolithic', dest='monolithic',
1308 action='store_true', default=False,
1309 help='Run in monolithic mode (without presenter)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001310 (self.options, self.args) = parser.parse_args(args)
1311
Hung-Te Lina846f602014-07-04 20:32:22 +08001312 signal.signal(signal.SIGINT, self.handle_sigint)
1313 # TODO(hungte) SIGTERM does not work properly without Telemetry and should
1314 # be fixed.
Hung-Te Lina846f602014-07-04 20:32:22 +08001315
Jon Salz46b89562012-07-05 11:49:22 +08001316 # Make sure factory directories exist.
1317 factory.get_log_root()
1318 factory.get_state_root()
1319 factory.get_test_data_root()
1320
Jon Salz0697cbf2012-07-04 15:14:04 +08001321 global _inited_logging # pylint: disable=W0603
1322 if not _inited_logging:
1323 factory.init_logging('goofy', verbose=self.options.verbose)
1324 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001325
Jon Salz0f996602012-10-03 15:26:48 +08001326 if self.options.print_test_list:
Joel Kitchingb85ed7f2014-10-08 18:24:39 +08001327 print(factory.read_test_list(
1328 self.options.print_test_list).__repr__(recursive=True))
Jon Salz0f996602012-10-03 15:26:48 +08001329 sys.exit(0)
1330
Jon Salzee85d522012-07-17 14:34:46 +08001331 event_log.IncrementBootSequence()
Jon Salzd15bbcf2013-05-21 17:33:57 +08001332 # Don't defer logging the initial event, so we can make sure
1333 # that device_id, reimage_id, etc. are all set up.
1334 self.event_log = EventLog('goofy', defer=False)
Jon Salz0697cbf2012-07-04 15:14:04 +08001335
Jon Salz0697cbf2012-07-04 15:14:04 +08001336 if env:
1337 self.env = env
1338 elif factory.in_chroot():
1339 self.env = test_environment.FakeChrootEnvironment()
1340 logging.warn(
1341 'Using chroot environment: will not actually run autotests')
Hung-Te Lina846f602014-07-04 20:32:22 +08001342 elif self.options.ui == 'chrome':
Ricky Liang09d66d82014-09-25 11:20:54 +08001343 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001344 self.env.goofy = self
Vic Yanga4931152014-08-11 16:36:24 -07001345 # web_socket_manager will be initialized later
1346 # pylint: disable=W0108
1347 self.env.has_sockets = lambda: self.web_socket_manager.has_sockets()
Jon Salz0697cbf2012-07-04 15:14:04 +08001348
1349 if self.options.restart:
1350 state.clear_state()
1351
Jon Salz0697cbf2012-07-04 15:14:04 +08001352 if self.options.ui_scale_factor != 1 and utils.in_qemu():
1353 logging.warn(
1354 'In QEMU; ignoring ui_scale_factor argument')
1355 self.options.ui_scale_factor = 1
1356
1357 logging.info('Started')
1358
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001359 if not self.options.monolithic:
Hung-Te Lin7bd55312014-12-30 16:43:36 +08001360 self.link_manager = PresenterLinkManager(
1361 check_interval=1,
1362 handshake_timeout=self.options.handshake_timeout,
1363 standalone=self.options.standalone)
Peter Ammon1e1ec572014-06-26 17:56:32 -07001364
Jon Salz0697cbf2012-07-04 15:14:04 +08001365 self.start_state_server()
1366 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1367 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001368 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001369 self.last_shutdown_time = (
1370 self.state_instance.get_shared_data('shutdown_time', optional=True))
1371 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001372 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001373
Ricky Liang6fe218c2013-12-27 15:17:17 +08001374 self.options.automation_mode = ParseAutomationMode(
1375 self.options.automation_mode)
1376 self.state_instance.set_shared_data('automation_mode',
1377 self.options.automation_mode)
1378 self.state_instance.set_shared_data(
1379 'automation_mode_prompt',
1380 AutomationModePrompt[self.options.automation_mode])
1381
Jon Salz128b0932013-07-03 16:55:26 +08001382 try:
1383 self.InitTestLists()
1384 except: # pylint: disable=W0702
1385 logging.exception('Unable to initialize test lists')
1386 self.state_instance.set_shared_data(
1387 'startup_error',
1388 'Unable to initialize test lists\n%s' % (
1389 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001390 if self.options.ui == 'chrome':
1391 # Create an empty test list with default options so that the rest of
1392 # startup can proceed.
1393 self.test_list = factory.FactoryTestList(
1394 [], self.state_instance, factory.Options())
1395 else:
1396 # Bail with an error; no point in starting up.
1397 sys.exit('No valid test list; exiting.')
1398
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001399 self.init_hooks()
1400
Jon Salz822838b2013-03-25 17:32:33 +08001401 if self.test_list.options.clear_state_on_start:
1402 self.state_instance.clear_test_state()
1403
Jon Salz670ce062014-05-16 15:53:50 +08001404 # If the phase is invalid, this will raise a ValueError.
1405 phase.SetPersistentPhase(self.test_list.options.phase)
1406
Dean Liao85ca86f2014-11-03 12:28:08 +08001407 # For netboot firmware, mainfw_type should be 'netboot'.
1408 if (system.SystemInfo().mainfw_type != 'nonchrome' and
1409 system.SystemInfo().firmware_version is None):
Vic Yang9bd4f772013-06-04 17:34:00 +08001410 self.state_instance.set_shared_data('startup_error',
1411 'Netboot firmware detected\n'
1412 'Connect Ethernet and reboot to re-image.\n'
1413 u'侦测到网路开机固件\n'
1414 u'请连接乙太网并重启')
1415
Jon Salz0697cbf2012-07-04 15:14:04 +08001416 if not self.state_instance.has_shared_data('ui_lang'):
1417 self.state_instance.set_shared_data('ui_lang',
1418 self.test_list.options.ui_lang)
1419 self.state_instance.set_shared_data(
1420 'test_list_options',
1421 self.test_list.options.__dict__)
1422 self.state_instance.test_list = self.test_list
1423
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001424 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001425
Jon Salz23926422012-09-01 03:38:13 +08001426 if self.options.dummy_shopfloor:
Joel Kitchingb85ed7f2014-10-08 18:24:39 +08001427 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = ('http://%s:%d/' %
1428 (net_utils.LOCALHOST, shopfloor.DEFAULT_SERVER_PORT))
Jon Salz23926422012-09-01 03:38:13 +08001429 self.dummy_shopfloor = Spawn(
1430 [os.path.join(factory.FACTORY_PATH, 'bin', 'shopfloor_server'),
1431 '--dummy'])
1432 elif self.test_list.options.shopfloor_server_url:
1433 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001434 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001435
Jon Salz0f996602012-10-03 15:26:48 +08001436 if self.test_list.options.time_sanitizer and not utils.in_chroot():
Jon Salz8fa8e832012-07-13 19:04:09 +08001437 self.time_sanitizer = time_sanitizer.TimeSanitizer(
1438 base_time=time_sanitizer.GetBaseTimeFromFile(
1439 # lsb-factory is written by the factory install shim during
1440 # installation, so it should have a good time obtained from
Jon Salz54882d02012-08-31 01:57:54 +08001441 # the mini-Omaha server. If it's not available, we'll use
1442 # /etc/lsb-factory (which will be much older, but reasonably
1443 # sane) and rely on a shopfloor sync to set a more accurate
1444 # time.
1445 '/usr/local/etc/lsb-factory',
1446 '/etc/lsb-release'))
Jon Salz8fa8e832012-07-13 19:04:09 +08001447 self.time_sanitizer.RunOnce()
1448
Vic Yangd8990da2013-06-27 16:57:43 +08001449 if self.test_list.options.check_cpu_usage_period_secs:
1450 self.cpu_usage_watcher = Spawn(['py/tools/cpu_usage_monitor.py',
1451 '-p', str(self.test_list.options.check_cpu_usage_period_secs)],
1452 cwd=factory.FACTORY_PATH)
1453
Jon Salz0697cbf2012-07-04 15:14:04 +08001454 self.init_states()
1455 self.start_event_server()
Hung-Te Lincc41d2a2014-10-29 13:35:20 +08001456
1457 if self.options.dummy_connection_manager:
1458 # Override network manager creation to dummy implmenetation.
1459 logging.info('Using dummy network manager (--dummy_connection_manager).')
1460 self.connection_manager = connection_manager.DummyConnectionManager()
1461 else:
1462 self.connection_manager = self.env.create_connection_manager(
1463 self.test_list.options.wlans,
1464 self.test_list.options.scan_wifi_period_secs)
1465
Jon Salz0697cbf2012-07-04 15:14:04 +08001466 # Note that we create a log watcher even if
1467 # sync_event_log_period_secs isn't set (no background
1468 # syncing), since we may use it to flush event logs as well.
1469 self.log_watcher = EventLogWatcher(
1470 self.test_list.options.sync_event_log_period_secs,
Jon Salzd15bbcf2013-05-21 17:33:57 +08001471 event_log_db_file=None,
Jon Salz16d10542012-07-23 12:18:45 +08001472 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001473 if self.test_list.options.sync_event_log_period_secs:
1474 self.log_watcher.StartWatchThread()
1475
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001476 # Creates a system log manager to scan logs periocially.
1477 # A scan includes clearing logs and optionally syncing logs if
1478 # enable_syng_log is True. We kick it to sync logs.
1479 self.system_log_manager = SystemLogManager(
1480 sync_log_paths=self.test_list.options.sync_log_paths,
1481 sync_log_period_secs=self.test_list.options.sync_log_period_secs,
1482 scan_log_period_secs=self.test_list.options.scan_log_period_secs,
Cheng-Yi Chiangb8a491c2014-01-20 14:37:57 +08001483 clear_log_paths=self.test_list.options.clear_log_paths,
1484 clear_log_excluded_paths=self.test_list.options.clear_log_excluded_paths)
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001485 self.system_log_manager.Start()
Cheng-Yi Chiang344b10f2013-05-03 16:44:03 +08001486
Jon Salz0697cbf2012-07-04 15:14:04 +08001487 self.update_system_info()
1488
Vic Yang4953fc12012-07-26 16:19:53 +08001489 assert ((self.test_list.options.min_charge_pct is None) ==
1490 (self.test_list.options.max_charge_pct is None))
Vic Yange83d9a12013-04-19 20:00:20 +08001491 if utils.in_chroot():
1492 logging.info('In chroot, ignoring charge manager and charge state')
Ricky Liangc392a1c2014-06-20 18:24:59 +08001493 elif (self.test_list.options.enable_charge_manager and
1494 self.test_list.options.min_charge_pct is not None):
Vic Yang4953fc12012-07-26 16:19:53 +08001495 self.charge_manager = ChargeManager(self.test_list.options.min_charge_pct,
1496 self.test_list.options.max_charge_pct)
Jon Salzad7353b2012-10-15 16:22:46 +08001497 system.SystemStatus.charge_manager = self.charge_manager
Cheng-Yi Chiangd8186952013-04-04 23:41:14 +08001498 else:
1499 # Goofy should set charger state to charge if charge_manager is disabled.
Dean Liao88b93192014-10-23 19:37:41 +08001500 self.charge()
Vic Yang4953fc12012-07-26 16:19:53 +08001501
Vic Yang6cee2472014-10-22 17:18:52 -07001502 if CoreDumpManager.CoreDumpEnabled():
1503 self.core_dump_manager = CoreDumpManager(
1504 self.test_list.options.core_dump_watchlist)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001505
Jon Salz0697cbf2012-07-04 15:14:04 +08001506 os.environ['CROS_FACTORY'] = '1'
1507 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1508
Shuo-Peng Liao1ff502e2013-06-30 18:37:02 +08001509 if not utils.in_chroot() and self.test_list.options.use_cpufreq_manager:
Ricky Liangecddbd42014-07-24 11:32:10 +08001510 logging.info('Enabling CPU frequency manager')
Jon Salzddf0d052013-06-18 12:52:44 +08001511 self.cpufreq_manager = CpufreqManager(event_log=self.event_log)
Jon Salzce6a7f82013-06-10 18:22:54 +08001512
Justin Chuang31b02432013-06-27 15:16:51 +08001513 # Startup hooks may want to skip some tests.
1514 self.update_skipped_tests()
Jon Salz416f9cc2013-05-10 18:32:50 +08001515
Jon Salze12c2b32013-06-25 16:24:34 +08001516 self.find_kcrashes()
1517
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001518 # Should not move earlier.
1519 self.hooks.OnStartup()
1520
Ricky Liang36512a32014-07-25 11:47:04 +08001521 # Only after this point the Goofy backend is ready for UI connection.
1522 self.ready_for_ui_connection = True
1523
Ricky Liang650f6bf2012-09-28 13:22:54 +08001524 # Create download path for autotest beforehand or autotests run at
1525 # the same time might fail due to race condition.
1526 if not factory.in_chroot():
1527 utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1528 'download'))
1529
Jon Salz0697cbf2012-07-04 15:14:04 +08001530 def state_change_callback(test, test_state):
1531 self.event_client.post_event(
Ricky Liang4bff3e32014-02-20 18:46:11 +08001532 Event(Event.Type.STATE_CHANGE, path=test.path, state=test_state))
Jon Salz0697cbf2012-07-04 15:14:04 +08001533 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001534
Vic Yange2c76a82014-10-30 12:48:19 -07001535 self.autotest_prespawner = prespawner.AutotestPrespawner()
1536 self.autotest_prespawner.start()
1537
1538 self.pytest_prespawner = prespawner.PytestPrespawner()
1539 self.pytest_prespawner.start()
Jon Salza6711d72012-07-18 14:33:03 +08001540
Ricky Liang48e47f92014-02-26 19:31:51 +08001541 tests_after_shutdown = self.state_instance.get_shared_data(
1542 'tests_after_shutdown', optional=True)
Jon Salz57717ca2012-04-04 16:47:25 +08001543
Jon Salz5c344f62012-07-13 14:31:16 +08001544 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
1545 if not force_auto_run and tests_after_shutdown is not None:
Ricky Liang48e47f92014-02-26 19:31:51 +08001546 logging.info('Resuming tests after shutdown: %s', tests_after_shutdown)
Jon Salz0697cbf2012-07-04 15:14:04 +08001547 self.tests_to_run.extend(
Ricky Liang4bff3e32014-02-20 18:46:11 +08001548 self.test_list.lookup_path(t) for t in tests_after_shutdown)
Peter Ammon1e1ec572014-06-26 17:56:32 -07001549 self.run_enqueue(self.run_next_test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001550 else:
Jon Salz5c344f62012-07-13 14:31:16 +08001551 if force_auto_run or self.test_list.options.auto_run_on_start:
Ricky Liang117484a2014-04-14 11:14:41 +08001552 # If automation mode is enabled, allow suppress auto_run_on_start.
1553 if (self.options.automation_mode == 'NONE' or
1554 self.options.auto_run_on_start):
Peter Ammon1e1ec572014-06-26 17:56:32 -07001555 self.run_enqueue(
Ricky Liang117484a2014-04-14 11:14:41 +08001556 lambda: self.run_tests(self.test_list, untested_only=True))
Jon Salz5c344f62012-07-13 14:31:16 +08001557 self.state_instance.set_shared_data('tests_after_shutdown', None)
Ricky Liang4bff3e32014-02-20 18:46:11 +08001558 self.restore_active_run_state()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001559
Vic Yang08505c72015-01-06 17:01:53 -08001560 system.GetBoard().OnTestStart()
1561
Dean Liao592e4d52013-01-10 20:06:39 +08001562 self.may_disable_cros_shortcut_keys()
1563
1564 def may_disable_cros_shortcut_keys(self):
1565 test_options = self.test_list.options
1566 if test_options.disable_cros_shortcut_keys:
1567 logging.info('Filter ChromeOS shortcut keys.')
1568 self.key_filter = KeyFilter(
1569 unmap_caps_lock=test_options.disable_caps_lock,
1570 caps_lock_keycode=test_options.caps_lock_keycode)
1571 self.key_filter.Start()
1572
Jon Salz0e6532d2012-10-25 16:30:11 +08001573 def _should_sync_time(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001574 """Returns True if we should attempt syncing time with shopfloor.
Jon Salz0e6532d2012-10-25 16:30:11 +08001575
1576 Args:
1577 foreground: If True, synchronizes even if background syncing
1578 is disabled (e.g., in explicit sync requests from the
1579 SyncShopfloor test).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001580 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001581 return ((foreground or
1582 self.test_list.options.sync_time_period_secs) and
Jon Salz54882d02012-08-31 01:57:54 +08001583 self.time_sanitizer and
1584 (not self.time_synced) and
1585 (not factory.in_chroot()))
1586
Jon Salz0e6532d2012-10-25 16:30:11 +08001587 def sync_time_with_shopfloor_server(self, foreground=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001588 """Syncs time with shopfloor server, if not yet synced.
Jon Salz54882d02012-08-31 01:57:54 +08001589
Jon Salz0e6532d2012-10-25 16:30:11 +08001590 Args:
1591 foreground: If True, synchronizes even if background syncing
1592 is disabled (e.g., in explicit sync requests from the
1593 SyncShopfloor test).
1594
Jon Salz54882d02012-08-31 01:57:54 +08001595 Returns:
1596 False if no time sanitizer is available, or True if this sync (or a
1597 previous sync) succeeded.
1598
1599 Raises:
1600 Exception if unable to contact the shopfloor server.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001601 """
Jon Salz0e6532d2012-10-25 16:30:11 +08001602 if self._should_sync_time(foreground):
Jon Salz54882d02012-08-31 01:57:54 +08001603 self.time_sanitizer.SyncWithShopfloor()
1604 self.time_synced = True
1605 return self.time_synced
1606
Jon Salzb92c5112012-09-21 15:40:11 +08001607 def log_disk_space_stats(self):
Jon Salz18e0e022013-06-11 17:13:39 +08001608 if (utils.in_chroot() or
1609 not self.test_list.options.log_disk_space_period_secs):
Jon Salzb92c5112012-09-21 15:40:11 +08001610 return
1611
1612 now = time.time()
1613 if (self.last_log_disk_space_time and
1614 now - self.last_log_disk_space_time <
1615 self.test_list.options.log_disk_space_period_secs):
1616 return
1617 self.last_log_disk_space_time = now
1618
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001619 # Upload event if stateful partition usage is above threshold.
1620 # Stateful partition is mounted on /usr/local, while
1621 # encrypted stateful partition is mounted on /var.
1622 # If there are too much logs in the factory process,
1623 # these two partitions might get full.
Jon Salzb92c5112012-09-21 15:40:11 +08001624 try:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001625 vfs_infos = disk_space.GetAllVFSInfo()
1626 stateful_info, encrypted_info = None, None
1627 for vfs_info in vfs_infos.values():
1628 if '/usr/local' in vfs_info.mount_points:
1629 stateful_info = vfs_info
1630 if '/var' in vfs_info.mount_points:
1631 encrypted_info = vfs_info
1632
1633 stateful = disk_space.GetPartitionUsage(stateful_info)
1634 encrypted = disk_space.GetPartitionUsage(encrypted_info)
1635
1636 above_threshold = (
1637 self.test_list.options.stateful_usage_threshold and
1638 max(stateful.bytes_used_pct,
1639 stateful.inodes_used_pct,
1640 encrypted.bytes_used_pct,
1641 encrypted.inodes_used_pct) >
1642 self.test_list.options.stateful_usage_threshold)
1643
1644 if above_threshold:
1645 self.event_log.Log('stateful_partition_usage',
1646 partitions={
1647 'stateful': {
1648 'bytes_used_pct': FloatDigit(stateful.bytes_used_pct, 2),
1649 'inodes_used_pct': FloatDigit(stateful.inodes_used_pct, 2)},
1650 'encrypted_stateful': {
1651 'bytes_used_pct': FloatDigit(encrypted.bytes_used_pct, 2),
1652 'inodes_used_pct': FloatDigit(encrypted.inodes_used_pct, 2)}
1653 })
1654 self.log_watcher.ScanEventLogs()
Cheng-Yi Chiang00798e72013-06-20 18:16:39 +08001655 if (not utils.in_chroot() and
1656 self.test_list.options.stateful_usage_above_threshold_action):
1657 Spawn(self.test_list.options.stateful_usage_above_threshold_action,
1658 call=True)
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001659
1660 message = disk_space.FormatSpaceUsedAll(vfs_infos)
Jon Salz3c493bb2013-02-07 17:24:58 +08001661 if message != self.last_log_disk_space_message:
Cheng-Yi Chiangd0406522013-04-01 15:40:18 +08001662 if above_threshold:
1663 logging.warning(message)
1664 else:
1665 logging.info(message)
Jon Salz3c493bb2013-02-07 17:24:58 +08001666 self.last_log_disk_space_message = message
Jon Salzb92c5112012-09-21 15:40:11 +08001667 except: # pylint: disable=W0702
1668 logging.exception('Unable to get disk space used')
1669
Justin Chuang83813982013-05-13 01:26:32 +08001670 def check_battery(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001671 """Checks the current battery status.
Justin Chuang83813982013-05-13 01:26:32 +08001672
1673 Logs current battery charging level and status to log. If the battery level
1674 is lower below warning_low_battery_pct, send warning event to shopfloor.
1675 If the battery level is lower below critical_low_battery_pct, flush disks.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001676 """
Justin Chuang83813982013-05-13 01:26:32 +08001677 if not self.test_list.options.check_battery_period_secs:
1678 return
1679
1680 now = time.time()
1681 if (self.last_check_battery_time and
1682 now - self.last_check_battery_time <
1683 self.test_list.options.check_battery_period_secs):
1684 return
1685 self.last_check_battery_time = now
1686
1687 message = ''
1688 log_level = logging.INFO
1689 try:
1690 power = system.GetBoard().power
1691 if not power.CheckBatteryPresent():
1692 message = 'Battery is not present'
1693 else:
1694 ac_present = power.CheckACPresent()
1695 charge_pct = power.GetChargePct(get_float=True)
1696 message = ('Current battery level %.1f%%, AC charger is %s' %
1697 (charge_pct, 'connected' if ac_present else 'disconnected'))
1698
1699 if charge_pct > self.test_list.options.critical_low_battery_pct:
1700 critical_low_battery = False
1701 else:
1702 critical_low_battery = True
1703 # Only sync disks when battery level is still above minimum
1704 # value. This can be used for offline analysis when shopfloor cannot
1705 # be connected.
1706 if charge_pct > MIN_BATTERY_LEVEL_FOR_DISK_SYNC:
1707 logging.warning('disk syncing for critical low battery situation')
1708 os.system('sync; sync; sync')
1709 else:
1710 logging.warning('disk syncing is cancelled '
1711 'because battery level is lower than %.1f',
1712 MIN_BATTERY_LEVEL_FOR_DISK_SYNC)
1713
1714 # Notify shopfloor server
1715 if (critical_low_battery or
1716 (not ac_present and
1717 charge_pct <= self.test_list.options.warning_low_battery_pct)):
1718 log_level = logging.WARNING
1719
1720 self.event_log.Log('low_battery',
1721 battery_level=charge_pct,
1722 charger_connected=ac_present,
1723 critical=critical_low_battery)
1724 self.log_watcher.KickWatchThread()
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001725 if self.test_list.options.enable_sync_log:
1726 self.system_log_manager.KickToSync()
Justin Chuang83813982013-05-13 01:26:32 +08001727 except: # pylint: disable=W0702
1728 logging.exception('Unable to check battery or notify shopfloor')
1729 finally:
1730 if message != self.last_check_battery_message:
1731 logging.log(log_level, message)
1732 self.last_check_battery_message = message
1733
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001734 def check_core_dump(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001735 """Checks if there is any core dumped file.
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001736
1737 Removes unwanted core dump files immediately.
1738 Syncs those files matching watch list to server with a delay between
1739 each sync. After the files have been synced to server, deletes the files.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001740 """
Vic Yang6cee2472014-10-22 17:18:52 -07001741 if not self.core_dump_manager:
1742 return
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001743 core_dump_files = self.core_dump_manager.ScanFiles()
1744 if core_dump_files:
1745 now = time.time()
1746 if (self.last_kick_sync_time and now - self.last_kick_sync_time <
1747 self.test_list.options.kick_sync_min_interval_secs):
1748 return
1749 self.last_kick_sync_time = now
1750
1751 # Sends event to server
1752 self.event_log.Log('core_dumped', files=core_dump_files)
1753 self.log_watcher.KickWatchThread()
1754
1755 # Syncs files to server
Cheng-Yi Chianga0f6eff2014-01-09 18:27:22 +08001756 if self.test_list.options.enable_sync_log:
1757 self.system_log_manager.KickToSync(
Cheng-Yi Chiangd3516a32013-07-17 15:30:47 +08001758 core_dump_files, self.core_dump_manager.ClearFiles)
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001759
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001760 def check_log_rotation(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001761 """Checks log rotation file presence/absence according to test_list option.
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001762
1763 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1764 is True, delete it otherwise. This must be done in idle loop because
1765 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1766 an autotest.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001767 """
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001768 if utils.in_chroot():
1769 return
1770 try:
1771 if self.test_list.options.disable_log_rotation:
1772 open(CLEANUP_LOGS_PAUSED, 'w').close()
1773 else:
1774 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1775 except: # pylint: disable=W0702
1776 # Oh well. Logs an error (but no trace)
1777 logging.info(
1778 'Unable to %s %s: %s',
1779 'touch' if self.test_list.options.disable_log_rotation else 'delete',
1780 CLEANUP_LOGS_PAUSED, utils.FormatExceptionOnly())
1781
Jon Salz8fa8e832012-07-13 19:04:09 +08001782 def sync_time_in_background(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001783 """Writes out current time and tries to sync with shopfloor server."""
Jon Salzb22d1172012-08-06 10:38:57 +08001784 if not self.time_sanitizer:
1785 return
1786
1787 # Write out the current time.
1788 self.time_sanitizer.SaveTime()
1789
Jon Salz54882d02012-08-31 01:57:54 +08001790 if not self._should_sync_time():
Jon Salz8fa8e832012-07-13 19:04:09 +08001791 return
1792
1793 now = time.time()
1794 if self.last_sync_time and (
1795 now - self.last_sync_time <
1796 self.test_list.options.sync_time_period_secs):
1797 # Not yet time for another check.
1798 return
1799 self.last_sync_time = now
1800
1801 def target():
1802 try:
Jon Salz54882d02012-08-31 01:57:54 +08001803 self.sync_time_with_shopfloor_server()
Jon Salz8fa8e832012-07-13 19:04:09 +08001804 except: # pylint: disable=W0702
1805 # Oh well. Log an error (but no trace)
1806 logging.info(
1807 'Unable to get time from shopfloor server: %s',
1808 utils.FormatExceptionOnly())
1809
1810 thread = threading.Thread(target=target)
1811 thread.daemon = True
1812 thread.start()
1813
Peter Ammon1e1ec572014-06-26 17:56:32 -07001814 def perform_periodic_tasks(self):
1815 """Override of base method to perform periodic work.
Vic Yang4953fc12012-07-26 16:19:53 +08001816
Peter Ammon1e1ec572014-06-26 17:56:32 -07001817 This method must not raise exceptions.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001818 """
Peter Ammon1e1ec572014-06-26 17:56:32 -07001819 super(Goofy, self).perform_periodic_tasks()
Jon Salzb22d1172012-08-06 10:38:57 +08001820
Vic Yang311ddb82012-09-26 12:08:28 +08001821 self.check_exclusive()
cychiang21886742012-07-05 15:16:32 +08001822 self.check_for_updates()
Jon Salz8fa8e832012-07-13 19:04:09 +08001823 self.sync_time_in_background()
Jon Salzb92c5112012-09-21 15:40:11 +08001824 self.log_disk_space_stats()
Justin Chuang83813982013-05-13 01:26:32 +08001825 self.check_battery()
Cheng-Yi Chiangcdfa4182013-05-05 03:20:19 +08001826 self.check_core_dump()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001827 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001828
Jon Salzd15bbcf2013-05-21 17:33:57 +08001829 def handle_event_logs(self, chunks):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001830 """Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001831
Jon Salz0697cbf2012-07-04 15:14:04 +08001832 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001833
1834 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001835 chunks: A list of Chunk objects.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001836 """
Vic Yang93027612013-05-06 02:42:49 +08001837 first_exception = None
1838 exception_count = 0
1839
Jon Salzd15bbcf2013-05-21 17:33:57 +08001840 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001841 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001842 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001843 start_time = time.time()
1844 shopfloor_client = shopfloor.get_instance(
1845 detect=True,
1846 timeout=self.test_list.options.shopfloor_timeout_secs)
Jon Salzd15bbcf2013-05-21 17:33:57 +08001847 shopfloor_client.UploadEvent(chunk.log_name + "." +
1848 event_log.GetReimageId(),
1849 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001850 logging.info(
1851 'Successfully synced %s in %.03f s',
1852 description, time.time() - start_time)
1853 except: # pylint: disable=W0702
Jon Salzd15bbcf2013-05-21 17:33:57 +08001854 first_exception = (first_exception or (chunk.log_name + ': ' +
Vic Yang93027612013-05-06 02:42:49 +08001855 utils.FormatExceptionOnly()))
1856 exception_count += 1
1857
1858 if exception_count:
1859 if exception_count == 1:
1860 msg = 'Log upload failed: %s' % first_exception
1861 else:
1862 msg = '%d log upload failed; first is: %s' % (
1863 exception_count, first_exception)
1864 raise Exception(msg)
1865
Jon Salz57717ca2012-04-04 16:47:25 +08001866
Jon Salz0697cbf2012-07-04 15:14:04 +08001867 def run_tests_with_status(self, statuses_to_run, starting_at=None,
1868 root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001869 """Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001870
Jon Salz0697cbf2012-07-04 15:14:04 +08001871 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001872
Jon Salz0697cbf2012-07-04 15:14:04 +08001873 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001874 statuses_to_run: The particular status that caller wants to run.
Jon Salz0697cbf2012-07-04 15:14:04 +08001875 starting_at: If provided, only auto-runs tests beginning with
1876 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001877 root: The root of tests to run. If not provided, it will be
1878 the root of all tests.
1879 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001880 root = root or self.test_list
Jon Salz57717ca2012-04-04 16:47:25 +08001881
Jon Salz0697cbf2012-07-04 15:14:04 +08001882 if starting_at:
1883 # Make sure they passed a test, not a string.
1884 assert isinstance(starting_at, factory.FactoryTest)
Jon Salz0405ab52012-03-16 15:26:52 +08001885
Jon Salz0697cbf2012-07-04 15:14:04 +08001886 tests_to_reset = []
1887 tests_to_run = []
Jon Salz0405ab52012-03-16 15:26:52 +08001888
Jon Salz0697cbf2012-07-04 15:14:04 +08001889 found_starting_at = False
Jon Salz0405ab52012-03-16 15:26:52 +08001890
Jon Salz0697cbf2012-07-04 15:14:04 +08001891 for test in root.get_top_level_tests():
1892 if starting_at:
1893 if test == starting_at:
1894 # We've found starting_at; do auto-run on all
1895 # subsequent tests.
1896 found_starting_at = True
1897 if not found_starting_at:
1898 # Don't start this guy yet
1899 continue
Jon Salz0405ab52012-03-16 15:26:52 +08001900
Jon Salz0697cbf2012-07-04 15:14:04 +08001901 status = test.get_state().status
1902 if status == TestState.ACTIVE or status in statuses_to_run:
1903 # Reset the test (later; we will need to abort
1904 # all active tests first).
1905 tests_to_reset.append(test)
1906 if status in statuses_to_run:
1907 tests_to_run.append(test)
Jon Salz0405ab52012-03-16 15:26:52 +08001908
Jon Salz6dc031d2013-06-19 13:06:23 +08001909 self.abort_active_tests('Operator requested run/re-run of certain tests')
Jon Salz258a40c2012-04-19 12:34:01 +08001910
Jon Salz0697cbf2012-07-04 15:14:04 +08001911 # Reset all statuses of the tests to run (in case any tests were active;
1912 # we want them to be run again).
1913 for test_to_reset in tests_to_reset:
1914 for test in test_to_reset.walk():
1915 test.update_state(status=TestState.UNTESTED)
Jon Salz57717ca2012-04-04 16:47:25 +08001916
Jon Salz0697cbf2012-07-04 15:14:04 +08001917 self.run_tests(tests_to_run, untested_only=True)
Jon Salz0405ab52012-03-16 15:26:52 +08001918
Jon Salz0697cbf2012-07-04 15:14:04 +08001919 def restart_tests(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001920 """Restarts all tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001921 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001922
Jon Salz6dc031d2013-06-19 13:06:23 +08001923 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001924 for test in root.walk():
Ricky Liangfea4ac92014-08-21 11:55:59 +08001925 test.update_state(status=TestState.UNTESTED)
Jon Salz0697cbf2012-07-04 15:14:04 +08001926 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001927
Jon Salz0697cbf2012-07-04 15:14:04 +08001928 def auto_run(self, starting_at=None, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001929 """"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001930
Jon Salz0697cbf2012-07-04 15:14:04 +08001931 Args:
1932 starting_at: If provide, only auto-runs tests beginning with
1933 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001934 root: If provided, the root of tests to run. If not provided, the root
1935 will be test_list (root of all tests).
1936 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001937 root = root or self.test_list
1938 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
1939 starting_at=starting_at,
1940 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001941
Jon Salz0697cbf2012-07-04 15:14:04 +08001942 def re_run_failed(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001943 """Re-runs failed tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001944 root = root or self.test_list
1945 self.run_tests_with_status([TestState.FAILED], root=root)
Jon Salz57717ca2012-04-04 16:47:25 +08001946
Jon Salz0697cbf2012-07-04 15:14:04 +08001947 def show_review_information(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001948 """Event handler for showing review information screen.
Jon Salz57717ca2012-04-04 16:47:25 +08001949
Peter Ammon1e1ec572014-06-26 17:56:32 -07001950 The information screen is rendered by main UI program (ui.py), so in
Jon Salz0697cbf2012-07-04 15:14:04 +08001951 goofy we only need to kill all active tests, set them as untested, and
1952 clear remaining tests.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001953 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001954 self.kill_active_tests(False)
Jon Salza6711d72012-07-18 14:33:03 +08001955 self.cancel_pending_tests()
Jon Salz57717ca2012-04-04 16:47:25 +08001956
Jon Salz0697cbf2012-07-04 15:14:04 +08001957 def handle_switch_test(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001958 """Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001959
Ricky Liang6fe218c2013-12-27 15:17:17 +08001960 Args:
1961 event: The SWITCH_TEST event.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001962 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001963 test = self.test_list.lookup_path(event.path)
1964 if not test:
1965 logging.error('Unknown test %r', event.key)
1966 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001967
Jon Salz0697cbf2012-07-04 15:14:04 +08001968 invoc = self.invocations.get(test)
1969 if invoc and test.backgroundable:
1970 # Already running: just bring to the front if it
1971 # has a UI.
1972 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001973 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001974 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001975
Jon Salz6dc031d2013-06-19 13:06:23 +08001976 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001977 for t in test.walk():
1978 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001979
Jon Salz0697cbf2012-07-04 15:14:04 +08001980 if self.test_list.options.auto_run_on_keypress:
1981 self.auto_run(starting_at=test)
1982 else:
1983 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001984
Jon Salz0697cbf2012-07-04 15:14:04 +08001985 def wait(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001986 """Waits for all pending invocations.
Jon Salz0697cbf2012-07-04 15:14:04 +08001987
1988 Useful for testing.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001989 """
Jon Salz1acc8742012-07-17 17:45:55 +08001990 while self.invocations:
1991 for k, v in self.invocations.iteritems():
1992 logging.info('Waiting for %s to complete...', k)
1993 v.thread.join()
1994 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001995
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001996if __name__ == '__main__':
Peter Ammona3d298c2014-09-23 10:11:02 -07001997 Goofy.run_main_and_exit()