• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2017 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 datetime
6import logging
7import os
8import re
9import time
10
11from autotest_lib.client.bin import utils
12from autotest_lib.client.common_lib import error
13from autotest_lib.server.cros import stress
14from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
15
16class firmware_EmmcWriteLoad(FirmwareTest):
17    """
18    Runs chromeos-install repeatedly while monitoring dmesg output for EMMC
19    timeout errors.
20
21    This test requires a USB disk plugged-in, which contains a Chrome OS test
22    image (built by "build_image test"). On runtime, this test first switches
23    DUT to developer mode. When dev_boot_usb=0, pressing Ctrl-U on developer
24    screen should not boot the USB disk. When dev_boot_usb=1, pressing Ctrl-U
25    should boot the USB disk.
26
27    The length of time in minutes should be specified by the parameter
28    -a 'minutes_to_run=240'
29    """
30    version = 1
31
32    INSTALL_COMMAND = '/usr/sbin/chromeos-install --yes'
33    ERROR_MESSAGE_REGEX = re.compile(
34            r'mmc[0-9]+: Timeout waiting for hardware interrupt', re.MULTILINE)
35
36    def initialize(self, host, cmdline_args, ec_wp=None):
37        """Initialize the test"""
38        dict_args = utils.args_to_dict(cmdline_args)
39        self.minutes_to_run = int(dict_args.get('minutes_to_run', 5))
40        super(firmware_EmmcWriteLoad, self).initialize(
41            host, cmdline_args, ec_wp=ec_wp)
42
43        self.switcher.setup_mode('dev')
44        # Use the USB key for Ctrl-U dev boot, not recovery.
45        self.setup_usbkey(usbkey=True, host=False, used_for_recovery=False)
46
47        self.original_dev_boot_usb = self.faft_client.system.get_dev_boot_usb()
48        logging.info('Original dev_boot_usb value: %s',
49                     str(self.original_dev_boot_usb))
50
51
52    def read_dmesg(self, filename):
53        """Put the contents of 'dmesg -cT' into the given file.
54
55        @param filename: The file to write 'dmesg -cT' into.
56        """
57        with open(filename, 'w') as f:
58            self._client.run('dmesg -cT', stdout_tee=f)
59
60        return utils.read_file(filename)
61
62    def check_for_emmc_error(self, dmesg):
63        """Check the current dmesg output for the specified error message regex.
64
65        @param dmesg: Contents of the dmesg buffer.
66
67        @return True if error found.
68        """
69        for line in dmesg.splitlines():
70            if self.ERROR_MESSAGE_REGEX.search(line):
71                return True
72
73        return False
74
75    def install_chrome_os(self):
76        """Runs the install command. """
77        self.faft_client.system.run_shell_command(self.INSTALL_COMMAND)
78
79    def poll_for_emmc_error(self, dmesg_file, poll_seconds=20):
80        """Continuously polls the contents of dmesg for the emmc failure message
81
82        @param dmesg_file: Contents of the dmesg buffer.
83        @param poll_seconds: Time to wait before checking dmesg again.
84
85        @return True if error found.
86        """
87        end_time = datetime.datetime.now() + \
88                   datetime.timedelta(minutes=self.minutes_to_run)
89
90        while datetime.datetime.now() <= end_time:
91            dmesg = self.read_dmesg(dmesg_file)
92            contains_error = self.check_for_emmc_error(dmesg)
93
94            if contains_error:
95                raise error.TestFail('eMMC error found. Dmesg output: %s' %
96                                     dmesg)
97            time.sleep(poll_seconds)
98
99    def cleanup(self):
100        """Cleanup the test"""
101        try:
102            self.ensure_dev_internal_boot(self.original_dev_boot_usb)
103        except Exception as e:
104            logging.error("Caught exception: %s", str(e))
105        super(firmware_EmmcWriteLoad, self).cleanup()
106
107    def run_once(self):
108        """Main test logic"""
109        self.faft_client.system.set_dev_boot_usb(1)
110        self.switcher.simple_reboot()
111        self.switcher.bypass_dev_boot_usb()
112        self.switcher.wait_for_client()
113
114        logging.info('Expected USB boot, set dev_boot_usb to the original.')
115        self.check_state((self.checkers.dev_boot_usb_checker, (True, True),
116                          'Device not booted from USB image properly.'))
117        stressor = stress.ControlledStressor(self.install_chrome_os)
118
119        dmesg_filename = os.path.join(self.resultsdir, 'dmesg')
120
121        logging.info('===== Starting OS install loop. =====')
122        logging.info('===== Running install for %s minutes. =====',
123                     self.minutes_to_run)
124        stressor.start()
125
126        self.poll_for_emmc_error(dmesg_file=dmesg_filename)
127
128        logging.info('Stopping install loop.')
129        # Usually takes a little over 3 minutes to install so make sure we
130        # wait long enough for a install iteration to complete.
131        stressor.stop(timeout=300)
132
133        logging.info("Installing OS one more time.")
134        # Installing OS one more time to ensure DUT is left in a good state
135        self.install_chrome_os()
136