1 import logging 2 3 from autotest_lib.client.common_lib.cros import crash_detector 4 from autotest_lib.client.common_lib.cros.cfm.usb import usb_device_collector 5 from autotest_lib.client.common_lib.cros.cfm.usb import usb_port_manager 6 from autotest_lib.server.cros.cfm import cfm_base_test 7 from autotest_lib.server.cros.cfm.configurable_test import action_context 8 9 class TestRunner(object): 10 """ 11 Runs a test. 12 """ 13 def __init__(self, context): 14 """ 15 Initializes. 16 17 @param context ActionContext providing the dependencies for the test. 18 """ 19 self.context = context 20 21 def run_test(self, cfm_test): 22 """ 23 Runs one test. 24 25 @param cfm_test CfmTest instance to execute. 26 """ 27 logging.info('RUNNING:\n%s', str(cfm_test)) 28 cfm_test.scenario.execute(self.context) 29 30 class HostFileContentsCollector(object): 31 """ 32 File contents collector that executes commands against the host. 33 """ 34 def __init__(self, host): 35 """ 36 Initializes with a host. 37 38 @param host a host object as available from CfmBaseTest.host 39 """ 40 self.host = host 41 42 def collect_file_contents(self, path): 43 """ 44 Returns the file contents of the file at the specified path. 45 46 @param path The path of the file. 47 @returns The contents of the file 48 """ 49 return self.host.run_output('cat "%s"' % path) 50 51 class ConfigurableCfmTest(cfm_base_test.CfmBaseTest): 52 """ 53 Base class for the actual Autotests that execute configurable CFM tests. 54 """ 55 version = 1 56 57 def initialize(self, host, cfm_test): 58 """ 59 Initializes the test. 60 61 @param host The host the test is run against 62 @param cfm_test CfmTest instance to execute. 63 """ 64 (super(ConfigurableCfmTest, self) 65 .initialize(host, 66 cfm_test.configuration.run_test_only, 67 cfm_test.configuration.skip_enrollment)) 68 self.cfm_test = cfm_test 69 device_collector = usb_device_collector.UsbDeviceCollector(host) 70 port_manager = usb_port_manager.UsbPortManager(host) 71 crash_file_detector = crash_detector.CrashDetector(host) 72 # Call get_new_crash_files() once. This records the current crash 73 # files so that subsequent calls only check the delta, i.e. 74 # new crash files from here on. 75 crash_file_detector.get_new_crash_files() 76 # self.cfm_facade is inherited from CfmBaseTest. 77 context = action_context.ActionContext( 78 cfm_facade=self.cfm_facade, 79 file_contents_collector=HostFileContentsCollector(host), 80 host=host, 81 usb_device_collector=device_collector, 82 usb_port_manager=port_manager, 83 crash_detector=crash_file_detector) 84 self.test_runner = TestRunner(context) 85 86 def run_once(self): 87 """ 88 Runs the test. 89 """ 90 self.test_runner.run_test(self.cfm_test) 91 92