1# Copyright (c) 2012 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 subprocess
6from autotest_lib.client.bin import test
7from autotest_lib.client.common_lib import error
8
9class kernel_AsyncDriverProbe(test.test):
10    """
11    Handle checking asynchronous device probing.
12    """
13    version = 1
14
15    def module_loaded(self, module):
16        """
17        Detect if the given module is already loaded in the kernel.
18
19        @param module: name of module to check
20        """
21        module = module.replace('-', '_')
22        match = "%s " % (module)
23        for line in open("/proc/modules"):
24            if line.startswith(match):
25                return True
26        return False
27
28    def rmmod(self, module):
29        """
30        Unload a module if it is already loaded in the kernel.
31
32        @param module: name of module to unload
33        """
34        if self.module_loaded(module):
35            subprocess.call(["rmmod", module])
36
37    def run_once(self):
38        """
39        Try loading the test module. It will time registration for
40        synchronous and asynchronous cases and will fail to load if
41        timing is off.
42        """
43
44        module = "test_async_driver_probe"
45
46        # Start from a clean slate.
47        self.rmmod(module)
48
49        exit_code = subprocess.call(["modprobe", "-n", "-q", module])
50        if exit_code:
51            raise error.TestNAError(
52                "%s.ko module does not seem to be available "
53                "(modprobe rc=%s); skipping async probe test" %
54                (module, exit_code))
55
56        # Try loading the module. If it loads successfully test passes.
57        subprocess.call(["modprobe", module])
58        loaded = self.module_loaded(module)
59
60        # Clean up after ourselves
61        self.rmmod(module)
62
63        if not loaded:
64            raise error.TestFail("Test module failed to load")
65