xref: /openbmc/openbmc/poky/meta/lib/oeqa/selftest/cases/package.py (revision 8460358c3d24c71d9d38fd126c745854a6301564)
1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7from oeqa.selftest.case import OESelftestTestCase
8from oeqa.utils.commands import bitbake, get_bb_vars, get_bb_var, runqemu
9import subprocess, os
10import oe.path
11import re
12
13class VersionOrdering(OESelftestTestCase):
14    # version1, version2, sort order
15    tests = (
16        ("1.0", "1.0", 0),
17        ("1.0", "2.0", -1),
18        ("2.0", "1.0", 1),
19        ("2.0-rc", "2.0", 1),
20        ("2.0~rc", "2.0", -1),
21        ("1.2rc2", "1.2.0", -1)
22        )
23
24    @classmethod
25    def setUpClass(cls):
26        super().setUpClass()
27
28        # Build the tools we need and populate a sysroot
29        bitbake("dpkg-native opkg-native rpm-native python3-native")
30        bitbake("build-sysroots -c build_native_sysroot")
31
32        # Get the paths so we can point into the sysroot correctly
33        vars = get_bb_vars(["STAGING_DIR", "BUILD_ARCH", "bindir_native", "libdir_native"])
34        cls.staging = oe.path.join(vars["STAGING_DIR"], vars["BUILD_ARCH"])
35        cls.bindir = oe.path.join(cls.staging, vars["bindir_native"])
36        cls.libdir = oe.path.join(cls.staging, vars["libdir_native"])
37
38    def setUpLocal(self):
39        # Just for convenience
40        self.staging = type(self).staging
41        self.bindir = type(self).bindir
42        self.libdir = type(self).libdir
43
44    def test_dpkg(self):
45        for ver1, ver2, sort in self.tests:
46            op = { -1: "<<", 0: "=", 1: ">>" }[sort]
47            status = subprocess.call((oe.path.join(self.bindir, "dpkg"), "--compare-versions", ver1, op, ver2))
48            self.assertEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
49
50            # Now do it again but with incorrect operations
51            op = { -1: ">>", 0: ">>", 1: "<<" }[sort]
52            status = subprocess.call((oe.path.join(self.bindir, "dpkg"), "--compare-versions", ver1, op, ver2))
53            self.assertNotEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
54
55            # Now do it again but with incorrect operations
56            op = { -1: "=", 0: "<<", 1: "=" }[sort]
57            status = subprocess.call((oe.path.join(self.bindir, "dpkg"), "--compare-versions", ver1, op, ver2))
58            self.assertNotEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
59
60    def test_opkg(self):
61        for ver1, ver2, sort in self.tests:
62            op = { -1: "<<", 0: "=", 1: ">>" }[sort]
63            status = subprocess.call((oe.path.join(self.bindir, "opkg"), "compare-versions", ver1, op, ver2))
64            self.assertEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
65
66            # Now do it again but with incorrect operations
67            op = { -1: ">>", 0: ">>", 1: "<<" }[sort]
68            status = subprocess.call((oe.path.join(self.bindir, "opkg"), "compare-versions", ver1, op, ver2))
69            self.assertNotEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
70
71            # Now do it again but with incorrect operations
72            op = { -1: "=", 0: "<<", 1: "=" }[sort]
73            status = subprocess.call((oe.path.join(self.bindir, "opkg"), "compare-versions", ver1, op, ver2))
74            self.assertNotEqual(status, 0, "%s %s %s failed" % (ver1, op, ver2))
75
76    def test_rpm(self):
77        # Need to tell the Python bindings where to find its configuration
78        env = os.environ.copy()
79        env["RPM_CONFIGDIR"] = oe.path.join(self.libdir, "rpm")
80
81        for ver1, ver2, sort in self.tests:
82            # The only way to test rpm is via the Python module, so we need to
83            # execute python3-native.  labelCompare returns -1/0/1 (like strcmp)
84            # so add 100 and use that as the exit code.
85            command = (oe.path.join(self.bindir, "python3-native", "python3"), "-c",
86                       "import sys, rpm; v1=(None, \"%s\", None); v2=(None, \"%s\", None); sys.exit(rpm.labelCompare(v1, v2) + 100)" % (ver1, ver2))
87            status = subprocess.call(command, env=env)
88            self.assertIn(status, (99, 100, 101))
89            self.assertEqual(status - 100, sort, "%s %s (%d) failed" % (ver1, ver2, sort))
90
91class PackageTests(OESelftestTestCase):
92    # Verify that a recipe cannot rename a package into an existing one
93    def test_package_name_conflict(self):
94        res = bitbake("packagenameconflict", ignore_status=True)
95        self.assertNotEqual(res.status, 0)
96        err = "package name already exists"
97        self.assertTrue(err in res.output)
98
99    # Verify that a recipe which sets up hardlink files has those preserved into split packages
100    # Also test file sparseness is preserved
101    def test_preserve_sparse_hardlinks(self):
102        bitbake("selftest-hardlink -c package")
103
104        dest = get_bb_var('PKGDEST', 'selftest-hardlink')
105        bindir = get_bb_var('bindir', 'selftest-hardlink')
106        libdir = get_bb_var('libdir', 'selftest-hardlink')
107        libexecdir = get_bb_var('libexecdir', 'selftest-hardlink')
108
109        def checkfiles():
110            # Recipe creates 4 hardlinked files, there is a copy in package/ and a copy in packages-split/
111            # so expect 8 in total.
112            self.assertEqual(os.stat(dest + "/selftest-hardlink" + bindir + "/hello1").st_nlink, 8)
113            self.assertEqual(os.stat(dest + "/selftest-hardlink" + libexecdir + "/hello3").st_nlink, 8)
114
115            # Check dbg version
116            # 2 items, a copy in both package/packages-split so 4
117            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + bindir + "/.debug/hello1").st_nlink, 4)
118            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + libexecdir + "/.debug/hello1").st_nlink, 4)
119
120            # Even though the libexecdir name is 'hello3' or 'hello4', that isn't the debug target name
121            self.assertEqual(os.path.exists(dest + "/selftest-hardlink-dbg" + libexecdir + "/.debug/hello3"), False)
122            self.assertEqual(os.path.exists(dest + "/selftest-hardlink-dbg" + libexecdir + "/.debug/hello4"), False)
123
124            # Check the staticdev libraries
125            # 101 items, a copy in both package/packages-split so 202
126            self.assertEqual(os.stat(dest + "/selftest-hardlink-staticdev" + libdir + "/libhello.a").st_nlink, 202)
127            self.assertEqual(os.stat(dest + "/selftest-hardlink-staticdev" + libdir + "/libhello-25.a").st_nlink, 202)
128            self.assertEqual(os.stat(dest + "/selftest-hardlink-staticdev" + libdir + "/libhello-50.a").st_nlink, 202)
129            self.assertEqual(os.stat(dest + "/selftest-hardlink-staticdev" + libdir + "/libhello-75.a").st_nlink, 202)
130
131            # Check static dbg
132            # 101 items, a copy in both package/packages-split so 202
133            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + libdir + "/.debug-static/libhello.a").st_nlink, 202)
134            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + libdir + "/.debug-static/libhello-25.a").st_nlink, 202)
135            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + libdir + "/.debug-static/libhello-50.a").st_nlink, 202)
136            self.assertEqual(os.stat(dest + "/selftest-hardlink-dbg" + libdir + "/.debug-static/libhello-75.a").st_nlink, 202)
137
138            # Test a sparse file remains sparse
139            sparsestat = os.stat(dest + "/selftest-hardlink" + bindir + "/sparsetest")
140            self.assertEqual(sparsestat.st_blocks, 0)
141            self.assertEqual(sparsestat.st_size, 1048576)
142
143        checkfiles()
144
145        # Clean and reinstall so its now definitely from sstate, then retest.
146        bitbake("selftest-hardlink -c clean")
147        bitbake("selftest-hardlink -c package")
148
149        checkfiles()
150
151    # Verify gdb to read symbols from separated debug hardlink file correctly
152    def test_gdb_hardlink_debug(self):
153        features = 'IMAGE_INSTALL:append = " selftest-hardlink"\n'
154        features += 'IMAGE_INSTALL:append = " selftest-hardlink-dbg"\n'
155        features += 'IMAGE_INSTALL:append = " selftest-hardlink-gdb"\n'
156        self.write_config(features)
157        bitbake("core-image-minimal")
158
159        def gdbtest(qemu, binary):
160            """
161            Check that gdb ``binary`` to read symbols from separated debug file
162            """
163            self.logger.info("gdbtest %s" % binary)
164            status, output = qemu.run_serial('/usr/bin/gdb.sh %s' % binary, timeout=60)
165            for l in output.split('\n'):
166                # Check debugging symbols exists
167                if '(no debugging symbols found)' in l:
168                    self.logger.error("No debugging symbols found. GDB result:\n%s" % output)
169                    return False
170
171                # Check debugging symbols works correctly. Don't look for a
172                # source file as optimisation can put the breakpoint inside
173                # stdio.h.
174                elif "Breakpoint 1 at" in l:
175                    return True
176
177            self.logger.error("GDB result:\n%d: %s", status, output)
178            return False
179
180        with runqemu('core-image-minimal') as qemu:
181            for binary in ['/usr/bin/hello1',
182                           '/usr/bin/hello2',
183                           '/usr/libexec/hello3',
184                           '/usr/libexec/hello4']:
185                if not gdbtest(qemu, binary):
186                    self.fail('GDB %s failed' % binary)
187
188    def test_preserve_ownership(self):
189        features = 'IMAGE_INSTALL:append = " selftest-chown"\n'
190        self.write_config(features)
191        bitbake("core-image-minimal")
192
193        def check_ownership(qemu, expected_gid, expected_uid, path):
194            self.logger.info("Check ownership of %s", path)
195            status, output = qemu.run_serial('stat -c "%U %G" ' + path)
196            self.assertEqual(status, 1, "stat failed: " + output)
197            try:
198                uid, gid = output.split()
199                self.assertEqual(uid, expected_uid)
200                self.assertEqual(gid, expected_gid)
201            except ValueError:
202                self.fail("Cannot parse output: " + output)
203
204        sysconfdir = get_bb_var('sysconfdir', 'selftest-chown')
205        with runqemu('core-image-minimal') as qemu:
206            for path in [ sysconfdir + "/selftest-chown/file",
207                          sysconfdir + "/selftest-chown/dir",
208                          sysconfdir + "/selftest-chown/symlink",
209                          sysconfdir + "/selftest-chown/fifotest/fifo"]:
210                check_ownership(qemu, "test", "test", path)
211