configurable_cfm_test.py revision 343c9be8c19c4ba7e322b2ca4b53df2345f2c11c
1import logging
2
3from autotest_lib.client.common_lib.cros import crash_detector
4from autotest_lib.client.common_lib.cros.cfm.usb import usb_device_collector
5from autotest_lib.client.common_lib.cros.cfm.usb import usb_port_manager
6from autotest_lib.server.cros.cfm import cfm_base_test
7from autotest_lib.server.cros.cfm.configurable_test import action_context
8
9class 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
30class 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
51class 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, cfm_test.configuration.run_test_only))
66        self.cfm_test = cfm_test
67        device_collector = usb_device_collector.UsbDeviceCollector(host)
68        port_manager = usb_port_manager.UsbPortManager(host)
69        crash_file_detector = crash_detector.CrashDetector(host)
70        # Call get_new_crash_files() once. This records the current crash
71        # files so that subsequent calls only check the delta, i.e.
72        # new crash files from here on.
73        crash_file_detector.get_new_crash_files()
74        # self.cfm_facade is inherited from CfmBaseTest.
75        context = action_context.ActionContext(
76                cfm_facade=self.cfm_facade,
77                file_contents_collector=HostFileContentsCollector(host),
78                host=host,
79                usb_device_collector=device_collector,
80                usb_port_manager=port_manager,
81                crash_detector=crash_file_detector)
82        self.test_runner = TestRunner(context)
83
84    def run_once(self):
85        """
86        Runs the test.
87        """
88        self.test_runner.run_test(self.cfm_test)
89
90