Home | History | Annotate | Download | only in detachablebase_TriggerHammerd
      1 # Copyright 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 
      5 import logging
      6 
      7 from autotest_lib.client.bin import test
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.client.common_lib import utils
     10 
     11 
     12 class detachablebase_TriggerHammerd(test.test):
     13     """Hammerd smoke test.
     14 
     15     Hammerd upstart job should be invoked on boot. The job should exit normally.
     16     """
     17     version = 1
     18 
     19     # The upstart job's name.
     20     PROCESS_NAME = 'hammerd'
     21 
     22     # Path of the system log.
     23     MESSAGE_PATH = '/var/log/messages'
     24 
     25     # Message to find the start line of each boot. The boot timing is usually
     26     # [    0.000000], but in case the boot process has delay, we accept any
     27     # timing within 1 second.
     28     BOOT_START_LINE_MSG = 'kernel: \[    0\.[0-9]\{6\}\] Linux version'
     29 
     30     # Message that is printed when hammerd is triggered on boot.
     31     # It is defined at `src/platform2/hammerd/init/hammerd-at-boot.sh`.
     32     TRIGGER_ON_BOOT_MSG = 'Force trigger hammerd at boot.'
     33 
     34     # Message that is printed when the hammerd job failed to terminated
     35     # normally.
     36     PROCESS_FAILED_MSG = '%s main process ([0-9]\+) terminated' % PROCESS_NAME
     37 
     38     def run_once(self):
     39         # Get the start line of message belonging to this current boot.
     40         start_line = utils.run(
     41                 'grep -ni "%s" "%s" | tail -n 1 | cut -d ":" -f 1' %
     42                 (self.BOOT_START_LINE_MSG, self.MESSAGE_PATH),
     43                 ignore_status=True).stdout.strip()
     44         logging.info('Start line: %s', start_line)
     45         if not start_line:
     46             raise error.TestError('Start line of boot is not found.')
     47 
     48         def _grep_messages(pattern):
     49             return utils.run('tail -n +%s %s | grep "%s"' %
     50                              (start_line, self.MESSAGE_PATH, pattern),
     51                              ignore_status=True).stdout
     52 
     53         # Check hammerd is triggered on boot.
     54         if not _grep_messages(self.TRIGGER_ON_BOOT_MSG):
     55             raise error.TestFail('hammerd is not triggered on boot.')
     56         # Check hammerd is terminated normally.
     57         if _grep_messages(self.PROCESS_FAILED_MSG):
     58             hammerd_log = _grep_messages(self.PROCESS_NAME)
     59             logging.error('Hammerd log: %s', hammerd_log)
     60             raise error.TestFail('hammerd terminated with non-zero value')
     61