blob: 3ff963c7a9f825efa8db4912143463a6000b0961 [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
Wei-Han Chenc17b4112016-11-22 14:56:51 +080013import itertools
Jon Salz0405ab52012-03-16 15:26:52 +080014import logging
Wei-Han Chenc17b4112016-11-22 14:56:51 +080015from optparse import OptionParser
Jon Salz0405ab52012-03-16 15:26:52 +080016import os
Jon Salze12c2b32013-06-25 16:24:34 +080017import shutil
Jon Salz77c151e2012-08-28 07:20:37 +080018import signal
Jon Salz0405ab52012-03-16 15:26:52 +080019import sys
Jon Salzeff94182013-06-19 15:06:28 +080020import syslog
Jon Salz0405ab52012-03-16 15:26:52 +080021import threading
22import time
23import traceback
Jon Salz258a40c2012-04-19 12:34:01 +080024import uuid
Jon Salzb10cf512012-08-09 17:29:21 +080025from xmlrpclib import Binary
Hung-Te Linf2f78f72012-02-08 19:27:11 +080026
Jon Salz0697cbf2012-07-04 15:14:04 +080027import factory_common # pylint: disable=W0611
Hung-Te Linb6287242016-05-18 14:39:05 +080028from cros.factory.device import device_utils
Vic Yangd80ea752014-09-24 16:07:14 +080029from cros.factory.goofy.goofy_base import GoofyBase
30from cros.factory.goofy.goofy_rpc import GoofyRPC
Earl Ouacbe99c2017-02-21 16:04:19 +080031from cros.factory.goofy import goofy_server
Vic Yangd80ea752014-09-24 16:07:14 +080032from cros.factory.goofy.invocation import TestInvocation
33from cros.factory.goofy.link_manager import PresenterLinkManager
Earl Oua3bca122016-10-21 16:00:30 +080034from cros.factory.goofy.plugins import plugin_controller
Vic Yange2c76a82014-10-30 12:48:19 -070035from cros.factory.goofy import prespawner
Wei-Ning Huang38b75f02015-02-25 18:25:14 +080036from cros.factory.goofy.terminal_manager import TerminalManager
Earl Oua3bca122016-10-21 16:00:30 +080037from cros.factory.goofy import test_environment
Wei-Han Chenc17b4112016-11-22 14:56:51 +080038from cros.factory.goofy.test_list_iterator import TestListIterator
Earl Oua3bca122016-10-21 16:00:30 +080039from cros.factory.goofy import updater
Vic Yangd80ea752014-09-24 16:07:14 +080040from cros.factory.goofy.web_socket_manager import WebSocketManager
Hung-Te Linb6287242016-05-18 14:39:05 +080041from cros.factory.test.e2e_test.common import AutomationMode
42from cros.factory.test.e2e_test.common import AutomationModePrompt
43from cros.factory.test.e2e_test.common import ParseAutomationMode
Earl Ouacbe99c2017-02-21 16:04:19 +080044from cros.factory.test.env import goofy_proxy
Hung-Te Linb6287242016-05-18 14:39:05 +080045from cros.factory.test.env import paths
Jon Salz83591782012-06-26 11:09:58 +080046from cros.factory.test.event import Event
47from cros.factory.test.event import EventClient
48from cros.factory.test.event import EventServer
Hung-Te Linb6287242016-05-18 14:39:05 +080049from cros.factory.test import event_log
50from cros.factory.test.event_log import EventLog
Hung-Te Linb6287242016-05-18 14:39:05 +080051from cros.factory.test.event_log import GetBootSequence
Hung-Te Lin91492a12014-11-25 18:56:30 +080052from cros.factory.test.event_log_watcher import EventLogWatcher
Earl Oua3bca122016-10-21 16:00:30 +080053from cros.factory.test import factory
jcliangcd688182012-08-20 21:01:26 +080054from cros.factory.test.factory import TestState
Hung-Te Lin3f096842016-01-13 17:37:06 +080055from cros.factory.test.rules import phase
Earl Oua3bca122016-10-21 16:00:30 +080056from cros.factory.test import shopfloor
57from cros.factory.test import state
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +080058from cros.factory.test.test_lists import test_lists
Earl Oua3bca122016-10-21 16:00:30 +080059from cros.factory.test import testlog
60from cros.factory.test import testlog_goofy
Hung-Te Linb6287242016-05-18 14:39:05 +080061from cros.factory.tools.key_filter import KeyFilter
Wei-Han Chen78f35f62017-03-06 20:11:20 +080062from cros.factory.utils import config_utils
Hung-Te Linf707b242016-01-08 23:11:42 +080063from cros.factory.utils import debug_utils
Jon Salz2af235d2013-06-24 14:47:21 +080064from cros.factory.utils import file_utils
Joel Kitchingb85ed7f2014-10-08 18:24:39 +080065from cros.factory.utils import net_utils
Hung-Te Lin4e6357c2016-01-08 14:32:00 +080066from cros.factory.utils import process_utils
67from cros.factory.utils import sys_utils
68from cros.factory.utils import time_utils
Hung-Te Linf707b242016-01-08 23:11:42 +080069from cros.factory.utils import type_utils
Hung-Te Linf2f78f72012-02-08 19:27:11 +080070
71
Hung-Te Linf2f78f72012-02-08 19:27:11 +080072HWID_CFG_PATH = '/usr/local/share/chromeos-hwid/cfg'
Joel Kitching625ff0f2016-05-16 14:59:40 -070073CACHES_DIR = os.path.join(paths.GetStateRoot(), 'caches')
Hung-Te Linf2f78f72012-02-08 19:27:11 +080074
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +080075CLEANUP_LOGS_PAUSED = '/var/lib/cleanup_logs_paused'
76
Jon Salz5c344f62012-07-13 14:31:16 +080077# Value for tests_after_shutdown that forces auto-run (e.g., after
78# a factory update, when the available set of tests might change).
79FORCE_AUTO_RUN = 'force_auto_run'
80
Wei-Han Chenc17b4112016-11-22 14:56:51 +080081# Key to load the test list iterator after shutdown test
82TESTS_AFTER_SHUTDOWN = 'tests_after_shutdown'
83
Justin Chuang83813982013-05-13 01:26:32 +080084
Ricky Liang45c73e72015-01-15 15:00:30 +080085MAX_CRASH_FILE_SIZE = 64 * 1024
Jon Salze12c2b32013-06-25 16:24:34 +080086
Hung-Te Linf707b242016-01-08 23:11:42 +080087Status = type_utils.Enum(['UNINITIALIZED', 'INITIALIZING', 'RUNNING',
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +080088 'TERMINATING', 'TERMINATED'])
Jon Salzd7550792013-07-12 05:49:27 +080089
Hung-Te Linf2f78f72012-02-08 19:27:11 +080090def get_hwid_cfg():
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +080091 """Returns the HWID config tag, or an empty string if none can be found."""
Jon Salz0697cbf2012-07-04 15:14:04 +080092 if 'CROS_HWID' in os.environ:
93 return os.environ['CROS_HWID']
94 if os.path.exists(HWID_CFG_PATH):
Ricky Liang45c73e72015-01-15 15:00:30 +080095 with open(HWID_CFG_PATH, 'r') as hwid_cfg_handle:
Jon Salz0697cbf2012-07-04 15:14:04 +080096 return hwid_cfg_handle.read().strip()
97 return ''
Hung-Te Linf2f78f72012-02-08 19:27:11 +080098
Jon Salz73e0fd02012-04-04 11:46:38 +080099_inited_logging = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800100
Ricky Liang45c73e72015-01-15 15:00:30 +0800101
Peter Ammon1e1ec572014-06-26 17:56:32 -0700102class Goofy(GoofyBase):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800103 """The main factory flow.
Jon Salz0697cbf2012-07-04 15:14:04 +0800104
105 Note that all methods in this class must be invoked from the main
106 (event) thread. Other threads, such as callbacks and TestInvocation
107 methods, should instead post events on the run queue.
108
109 TODO: Unit tests. (chrome-os-partner:7409)
110
111 Properties:
112 uuid: A unique UUID for this invocation of Goofy.
113 state_instance: An instance of FactoryState.
114 state_server: The FactoryState XML/RPC server.
115 state_server_thread: A thread running state_server.
116 event_server: The EventServer socket server.
117 event_server_thread: A thread running event_server.
118 event_client: A client to the event server.
Earl Oua3bca122016-10-21 16:00:30 +0800119 plugin_controller: The PluginController object.
Jon Salz0697cbf2012-07-04 15:14:04 +0800120 invocations: A map from FactoryTest objects to the corresponding
121 TestInvocations objects representing active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800122 options: Command-line options.
123 args: Command-line args.
124 test_list: The test list.
Jon Salz128b0932013-07-03 16:55:26 +0800125 test_lists: All new-style test lists.
Ricky Liang4bff3e32014-02-20 18:46:11 +0800126 run_id: The identifier for latest test run.
127 scheduled_run_tests: The list of tests scheduled for latest test run.
Jon Salz0697cbf2012-07-04 15:14:04 +0800128 event_handlers: Map of Event.Type to the method used to handle that
129 event. If the method has an 'event' argument, the event is passed
130 to the handler.
Jon Salz416f9cc2013-05-10 18:32:50 +0800131 hooks: A Hooks object containing hooks for various Goofy actions.
Jon Salzd7550792013-07-12 05:49:27 +0800132 status: The current Goofy status (a member of the Status enum).
Peter Ammon948b7172014-07-15 12:43:06 -0700133 link_manager: Instance of PresenterLinkManager for communicating
134 with GoofyPresenter
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800135 """
Ricky Liang45c73e72015-01-15 15:00:30 +0800136
Jon Salz0697cbf2012-07-04 15:14:04 +0800137 def __init__(self):
Peter Ammon1e1ec572014-06-26 17:56:32 -0700138 super(Goofy, self).__init__()
Jon Salz0697cbf2012-07-04 15:14:04 +0800139 self.uuid = str(uuid.uuid4())
140 self.state_instance = None
Earl Ouacbe99c2017-02-21 16:04:19 +0800141 self.goofy_server = None
142 self.goofy_server_thread = None
Jon Salz16d10542012-07-23 12:18:45 +0800143 self.goofy_rpc = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800144 self.event_server = None
145 self.event_server_thread = None
146 self.event_client = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800147 self.log_watcher = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800148 self.event_log = None
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800149 self.testlog = None
Vic Yange2c76a82014-10-30 12:48:19 -0700150 self.autotest_prespawner = None
Earl Oua3bca122016-10-21 16:00:30 +0800151 self.plugin_controller = None
Vic Yange2c76a82014-10-30 12:48:19 -0700152 self.pytest_prespawner = None
Vic Yanga3cecf82014-12-26 00:44:21 -0800153 self._ui_initialized = False
Jon Salzc79a9982012-08-30 04:42:01 +0800154 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800155 self.invocations = {}
Jon Salz0697cbf2012-07-04 15:14:04 +0800156 self.visible_test = None
157 self.chrome = None
Jon Salz416f9cc2013-05-10 18:32:50 +0800158 self.hooks = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800159
160 self.options = None
161 self.args = None
162 self.test_list = None
Jon Salz128b0932013-07-03 16:55:26 +0800163 self.test_lists = None
Ricky Liang4bff3e32014-02-20 18:46:11 +0800164 self.run_id = None
165 self.scheduled_run_tests = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800166 self.env = None
Jon Salzb22d1172012-08-06 10:38:57 +0800167 self.last_idle = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800168 self.last_shutdown_time = None
cychiang21886742012-07-05 15:16:32 +0800169 self.last_update_check = None
Cheng-Yi Chiang194d3c02015-03-16 14:37:15 +0800170 self._suppress_periodic_update_messages = False
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +0800171 self._suppress_event_log_error_messages = False
Earl Ouab979142016-10-25 16:48:06 +0800172 self.exclusive_resources = set()
Dean Liao592e4d52013-01-10 20:06:39 +0800173 self.key_filter = None
Jon Salzd7550792013-07-12 05:49:27 +0800174 self.status = Status.UNINITIALIZED
Ricky Liang36512a32014-07-25 11:47:04 +0800175 self.ready_for_ui_connection = False
Peter Ammon1e1ec572014-06-26 17:56:32 -0700176 self.link_manager = None
Hung-Te Linef7f2be2015-07-20 20:38:51 +0800177 self.is_restart_requested = False
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800178 self.test_list_iterator = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800179
Hung-Te Lin6a72c642015-12-13 22:09:09 +0800180 # TODO(hungte) Support controlling remote DUT.
Hung-Te Linb6287242016-05-18 14:39:05 +0800181 self.dut = device_utils.CreateDUTInterface()
Hung-Te Lin6a72c642015-12-13 22:09:09 +0800182
Jon Salz85a39882012-07-05 16:45:04 +0800183 def test_or_root(event, parent_or_group=True):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800184 """Returns the test affected by a particular event.
Jon Salz85a39882012-07-05 16:45:04 +0800185
186 Args:
187 event: The event containing an optional 'path' attribute.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800188 parent_or_group: If True, returns the top-level parent for a test (the
Jon Salz85a39882012-07-05 16:45:04 +0800189 root node of the tests that need to be run together if the given test
190 path is to be run).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800191 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800192 try:
193 path = event.path
194 except AttributeError:
195 path = None
196
197 if path:
Jon Salz85a39882012-07-05 16:45:04 +0800198 test = self.test_list.lookup_path(path)
199 if parent_or_group:
200 test = test.get_top_level_parent_or_group()
201 return test
Jon Salz0697cbf2012-07-04 15:14:04 +0800202 else:
203 return self.test_list
204
205 self.event_handlers = {
Ricky Liang45c73e72015-01-15 15:00:30 +0800206 Event.Type.SWITCH_TEST: self.handle_switch_test,
Ricky Liang45c73e72015-01-15 15:00:30 +0800207 Event.Type.RESTART_TESTS:
208 lambda event: self.restart_tests(root=test_or_root(event)),
209 Event.Type.AUTO_RUN:
210 lambda event: self.auto_run(root=test_or_root(event)),
Ricky Liang45c73e72015-01-15 15:00:30 +0800211 Event.Type.RUN_TESTS_WITH_STATUS:
212 lambda event: self.run_tests_with_status(
213 event.status,
214 root=test_or_root(event)),
Ricky Liang45c73e72015-01-15 15:00:30 +0800215 Event.Type.UPDATE_SYSTEM_INFO:
216 lambda event: self.update_system_info(),
217 Event.Type.STOP:
218 lambda event: self.stop(root=test_or_root(event, False),
219 fail=getattr(event, 'fail', False),
220 reason=getattr(event, 'reason', None)),
221 Event.Type.SET_VISIBLE_TEST:
222 lambda event: self.set_visible_test(
223 self.test_list.lookup_path(event.path)),
224 Event.Type.CLEAR_STATE:
225 lambda event: self.clear_state(
226 self.test_list.lookup_path(event.path)),
Wei-Ning Huang38b75f02015-02-25 18:25:14 +0800227 Event.Type.KEY_FILTER_MODE: self.handle_key_filter_mode,
Jon Salz0697cbf2012-07-04 15:14:04 +0800228 }
229
Jon Salz0697cbf2012-07-04 15:14:04 +0800230 self.web_socket_manager = None
Wei-Ning Huang38b75f02015-02-25 18:25:14 +0800231 self.terminal_manager = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800232
233 def destroy(self):
Ricky Liang74237a02014-09-18 15:11:23 +0800234 """Performs any shutdown tasks. Overrides base class method."""
Jon Salzd7550792013-07-12 05:49:27 +0800235 self.status = Status.TERMINATING
Jon Salz0697cbf2012-07-04 15:14:04 +0800236 if self.chrome:
237 self.chrome.kill()
238 self.chrome = None
Jon Salzc79a9982012-08-30 04:42:01 +0800239 if self.dummy_shopfloor:
240 self.dummy_shopfloor.kill()
241 self.dummy_shopfloor = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800242 if self.web_socket_manager:
243 logging.info('Stopping web sockets')
244 self.web_socket_manager.close()
245 self.web_socket_manager = None
Earl Ouacbe99c2017-02-21 16:04:19 +0800246 if self.goofy_server_thread:
247 logging.info('Stopping goofy server')
248 self.goofy_server.shutdown()
249 self.goofy_server_thread.join()
250 self.goofy_server.server_close()
251 self.goofy_server_thread = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800252 if self.state_instance:
253 self.state_instance.close()
254 if self.event_server_thread:
255 logging.info('Stopping event server')
256 self.event_server.shutdown() # pylint: disable=E1101
257 self.event_server_thread.join()
258 self.event_server.server_close()
259 self.event_server_thread = None
260 if self.log_watcher:
261 if self.log_watcher.IsThreadStarted():
262 self.log_watcher.StopWatchThread()
263 self.log_watcher = None
Vic Yange2c76a82014-10-30 12:48:19 -0700264 if self.autotest_prespawner:
265 logging.info('Stopping autotest prespawner')
266 self.autotest_prespawner.stop()
267 self.autotest_prespawner = None
268 if self.pytest_prespawner:
269 logging.info('Stopping pytest prespawner')
270 self.pytest_prespawner.stop()
271 self.pytest_prespawner = None
Jon Salz0697cbf2012-07-04 15:14:04 +0800272 if self.event_client:
273 logging.info('Closing event client')
274 self.event_client.close()
275 self.event_client = None
276 if self.event_log:
277 self.event_log.Close()
278 self.event_log = None
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800279 if self.testlog:
280 self.testlog.Close()
281 self.testlog = None
Dean Liao592e4d52013-01-10 20:06:39 +0800282 if self.key_filter:
283 self.key_filter.Stop()
Peter Ammon1e1ec572014-06-26 17:56:32 -0700284 if self.link_manager:
285 self.link_manager.Stop()
286 self.link_manager = None
Earl Oua3bca122016-10-21 16:00:30 +0800287 if self.plugin_controller:
288 self.plugin_controller.StopAndDestroyAllPlugins()
289 self.plugin_controller = None
Dean Liao592e4d52013-01-10 20:06:39 +0800290
Peter Ammon1e1ec572014-06-26 17:56:32 -0700291 super(Goofy, self).destroy()
Jon Salz0697cbf2012-07-04 15:14:04 +0800292 logging.info('Done destroying Goofy')
Jon Salzd7550792013-07-12 05:49:27 +0800293 self.status = Status.TERMINATED
Jon Salz0697cbf2012-07-04 15:14:04 +0800294
Earl Ouacbe99c2017-02-21 16:04:19 +0800295 def start_goofy_server(self):
296 self.goofy_server = goofy_server.GoofyServer(
297 (goofy_proxy.DEFAULT_GOOFY_ADDRESS, goofy_proxy.DEFAULT_GOOFY_PORT))
298 logging.info('Starting goofy server')
299 self.goofy_server_thread = threading.Thread(
300 target=self.goofy_server.serve_forever,
301 name='GoofyServer')
302 self.goofy_server_thread.start()
303
304 # Setup static file path
305 self.goofy_server.RegisterPath(
306 '/', os.path.join(paths.FACTORY_PACKAGE_PATH, 'goofy/static'))
307
308 def init_state_instance(self):
Jon Salz2af235d2013-06-24 14:47:21 +0800309 # Before starting state server, remount stateful partitions with
310 # no commit flag. The default commit time (commit=600) makes corruption
311 # too likely.
Hung-Te Lin1968d9c2016-01-08 22:55:46 +0800312 sys_utils.ResetCommitTime()
Earl Ouacbe99c2017-02-21 16:04:19 +0800313 self.state_instance = state.FactoryState()
314 self.goofy_server.AddRPCInstance(goofy_proxy.STATE_URL, self.state_instance)
Jon Salz2af235d2013-06-24 14:47:21 +0800315
Earl Ouacbe99c2017-02-21 16:04:19 +0800316 # Setup Goofy RPC.
317 # TODO(shunhsingou): separate goofy_rpc and state server instead of
318 # injecting goofy_rpc functions into state.
Jon Salz16d10542012-07-23 12:18:45 +0800319 self.goofy_rpc = GoofyRPC(self)
320 self.goofy_rpc.RegisterMethods(self.state_instance)
Jon Salz0697cbf2012-07-04 15:14:04 +0800321
322 def start_event_server(self):
323 self.event_server = EventServer()
324 logging.info('Starting factory event server')
325 self.event_server_thread = threading.Thread(
Ricky Liang45c73e72015-01-15 15:00:30 +0800326 target=self.event_server.serve_forever,
327 name='EventServer') # pylint: disable=E1101
Jon Salz0697cbf2012-07-04 15:14:04 +0800328 self.event_server_thread.start()
329
330 self.event_client = EventClient(
Ricky Liang45c73e72015-01-15 15:00:30 +0800331 callback=self.handle_event, event_loop=self.run_queue)
Jon Salz0697cbf2012-07-04 15:14:04 +0800332
333 self.web_socket_manager = WebSocketManager(self.uuid)
Earl Ouacbe99c2017-02-21 16:04:19 +0800334 self.goofy_server.AddHTTPGetHandler(
335 '/event', self.web_socket_manager.handle_web_socket)
Jon Salz0697cbf2012-07-04 15:14:04 +0800336
Wei-Ning Huang38b75f02015-02-25 18:25:14 +0800337 def start_terminal_server(self):
338 self.terminal_manager = TerminalManager()
Earl Ouacbe99c2017-02-21 16:04:19 +0800339 self.goofy_server.AddHTTPGetHandler(
340 '/pty', self.terminal_manager.handle_web_socket)
Wei-Ning Huang38b75f02015-02-25 18:25:14 +0800341
Jon Salz0697cbf2012-07-04 15:14:04 +0800342 def set_visible_test(self, test):
343 if self.visible_test == test:
344 return
Jon Salz2f2d42c2012-07-30 12:30:34 +0800345 if test and not test.has_ui:
346 return
Jon Salz0697cbf2012-07-04 15:14:04 +0800347
348 if test:
349 test.update_state(visible=True)
350 if self.visible_test:
351 self.visible_test.update_state(visible=False)
352 self.visible_test = test
353
Ricky Liang48e47f92014-02-26 19:31:51 +0800354 def shutdown(self, operation):
355 """Starts shutdown procedure.
356
357 Args:
Vic (Chun-Ju) Yang05b0d952014-04-28 17:39:09 +0800358 operation: The shutdown operation (reboot, full_reboot, or halt).
Ricky Liang48e47f92014-02-26 19:31:51 +0800359 """
360 active_tests = []
361 for test in self.test_list.walk():
362 if not test.is_leaf():
363 continue
364
365 test_state = test.get_state()
366 if test_state.status == TestState.ACTIVE:
367 active_tests.append(test)
368
Ricky Liang48e47f92014-02-26 19:31:51 +0800369 if not (len(active_tests) == 1 and
370 isinstance(active_tests[0], factory.ShutdownStep)):
371 logging.error(
372 'Calling Goofy shutdown outside of the shutdown factory test')
373 return
374
375 logging.info('Start Goofy shutdown (%s)', operation)
376 # Save pending test list in the state server
377 self.state_instance.set_shared_data(
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800378 TESTS_AFTER_SHUTDOWN, self.test_list_iterator)
Ricky Liang48e47f92014-02-26 19:31:51 +0800379 # Save shutdown time
380 self.state_instance.set_shared_data('shutdown_time', time.time())
381
382 with self.env.lock:
383 self.event_log.Log('shutdown', operation=operation)
384 shutdown_result = self.env.shutdown(operation)
385 if shutdown_result:
386 # That's all, folks!
Peter Ammon1e1ec572014-06-26 17:56:32 -0700387 self.run_enqueue(None)
Ricky Liang48e47f92014-02-26 19:31:51 +0800388 else:
389 # Just pass (e.g., in the chroot).
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800390 self.state_instance.set_shared_data(TESTS_AFTER_SHUTDOWN, None)
Ricky Liang48e47f92014-02-26 19:31:51 +0800391 # Send event with no fields to indicate that there is no
392 # longer a pending shutdown.
393 self.event_client.post_event(Event(Event.Type.PENDING_SHUTDOWN))
394
395 def handle_shutdown_complete(self, test):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800396 """Handles the case where a shutdown was detected during a shutdown step.
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800397
Ricky Liang6fe218c2013-12-27 15:17:17 +0800398 Args:
399 test: The ShutdownStep.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800400 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800401 test_state = test.update_state(increment_shutdown_count=1)
402 logging.info('Detected shutdown (%d of %d)',
Ricky Liang48e47f92014-02-26 19:31:51 +0800403 test_state.shutdown_count, test.iterations)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800404
Ricky Liang48e47f92014-02-26 19:31:51 +0800405 tests_after_shutdown = self.state_instance.get_shared_data(
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800406 TESTS_AFTER_SHUTDOWN, optional=True)
407
408 # Make this shutdown test the next test to run. This is to continue on
409 # post-shutdown verification in the shutdown step.
Ricky Liang48e47f92014-02-26 19:31:51 +0800410 if not tests_after_shutdown:
Ricky Liang48e47f92014-02-26 19:31:51 +0800411 self.state_instance.set_shared_data(
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800412 TESTS_AFTER_SHUTDOWN, TestListIterator(test))
413 else:
414 # unset inited, so we will start from the reboot test.
415 tests_after_shutdown.inited = False
416 self.state_instance.set_shared_data(
417 TESTS_AFTER_SHUTDOWN, tests_after_shutdown)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800418
Ricky Liang48e47f92014-02-26 19:31:51 +0800419 # Set 'post_shutdown' to inform shutdown test that a shutdown just occurred.
Ricky Liangb7eb8772014-09-15 18:05:22 +0800420 self.state_instance.set_shared_data(
421 state.POST_SHUTDOWN_TAG % test.path,
422 self.state_instance.get_test_state(test.path).invocation)
Jon Salz258a40c2012-04-19 12:34:01 +0800423
Jon Salz0697cbf2012-07-04 15:14:04 +0800424 def init_states(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800425 """Initializes all states on startup."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800426 for test in self.test_list.get_all_tests():
427 # Make sure the state server knows about all the tests,
428 # defaulting to an untested state.
429 test.update_state(update_parent=False, visible=False)
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800430
Earl Ouf76e55c2017-03-07 11:48:34 +0800431 is_unexpected_shutdown = False
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800432
Jon Salz0697cbf2012-07-04 15:14:04 +0800433 # Any 'active' tests should be marked as failed now.
434 for test in self.test_list.walk():
Jon Salza6711d72012-07-18 14:33:03 +0800435 if not test.is_leaf():
436 # Don't bother with parents; they will be updated when their
437 # children are updated.
438 continue
439
Jon Salz0697cbf2012-07-04 15:14:04 +0800440 test_state = test.get_state()
441 if test_state.status != TestState.ACTIVE:
442 continue
443 if isinstance(test, factory.ShutdownStep):
444 # Shutdown while the test was active - that's good.
Ricky Liang48e47f92014-02-26 19:31:51 +0800445 self.handle_shutdown_complete(test)
Jon Salz0697cbf2012-07-04 15:14:04 +0800446 else:
Earl Ouf76e55c2017-03-07 11:48:34 +0800447 is_unexpected_shutdown = True
Jon Salz0697cbf2012-07-04 15:14:04 +0800448 error_msg = 'Unexpected shutdown while test was running'
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800449 # TODO(itspeter): Add testlog to collect expired session infos.
Jon Salz0697cbf2012-07-04 15:14:04 +0800450 self.event_log.Log('end_test',
Ricky Liang45c73e72015-01-15 15:00:30 +0800451 path=test.path,
452 status=TestState.FAILED,
453 invocation=test.get_state().invocation,
Earl Ouf76e55c2017-03-07 11:48:34 +0800454 error_msg=error_msg)
Jon Salz0697cbf2012-07-04 15:14:04 +0800455 test.update_state(
Ricky Liang45c73e72015-01-15 15:00:30 +0800456 status=TestState.FAILED,
457 error_msg=error_msg)
Chun-Ta Lin87c2dac2015-05-02 01:35:01 -0700458 # Trigger the OnTestFailure callback.
Claire Changd1961a22015-08-05 16:15:55 +0800459 self.run_queue.put(lambda: self.test_fail(test))
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800460
Jon Salz50efe942012-07-26 11:54:10 +0800461 if not test.never_fails:
462 # For "never_fails" tests (such as "Start"), don't cancel
463 # pending tests, since reboot is expected.
464 factory.console.info('Unexpected shutdown while test %s '
465 'running; cancelling any pending tests',
466 test.path)
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800467 # cancel pending tests by replace the iterator with an empty one
468 self.state_instance.set_shared_data(
469 TESTS_AFTER_SHUTDOWN,
470 TestListIterator(None))
Jon Salz008f4ea2012-08-28 05:39:45 +0800471
Earl Ouf76e55c2017-03-07 11:48:34 +0800472 if is_unexpected_shutdown:
473 logging.warning("Unexpected shutdown.")
474 self.dut.hooks.OnUnexpectedReboot()
475
Jon Salz0697cbf2012-07-04 15:14:04 +0800476 def handle_event(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800477 """Handles an event from the event server."""
Jon Salz0697cbf2012-07-04 15:14:04 +0800478 handler = self.event_handlers.get(event.type)
479 if handler:
480 handler(event)
481 else:
482 # We don't register handlers for all event types - just ignore
483 # this event.
484 logging.debug('Unbound event type %s', event.type)
Jon Salz4f6c7172012-06-11 20:45:36 +0800485
Vic Yangaabf9fd2013-04-09 18:56:13 +0800486 def check_critical_factory_note(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800487 """Returns True if the last factory note is critical."""
Vic Yangaabf9fd2013-04-09 18:56:13 +0800488 notes = self.state_instance.get_shared_data('factory_note', True)
489 return notes and notes[-1]['level'] == 'CRITICAL'
490
Hung-Te Linef7f2be2015-07-20 20:38:51 +0800491 def schedule_restart(self):
492 """Schedules a restart event when any invocation is completed."""
493 self.is_restart_requested = True
494
495 def invocation_completion(self):
496 """Callback when an invocation is completed."""
497 if self.is_restart_requested:
498 logging.info('Restart by scheduled event.')
499 self.is_restart_requested = False
500 self.restart_tests()
501 else:
502 self.run_next_test()
503
Jon Salz0697cbf2012-07-04 15:14:04 +0800504 def run_next_test(self):
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800505 """Runs the next eligible test.
henryhsu4cc6b022014-04-22 17:12:42 +0800506
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800507 self.test_list_iterator (a TestListIterator object) will determine which
508 test should be run.
henryhsu4cc6b022014-04-22 17:12:42 +0800509 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800510 self.reap_completed_tests()
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800511
512 if self.invocations:
513 # there are tests still running, we cannot start new tests
Vic Yangaabf9fd2013-04-09 18:56:13 +0800514 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800515
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800516 if self.check_critical_factory_note():
517 logging.info('has critical factory note, stop running')
518 self.test_list_iterator.stop()
519 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800520
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800521 while True:
522 try:
523 path = self.test_list_iterator.next()
524 test = self.test_list.lookup_path(path)
525 except StopIteration:
526 logging.info('no next test, stop running')
Jon Salz0697cbf2012-07-04 15:14:04 +0800527 return
Jon Salz94eb56f2012-06-12 18:01:12 +0800528
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800529 # check if we have run all required tests
Jon Salz304a75d2012-07-06 11:14:15 +0800530 untested = set()
Jon Salza1412922012-07-23 16:04:17 +0800531 for requirement in test.require_run:
532 for i in requirement.test.walk():
533 if i == test:
Jon Salz304a75d2012-07-06 11:14:15 +0800534 # We've hit this test itself; stop checking
535 break
Jon Salza1412922012-07-23 16:04:17 +0800536 if ((i.get_state().status == TestState.UNTESTED) or
537 (requirement.passed and i.get_state().status !=
538 TestState.PASSED)):
Jon Salz304a75d2012-07-06 11:14:15 +0800539 # Found an untested test; move on to the next
540 # element in require_run.
Jon Salza1412922012-07-23 16:04:17 +0800541 untested.add(i)
Jon Salz304a75d2012-07-06 11:14:15 +0800542 break
543
544 if untested:
545 untested_paths = ', '.join(sorted([x.path for x in untested]))
546 if self.state_instance.get_shared_data('engineering_mode',
547 optional=True):
548 # In engineering mode, we'll let it go.
549 factory.console.warn('In engineering mode; running '
550 '%s even though required tests '
551 '[%s] have not completed',
552 test.path, untested_paths)
553 else:
554 # Not in engineering mode; mark it failed.
555 error_msg = ('Required tests [%s] have not been run yet'
556 % untested_paths)
557 factory.console.error('Not running %s: %s',
558 test.path, error_msg)
559 test.update_state(status=TestState.FAILED,
560 error_msg=error_msg)
561 continue
562
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800563 # okay, let's run the test
Ricky Liang48e47f92014-02-26 19:31:51 +0800564 if (isinstance(test, factory.ShutdownStep) and
Ricky Liangb7eb8772014-09-15 18:05:22 +0800565 self.state_instance.get_shared_data(
566 state.POST_SHUTDOWN_TAG % test.path, optional=True)):
Ricky Liang48e47f92014-02-26 19:31:51 +0800567 # Invoking post shutdown method of shutdown test. We should retain the
568 # iterations_left and retries_left of the original test state.
569 test_state = self.state_instance.get_test_state(test.path)
570 self._run_test(test, test_state.iterations_left,
571 test_state.retries_left)
572 else:
573 # Starts a new test run; reset iterations and retries.
574 self._run_test(test, test.iterations, test.retries)
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800575 return # to leave while
Jon Salz1acc8742012-07-17 17:45:55 +0800576
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800577 def _run_test(self, test, iterations_left=None, retries_left=None):
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800578 """Invokes the test.
579
580 The argument `test` should be either a leaf test (no subtests) or a parallel
581 test (all subtests should be run in parallel).
582 """
Vic Yanga3cecf82014-12-26 00:44:21 -0800583 if not self._ui_initialized and not test.is_no_host():
584 self.init_ui()
Jon Salz1acc8742012-07-17 17:45:55 +0800585
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800586 if test.is_leaf():
587 invoc = TestInvocation(
588 self, test, on_completion=self.invocation_completion,
589 on_test_failure=lambda: self.test_fail(test))
590 new_state = test.update_state(
591 status=TestState.ACTIVE, increment_count=1, error_msg='',
592 invocation=invoc.uuid, iterations_left=iterations_left,
593 retries_left=retries_left,
594 visible=(self.visible_test == test))
595 invoc.count = new_state.count
596 self.invocations[test] = invoc
597 if self.visible_test is None and test.has_ui:
598 self.set_visible_test(test)
599 self.check_plugins()
600 invoc.start()
601 else:
602 assert test.is_parallel()
603 for subtest in test.subtests:
604 # TODO(stimim): what if the subtests *must* be run in parallel?
605 # for example, stressapptest and countdown test.
606
607 # Make sure we don't need to skip it:
608 if not self.test_list_iterator.check_skip(subtest):
609 self._run_test(subtest, subtest.iterations, subtest.retries)
Jon Salz5f2a0672012-05-22 17:14:06 +0800610
Earl Oua3bca122016-10-21 16:00:30 +0800611 def check_plugins(self):
612 """Check plugins to be paused or resumed."""
613 exclusive_resources = set()
614 for test in self.invocations:
615 exclusive_resources = exclusive_resources.union(
616 test.get_exclusive_resources())
617 self.plugin_controller.PauseAndResumePluginByResource(exclusive_resources)
618
cychiang21886742012-07-05 15:16:32 +0800619 def check_for_updates(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800620 """Schedules an asynchronous check for updates if necessary."""
cychiang21886742012-07-05 15:16:32 +0800621 if not self.test_list.options.update_period_secs:
622 # Not enabled.
623 return
624
625 now = time.time()
626 if self.last_update_check and (
627 now - self.last_update_check <
628 self.test_list.options.update_period_secs):
629 # Not yet time for another check.
630 return
631
632 self.last_update_check = now
633
634 def handle_check_for_update(reached_shopfloor, md5sum, needs_update):
635 if reached_shopfloor:
636 new_update_md5sum = md5sum if needs_update else None
Hung-Te Line594e5d2015-12-16 02:36:05 +0800637 if self.dut.info.update_md5sum != new_update_md5sum:
cychiang21886742012-07-05 15:16:32 +0800638 logging.info('Received new update MD5SUM: %s', new_update_md5sum)
Hung-Te Line594e5d2015-12-16 02:36:05 +0800639 self.dut.info.Overrides('update_md5sum', new_update_md5sum)
Peter Ammon1e1ec572014-06-26 17:56:32 -0700640 self.run_enqueue(self.update_system_info)
Cheng-Yi Chiang194d3c02015-03-16 14:37:15 +0800641 else:
642 if not self._suppress_periodic_update_messages:
643 logging.warning('Suppress error messages for periodic update checking'
644 ' after the first one.')
645 self._suppress_periodic_update_messages = True
cychiang21886742012-07-05 15:16:32 +0800646
647 updater.CheckForUpdateAsync(
Ricky Liang45c73e72015-01-15 15:00:30 +0800648 handle_check_for_update,
Cheng-Yi Chiang194d3c02015-03-16 14:37:15 +0800649 self.test_list.options.shopfloor_timeout_secs,
650 self._suppress_periodic_update_messages)
cychiang21886742012-07-05 15:16:32 +0800651
Jon Salza6711d72012-07-18 14:33:03 +0800652 def cancel_pending_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800653 """Cancels any tests in the run queue."""
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800654 self.run_tests(None)
Jon Salza6711d72012-07-18 14:33:03 +0800655
Ricky Liang4bff3e32014-02-20 18:46:11 +0800656 def restore_active_run_state(self):
657 """Restores active run id and the list of scheduled tests."""
658 self.run_id = self.state_instance.get_shared_data('run_id', optional=True)
659 self.scheduled_run_tests = self.state_instance.get_shared_data(
660 'scheduled_run_tests', optional=True)
661
662 def set_active_run_state(self):
663 """Sets active run id and the list of scheduled tests."""
664 self.run_id = str(uuid.uuid4())
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800665 # try our best to predict which tests will be run.
666 self.scheduled_run_tests = self.test_list_iterator.get_pending_tests()
Ricky Liang4bff3e32014-02-20 18:46:11 +0800667 self.state_instance.set_shared_data('run_id', self.run_id)
668 self.state_instance.set_shared_data('scheduled_run_tests',
669 self.scheduled_run_tests)
670
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800671 def run_tests(self, subtree, status_filter=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800672 """Runs tests under subtree.
Jon Salz258a40c2012-04-19 12:34:01 +0800673
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800674 Run tests under a given subtree.
Jon Salzb1b39092012-05-03 02:05:09 +0800675
Ricky Liang6fe218c2013-12-27 15:17:17 +0800676 Args:
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800677 subtree: root of subtree to run or None to run nothing.
Chih-Yu Huang85dc63c2015-08-12 15:21:28 +0800678 status_filter: List of available test states. Only run the tests which
679 states are in the list. Set to None if all test states are available.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800680 """
Hung-Te Lin410f70a2015-12-15 14:53:42 +0800681 self.dut.hooks.OnTestStart()
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800682 self.test_list_iterator = TestListIterator(
683 subtree, status_filter, self.test_list)
684 if subtree is not None:
Ricky Liang4bff3e32014-02-20 18:46:11 +0800685 self.set_active_run_state()
Jon Salz0697cbf2012-07-04 15:14:04 +0800686 self.run_next_test()
Hung-Te Linf2f78f72012-02-08 19:27:11 +0800687
Jon Salz0697cbf2012-07-04 15:14:04 +0800688 def reap_completed_tests(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800689 """Removes completed tests from the set of active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800690
691 Also updates the visible test if it was reaped.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800692 """
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800693 test_completed = False
Jon Salz0697cbf2012-07-04 15:14:04 +0800694 for t, v in dict(self.invocations).iteritems():
695 if v.is_completed():
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800696 test_completed = True
Jon Salz1acc8742012-07-17 17:45:55 +0800697 new_state = t.update_state(**v.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800698 del self.invocations[t]
699
Johny Lin62ed2a32015-05-13 11:57:12 +0800700 # Stop on failure if flag is true and there is no retry chances.
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800701 if (self.test_list.options.stop_on_failure and
Johny Lin62ed2a32015-05-13 11:57:12 +0800702 new_state.retries_left < 0 and
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800703 new_state.status == TestState.FAILED):
704 # Clean all the tests to cause goofy to stop.
Ricky Liang45c73e72015-01-15 15:00:30 +0800705 factory.console.info('Stop on failure triggered. Empty the queue.')
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800706 self.cancel_pending_tests()
Chun-Ta Lin54e17e42012-09-06 22:05:13 +0800707
Jon Salz1acc8742012-07-17 17:45:55 +0800708 if new_state.iterations_left and new_state.status == TestState.PASSED:
709 # Play it again, Sam!
710 self._run_test(t)
Cheng-Yi Chiangce05c002013-04-04 02:13:17 +0800711 # new_state.retries_left is obtained after update.
712 # For retries_left == 0, test can still be run for the last time.
713 elif (new_state.retries_left >= 0 and
714 new_state.status == TestState.FAILED):
715 # Still have to retry, Sam!
716 self._run_test(t)
Jon Salz1acc8742012-07-17 17:45:55 +0800717
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800718 if test_completed:
Vic Yangf01c59f2013-04-19 17:37:56 +0800719 self.log_watcher.KickWatchThread()
Cheng-Yi Chiang5ac22ca2013-04-12 17:45:26 +0800720
Jon Salz0697cbf2012-07-04 15:14:04 +0800721 if (self.visible_test is None or
Jon Salz85a39882012-07-05 16:45:04 +0800722 self.visible_test not in self.invocations):
Jon Salz0697cbf2012-07-04 15:14:04 +0800723 self.set_visible_test(None)
724 # Make the first running test, if any, the visible test
725 for t in self.test_list.walk():
726 if t in self.invocations:
727 self.set_visible_test(t)
728 break
729
Jon Salz6dc031d2013-06-19 13:06:23 +0800730 def kill_active_tests(self, abort, root=None, reason=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800731 """Kills and waits for all active tests.
Jon Salz0697cbf2012-07-04 15:14:04 +0800732
Jon Salz85a39882012-07-05 16:45:04 +0800733 Args:
734 abort: True to change state of killed tests to FAILED, False for
Jon Salz0697cbf2012-07-04 15:14:04 +0800735 UNTESTED.
Jon Salz85a39882012-07-05 16:45:04 +0800736 root: If set, only kills tests with root as an ancestor.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800737 reason: If set, the abort reason.
738 """
Jon Salz0697cbf2012-07-04 15:14:04 +0800739 self.reap_completed_tests()
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800740 # since we remove objects while iterating, make a copy
741 for test, invoc in dict(self.invocations).iteritems():
Jon Salz85a39882012-07-05 16:45:04 +0800742 if root and not test.has_ancestor(root):
743 continue
744
Ricky Liang45c73e72015-01-15 15:00:30 +0800745 factory.console.info('Killing active test %s...', test.path)
Jon Salz6dc031d2013-06-19 13:06:23 +0800746 invoc.abort_and_join(reason)
Ricky Liang45c73e72015-01-15 15:00:30 +0800747 factory.console.info('Killed %s', test.path)
Jon Salz1acc8742012-07-17 17:45:55 +0800748 test.update_state(**invoc.update_state_on_completion)
Jon Salz0697cbf2012-07-04 15:14:04 +0800749 del self.invocations[test]
Jon Salz1acc8742012-07-17 17:45:55 +0800750
Jon Salz0697cbf2012-07-04 15:14:04 +0800751 if not abort:
752 test.update_state(status=TestState.UNTESTED)
753 self.reap_completed_tests()
754
Jon Salz6dc031d2013-06-19 13:06:23 +0800755 def stop(self, root=None, fail=False, reason=None):
756 self.kill_active_tests(fail, root, reason)
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800757
758 if not root:
759 self.test_list_iterator.stop()
760 else:
761 # only skip tests under `root`
762 self.test_list_iterator = itertools.dropwhile(
763 lambda path: self.test_list.lookup_path(path).has_ancestor(root),
764 self.test_list_iterator)
Jon Salz85a39882012-07-05 16:45:04 +0800765 self.run_next_test()
Jon Salz0697cbf2012-07-04 15:14:04 +0800766
Jon Salz4712ac72013-02-07 17:12:05 +0800767 def clear_state(self, root=None):
Jon Salzd7550792013-07-12 05:49:27 +0800768 if root is None:
769 root = self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +0800770 self.stop(root, reason='Clearing test state')
Jon Salz4712ac72013-02-07 17:12:05 +0800771 for f in root.walk():
772 if f.is_leaf():
773 f.update_state(status=TestState.UNTESTED)
774
Jon Salz6dc031d2013-06-19 13:06:23 +0800775 def abort_active_tests(self, reason=None):
776 self.kill_active_tests(True, reason=reason)
Jon Salz0697cbf2012-07-04 15:14:04 +0800777
778 def main(self):
Jon Salzeff94182013-06-19 15:06:28 +0800779 syslog.openlog('goofy')
780
Jon Salz0697cbf2012-07-04 15:14:04 +0800781 try:
Jon Salzd7550792013-07-12 05:49:27 +0800782 self.status = Status.INITIALIZING
Jon Salz0697cbf2012-07-04 15:14:04 +0800783 self.init()
784 self.event_log.Log('goofy_init',
Ricky Liang45c73e72015-01-15 15:00:30 +0800785 success=True)
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800786 testlog.Log(
Joel Kitching9eb203a2016-04-21 15:36:30 +0800787 testlog.StationInit({
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800788 'stationDeviceId': testlog_goofy.GetDeviceID(),
Joel Kitching21bc69b2016-07-13 08:29:52 -0700789 'stationInstallationId': testlog_goofy.GetInstallationID(),
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800790 'count': testlog_goofy.GetInitCount(),
Joel Kitching9eb203a2016-04-21 15:36:30 +0800791 'success': True}))
Jon Salz0697cbf2012-07-04 15:14:04 +0800792 except:
Joel Kitching9eb203a2016-04-21 15:36:30 +0800793 try:
794 if self.event_log:
Jon Salz0697cbf2012-07-04 15:14:04 +0800795 self.event_log.Log('goofy_init',
Ricky Liang45c73e72015-01-15 15:00:30 +0800796 success=False,
797 trace=traceback.format_exc())
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800798 if self.testlog:
799 testlog.Log(
Joel Kitching9eb203a2016-04-21 15:36:30 +0800800 testlog.StationInit({
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800801 'stationDeviceId': testlog_goofy.GetDeviceID(),
Joel Kitching21bc69b2016-07-13 08:29:52 -0700802 'stationInstallationId': testlog_goofy.GetInstallationID(),
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800803 'count': testlog_goofy.GetInitCount(),
Joel Kitching9eb203a2016-04-21 15:36:30 +0800804 'success': False,
805 'failureMessage': traceback.format_exc()}))
806 except: # pylint: disable=W0702
807 pass
Jon Salz0697cbf2012-07-04 15:14:04 +0800808 raise
809
Jon Salzd7550792013-07-12 05:49:27 +0800810 self.status = Status.RUNNING
Jon Salzeff94182013-06-19 15:06:28 +0800811 syslog.syslog('Goofy (factory test harness) starting')
Chun-Ta Lin5d12b592015-06-30 00:54:23 -0700812 syslog.syslog('Boot sequence = %d' % GetBootSequence())
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +0800813 syslog.syslog('Goofy init count = %d' % testlog_goofy.GetInitCount())
Jon Salz0697cbf2012-07-04 15:14:04 +0800814 self.run()
815
816 def update_system_info(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800817 """Updates system info."""
Hung-Te Line594e5d2015-12-16 02:36:05 +0800818 info = self.dut.info.GetAll()
819 self.state_instance.set_shared_data('system_info', info)
Jon Salz0697cbf2012-07-04 15:14:04 +0800820 self.event_client.post_event(Event(Event.Type.SYSTEM_INFO,
Hung-Te Line594e5d2015-12-16 02:36:05 +0800821 system_info=info))
822 logging.info('System info: %r', info)
Jon Salz0697cbf2012-07-04 15:14:04 +0800823
Jon Salzeb42f0d2012-07-27 19:14:04 +0800824 def update_factory(self, auto_run_on_restart=False, post_update_hook=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800825 """Commences updating factory software.
Jon Salzeb42f0d2012-07-27 19:14:04 +0800826
827 Args:
828 auto_run_on_restart: Auto-run when the machine comes back up.
829 post_update_hook: Code to call after update but immediately before
830 restart.
831
832 Returns:
833 Never if the update was successful (we just reboot).
834 False if the update was unnecessary (no update available).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +0800835 """
Jon Salz6dc031d2013-06-19 13:06:23 +0800836 self.kill_active_tests(False, reason='Factory software update')
Jon Salza6711d72012-07-18 14:33:03 +0800837 self.cancel_pending_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +0800838
Jon Salz5c344f62012-07-13 14:31:16 +0800839 def pre_update_hook():
840 if auto_run_on_restart:
Wei-Han Chenc17b4112016-11-22 14:56:51 +0800841 self.state_instance.set_shared_data(TESTS_AFTER_SHUTDOWN,
Jon Salz5c344f62012-07-13 14:31:16 +0800842 FORCE_AUTO_RUN)
843 self.state_instance.close()
844
Jon Salzeb42f0d2012-07-27 19:14:04 +0800845 if updater.TryUpdate(pre_update_hook=pre_update_hook):
846 if post_update_hook:
847 post_update_hook()
848 self.env.shutdown('reboot')
Jon Salz0697cbf2012-07-04 15:14:04 +0800849
Ricky Liang8fecf412014-05-22 10:56:14 +0800850 def handle_sigint(self, dummy_signum, dummy_frame): # pylint: disable=W0613
Jon Salz77c151e2012-08-28 07:20:37 +0800851 logging.error('Received SIGINT')
Peter Ammon1e1ec572014-06-26 17:56:32 -0700852 self.run_enqueue(None)
Jon Salz77c151e2012-08-28 07:20:37 +0800853 raise KeyboardInterrupt()
854
Ricky Liang8fecf412014-05-22 10:56:14 +0800855 def handle_sigterm(self, dummy_signum, dummy_frame): # pylint: disable=W0613
856 logging.error('Received SIGTERM')
Hung-Te Lin94ca4742014-07-09 20:13:50 +0800857 self.env.terminate()
858 self.run_queue.put(None)
Ricky Liang8fecf412014-05-22 10:56:14 +0800859 raise RuntimeError('Received SIGTERM')
860
Jon Salze12c2b32013-06-25 16:24:34 +0800861 def find_kcrashes(self):
862 """Finds kcrash files, logs them, and marks them as seen."""
863 seen_crashes = set(
864 self.state_instance.get_shared_data('seen_crashes', optional=True)
865 or [])
866
867 for path in glob.glob('/var/spool/crash/*'):
868 if not os.path.isfile(path):
869 continue
870 if path in seen_crashes:
871 continue
872 try:
873 stat = os.stat(path)
Hung-Te Lin4e6357c2016-01-08 14:32:00 +0800874 mtime = time_utils.TimeString(stat.st_mtime)
Jon Salze12c2b32013-06-25 16:24:34 +0800875 logging.info(
876 'Found new crash file %s (%d bytes at %s)',
877 path, stat.st_size, mtime)
878 extra_log_args = {}
879
880 try:
881 _, ext = os.path.splitext(path)
882 if ext in ['.kcrash', '.meta']:
883 ext = ext.replace('.', '')
884 with open(path) as f:
885 data = f.read(MAX_CRASH_FILE_SIZE)
886 tell = f.tell()
887 logging.info(
888 'Contents of %s%s:%s',
889 path,
890 ('' if tell == stat.st_size
891 else '(truncated to %d bytes)' % MAX_CRASH_FILE_SIZE),
892 ('\n' + data).replace('\n', '\n ' + ext + '> '))
893 extra_log_args['data'] = data
894
895 # Copy to /var/factory/kcrash for posterity
Joel Kitching625ff0f2016-05-16 14:59:40 -0700896 kcrash_dir = paths.GetFactoryRoot('kcrash')
Hung-Te Lin4e6357c2016-01-08 14:32:00 +0800897 file_utils.TryMakeDirs(kcrash_dir)
Jon Salze12c2b32013-06-25 16:24:34 +0800898 shutil.copy(path, kcrash_dir)
899 logging.info('Copied to %s',
900 os.path.join(kcrash_dir, os.path.basename(path)))
901 finally:
902 # Even if something goes wrong with the above, still try to
903 # log to event log
904 self.event_log.Log('crash_file',
905 path=path, size=stat.st_size, mtime=mtime,
906 **extra_log_args)
907 except: # pylint: disable=W0702
908 logging.exception('Unable to handle crash files %s', path)
909 seen_crashes.add(path)
910
911 self.state_instance.set_shared_data('seen_crashes', list(seen_crashes))
912
Jon Salz128b0932013-07-03 16:55:26 +0800913 def GetTestList(self, test_list_id):
914 """Returns the test list with the given ID.
915
916 Raises:
917 TestListError: The test list ID is not valid.
918 """
919 try:
920 return self.test_lists[test_list_id]
921 except KeyError:
922 raise test_lists.TestListError(
923 '%r is not a valid test list ID (available IDs are [%s])' % (
924 test_list_id, ', '.join(sorted(self.test_lists.keys()))))
925
926 def InitTestLists(self):
Joel Kitching50a63ea2016-02-22 13:15:09 +0800927 """Reads in all test lists and sets the active test list.
928
929 Returns:
930 True if the active test list could be set, False if failed.
931 """
932 startup_errors = []
933 self.test_lists, failed_files = test_lists.BuildAllTestLists(
Ricky Liang27051552014-05-04 14:22:26 +0800934 force_generic=(self.options.automation_mode is not None))
Jon Salzd7550792013-07-12 05:49:27 +0800935 logging.info('Loaded test lists: [%s]',
936 test_lists.DescribeTestLists(self.test_lists))
Jon Salz128b0932013-07-03 16:55:26 +0800937
Joel Kitching50a63ea2016-02-22 13:15:09 +0800938 # Check for any syntax errors in test list files.
939 if failed_files:
940 logging.info('Failed test list files: [%s]',
941 ' '.join(failed_files.keys()))
942 for f, exc_info in failed_files.iteritems():
943 logging.error('Error in test list file: %s', f,
944 exc_info=exc_info)
945
946 # Limit the stack trace to the very last entry.
947 exc_type, exc_value, exc_traceback = exc_info
948 while exc_traceback and exc_traceback.tb_next:
949 exc_traceback = exc_traceback.tb_next
950
951 exc_string = ''.join(
952 traceback.format_exception(
953 exc_type, exc_value, exc_traceback)).rstrip()
954 startup_errors.append('Error in test list file (%s):\n%s'
955 % (f, exc_string))
956
Jon Salz128b0932013-07-03 16:55:26 +0800957 if not self.options.test_list:
958 self.options.test_list = test_lists.GetActiveTestListId()
959
Joel Kitching50a63ea2016-02-22 13:15:09 +0800960 # Check for a non-existent test list ID.
961 try:
Wei-Han Chen84fee7c2016-08-26 21:56:25 +0800962 self.test_list = self.GetTestList(self.options.test_list)
Joel Kitching50a63ea2016-02-22 13:15:09 +0800963 logging.info('Active test list: %s', self.test_list.test_list_id)
964 except test_lists.TestListError as e:
965 logging.exception('Invalid active test list: %s',
966 self.options.test_list)
967 startup_errors.append(e.message)
Jon Salz128b0932013-07-03 16:55:26 +0800968
Joel Kitching50a63ea2016-02-22 13:15:09 +0800969 # We may have failed loading the active test list.
970 if self.test_list:
Joel Kitching50a63ea2016-02-22 13:15:09 +0800971 self.test_list.state_instance = self.state_instance
Jon Salz128b0932013-07-03 16:55:26 +0800972
Joel Kitching50a63ea2016-02-22 13:15:09 +0800973 # Prepare DUT link.
974 if self.test_list.options.dut_options:
975 logging.info('dut_options set by %s: %r', self.test_list.test_list_id,
976 self.test_list.options.dut_options)
Hung-Te Linb6287242016-05-18 14:39:05 +0800977 device_utils.PrepareDUTLink(**self.test_list.options.dut_options)
Wei-Han Chene8a025f2016-01-14 16:42:02 +0800978
Joel Kitching50a63ea2016-02-22 13:15:09 +0800979 # Show all startup errors.
980 if startup_errors:
981 self.state_instance.set_shared_data(
982 'startup_error', '\n\n'.join(startup_errors))
983
984 # Only return False if failed to load the active test list.
985 return bool(self.test_list)
Jon Salz128b0932013-07-03 16:55:26 +0800986
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +0800987 def init_hooks(self):
988 """Initializes hooks.
989
990 Must run after self.test_list ready.
991 """
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +0800992 module, cls = self.test_list.options.hooks_class.rsplit('.', 1)
993 self.hooks = getattr(__import__(module, fromlist=[cls]), cls)()
994 assert isinstance(self.hooks, factory.Hooks), (
Ricky Liang45c73e72015-01-15 15:00:30 +0800995 'hooks should be of type Hooks but is %r' % type(self.hooks))
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +0800996 self.hooks.test_list = self.test_list
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +0800997 self.hooks.OnCreatedTestList()
Shuo-Peng Liao52b90da2013-06-30 17:00:06 +0800998
Vic Yanga3cecf82014-12-26 00:44:21 -0800999 def init_ui(self):
1000 """Initialize UI."""
1001 self._ui_initialized = True
1002 if self.options.ui == 'chrome':
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001003 if self.options.monolithic:
Hung-Te Lin7bd55312014-12-30 16:43:36 +08001004 self.env.launch_chrome()
1005 else:
1006 # The presenter is responsible for launching Chrome. Let's just
1007 # wait here.
1008 self.env.controller_ready_for_ui()
Vic Yanga3cecf82014-12-26 00:44:21 -08001009 logging.info('Waiting for a web socket connection')
1010 self.web_socket_manager.wait()
1011
1012 # Wait for the test widget size to be set; this is done in
1013 # an asynchronous RPC so there is a small chance that the
1014 # web socket might be opened first.
1015 for _ in range(100): # 10 s
1016 try:
1017 if self.state_instance.get_shared_data('test_widget_size'):
1018 break
1019 except KeyError:
1020 pass # Retry
1021 time.sleep(0.1) # 100 ms
1022 else:
1023 logging.warn('Never received test_widget_size from UI')
1024
Jon Salz0697cbf2012-07-04 15:14:04 +08001025 def init(self, args=None, env=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001026 """Initializes Goofy.
Jon Salz0697cbf2012-07-04 15:14:04 +08001027
1028 Args:
1029 args: A list of command-line arguments. Uses sys.argv if
1030 args is None.
1031 env: An Environment instance to use (or None to choose
1032 FakeChrootEnvironment or DUTEnvironment as appropriate).
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001033 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001034 parser = OptionParser()
1035 parser.add_option('-v', '--verbose', dest='verbose',
Jon Salz8fa8e832012-07-13 19:04:09 +08001036 action='store_true',
1037 help='Enable debug logging')
Jon Salz0697cbf2012-07-04 15:14:04 +08001038 parser.add_option('--print_test_list', dest='print_test_list',
Wei-Han Chen84fee7c2016-08-26 21:56:25 +08001039 metavar='TEST_LIST_ID',
1040 help='Print the content of TEST_LIST_ID and exit')
Jon Salz0697cbf2012-07-04 15:14:04 +08001041 parser.add_option('--restart', dest='restart',
Jon Salz8fa8e832012-07-13 19:04:09 +08001042 action='store_true',
1043 help='Clear all test state')
Jon Salz0697cbf2012-07-04 15:14:04 +08001044 parser.add_option('--ui', dest='ui', type='choice',
Jon Salz7b5482e2014-08-04 17:48:41 +08001045 choices=['none', 'chrome'],
Jon Salz2f881df2013-02-01 17:00:35 +08001046 default='chrome',
Jon Salz8fa8e832012-07-13 19:04:09 +08001047 help='UI to use')
Jon Salz0697cbf2012-07-04 15:14:04 +08001048 parser.add_option('--ui_scale_factor', dest='ui_scale_factor',
Jon Salz8fa8e832012-07-13 19:04:09 +08001049 type='int', default=1,
1050 help=('Factor by which to scale UI '
1051 '(Chrome UI only)'))
Jon Salz0697cbf2012-07-04 15:14:04 +08001052 parser.add_option('--test_list', dest='test_list',
Wei-Han Chen84fee7c2016-08-26 21:56:25 +08001053 metavar='TEST_LIST_ID',
1054 help='Use test list whose id is TEST_LIST_ID')
Jon Salzc79a9982012-08-30 04:42:01 +08001055 parser.add_option('--dummy_shopfloor', action='store_true',
1056 help='Use a dummy shopfloor server')
Ricky Liang6fe218c2013-12-27 15:17:17 +08001057 parser.add_option('--automation-mode',
1058 choices=[m.lower() for m in AutomationMode],
Ricky Liang45c73e72015-01-15 15:00:30 +08001059 default='none', help='Factory test automation mode.')
Ricky Liang117484a2014-04-14 11:14:41 +08001060 parser.add_option('--no-auto-run-on-start', dest='auto_run_on_start',
1061 action='store_false', default=True,
1062 help=('do not automatically run the test list on goofy '
1063 'start; this is only valid when factory test '
1064 'automation is enabled'))
Chun-Ta Lina8dd3172014-11-26 16:15:13 +08001065 parser.add_option('--handshake_timeout', dest='handshake_timeout',
1066 type='float', default=0.3,
1067 help=('RPC timeout when doing handshake between device '
1068 'and presenter.'))
Vic Yang7d693c42014-09-14 09:52:39 +08001069 parser.add_option('--standalone', dest='standalone',
1070 action='store_true', default=False,
1071 help=('Assume the presenter is running on the same '
1072 'machines.'))
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001073 parser.add_option('--monolithic', dest='monolithic',
1074 action='store_true', default=False,
1075 help='Run in monolithic mode (without presenter)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001076 (self.options, self.args) = parser.parse_args(args)
1077
Hung-Te Lina846f602014-07-04 20:32:22 +08001078 signal.signal(signal.SIGINT, self.handle_sigint)
1079 # TODO(hungte) SIGTERM does not work properly without Telemetry and should
1080 # be fixed.
Hung-Te Lina846f602014-07-04 20:32:22 +08001081
Jon Salz46b89562012-07-05 11:49:22 +08001082 # Make sure factory directories exist.
Joel Kitching625ff0f2016-05-16 14:59:40 -07001083 paths.GetLogRoot()
1084 paths.GetStateRoot()
1085 paths.GetTestDataRoot()
Jon Salz46b89562012-07-05 11:49:22 +08001086
Jon Salz0697cbf2012-07-04 15:14:04 +08001087 global _inited_logging # pylint: disable=W0603
1088 if not _inited_logging:
1089 factory.init_logging('goofy', verbose=self.options.verbose)
1090 _inited_logging = True
Jon Salz8fa8e832012-07-13 19:04:09 +08001091
Wei-Han Chen78f35f62017-03-06 20:11:20 +08001092 try:
1093 goofy_default_options = config_utils.LoadConfig(validate_schema=False)
1094 for key, value in goofy_default_options.iteritems():
1095 if getattr(self.options, key, None) is None:
1096 logging.info('self.options.%s = %r', key, value)
1097 setattr(self.options, key, value)
1098 except Exception:
1099 logging.exception('failed to load goofy overriding options')
1100
Jon Salz0f996602012-10-03 15:26:48 +08001101 if self.options.print_test_list:
Wei-Han Chen84fee7c2016-08-26 21:56:25 +08001102 test_list = test_lists.BuildTestList(self.options.print_test_list)
1103 print(test_list.__repr__(recursive=True))
Jon Salz0f996602012-10-03 15:26:48 +08001104 sys.exit(0)
1105
Jon Salzee85d522012-07-17 14:34:46 +08001106 event_log.IncrementBootSequence()
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +08001107 testlog_goofy.IncrementInitCount()
1108
Jon Salzd15bbcf2013-05-21 17:33:57 +08001109 # Don't defer logging the initial event, so we can make sure
1110 # that device_id, reimage_id, etc. are all set up.
1111 self.event_log = EventLog('goofy', defer=False)
Chun-Ta Lin53cbbd52016-06-08 21:42:19 +08001112 self.testlog = testlog.Testlog(
1113 log_root=paths.GetLogRoot(), uuid=self.uuid)
1114 # Direct the logging calls to testlog as well.
1115 testlog.CapturePythonLogging(
1116 callback=self.testlog.primary_json.Log,
1117 level=logging.getLogger().getEffectiveLevel())
Jon Salz0697cbf2012-07-04 15:14:04 +08001118
Jon Salz0697cbf2012-07-04 15:14:04 +08001119 if env:
1120 self.env = env
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +08001121 elif sys_utils.InChroot():
Jon Salz0697cbf2012-07-04 15:14:04 +08001122 self.env = test_environment.FakeChrootEnvironment()
1123 logging.warn(
Ricky Liang45c73e72015-01-15 15:00:30 +08001124 'Using chroot environment: will not actually run autotests')
Hung-Te Lina846f602014-07-04 20:32:22 +08001125 elif self.options.ui == 'chrome':
Ricky Liang09d66d82014-09-25 11:20:54 +08001126 self.env = test_environment.DUTEnvironment()
Jon Salz0697cbf2012-07-04 15:14:04 +08001127 self.env.goofy = self
Vic Yanga4931152014-08-11 16:36:24 -07001128 # web_socket_manager will be initialized later
1129 # pylint: disable=W0108
1130 self.env.has_sockets = lambda: self.web_socket_manager.has_sockets()
Jon Salz0697cbf2012-07-04 15:14:04 +08001131
1132 if self.options.restart:
1133 state.clear_state()
1134
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +08001135 if self.options.ui_scale_factor != 1 and sys_utils.InQEMU():
Jon Salz0697cbf2012-07-04 15:14:04 +08001136 logging.warn(
Ricky Liang45c73e72015-01-15 15:00:30 +08001137 'In QEMU; ignoring ui_scale_factor argument')
Jon Salz0697cbf2012-07-04 15:14:04 +08001138 self.options.ui_scale_factor = 1
1139
1140 logging.info('Started')
1141
Hung-Te Lin8f6a3782015-01-06 22:58:32 +08001142 if not self.options.monolithic:
Hung-Te Lin7bd55312014-12-30 16:43:36 +08001143 self.link_manager = PresenterLinkManager(
1144 check_interval=1,
1145 handshake_timeout=self.options.handshake_timeout,
1146 standalone=self.options.standalone)
Peter Ammon1e1ec572014-06-26 17:56:32 -07001147
Earl Ouacbe99c2017-02-21 16:04:19 +08001148 self.start_goofy_server()
1149 self.init_state_instance()
Jon Salz0697cbf2012-07-04 15:14:04 +08001150 self.state_instance.set_shared_data('hwid_cfg', get_hwid_cfg())
1151 self.state_instance.set_shared_data('ui_scale_factor',
Ricky Liang09216dc2013-02-22 17:26:45 +08001152 self.options.ui_scale_factor)
Jon Salz0697cbf2012-07-04 15:14:04 +08001153 self.last_shutdown_time = (
Ricky Liang45c73e72015-01-15 15:00:30 +08001154 self.state_instance.get_shared_data('shutdown_time', optional=True))
Jon Salz0697cbf2012-07-04 15:14:04 +08001155 self.state_instance.del_shared_data('shutdown_time', optional=True)
Jon Salzb19ea072013-02-07 16:35:00 +08001156 self.state_instance.del_shared_data('startup_error', optional=True)
Jon Salz0697cbf2012-07-04 15:14:04 +08001157
Ricky Liang6fe218c2013-12-27 15:17:17 +08001158 self.options.automation_mode = ParseAutomationMode(
1159 self.options.automation_mode)
1160 self.state_instance.set_shared_data('automation_mode',
1161 self.options.automation_mode)
1162 self.state_instance.set_shared_data(
1163 'automation_mode_prompt',
1164 AutomationModePrompt[self.options.automation_mode])
1165
Joel Kitching50a63ea2016-02-22 13:15:09 +08001166 success = False
1167 exc_info = None
Jon Salz128b0932013-07-03 16:55:26 +08001168 try:
Joel Kitching50a63ea2016-02-22 13:15:09 +08001169 success = self.InitTestLists()
Jon Salz128b0932013-07-03 16:55:26 +08001170 except: # pylint: disable=W0702
Joel Kitching50a63ea2016-02-22 13:15:09 +08001171 exc_info = sys.exc_info()
1172
1173 if not success:
1174 if exc_info:
1175 logging.exception('Unable to initialize test lists')
1176 self.state_instance.set_shared_data(
1177 'startup_error',
1178 'Unable to initialize test lists\n%s' % (
1179 traceback.format_exc()))
Jon Salzb19ea072013-02-07 16:35:00 +08001180 if self.options.ui == 'chrome':
1181 # Create an empty test list with default options so that the rest of
1182 # startup can proceed.
1183 self.test_list = factory.FactoryTestList(
1184 [], self.state_instance, factory.Options())
1185 else:
1186 # Bail with an error; no point in starting up.
1187 sys.exit('No valid test list; exiting.')
1188
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001189 self.init_hooks()
1190
Jon Salz822838b2013-03-25 17:32:33 +08001191 if self.test_list.options.clear_state_on_start:
1192 self.state_instance.clear_test_state()
1193
Jon Salz670ce062014-05-16 15:53:50 +08001194 # If the phase is invalid, this will raise a ValueError.
1195 phase.SetPersistentPhase(self.test_list.options.phase)
1196
Dean Liao85ca86f2014-11-03 12:28:08 +08001197 # For netboot firmware, mainfw_type should be 'netboot'.
Hung-Te Line594e5d2015-12-16 02:36:05 +08001198 if (self.dut.info.mainfw_type != 'nonchrome' and
1199 self.dut.info.firmware_version is None):
Ricky Liang45c73e72015-01-15 15:00:30 +08001200 self.state_instance.set_shared_data(
1201 'startup_error',
Vic Yang9bd4f772013-06-04 17:34:00 +08001202 'Netboot firmware detected\n'
1203 'Connect Ethernet and reboot to re-image.\n'
1204 u'侦测到网路开机固件\n'
1205 u'请连接乙太网并重启')
1206
Jon Salz0697cbf2012-07-04 15:14:04 +08001207 if not self.state_instance.has_shared_data('ui_lang'):
1208 self.state_instance.set_shared_data('ui_lang',
Ricky Liang45c73e72015-01-15 15:00:30 +08001209 self.test_list.options.ui_lang)
Jon Salz0697cbf2012-07-04 15:14:04 +08001210 self.state_instance.set_shared_data(
Ricky Liang45c73e72015-01-15 15:00:30 +08001211 'test_list_options',
1212 self.test_list.options.__dict__)
Jon Salz0697cbf2012-07-04 15:14:04 +08001213 self.state_instance.test_list = self.test_list
1214
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001215 self.check_log_rotation()
Jon Salz83ef34b2012-11-01 19:46:35 +08001216
Jon Salz23926422012-09-01 03:38:13 +08001217 if self.options.dummy_shopfloor:
Ricky Liang45c73e72015-01-15 15:00:30 +08001218 os.environ[shopfloor.SHOPFLOOR_SERVER_ENV_VAR_NAME] = (
1219 'http://%s:%d/' %
Joel Kitchingb85ed7f2014-10-08 18:24:39 +08001220 (net_utils.LOCALHOST, shopfloor.DEFAULT_SERVER_PORT))
Hung-Te Lin4e6357c2016-01-08 14:32:00 +08001221 self.dummy_shopfloor = process_utils.Spawn(
Wei-Han Chen2ebb92d2016-01-12 14:51:41 +08001222 [os.path.join(paths.FACTORY_PATH, 'bin', 'shopfloor_server'),
Jon Salz23926422012-09-01 03:38:13 +08001223 '--dummy'])
1224 elif self.test_list.options.shopfloor_server_url:
1225 shopfloor.set_server_url(self.test_list.options.shopfloor_server_url)
Jon Salz2bf2f6b2013-03-28 18:49:26 +08001226 shopfloor.set_enabled(True)
Jon Salz23926422012-09-01 03:38:13 +08001227
Jon Salz0697cbf2012-07-04 15:14:04 +08001228 self.init_states()
1229 self.start_event_server()
Wei-Ning Huang38b75f02015-02-25 18:25:14 +08001230 self.start_terminal_server()
Hung-Te Lincc41d2a2014-10-29 13:35:20 +08001231
Earl Oua3bca122016-10-21 16:00:30 +08001232 # Load and run Goofy plugins.
1233 self.plugin_controller = plugin_controller.PluginController(
1234 self.test_list.options.plugin_config_name, self)
1235 self.plugin_controller.StartAllPlugins()
1236
Joel Kitchingd28fbda2016-11-25 16:58:26 +08001237 # Set reference to the Instalog plugin.
1238 self.testlog.SetInstalogPlugin(
1239 self.plugin_controller.GetPluginInstance('instalog'))
1240
Jon Salz0697cbf2012-07-04 15:14:04 +08001241 # Note that we create a log watcher even if
1242 # sync_event_log_period_secs isn't set (no background
1243 # syncing), since we may use it to flush event logs as well.
1244 self.log_watcher = EventLogWatcher(
Ricky Liang45c73e72015-01-15 15:00:30 +08001245 self.test_list.options.sync_event_log_period_secs,
1246 event_log_db_file=None,
1247 handle_event_logs_callback=self.handle_event_logs)
Jon Salz0697cbf2012-07-04 15:14:04 +08001248 if self.test_list.options.sync_event_log_period_secs:
1249 self.log_watcher.StartWatchThread()
1250
1251 self.update_system_info()
1252
1253 os.environ['CROS_FACTORY'] = '1'
1254 os.environ['CROS_DISABLE_SITE_SYSINFO'] = '1'
1255
Jon Salze12c2b32013-06-25 16:24:34 +08001256 self.find_kcrashes()
1257
Shuo-Peng Liao268b40b2013-07-01 15:58:59 +08001258 # Should not move earlier.
1259 self.hooks.OnStartup()
1260
Ricky Liang36512a32014-07-25 11:47:04 +08001261 # Only after this point the Goofy backend is ready for UI connection.
1262 self.ready_for_ui_connection = True
1263
Ricky Liang650f6bf2012-09-28 13:22:54 +08001264 # Create download path for autotest beforehand or autotests run at
1265 # the same time might fail due to race condition.
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +08001266 if not sys_utils.InChroot():
Hung-Te Lin4e6357c2016-01-08 14:32:00 +08001267 file_utils.TryMakeDirs(os.path.join('/usr/local/autotest', 'tests',
1268 'download'))
Ricky Liang650f6bf2012-09-28 13:22:54 +08001269
Jon Salz0697cbf2012-07-04 15:14:04 +08001270 def state_change_callback(test, test_state):
1271 self.event_client.post_event(
Ricky Liang4bff3e32014-02-20 18:46:11 +08001272 Event(Event.Type.STATE_CHANGE, path=test.path, state=test_state))
Jon Salz0697cbf2012-07-04 15:14:04 +08001273 self.test_list.state_change_callback = state_change_callback
Jon Salz73e0fd02012-04-04 11:46:38 +08001274
Vic Yange2c76a82014-10-30 12:48:19 -07001275 self.autotest_prespawner = prespawner.AutotestPrespawner()
1276 self.autotest_prespawner.start()
1277
1278 self.pytest_prespawner = prespawner.PytestPrespawner()
1279 self.pytest_prespawner.start()
Jon Salza6711d72012-07-18 14:33:03 +08001280
Ricky Liang48e47f92014-02-26 19:31:51 +08001281 tests_after_shutdown = self.state_instance.get_shared_data(
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001282 TESTS_AFTER_SHUTDOWN, optional=True)
Jon Salz5c344f62012-07-13 14:31:16 +08001283 force_auto_run = (tests_after_shutdown == FORCE_AUTO_RUN)
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001284
Jon Salz5c344f62012-07-13 14:31:16 +08001285 if not force_auto_run and tests_after_shutdown is not None:
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001286 logging.info('Resuming tests after shutdown: %r', tests_after_shutdown)
1287 self.test_list_iterator = tests_after_shutdown
1288 self.test_list_iterator.set_test_list(self.test_list)
Peter Ammon1e1ec572014-06-26 17:56:32 -07001289 self.run_enqueue(self.run_next_test)
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001290 elif force_auto_run or self.test_list.options.auto_run_on_start:
1291 # If automation mode is enabled, allow suppress auto_run_on_start.
1292 if (self.options.automation_mode == 'NONE' or
1293 self.options.auto_run_on_start):
1294 status_filter = [TestState.UNTESTED]
1295 if self.test_list.options.retry_failed_on_start:
1296 status_filter.append(TestState.FAILED)
1297 self.run_enqueue(lambda: self.run_tests(self.test_list, status_filter))
1298 self.state_instance.set_shared_data(TESTS_AFTER_SHUTDOWN, None)
Ricky Liang4bff3e32014-02-20 18:46:11 +08001299 self.restore_active_run_state()
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001300
Hung-Te Lin410f70a2015-12-15 14:53:42 +08001301 self.dut.hooks.OnTestStart()
Vic Yang08505c72015-01-06 17:01:53 -08001302
Dean Liao592e4d52013-01-10 20:06:39 +08001303 self.may_disable_cros_shortcut_keys()
1304
1305 def may_disable_cros_shortcut_keys(self):
1306 test_options = self.test_list.options
1307 if test_options.disable_cros_shortcut_keys:
1308 logging.info('Filter ChromeOS shortcut keys.')
1309 self.key_filter = KeyFilter(
1310 unmap_caps_lock=test_options.disable_caps_lock,
1311 caps_lock_keycode=test_options.caps_lock_keycode)
1312 self.key_filter.Start()
1313
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001314 def check_log_rotation(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001315 """Checks log rotation file presence/absence according to test_list option.
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001316
1317 Touch /var/lib/cleanup_logs_paused if test_list.options.disable_log_rotation
1318 is True, delete it otherwise. This must be done in idle loop because
1319 autotest client will touch /var/lib/cleanup_logs_paused each time it runs
1320 an autotest.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001321 """
Hung-Te Linf5f2d7f2016-01-08 17:12:46 +08001322 if sys_utils.InChroot():
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001323 return
1324 try:
1325 if self.test_list.options.disable_log_rotation:
1326 open(CLEANUP_LOGS_PAUSED, 'w').close()
1327 else:
1328 file_utils.TryUnlink(CLEANUP_LOGS_PAUSED)
1329 except: # pylint: disable=W0702
1330 # Oh well. Logs an error (but no trace)
1331 logging.info(
1332 'Unable to %s %s: %s',
1333 'touch' if self.test_list.options.disable_log_rotation else 'delete',
Hung-Te Linf707b242016-01-08 23:11:42 +08001334 CLEANUP_LOGS_PAUSED, debug_utils.FormatExceptionOnly())
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001335
Peter Ammon1e1ec572014-06-26 17:56:32 -07001336 def perform_periodic_tasks(self):
1337 """Override of base method to perform periodic work.
Vic Yang4953fc12012-07-26 16:19:53 +08001338
Peter Ammon1e1ec572014-06-26 17:56:32 -07001339 This method must not raise exceptions.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001340 """
Peter Ammon1e1ec572014-06-26 17:56:32 -07001341 super(Goofy, self).perform_periodic_tasks()
Jon Salzb22d1172012-08-06 10:38:57 +08001342
Earl Oua3bca122016-10-21 16:00:30 +08001343 self.check_plugins()
cychiang21886742012-07-05 15:16:32 +08001344 self.check_for_updates()
Cheng-Yi Chiang39d32ad2013-07-23 15:02:38 +08001345 self.check_log_rotation()
Jon Salz57717ca2012-04-04 16:47:25 +08001346
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +08001347 def handle_event_logs(self, chunks, periodic=False):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001348 """Callback for event watcher.
Jon Salz258a40c2012-04-19 12:34:01 +08001349
Jon Salz0697cbf2012-07-04 15:14:04 +08001350 Attempts to upload the event logs to the shopfloor server.
Vic Yang93027612013-05-06 02:42:49 +08001351
1352 Args:
Jon Salzd15bbcf2013-05-21 17:33:57 +08001353 chunks: A list of Chunk objects.
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +08001354 periodic: This event log handling is periodic. Error messages
1355 will only be shown for the first time.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001356 """
Vic Yang93027612013-05-06 02:42:49 +08001357 first_exception = None
1358 exception_count = 0
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +08001359 # Suppress error messages for periodic event syncing except for the
1360 # first time. If event syncing is not periodic, always show the error
1361 # messages.
1362 quiet = self._suppress_event_log_error_messages if periodic else False
Vic Yang93027612013-05-06 02:42:49 +08001363
Jon Salzd15bbcf2013-05-21 17:33:57 +08001364 for chunk in chunks:
Vic Yang93027612013-05-06 02:42:49 +08001365 try:
Jon Salzcddb6402013-05-23 12:56:42 +08001366 description = 'event logs (%s)' % str(chunk)
Vic Yang93027612013-05-06 02:42:49 +08001367 start_time = time.time()
1368 shopfloor_client = shopfloor.get_instance(
Ricky Liang45c73e72015-01-15 15:00:30 +08001369 detect=True,
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +08001370 timeout=self.test_list.options.shopfloor_timeout_secs,
1371 quiet=quiet)
Ricky Liang45c73e72015-01-15 15:00:30 +08001372 shopfloor_client.UploadEvent(chunk.log_name + '.' +
Jon Salzd15bbcf2013-05-21 17:33:57 +08001373 event_log.GetReimageId(),
1374 Binary(chunk.chunk))
Vic Yang93027612013-05-06 02:42:49 +08001375 logging.info(
Ricky Liang45c73e72015-01-15 15:00:30 +08001376 'Successfully synced %s in %.03f s',
1377 description, time.time() - start_time)
1378 except: # pylint: disable=W0702
Hung-Te Linf707b242016-01-08 23:11:42 +08001379 first_exception = (first_exception or
1380 (chunk.log_name + ': ' +
1381 debug_utils.FormatExceptionOnly()))
Vic Yang93027612013-05-06 02:42:49 +08001382 exception_count += 1
1383
1384 if exception_count:
1385 if exception_count == 1:
1386 msg = 'Log upload failed: %s' % first_exception
1387 else:
1388 msg = '%d log upload failed; first is: %s' % (
1389 exception_count, first_exception)
Cheng-Yi Chiangf5b21012015-03-17 15:37:14 +08001390 # For periodic event log syncing, only show the first error messages.
1391 if periodic:
1392 if not self._suppress_event_log_error_messages:
1393 self._suppress_event_log_error_messages = True
1394 logging.warning('Suppress periodic shopfloor error messages for '
1395 'event log syncing after the first one.')
1396 raise Exception(msg)
1397 # For event log syncing by request, show the error messages.
1398 else:
1399 raise Exception(msg)
Vic Yang93027612013-05-06 02:42:49 +08001400
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001401 def run_tests_with_status(self, statuses_to_run, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001402 """Runs all top-level tests with a particular status.
Jon Salz0405ab52012-03-16 15:26:52 +08001403
Jon Salz0697cbf2012-07-04 15:14:04 +08001404 All active tests, plus any tests to re-run, are reset.
Jon Salz57717ca2012-04-04 16:47:25 +08001405
Jon Salz0697cbf2012-07-04 15:14:04 +08001406 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001407 statuses_to_run: The particular status that caller wants to run.
Jon Salz0697cbf2012-07-04 15:14:04 +08001408 starting_at: If provided, only auto-runs tests beginning with
1409 this test.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001410 root: The root of tests to run. If not provided, it will be
1411 the root of all tests.
1412 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001413 root = root or self.test_list
Jon Salz6dc031d2013-06-19 13:06:23 +08001414 self.abort_active_tests('Operator requested run/re-run of certain tests')
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001415 self.run_tests(root, status_filter=statuses_to_run)
Jon Salz0405ab52012-03-16 15:26:52 +08001416
Jon Salz0697cbf2012-07-04 15:14:04 +08001417 def restart_tests(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001418 """Restarts all tests."""
Jon Salz0697cbf2012-07-04 15:14:04 +08001419 root = root or self.test_list
Jon Salz0405ab52012-03-16 15:26:52 +08001420
Jon Salz6dc031d2013-06-19 13:06:23 +08001421 self.abort_active_tests('Operator requested restart of certain tests')
Jon Salz0697cbf2012-07-04 15:14:04 +08001422 for test in root.walk():
Ricky Liangfea4ac92014-08-21 11:55:59 +08001423 test.update_state(status=TestState.UNTESTED)
Jon Salz0697cbf2012-07-04 15:14:04 +08001424 self.run_tests(root)
Hung-Te Lin96632362012-03-20 21:14:18 +08001425
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001426 def auto_run(self, root=None):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001427 """"Auto-runs" tests that have not been run yet.
Hung-Te Lin96632362012-03-20 21:14:18 +08001428
Jon Salz0697cbf2012-07-04 15:14:04 +08001429 Args:
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001430 root: If provided, the root of tests to run. If not provided, the root
1431 will be test_list (root of all tests).
1432 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001433 root = root or self.test_list
1434 self.run_tests_with_status([TestState.UNTESTED, TestState.ACTIVE],
Ricky Liang45c73e72015-01-15 15:00:30 +08001435 root=root)
Jon Salz968e90b2012-03-18 16:12:43 +08001436
Jon Salz0697cbf2012-07-04 15:14:04 +08001437 def handle_switch_test(self, event):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001438 """Switches to a particular test.
Jon Salz0405ab52012-03-16 15:26:52 +08001439
Ricky Liang6fe218c2013-12-27 15:17:17 +08001440 Args:
1441 event: The SWITCH_TEST event.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001442 """
Jon Salz0697cbf2012-07-04 15:14:04 +08001443 test = self.test_list.lookup_path(event.path)
1444 if not test:
1445 logging.error('Unknown test %r', event.key)
1446 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001447
Jon Salz0697cbf2012-07-04 15:14:04 +08001448 invoc = self.invocations.get(test)
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001449 if invoc:
Jon Salz0697cbf2012-07-04 15:14:04 +08001450 # Already running: just bring to the front if it
1451 # has a UI.
1452 logging.info('Setting visible test to %s', test.path)
Jon Salz36fbbb52012-07-05 13:45:06 +08001453 self.set_visible_test(test)
Jon Salz0697cbf2012-07-04 15:14:04 +08001454 return
Jon Salz73e0fd02012-04-04 11:46:38 +08001455
Jon Salz6dc031d2013-06-19 13:06:23 +08001456 self.abort_active_tests('Operator requested abort (switch_test)')
Jon Salz0697cbf2012-07-04 15:14:04 +08001457 for t in test.walk():
1458 t.update_state(status=TestState.UNTESTED)
Jon Salz73e0fd02012-04-04 11:46:38 +08001459
Wei-Han Chenc17b4112016-11-22 14:56:51 +08001460 self.run_tests(test)
Jon Salz73e0fd02012-04-04 11:46:38 +08001461
Wei-Ning Huang38b75f02015-02-25 18:25:14 +08001462 def handle_key_filter_mode(self, event):
1463 if self.key_filter:
1464 if getattr(event, 'enabled'):
1465 self.key_filter.Start()
1466 else:
1467 self.key_filter.Stop()
1468
Jon Salz0697cbf2012-07-04 15:14:04 +08001469 def wait(self):
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001470 """Waits for all pending invocations.
Jon Salz0697cbf2012-07-04 15:14:04 +08001471
1472 Useful for testing.
Cheng-Yi Chiang1e3e2692013-12-24 18:02:36 +08001473 """
Jon Salz1acc8742012-07-17 17:45:55 +08001474 while self.invocations:
1475 for k, v in self.invocations.iteritems():
1476 logging.info('Waiting for %s to complete...', k)
1477 v.thread.join()
1478 self.reap_completed_tests()
Jon Salz0697cbf2012-07-04 15:14:04 +08001479
Claire Changd1961a22015-08-05 16:15:55 +08001480 def test_fail(self, test):
Hung-Te Lin410f70a2015-12-15 14:53:42 +08001481 self.dut.hooks.OnTestFailure(test)
Claire Changd1961a22015-08-05 16:15:55 +08001482 if self.link_manager:
1483 self.link_manager.UpdateStatus(False)
1484
Wei-Han Chenced08ef2016-11-08 09:40:02 +08001485
Hung-Te Linf2f78f72012-02-08 19:27:11 +08001486if __name__ == '__main__':
Peter Ammona3d298c2014-09-23 10:11:02 -07001487 Goofy.run_main_and_exit()