xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/selftest/cases/kerneldevelopment.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyunimport os
2*4882a593Smuzhiyunfrom oeqa.selftest.case import OESelftestTestCase
3*4882a593Smuzhiyunfrom oeqa.utils.commands import runCmd, get_bb_var
4*4882a593Smuzhiyunfrom oeqa.utils.git import GitRepo
5*4882a593Smuzhiyun
6*4882a593Smuzhiyunclass KernelDev(OESelftestTestCase):
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun    @classmethod
9*4882a593Smuzhiyun    def setUpClass(cls):
10*4882a593Smuzhiyun        super(KernelDev, cls).setUpClass()
11*4882a593Smuzhiyun        # Create the recipe directory structure inside the created layer
12*4882a593Smuzhiyun        cls.layername = 'meta-kerneltest'
13*4882a593Smuzhiyun        runCmd('bitbake-layers create-layer %s' % cls.layername)
14*4882a593Smuzhiyun        runCmd('mkdir -p %s/recipes-kernel/linux/linux-yocto' % cls.layername)
15*4882a593Smuzhiyun        cls.recipes_linuxyocto_dir = os.path.join \
16*4882a593Smuzhiyun            (cls.builddir, cls.layername, 'recipes-kernel', 'linux', 'linux-yocto')
17*4882a593Smuzhiyun        cls.recipeskernel_dir = os.path.dirname(cls.recipes_linuxyocto_dir)
18*4882a593Smuzhiyun        runCmd('bitbake-layers add-layer %s' % cls.layername)
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun    @classmethod
21*4882a593Smuzhiyun    def tearDownClass(cls):
22*4882a593Smuzhiyun        runCmd('bitbake-layers remove-layer %s' % cls.layername, ignore_status=True)
23*4882a593Smuzhiyun        runCmd('rm -rf %s' % cls.layername)
24*4882a593Smuzhiyun        super(KernelDev, cls).tearDownClass()
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun    def setUp(self):
27*4882a593Smuzhiyun        super(KernelDev, self).setUp()
28*4882a593Smuzhiyun        self.set_machine_config('MACHINE = "qemux86-64"\n')
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun    def test_apply_patches(self):
31*4882a593Smuzhiyun        """
32*4882a593Smuzhiyun        Summary:     Able to apply a single patch to the Linux kernel source
33*4882a593Smuzhiyun        Expected:    The README file should exist and the patch changes should be
34*4882a593Smuzhiyun                     displayed at the end of the file.
35*4882a593Smuzhiyun        Product:     Kernel Development
36*4882a593Smuzhiyun        Author:      Yeoh Ee Peng <ee.peng.yeoh@intel.com>
37*4882a593Smuzhiyun        AutomatedBy: Mazliana Mohamad <mazliana.mohamad@intel.com>
38*4882a593Smuzhiyun        """
39*4882a593Smuzhiyun        runCmd('bitbake virtual/kernel -c patch')
40*4882a593Smuzhiyun        kernel_source = get_bb_var('STAGING_KERNEL_DIR')
41*4882a593Smuzhiyun        readme = os.path.join(kernel_source, 'README')
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun        # This test step adds modified file 'README' to git and creates a
44*4882a593Smuzhiyun        # patch file '0001-KERNEL_DEV_TEST_CASE.patch' at the same location as file
45*4882a593Smuzhiyun        patch_content = 'This is a test to apply a patch to the kernel'
46*4882a593Smuzhiyun        with open(readme, 'a+') as f:
47*4882a593Smuzhiyun            f.write(patch_content)
48*4882a593Smuzhiyun        repo = GitRepo('%s' % kernel_source, is_topdir=True)
49*4882a593Smuzhiyun        repo.run_cmd('add %s' % readme)
50*4882a593Smuzhiyun        repo.run_cmd(['commit', '-m', 'KERNEL_DEV_TEST_CASE'])
51*4882a593Smuzhiyun        repo.run_cmd(['format-patch', '-1'])
52*4882a593Smuzhiyun        patch_name = '0001-KERNEL_DEV_TEST_CASE.patch'
53*4882a593Smuzhiyun        patchpath = os.path.join(kernel_source, patch_name)
54*4882a593Smuzhiyun        runCmd('mv %s %s' % (patchpath, self.recipes_linuxyocto_dir))
55*4882a593Smuzhiyun        runCmd('rm %s ' % readme)
56*4882a593Smuzhiyun        self.assertFalse(os.path.exists(readme))
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun        recipe_append = os.path.join(self.recipeskernel_dir, 'linux-yocto_%.bbappend')
59*4882a593Smuzhiyun        with open(recipe_append, 'w+') as fh:
60*4882a593Smuzhiyun            fh.write('SRC_URI += "file://%s"\n' % patch_name)
61*4882a593Smuzhiyun            fh.write('FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"')
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun        runCmd('bitbake virtual/kernel -c clean')
64*4882a593Smuzhiyun        runCmd('bitbake virtual/kernel -c patch')
65*4882a593Smuzhiyun        self.assertTrue(os.path.exists(readme))
66*4882a593Smuzhiyun        result = runCmd('tail -n 1 %s' % readme)
67*4882a593Smuzhiyun        self.assertEqual(result.output, patch_content)
68