Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | import os |
| 7 | import socketserver |
| 8 | import subprocess |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 9 | import time |
| 10 | import urllib |
| 11 | import pathlib |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 12 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 13 | from oeqa.core.decorator import OETestTag |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 14 | from oeqa.selftest.case import OESelftestTestCase |
| 15 | from oeqa.utils.commands import bitbake, get_bb_var, runqemu |
| 16 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 17 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 18 | class Debuginfod(OESelftestTestCase): |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 19 | |
| 20 | def wait_for_debuginfod(self, port): |
| 21 | """ |
| 22 | debuginfod takes time to scan the packages and requesting too early may |
| 23 | result in a test failure if the right packages haven't been scanned yet. |
| 24 | |
| 25 | Request the metrics endpoint periodically and wait for there to be no |
| 26 | busy scanning threads. |
| 27 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 28 | Returns if debuginfod is ready, raises an exception if not within the |
| 29 | timeout. |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 30 | """ |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 31 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 32 | # Wait two minutes |
| 33 | countdown = 24 |
| 34 | delay = 5 |
| 35 | latest = None |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 36 | |
| 37 | while countdown: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 38 | self.logger.info("waiting...") |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 39 | time.sleep(delay) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 40 | |
| 41 | self.logger.info("polling server") |
| 42 | if self.debuginfod.poll(): |
| 43 | self.logger.info("server dead") |
| 44 | self.debuginfod.communicate() |
| 45 | self.fail("debuginfod terminated unexpectedly") |
| 46 | self.logger.info("server alive") |
| 47 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 48 | try: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 49 | with urllib.request.urlopen("http://localhost:%d/metrics" % port, timeout=10) as f: |
| 50 | for line in f.read().decode("ascii").splitlines(): |
| 51 | key, value = line.rsplit(" ", 1) |
| 52 | if key == "thread_busy{role=\"scan\"}": |
| 53 | latest = int(value) |
| 54 | self.logger.info("Waiting for %d scan jobs to finish" % latest) |
| 55 | if latest == 0: |
| 56 | return |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 57 | except urllib.error.URLError as e: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 58 | # TODO: how to catch just timeouts? |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 59 | self.logger.error(e) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 60 | |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 61 | countdown -= 1 |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 62 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 63 | raise TimeoutError("Cannot connect debuginfod, still %d scan jobs running" % latest) |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 64 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 65 | def start_debuginfod(self): |
| 66 | # We assume that the caller has already bitbake'd elfutils-native:do_addto_recipe_sysroot |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 67 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 68 | # Save some useful paths for later |
| 69 | native_sysroot = pathlib.Path(get_bb_var("RECIPE_SYSROOT_NATIVE", "elfutils-native")) |
| 70 | native_bindir = native_sysroot / "usr" / "bin" |
| 71 | self.debuginfod = native_bindir / "debuginfod" |
| 72 | self.debuginfod_find = native_bindir / "debuginfod-find" |
| 73 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 74 | cmd = [ |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 75 | self.debuginfod, |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 76 | "--verbose", |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 77 | # In-memory database, this is a one-shot test |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 78 | "--database=:memory:", |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 79 | # Don't use all the host cores |
| 80 | "--concurrency=8", |
| 81 | "--connection-pool=8", |
| 82 | # Disable rescanning, this is a one-shot test |
| 83 | "--rescan-time=0", |
| 84 | "--groom-time=0", |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 85 | get_bb_var("DEPLOY_DIR"), |
| 86 | ] |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 87 | |
| 88 | format = get_bb_var("PACKAGE_CLASSES").split()[0] |
| 89 | if format == "package_deb": |
| 90 | cmd.append("--scan-deb-dir") |
| 91 | elif format == "package_ipk": |
| 92 | cmd.append("--scan-deb-dir") |
| 93 | elif format == "package_rpm": |
| 94 | cmd.append("--scan-rpm-dir") |
| 95 | else: |
| 96 | self.fail("Unknown package class %s" % format) |
| 97 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 98 | # Find a free port. Racey but the window is small. |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 99 | with socketserver.TCPServer(("localhost", 0), None) as s: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 100 | self.port = s.server_address[1] |
| 101 | cmd.append("--port=%d" % self.port) |
| 102 | |
| 103 | self.logger.info(f"Starting server {cmd}") |
| 104 | self.debuginfod = subprocess.Popen(cmd, env={}) |
| 105 | self.wait_for_debuginfod(self.port) |
| 106 | |
| 107 | |
| 108 | def test_debuginfod_native(self): |
| 109 | """ |
| 110 | Test debuginfod outside of qemu, by building a package and looking up a |
| 111 | binary's debuginfo using elfutils-native. |
| 112 | """ |
| 113 | |
| 114 | self.write_config(""" |
| 115 | TMPDIR = "${TOPDIR}/tmp-debuginfod" |
| 116 | DISTRO_FEATURES:append = " debuginfod" |
| 117 | """) |
| 118 | bitbake("elfutils-native:do_addto_recipe_sysroot xz xz:do_package") |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 119 | |
| 120 | try: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 121 | self.start_debuginfod() |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 122 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 123 | env = os.environ.copy() |
| 124 | env["DEBUGINFOD_URLS"] = "http://localhost:%d/" % self.port |
| 125 | |
| 126 | pkgs = pathlib.Path(get_bb_var("PKGDEST", "xz")) |
| 127 | cmd = (self.debuginfod_find, "debuginfo", pkgs / "xz" / "usr" / "bin" / "xz.xz") |
| 128 | self.logger.info(f"Starting client {cmd}") |
| 129 | output = subprocess.check_output(cmd, env=env, text=True) |
| 130 | # This should be more comprehensive |
| 131 | self.assertIn("/.cache/debuginfod_client/", output) |
| 132 | finally: |
| 133 | self.debuginfod.kill() |
| 134 | |
| 135 | @OETestTag("runqemu") |
| 136 | def test_debuginfod_qemu(self): |
| 137 | """ |
| 138 | Test debuginfod-find inside a qemu, talking to a debuginfod on the host. |
| 139 | """ |
| 140 | |
| 141 | self.write_config(""" |
| 142 | TMPDIR = "${TOPDIR}/tmp-debuginfod" |
| 143 | DISTRO_FEATURES:append = " debuginfod" |
| 144 | CORE_IMAGE_EXTRA_INSTALL += "elfutils xz" |
| 145 | """) |
| 146 | bitbake("core-image-minimal elfutils-native:do_addto_recipe_sysroot") |
| 147 | |
| 148 | try: |
| 149 | self.start_debuginfod() |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 150 | |
| 151 | with runqemu("core-image-minimal", runqemuparams="nographic") as qemu: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 152 | cmd = "DEBUGINFOD_URLS=http://%s:%d/ debuginfod-find debuginfo /usr/bin/xz" % (qemu.server_ip, self.port) |
Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame] | 153 | self.logger.info(f"Starting client {cmd}") |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 154 | status, output = qemu.run_serial(cmd) |
| 155 | # This should be more comprehensive |
| 156 | self.assertIn("/.cache/debuginfod_client/", output) |
| 157 | finally: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 158 | self.debuginfod.kill() |