blob: c8094059997ce3f0bfada81ed16d8a764870e85c [file] [log] [blame]
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08001# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08006import os
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08007import re
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +08008import tempfile
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +08009import time
10import xmlrpclib
11
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +080012from autotest_lib.client.bin import utils
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080013from autotest_lib.client.common_lib import error
Tom Wai-Hong Tam22b77302011-11-03 13:03:48 +080014from autotest_lib.server.cros.servo_test import ServoTest
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080015
16
17class FAFTSequence(ServoTest):
18 """
19 The base class of Fully Automated Firmware Test Sequence.
20
21 Many firmware tests require several reboot cycles and verify the resulted
22 system states. To do that, an Autotest test case should detailly handle
23 every action on each step. It makes the test case hard to read and many
24 duplicated code. The base class FAFTSequence is to solve this problem.
25
26 The actions of one reboot cycle is defined in a dict, namely FAFT_STEP.
27 There are four functions in the FAFT_STEP dict:
28 state_checker: a function to check the current is valid or not,
29 returning True if valid, otherwise, False to break the whole
30 test sequence.
31 userspace_action: a function to describe the action ran in userspace.
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080032 reboot_action: a function to do reboot, default: sync_and_hw_reboot.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080033 firmware_action: a function to describe the action ran after reboot.
34
Tom Wai-Hong Tam7c17ff22011-10-26 09:44:09 +080035 And configurations:
36 install_deps_after_boot: if True, install the Autotest dependency after
37 boot; otherwise, do nothing. It is for the cases of recovery mode
38 test. The test boots a USB/SD image instead of an internal image.
39 The previous installed Autotest dependency on the internal image
40 is lost. So need to install it again.
41
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080042 The default FAFT_STEP checks nothing in state_checker and does nothing in
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +080043 userspace_action and firmware_action. Its reboot_action is a hardware
44 reboot. You can change the default FAFT_STEP by calling
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +080045 self.register_faft_template(FAFT_STEP).
46
47 A FAFT test case consists of several FAFT_STEP's, namely FAFT_SEQUENCE.
48 FAFT_SEQUENCE is an array of FAFT_STEP's. Any missing fields on FAFT_STEP
49 fall back to default.
50
51 In the run_once(), it should register and run FAFT_SEQUENCE like:
52 def run_once(self):
53 self.register_faft_sequence(FAFT_SEQUENCE)
54 self.run_faft_sequnce()
55
56 Note that in the last step, we only run state_checker. The
57 userspace_action, reboot_action, and firmware_action are not executed.
58
59 Attributes:
60 _faft_template: The default FAFT_STEP of each step. The actions would
61 be over-written if the registered FAFT_SEQUENCE is valid.
62 _faft_sequence: The registered FAFT_SEQUENCE.
63 """
64 version = 1
65
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080066
67 # Mapping of partition number of kernel and rootfs.
68 KERNEL_MAP = {'a':'2', 'b':'4', '2':'2', '4':'4', '3':'2', '5':'4'}
69 ROOTFS_MAP = {'a':'3', 'b':'5', '2':'3', '4':'5', '3':'3', '5':'5'}
70 OTHER_KERNEL_MAP = {'a':'4', 'b':'2', '2':'4', '4':'2', '3':'4', '5':'2'}
71 OTHER_ROOTFS_MAP = {'a':'5', 'b':'3', '2':'5', '4':'3', '3':'5', '5':'3'}
72
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +080073 # Delay timing
74 FIRMWARE_SCREEN_DELAY = 10
75 TEXT_SCREEN_DELAY = 20
Tom Wai-Hong Tam9ca742a2011-12-05 15:48:57 +080076 USB_PLUG_DELAY = 10
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +080077 SYNC_DELAY = 5
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +080078
Tom Wai-Hong Tamf954d172011-12-08 17:14:15 +080079 # Recovery reason codes, copied from:
80 # vboot_reference/firmware/lib/vboot_nvstorage.h
81 # vboot_reference/firmware/lib/vboot_struct.h
82 RECOVERY_REASON = {
83 # Recovery not requested
84 'NOT_REQUESTED': '0', # 0x00
85 # Recovery requested from legacy utility
86 'LEGACY': '1', # 0x01
87 # User manually requested recovery via recovery button
88 'RO_MANUAL': '2', # 0x02
89 # RW firmware failed signature check
90 'RO_INVALID_RW': '3', # 0x03
91 # S3 resume failed
92 'RO_S3_RESUME': '4', # 0x04
93 # TPM error in read-only firmware
94 'RO_TPM_ERROR': '5', # 0x05
95 # Shared data error in read-only firmware
96 'RO_SHARED_DATA': '6', # 0x06
97 # Test error from S3Resume()
98 'RO_TEST_S3': '7', # 0x07
99 # Test error from LoadFirmwareSetup()
100 'RO_TEST_LFS': '8', # 0x08
101 # Test error from LoadFirmware()
102 'RO_TEST_LF': '9', # 0x09
103 # RW firmware failed signature check
104 'RW_NOT_DONE': '16', # 0x10
105 'RW_DEV_MISMATCH': '17', # 0x11
106 'RW_REC_MISMATCH': '18', # 0x12
107 'RW_VERIFY_KEYBLOCK': '19', # 0x13
108 'RW_KEY_ROLLBACK': '20', # 0x14
109 'RW_DATA_KEY_PARSE': '21', # 0x15
110 'RW_VERIFY_PREAMBLE': '22', # 0x16
111 'RW_FW_ROLLBACK': '23', # 0x17
112 'RW_HEADER_VALID': '24', # 0x18
113 'RW_GET_FW_BODY': '25', # 0x19
114 'RW_HASH_WRONG_SIZE': '26', # 0x1A
115 'RW_VERIFY_BODY': '27', # 0x1B
116 'RW_VALID': '28', # 0x1C
117 # Read-only normal path requested by firmware preamble, but
118 # unsupported by firmware.
119 'RW_NO_RO_NORMAL': '29', # 0x1D
120 # Firmware boot failure outside of verified boot
121 'RO_FIRMWARE': '32', # 0x20
122 # Recovery mode TPM initialization requires a system reboot.
123 # The system was already in recovery mode for some other reason
124 # when this happened.
125 'RO_TPM_REBOOT': '33', # 0x21
126 # Unspecified/unknown error in read-only firmware
127 'RO_UNSPECIFIED': '63', # 0x3F
128 # User manually requested recovery by pressing a key at developer
129 # warning screen.
130 'RW_DEV_SCREEN': '65', # 0x41
131 # No OS kernel detected
132 'RW_NO_OS': '66', # 0x42
133 # OS kernel failed signature check
134 'RW_INVALID_OS': '67', # 0x43
135 # TPM error in rewritable firmware
136 'RW_TPM_ERROR': '68', # 0x44
137 # RW firmware in dev mode, but dev switch is off.
138 'RW_DEV_MISMATCH': '69', # 0x45
139 # Shared data error in rewritable firmware
140 'RW_SHARED_DATA': '70', # 0x46
141 # Test error from LoadKernel()
142 'RW_TEST_LK': '71', # 0x47
143 # No bootable disk found
144 'RW_NO_DISK': '72', # 0x48
145 # Unspecified/unknown error in rewritable firmware
146 'RW_UNSPECIFIED': '127', # 0x7F
147 # DM-verity error
148 'KE_DM_VERITY': '129', # 0x81
149 # Unspecified/unknown error in kernel
150 'KE_UNSPECIFIED': '191', # 0xBF
151 # Recovery mode test from user-mode
152 'US_TEST': '193', # 0xC1
153 # Unspecified/unknown error in user-mode
154 'US_UNSPECIFIED': '255', # 0xFF
155 }
156
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800157 _faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800158 _faft_sequence = ()
159
160
161 def setup(self):
162 """Autotest setup function."""
163 super(FAFTSequence, self).setup()
164 if not self._remote_infos['faft']['used']:
165 raise error.TestError('The use_faft flag should be enabled.')
166 self.register_faft_template({
167 'state_checker': (None),
168 'userspace_action': (None),
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +0800169 'reboot_action': (self.sync_and_hw_reboot),
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800170 'firmware_action': (None)
171 })
172
173
174 def cleanup(self):
175 """Autotest cleanup function."""
176 self._faft_sequence = ()
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800177 self._faft_template = {}
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800178 super(FAFTSequence, self).cleanup()
179
180
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800181 def assert_test_image_in_usb_disk(self):
182 """Assert an USB disk plugged-in on servo and a test image inside.
183
184 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800185 error.TestError: if USB disk not detected or not a test image.
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800186 """
187 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
Jon Salzc88e5b62011-11-30 14:38:54 +0800188 usb_dev = self.servo.probe_host_usb_dev()
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800189 if not usb_dev:
190 raise error.TestError(
191 'An USB disk should be plugged in the servo board.')
192
193 tmp_dir = tempfile.mkdtemp()
Tom Wai-Hong Tamb0e80852011-12-07 16:15:06 +0800194 utils.system('sudo mount -r -t ext2 %s3 %s' % (usb_dev, tmp_dir))
Tom Wai-Hong Tame77459e2011-11-03 17:19:46 +0800195 code = utils.system(
196 'grep -qE "(Test Build|testimage-channel)" %s/etc/lsb-release' %
197 tmp_dir, ignore_status=True)
Tom Wai-Hong Tam76c75072011-10-25 18:00:12 +0800198 utils.system('sudo umount %s' % tmp_dir)
199 os.removedirs(tmp_dir)
200 if code != 0:
201 raise error.TestError(
202 'The image in the USB disk should be a test image.')
203
204
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800205 def _parse_crossystem_output(self, lines):
206 """Parse the crossystem output into a dict.
207
208 Args:
209 lines: The list of crossystem output strings.
210
211 Returns:
212 A dict which contains the crossystem keys/values.
213
214 Raises:
215 error.TestError: If wrong format in crossystem output.
216
217 >>> seq = FAFTSequence()
218 >>> seq._parse_crossystem_output([ \
219 "arch = x86 # Platform architecture", \
220 "cros_debug = 1 # OS should allow debug", \
221 ])
222 {'cros_debug': '1', 'arch': 'x86'}
223 >>> seq._parse_crossystem_output([ \
224 "arch=x86", \
225 ])
226 Traceback (most recent call last):
227 ...
228 TestError: Failed to parse crossystem output: arch=x86
229 >>> seq._parse_crossystem_output([ \
230 "arch = x86 # Platform architecture", \
231 "arch = arm # Platform architecture", \
232 ])
233 Traceback (most recent call last):
234 ...
235 TestError: Duplicated crossystem key: arch
236 """
237 pattern = "^([^ =]*) *= *(.*[^ ]) *# [^#]*$"
238 parsed_list = {}
239 for line in lines:
240 matched = re.match(pattern, line.strip())
241 if not matched:
242 raise error.TestError("Failed to parse crossystem output: %s"
243 % line)
244 (name, value) = (matched.group(1), matched.group(2))
245 if name in parsed_list:
246 raise error.TestError("Duplicated crossystem key: %s" % name)
247 parsed_list[name] = value
248 return parsed_list
249
250
251 def crossystem_checker(self, expected_dict):
252 """Check the crossystem values matched.
253
254 Given an expect_dict which describes the expected crossystem values,
255 this function check the current crossystem values are matched or not.
256
257 Args:
258 expected_dict: A dict which contains the expected values.
259
260 Returns:
261 True if the crossystem value matched; otherwise, False.
262 """
263 lines = self.faft_client.run_shell_command_get_output('crossystem')
264 got_dict = self._parse_crossystem_output(lines)
265 for key in expected_dict:
266 if key not in got_dict:
267 logging.info('Expected key "%s" not in crossystem result' % key)
268 return False
269 if isinstance(expected_dict[key], str):
270 if got_dict[key] != expected_dict[key]:
271 logging.info("Expected '%s' value '%s' but got '%s'" %
272 (key, expected_dict[key], got_dict[key]))
273 return False
274 elif isinstance(expected_dict[key], tuple):
275 # Expected value is a tuple of possible actual values.
276 if got_dict[key] not in expected_dict[key]:
277 logging.info("Expected '%s' values %s but got '%s'" %
278 (key, str(expected_dict[key]), got_dict[key]))
279 return False
280 else:
281 logging.info("The expected_dict is neither a str nor a dict.")
282 return False
283 return True
284
285
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800286 def root_part_checker(self, expected_part):
287 """Check the partition number of the root device matched.
288
289 Args:
290 expected_part: A string containing the number of the expected root
291 partition.
292
293 Returns:
294 True if the currect root partition number matched; otherwise, False.
295 """
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800296 part = self.faft_client.get_root_part()[-1]
297 if self.ROOTFS_MAP[expected_part] != part:
298 logging.info("Expected root part %s but got %s" %
299 (self.ROOTFS_MAP[expected_part], part))
300 return False
301 return True
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800302
303
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800304 def _join_part(self, dev, part):
305 """Return a concatenated string of device and partition number.
306
307 Args:
308 dev: A string of device, e.g.'/dev/sda'.
309 part: A string of partition number, e.g.'3'.
310
311 Returns:
312 A concatenated string of device and partition number, e.g.'/dev/sda3'.
313
314 >>> seq = FAFTSequence()
315 >>> seq._join_part('/dev/sda', '3')
316 '/dev/sda3'
317 >>> seq._join_part('/dev/mmcblk0', '2')
318 '/dev/mmcblk0p2'
319 """
320 if 'mmcblk' in dev:
321 return dev + 'p' + part
322 else:
323 return dev + part
324
325
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800326 def copy_kernel_and_rootfs(self, from_part, to_part):
327 """Copy kernel and rootfs from from_part to to_part.
328
329 Args:
330 from_part: A string of partition number to be copied from.
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800331 to_part: A string of partition number to be copied to.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800332 """
333 root_dev = self.faft_client.get_root_dev()
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800334 logging.info('Copying kernel from %s to %s. Please wait...' %
335 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800336 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800337 (self._join_part(root_dev, self.KERNEL_MAP[from_part]),
338 self._join_part(root_dev, self.KERNEL_MAP[to_part])))
339 logging.info('Copying rootfs from %s to %s. Please wait...' %
340 (from_part, to_part))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800341 self.faft_client.run_shell_command('dd if=%s of=%s bs=4M' %
Tom Wai-Hong Tamf2103be2011-11-10 07:26:56 +0800342 (self._join_part(root_dev, self.ROOTFS_MAP[from_part]),
343 self._join_part(root_dev, self.ROOTFS_MAP[to_part])))
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800344
345
346 def ensure_kernel_boot(self, part):
347 """Ensure the request kernel boot.
348
349 If not, it duplicates the current kernel to the requested kernel
350 and sets the requested higher priority to ensure it boot.
351
352 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800353 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800354 """
355 if not self.root_part_checker(part):
356 self.copy_kernel_and_rootfs(from_part=self.OTHER_KERNEL_MAP[part],
357 to_part=part)
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800358 self.run_faft_step({
359 'userspace_action': (self.reset_and_prioritize_kernel, part),
360 })
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800361
362
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800363 def wait_fw_screen_and_ctrl_d(self):
364 """Wait for firmware warning screen and press Ctrl-D."""
365 time.sleep(self.FIRMWARE_SCREEN_DELAY)
366 self.servo.ctrl_d()
367
368
Tom Wai-Hong Tam5d2f4702011-12-06 10:42:31 +0800369 def wait_fw_screen_and_plug_usb(self):
370 """Wait for firmware warning screen and then unplug and plug the USB."""
371 time.sleep(self.FIRMWARE_SCREEN_DELAY)
372 self.servo.set('usb_mux_sel1', 'servo_sees_usbkey')
373 time.sleep(self.USB_PLUG_DELAY)
374 self.servo.set('usb_mux_sel1', 'dut_sees_usbkey')
375
376
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800377 def setup_tried_fwb(self, tried_fwb):
378 """Setup for fw B tried state.
379
380 It makes sure the system in the requested fw B tried state. If not, it
381 tries to do so.
382
383 Args:
384 tried_fwb: True if requested in tried_fwb=1; False if tried_fwb=0.
385 """
386 if tried_fwb:
387 if not self.crossystem_checker({'tried_fwb': '1'}):
388 logging.info(
389 'Firmware is not booted with tried_fwb. Reboot into it.')
390 self.run_faft_step({
391 'userspace_action': self.faft_client.set_try_fw_b,
392 })
393 else:
394 if not self.crossystem_checker({'tried_fwb': '0'}):
395 logging.info(
396 'Firmware is booted with tried_fwb. Reboot to clear.')
397 self.run_faft_step({})
398
399
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800400 def setup_dev_mode(self, dev_mode):
401 """Setup for development mode.
402
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800403 It makes sure the system in the requested normal/dev mode. If not, it
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800404 tries to do so.
405
406 Args:
407 dev_mode: True if requested in dev mode; False if normal mode.
408 """
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800409 # Change the default firmware_action for dev mode passing the fw screen.
410 self.register_faft_template({
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800411 'firmware_action': (self.wait_fw_screen_and_ctrl_d if dev_mode
412 else None),
413 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800414 if dev_mode:
415 if not self.crossystem_checker({'devsw_cur': '1'}):
416 logging.info('Dev switch is not on. Now switch it on.')
417 self.servo.enable_development_mode()
418 if not self.crossystem_checker({'devsw_boot': '1',
419 'mainfw_type': 'developer'}):
420 logging.info('System is not in dev mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800421 self.run_faft_step({
422 'userspace_action': (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800423 'chromeos-firmwareupdate --mode todev && reboot'),
424 'reboot_action': None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800425 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800426 else:
427 if not self.crossystem_checker({'devsw_cur': '0'}):
428 logging.info('Dev switch is not off. Now switch it off.')
429 self.servo.disable_development_mode()
430 if not self.crossystem_checker({'devsw_boot': '0',
431 'mainfw_type': 'normal'}):
432 logging.info('System is not in normal mode. Reboot into it.')
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800433 self.run_faft_step({
434 'userspace_action': (self.faft_client.run_shell_command,
Tom Wai-Hong Tamc7ecfca2011-12-06 11:12:31 +0800435 'chromeos-firmwareupdate --mode tonormal && reboot'),
436 'reboot_action': None,
Tom Wai-Hong Tamfd590c92011-11-25 11:50:57 +0800437 })
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800438
439
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800440 def setup_kernel(self, part):
441 """Setup for kernel test.
442
443 It makes sure both kernel A and B bootable and the current boot is
444 the requested kernel part.
445
446 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800447 part: A string of kernel partition number or 'a'/'b'.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800448 """
449 self.ensure_kernel_boot(part)
450 self.copy_kernel_and_rootfs(from_part=part,
451 to_part=self.OTHER_KERNEL_MAP[part])
452 self.reset_and_prioritize_kernel(part)
453
454
455 def reset_and_prioritize_kernel(self, part):
456 """Make the requested partition highest priority.
457
458 This function also reset kerenl A and B to bootable.
459
460 Args:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800461 part: A string of partition number to be prioritized.
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800462 """
463 root_dev = self.faft_client.get_root_dev()
464 # Reset kernel A and B to bootable.
465 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
466 (self.KERNEL_MAP['a'], root_dev))
467 self.faft_client.run_shell_command('cgpt add -i%s -P1 -S1 -T0 %s' %
468 (self.KERNEL_MAP['b'], root_dev))
469 # Set kernel part highest priority.
470 self.faft_client.run_shell_command('cgpt prioritize -i%s %s' %
471 (self.KERNEL_MAP[part], root_dev))
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800472 # Safer to sync and wait until the cgpt status written to the disk.
473 self.faft_client.run_shell_command('sync')
474 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tamcfda61f2011-11-02 17:41:01 +0800475
476
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +0800477 def sync_and_hw_reboot(self):
478 """Request the client sync and do a warm reboot.
479
480 This is the default reboot action on FAFT.
481 """
482 self.faft_client.run_shell_command('sync')
Tom Wai-Hong Tam6a863ba2011-12-08 10:13:28 +0800483 time.sleep(self.SYNC_DELAY)
Tom Wai-Hong Tamf1e34972011-11-02 17:07:04 +0800484 self.servo.warm_reset()
485
486
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800487 def _call_action(self, action_tuple):
488 """Call the action function with/without arguments.
489
490 Args:
491 action_tuple: A function, or a tuple which consisted of a function
492 and its arguments (if any).
493
494 Returns:
495 The result value of the action function.
496 """
497 if isinstance(action_tuple, tuple):
498 action = action_tuple[0]
499 args = action_tuple[1:]
500 if callable(action):
501 logging.info('calling %s with parameter %s' % (
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800502 str(action), str(action_tuple[1])))
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800503 return action(*args)
504 else:
505 logging.info('action is not callable!')
506 else:
507 action = action_tuple
508 if action is not None:
509 if callable(action):
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800510 logging.info('calling %s' % str(action))
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800511 return action()
512 else:
513 logging.info('action is not callable!')
514
515 return None
516
517
518 def register_faft_template(self, template):
519 """Register FAFT template, the default FAFT_STEP of each step.
520
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800521 Any missing field falls back to the original faft_template.
522
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800523 Args:
524 template: A FAFT_STEP dict.
525 """
Tom Wai-Hong Tam109f63c2011-12-08 14:58:27 +0800526 self._faft_template.update(template)
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800527
528
529 def register_faft_sequence(self, sequence):
530 """Register FAFT sequence.
531
532 Args:
533 sequence: A FAFT_SEQUENCE array which consisted of FAFT_STEP dicts.
534 """
535 self._faft_sequence = sequence
536
537
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800538 def run_faft_step(self, step, no_reboot=False):
539 """Run a single FAFT step.
540
541 Any missing field falls back to faft_template. An empty step means
542 running the default faft_template.
543
544 Args:
545 step: A FAFT_STEP dict.
546 no_reboot: True to prevent running reboot_action and firmware_action.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800547
548 Raises:
Tom Wai-Hong Tama9c1a502011-11-10 06:39:26 +0800549 error.TestFail: An error when the test failed.
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800550 """
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800551 test = {}
552 test.update(self._faft_template)
553 test.update(step)
554
555 if test['state_checker']:
556 if not self._call_action(test['state_checker']):
557 raise error.TestFail('State checker failed!')
558
559 self._call_action(test['userspace_action'])
560
561 # Don't run reboot_action and firmware_action if no_reboot is True.
562 if not no_reboot:
563 self._call_action(test['reboot_action'])
564 self.wait_for_client_offline()
565 self._call_action(test['firmware_action'])
566
567 if 'install_deps_after_boot' in test:
568 self.wait_for_client(
569 install_deps=test['install_deps_after_boot'])
570 else:
571 self.wait_for_client()
572
573
574 def run_faft_sequence(self):
575 """Run FAFT sequence which was previously registered."""
Tom Wai-Hong Tama70f0fe2011-09-02 18:28:47 +0800576 sequence = self._faft_sequence
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800577 index = 1
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800578 for step in sequence:
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800579 logging.info('======== Running FAFT sequence step %d ========' %
580 index)
Tom Wai-Hong Tam2c50dff2011-11-11 07:01:01 +0800581 # Don't reboot in the last step.
582 self.run_faft_step(step, no_reboot=(step is sequence[-1]))
Tom Wai-Hong Tame8f291a2011-12-08 22:03:53 +0800583 index += 1