1#
2# SPDX-License-Identifier: MIT
3#
4
5import os
6
7from oeqa.runtime.case import OERuntimeTestCase
8from oeqa.core.decorator.depends import OETestDepends
9from oeqa.core.decorator.data import skipIfNotFeature
10from oeqa.runtime.decorator.package import OEHasPackage
11
12class KernelModuleTest(OERuntimeTestCase):
13
14    @classmethod
15    def setUp(cls):
16        src = os.path.join(cls.tc.runtime_files_dir, 'hellomod.c')
17        dst = '/tmp/hellomod.c'
18        cls.tc.target.copyTo(src, dst)
19
20        src = os.path.join(cls.tc.runtime_files_dir, 'hellomod_makefile')
21        dst = '/tmp/Makefile'
22        cls.tc.target.copyTo(src, dst)
23
24    @classmethod
25    def tearDown(cls):
26        files = '/tmp/Makefile /tmp/hellomod.c'
27        cls.tc.target.run('rm %s' % files)
28
29    @skipIfNotFeature('tools-sdk',
30                      'Test requires tools-sdk to be in IMAGE_FEATURES')
31    @OETestDepends(['gcc.GccCompileTest.test_gcc_compile'])
32    @OEHasPackage(['kernel-devsrc'])
33    @OEHasPackage(['make'])
34    @OEHasPackage(['gcc'])
35    def test_kernel_module(self):
36        cmds = [
37            'cd /usr/src/kernel && make scripts prepare',
38            'cd /tmp && make',
39            'cd /tmp && insmod hellomod.ko',
40            'lsmod | grep hellomod',
41            'dmesg | grep Hello',
42            'rmmod hellomod', 'dmesg | grep "Cleaning up hellomod"'
43            ]
44        for cmd in cmds:
45            status, output = self.target.run(cmd, 900)
46            self.assertEqual(status, 0, msg='\n'.join([cmd, output]))
47