• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 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, re, time
6
7from autotest_lib.client.common_lib import error
8from autotest_lib.server.cros import vboot_constants as vboot
9from autotest_lib.server.cros.faft.firmware_test import FirmwareTest
10from autotest_lib.server.cros.watchdog_tester import WatchdogTester
11
12POWER_DIR = '/var/lib/power_manager'
13TMP_POWER_DIR = '/tmp/power_manager'
14
15class firmware_EventLog(FirmwareTest):
16    """
17    Test to ensure eventlog is written on boot and suspend/resume.
18    """
19    version = 1
20
21    _TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
22
23    def initialize(self, host, cmdline_args):
24        super(firmware_EventLog, self).initialize(host, cmdline_args)
25        self.host = host
26        self.switcher.setup_mode('normal')
27        self.setup_usbkey(usbkey=True, host=False)
28
29    def _has_event(self, pattern):
30        return bool(filter(re.compile(pattern).search, self._events))
31
32    def _gather_events(self):
33        entries = self.faft_client.system.run_shell_command_get_output(
34                'mosys eventlog list')
35        now = self._now()
36        self._events = []
37        for line in reversed(entries):
38            _, time_string, event = line.split(' | ', 2)
39            timestamp = time.strptime(time_string, self._TIME_FORMAT)
40            if timestamp > now:
41                logging.error('Found prophecy: "%s"', line)
42                raise error.TestFail('Event timestamp lies in the future')
43            if timestamp < self._cutoff_time:
44                logging.debug('Event "%s" too early, stopping search', line)
45                break
46            logging.info('Found event: "%s"', line)
47            self._events.append(event)
48
49    # This assumes that Linux and the firmware use the same RTC. mosys converts
50    # timestamps to localtime, and so do we (by calling date without --utc).
51    def _now(self):
52        time_string = self.faft_client.system.run_shell_command_get_output(
53                'date +"%s"' % self._TIME_FORMAT)[0]
54        logging.debug('Current local system time on DUT is "%s"', time_string)
55        return time.strptime(time_string, self._TIME_FORMAT)
56
57    def disable_suspend_to_idle(self):
58        """Disable the powerd preference for suspend_to_idle."""
59        logging.info('Disabling suspend_to_idle')
60        # Make temporary directory to hold powerd preferences so we
61        # do not leave behind any state if the test is aborted.
62        self.host.run('mkdir -p %s' % TMP_POWER_DIR)
63        self.host.run('echo 0 > %s/suspend_to_idle' % TMP_POWER_DIR)
64        self.host.run('mount --bind %s %s' % (TMP_POWER_DIR, POWER_DIR))
65        self.host.run('restart powerd')
66
67    def teardown_powerd_prefs(self):
68        """Clean up custom powerd preference changes."""
69        self.host.run('umount %s' % POWER_DIR)
70        self.host.run('restart powerd')
71
72    def run_once(self):
73        """Runs a single iteration of the test."""
74        if not self.faft_config.has_eventlog:
75            raise error.TestNAError('This board has no eventlog support.')
76
77        logging.info('Verifying eventlog behavior on normal mode boot')
78        self._cutoff_time = self._now()
79        self.switcher.mode_aware_reboot()
80        self.check_state((self.checkers.crossystem_checker, {
81                              'devsw_boot': '0',
82                              'mainfw_type': 'normal',
83                              }))
84        self._gather_events()
85        if not self._has_event(r'System boot'):
86            raise error.TestFail('No "System boot" event on normal boot.')
87        # ' Wake' to match 'FW Wake' and 'ACPI Wake' but not 'Wake Source'
88        if self._has_event(r'Developer Mode|Recovery Mode|Sleep| Wake'):
89            raise error.TestFail('Incorrect event logged on normal boot.')
90
91        logging.debug('Transitioning to dev mode for next test')
92        self.switcher.reboot_to_mode(to_mode='dev')
93
94        logging.info('Verifying eventlog behavior on developer mode boot')
95        self._cutoff_time = self._now()
96        self.switcher.mode_aware_reboot()
97        self.check_state((self.checkers.crossystem_checker, {
98                              'devsw_boot': '1',
99                              'mainfw_type': 'developer',
100                              }))
101        self._gather_events()
102        if (not self._has_event(r'System boot') or
103            not self._has_event(r'Chrome OS Developer Mode')):
104            raise error.TestFail('Missing required event on dev mode boot.')
105        if self._has_event(r'Recovery Mode|Sleep| Wake'):
106            raise error.TestFail('Incorrect event logged on dev mode boot.')
107
108        logging.debug('Transitioning back to normal mode for final tests')
109        self.switcher.reboot_to_mode(to_mode='normal')
110
111        logging.info('Verifying eventlog behavior in recovery mode')
112        self._cutoff_time = self._now()
113        self.switcher.reboot_to_mode(to_mode='rec')
114        logging.debug('Check we booted into recovery')
115        self.check_state((self.checkers.crossystem_checker, {
116                         'mainfw_type': 'recovery',
117                         'recovery_reason' : vboot.RECOVERY_REASON['RO_MANUAL'],
118                         }))
119        self.switcher.mode_aware_reboot()
120        self.check_state((self.checkers.crossystem_checker, {
121                              'devsw_boot': '0',
122                              'mainfw_type': 'normal',
123                              }))
124        self._gather_events()
125        if (not self._has_event(r'System boot') or
126            not self._has_event(r'Chrome OS Recovery Mode \| Recovery Button')):
127            raise error.TestFail('Missing required event in recovery mode.')
128        if self._has_event(r'Developer Mode|Sleep|FW Wake|ACPI Wake \| S3'):
129            raise error.TestFail('Incorrect event logged in recovery mode.')
130
131        logging.info('Verifying eventlog behavior on suspend/resume')
132        self._cutoff_time = self._now()
133        self.faft_client.system.run_shell_command(
134                'powerd_dbus_suspend -wakeup_timeout=10')
135        time.sleep(5)   # a little slack time for powerd to write the 'Wake'
136        self._gather_events()
137
138        # Ensure system did not log developer or recovery mode event
139        if self._has_event(r'System |Developer Mode|Recovery Mode'):
140            raise error.TestFail('Incorrect event logged on suspend/resume.')
141
142        # Accept any set: Wake/Sleep, ACPI Enter/Wake S3, S0ix Enter/Exit
143        if ((not self._has_event(r'^Wake') or not self._has_event(r'Sleep')) and
144            (not self._has_event(r'ACPI Enter \| S3') or
145             not self._has_event(r'ACPI Wake \| S3')) and
146            (not self._has_event(r'S0ix Enter') or
147             not self._has_event(r'S0ix Exit'))):
148            # If previous attempt failed force S3 mode and try again
149            logging.info('Enabling S3 to retest suspend/resume')
150            self.disable_suspend_to_idle()
151            self._cutoff_time = self._now()
152            self.faft_client.system.run_shell_command(
153                'powerd_dbus_suspend -wakeup_timeout=10')
154            time.sleep(5)   # a little slack time for powerd to write the 'Wake'
155            self.teardown_powerd_prefs()
156            self._gather_events()
157            if (not self._has_event(r'ACPI Enter \| S3') or
158                not self._has_event(r'ACPI Wake \| S3')):
159                raise error.TestFail('Missing required event on suspend/resume')
160
161        watchdog = WatchdogTester(self.host)
162        if not watchdog.is_supported():
163            logging.info('No hardware watchdog on this platform, skipping')
164        elif self.faft_client.system.run_shell_command_get_output(
165                'crossystem arch')[0] != 'x86': # TODO: Implement event on x86
166            logging.info('Verifying eventlog behavior with hardware watchdog')
167            self._cutoff_time = self._now()
168            with watchdog:
169                watchdog.trigger_watchdog()
170            self._gather_events()
171            if (not self._has_event(r'System boot') or
172                not self._has_event(r'Hardware watchdog reset')):
173                raise error.TestFail('Did not log hardware watchdog event')
174