Home | History | Annotate | Download | only in debugd_DevTools
      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 
      5 import logging
      6 
      7 import common
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.server import test
     10 from autotest_lib.server.cros import debugd_dev_tools
     11 
     12 
     13 class debugd_DevTools(test.test):
     14     """
     15     Debugd dev tools test. See control file for details.
     16     """
     17     version = 1
     18 
     19 
     20     def create_tools(self, host):
     21         """
     22         Creates and initializes the tools needed for the test.
     23 
     24         Saves a RootfsVerificationTool to self.rootfs_tool and the rest
     25         to self.tools. The RootfsVerificationTool is handled separately
     26         because it can't be disabled and is required first for the
     27         other tools to function properly.
     28 
     29         @param host: Host device.
     30 
     31         @throw error.TestNAError: Dev tools are unavailable.
     32         """
     33         if not debugd_dev_tools.are_dev_tools_available(host):
     34             raise error.TestNAError('Cannot access dev tools. Make sure the '
     35                                     'device is in dev mode with no owner and '
     36                                     'the boot lockbox is not finalized.')
     37 
     38         logging.debug('Creating dev tools.')
     39         self.rootfs_tool = debugd_dev_tools.RootfsVerificationTool()
     40         self.tools = (debugd_dev_tools.BootFromUsbTool(),
     41                       debugd_dev_tools.SshServerTool(),
     42                       debugd_dev_tools.SystemPasswordTool())
     43 
     44         logging.debug('Initializing dev tools.')
     45         self.rootfs_tool.initialize(host)
     46         for tool in self.tools:
     47             tool.initialize(host, save_initial_state=True)
     48 
     49 
     50     def cleanup_tools(self, host):
     51         """
     52         Performs cleanup to return the device to its initial state.
     53 
     54         Any tools that fail to clean up will print a warning but will
     55         not register a test failure.
     56 
     57         @param host: Host device.
     58         """
     59         logging.debug('Cleaning up tools.')
     60         for tool in self.tools:
     61             try:
     62                 tool.restore_state()
     63             except debugd_dev_tools.FeatureUnavailableError as e:
     64                 logging.warning('Could not restore %s - device state may be '
     65                                 'altered by test (%s).', tool, e)
     66         debugd_dev_tools.remove_temp_files(host)
     67 
     68 
     69     def test_tool(self, tool):
     70         """
     71         Tests an individual tool.
     72 
     73         Functionality is tested by disabling, enabling, then disabling
     74         again. Certain tools may be unavailable on a board (e.g. USB
     75         boot on Mario), which will log a warning but not register a
     76         test failure.
     77 
     78         @param tool: Tool object to test.
     79 
     80         @throw debugd_dev_tools.AccessError: Dev tool access failed.
     81         @throw error.TestFail: A tool failed to affect device state.
     82         """
     83         # Start by disabling the tool. If disable fails we may still be
     84         # able to test enabling the tool.
     85         logging.debug('Disabling %s.', tool)
     86         try:
     87             tool.disable()
     88         except debugd_dev_tools.FeatureUnavailableError as e:
     89             # If the tool can't be disabled and is already enabled there's no
     90             # way to test if our enable function is working or not.
     91             if tool.is_enabled():
     92                 logging.warning('Skipping %s - cannot disable (%s).', tool, e)
     93                 return
     94         if tool.is_enabled():
     95             raise error.TestFail('%s did not disable correctly.' % tool)
     96 
     97         # Now enable the tool and make sure it worked.
     98         logging.debug('Enabling %s.', tool)
     99         try:
    100             tool.enable()
    101         except debugd_dev_tools.FeatureUnavailableError as e:
    102             logging.warning('Skipping %s - cannot enable (%s).', tool, e)
    103             return
    104         if not tool.is_enabled():
    105             raise error.TestFail('%s did not enable correctly.' % tool)
    106 
    107         # Disable one more time to confirm our disable routine works.
    108         logging.debug('Disabling %s.', tool)
    109         try:
    110             tool.disable()
    111         except debugd_dev_tools.FeatureUnavailableError:
    112             return
    113         if tool.is_enabled():
    114             raise error.TestFail('%s did not disable correctly.' % tool)
    115 
    116 
    117     def run_once(self, host=None):
    118         self.create_tools(host)
    119         try:
    120             # First remove rootfs verification if it's not already.
    121             if not self.rootfs_tool.is_enabled():
    122                 logging.debug('Removing rootfs verification.')
    123                 self.rootfs_tool.enable()
    124 
    125             for tool in self.tools:
    126                 self.test_tool(tool)
    127         finally:
    128             self.cleanup_tools(host)
    129