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 6 7import common 8from autotest_lib.client.common_lib import error 9from autotest_lib.server import test 10from autotest_lib.server.cros import debugd_dev_tools 11 12 13class 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 by disabling, enabling, then disabling again. 72 73 @param tool: Tool object to test. 74 75 @throw debugd_dev_tools.AccessError: Dev tool access failed. 76 @throw error.TestFail: A tool failed to affect device state. 77 """ 78 # Start by disabling the tool. If disable fails we may still be 79 # able to test enabling the tool. 80 logging.debug('Disabling %s.', tool) 81 tool.disable() 82 if tool.is_enabled(): 83 raise error.TestFail('%s did not disable correctly.' % tool) 84 85 # Now enable the tool and make sure it worked. 86 logging.debug('Enabling %s.', tool) 87 tool.enable() 88 if not tool.is_enabled(): 89 raise error.TestFail('%s did not enable correctly.' % tool) 90 91 # Disable one more time to confirm our disable routine works. 92 logging.debug('Disabling %s.', tool) 93 tool.disable() 94 if tool.is_enabled(): 95 raise error.TestFail('%s did not disable correctly.' % tool) 96 97 98 def run_once(self, host=None): 99 """Main test function.""" 100 self.create_tools(host) 101 try: 102 # First remove rootfs verification if it's not already. 103 if not self.rootfs_tool.is_enabled(): 104 logging.debug('Removing rootfs verification.') 105 self.rootfs_tool.enable() 106 107 for tool in self.tools: 108 self.test_tool(tool) 109 finally: 110 self.cleanup_tools(host) 111