xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oeqa/selftest/cases/containerimage.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# SPDX-License-Identifier: MIT
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun
5*4882a593Smuzhiyunimport os
6*4882a593Smuzhiyun
7*4882a593Smuzhiyunfrom oeqa.selftest.case import OESelftestTestCase
8*4882a593Smuzhiyunfrom oeqa.utils.commands import bitbake, get_bb_vars, runCmd
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun# This test builds an image with using the "container" IMAGE_FSTYPE, and
11*4882a593Smuzhiyun# ensures that then files in the image are only the ones expected.
12*4882a593Smuzhiyun#
13*4882a593Smuzhiyun# The only package added to the image is container_image_testpkg, which
14*4882a593Smuzhiyun# contains one file. However, due to some other things not cleaning up during
15*4882a593Smuzhiyun# rootfs creation, there is some cruft. Ideally bugs will be filed and the
16*4882a593Smuzhiyun# cruft removed, but for now we ignore some known set.
17*4882a593Smuzhiyun#
18*4882a593Smuzhiyun# Also for performance reasons we're only checking the cruft when using ipk.
19*4882a593Smuzhiyun# When using deb, and rpm it is a bit different and we could test all
20*4882a593Smuzhiyun# of them, but this test is more to catch if other packages get added by
21*4882a593Smuzhiyun# default other than what is in ROOTFS_BOOTSTRAP_INSTALL.
22*4882a593Smuzhiyun#
23*4882a593Smuzhiyunclass ContainerImageTests(OESelftestTestCase):
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun    # Verify that when specifying a IMAGE_TYPEDEP: of the form "foo.bar" that
26*4882a593Smuzhiyun    # the conversion type bar gets added as a dep as well
27*4882a593Smuzhiyun    def test_expected_files(self):
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun        def get_each_path_part(path):
30*4882a593Smuzhiyun            if path:
31*4882a593Smuzhiyun                part = [ '.' + path + '/' ]
32*4882a593Smuzhiyun                result = get_each_path_part(path.rsplit('/', 1)[0])
33*4882a593Smuzhiyun                if result:
34*4882a593Smuzhiyun                    return part + result
35*4882a593Smuzhiyun                else:
36*4882a593Smuzhiyun                    return part
37*4882a593Smuzhiyun            else:
38*4882a593Smuzhiyun                return None
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun        self.write_config("""PREFERRED_PROVIDER_virtual/kernel = "linux-dummy"
41*4882a593SmuzhiyunIMAGE_FSTYPES = "container"
42*4882a593SmuzhiyunPACKAGE_CLASSES = "package_ipk"
43*4882a593SmuzhiyunIMAGE_FEATURES = ""
44*4882a593SmuzhiyunIMAGE_BUILDINFO_FILE = ""
45*4882a593SmuzhiyunINIT_MANAGER = "sysvinit"
46*4882a593SmuzhiyunIMAGE_INSTALL:remove = "ssh-pregen-hostkeys"
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun""")
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun        bbvars = get_bb_vars(['bindir', 'sysconfdir', 'localstatedir',
51*4882a593Smuzhiyun                              'DEPLOY_DIR_IMAGE', 'IMAGE_LINK_NAME'],
52*4882a593Smuzhiyun                              target='container-test-image')
53*4882a593Smuzhiyun        expected_files = [
54*4882a593Smuzhiyun                    './',
55*4882a593Smuzhiyun                    '.{bindir}/theapp',
56*4882a593Smuzhiyun                    '.{sysconfdir}/default/',
57*4882a593Smuzhiyun                    '.{sysconfdir}/default/postinst',
58*4882a593Smuzhiyun                    '.{sysconfdir}/ld.so.cache',
59*4882a593Smuzhiyun                    '.{sysconfdir}/timestamp',
60*4882a593Smuzhiyun                    '.{sysconfdir}/version',
61*4882a593Smuzhiyun                    './run/',
62*4882a593Smuzhiyun                    '.{localstatedir}/cache/',
63*4882a593Smuzhiyun                    '.{localstatedir}/lib/'
64*4882a593Smuzhiyun                ]
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun        expected_files = [ x.format(bindir=bbvars['bindir'],
67*4882a593Smuzhiyun                                    sysconfdir=bbvars['sysconfdir'],
68*4882a593Smuzhiyun                                    localstatedir=bbvars['localstatedir'])
69*4882a593Smuzhiyun                                    for x in expected_files ]
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun        # Since tar lists all directories individually, make sure each element
72*4882a593Smuzhiyun        # from bindir, sysconfdir, etc is added
73*4882a593Smuzhiyun        expected_files += get_each_path_part(bbvars['bindir'])
74*4882a593Smuzhiyun        expected_files += get_each_path_part(bbvars['sysconfdir'])
75*4882a593Smuzhiyun        expected_files += get_each_path_part(bbvars['localstatedir'])
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun        expected_files = sorted(expected_files)
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun        # Build the image of course
80*4882a593Smuzhiyun        bitbake('container-test-image')
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun        image = os.path.join(bbvars['DEPLOY_DIR_IMAGE'],
83*4882a593Smuzhiyun                             bbvars['IMAGE_LINK_NAME'] + '.tar.bz2')
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun        # Ensure the files in the image are what we expect
86*4882a593Smuzhiyun        result = runCmd("tar tf {} | sort".format(image), shell=True)
87*4882a593Smuzhiyun        self.assertEqual(result.output.split('\n'), expected_files)
88