1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6import os
7import time
8import contextlib
9from oeqa.core.decorator import OETestTag
10from oeqa.core.case import OEPTestResultTestCase
11from oeqa.selftest.case import OESelftestTestCase
12from oeqa.utils.commands import bitbake, get_bb_var, runqemu
13from oeqa.utils.nfs import unfs_server
14
15def parse_values(content):
16    for i in content:
17        for v in ["PASS", "FAIL", "XPASS", "XFAIL", "UNRESOLVED", "UNSUPPORTED", "UNTESTED", "ERROR", "WARNING"]:
18            if i.startswith(v + ": "):
19                yield i[len(v) + 2:].strip(), v
20                break
21
22class GlibcSelfTestBase(OESelftestTestCase, OEPTestResultTestCase):
23    def run_check(self, ssh = None):
24        # configure ssh target
25        features = []
26        if ssh is not None:
27            features.append('TOOLCHAIN_TEST_TARGET = "ssh"')
28            features.append('TOOLCHAIN_TEST_HOST = "{0}"'.format(ssh))
29            features.append('TOOLCHAIN_TEST_HOST_USER = "root"')
30            features.append('TOOLCHAIN_TEST_HOST_PORT = "22"')
31            # force single threaded test execution
32            features.append('EGLIBCPARALLELISM:task-check:pn-glibc-testsuite = "PARALLELMFLAGS="-j1""')
33        self.write_config("\n".join(features))
34
35        start_time = time.time()
36
37        bitbake("glibc-testsuite -c check")
38
39        end_time = time.time()
40
41        builddir = get_bb_var("B", "glibc-testsuite")
42
43        ptestsuite = "glibc-user" if ssh is None else "glibc"
44        self.ptest_section(ptestsuite, duration = int(end_time - start_time))
45        with open(os.path.join(builddir, "tests.sum"), "r",  errors='replace') as f:
46            for test, result in parse_values(f):
47                self.ptest_result(ptestsuite, test, result)
48
49    def run_check_emulated(self):
50        with contextlib.ExitStack() as s:
51            # use the base work dir, as the nfs mount, since the recipe directory may not exist
52            tmpdir = get_bb_var("BASE_WORKDIR")
53            nfsport, mountport = s.enter_context(unfs_server(tmpdir, udp = False))
54
55            # build core-image-minimal with required packages
56            default_installed_packages = [
57                "glibc-charmaps",
58                "libgcc",
59                "libstdc++",
60                "libatomic",
61                "libgomp",
62                # "python3",
63                # "python3-pexpect",
64                "nfs-utils",
65                ]
66            features = []
67            features.append('IMAGE_FEATURES += "ssh-server-openssh"')
68            features.append('CORE_IMAGE_EXTRA_INSTALL += "{0}"'.format(" ".join(default_installed_packages)))
69            self.write_config("\n".join(features))
70            bitbake("core-image-minimal")
71
72            # start runqemu
73            qemu = s.enter_context(runqemu("core-image-minimal", runqemuparams = "nographic", qemuparams = "-m 1024"))
74
75            # validate that SSH is working
76            status, _ = qemu.run("uname")
77            self.assertEqual(status, 0)
78
79            # setup nfs mount
80            if qemu.run("mkdir -p \"{0}\"".format(tmpdir))[0] != 0:
81                raise Exception("Failed to setup NFS mount directory on target")
82            mountcmd = "mount -o noac,nfsvers=3,port={0},mountport={1} \"{2}:{3}\" \"{3}\"".format(nfsport, mountport, qemu.server_ip, tmpdir)
83            status, output = qemu.run(mountcmd)
84            if status != 0:
85                raise Exception("Failed to setup NFS mount on target ({})".format(repr(output)))
86
87            self.run_check(ssh = qemu.ip)
88
89@OETestTag("toolchain-user")
90class GlibcSelfTest(GlibcSelfTestBase):
91    def test_glibc(self):
92        self.run_check()
93
94@OETestTag("toolchain-system")
95@OETestTag("runqemu")
96class GlibcSelfTestSystemEmulated(GlibcSelfTestBase):
97    def test_glibc(self):
98        self.run_check_emulated()
99
100