]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add simple integration test for systemd-imdsd
authorLennart Poettering <lennart@amutable.com>
Fri, 6 Mar 2026 16:31:10 +0000 (17:31 +0100)
committerLennart Poettering <lennart@amutable.com>
Thu, 26 Mar 2026 09:54:15 +0000 (10:54 +0100)
mkosi/mkosi.sanitizers/mkosi.postinst
test/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py [new file with mode: 0755]
test/units/TEST-74-AUX-UTILS.imds.sh [new file with mode: 0755]

index 72356005e93371cdc50f1c85ab2a57e4a10c8aaf..17c7d7bad90a4be13d984d3b57896a691121cf6d 100755 (executable)
@@ -44,6 +44,7 @@ wrap=(
     /usr/lib/polkit-1/polkitd
     /usr/lib/systemd/tests/testdata/TEST-74-AUX-UTILS.units/proxy-echo.py
     /usr/libexec/polkit-1/polkitd
+    /usr/lib/systemd/tests/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py
     agetty
     btrfs
     capsh
diff --git a/test/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py b/test/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py
new file mode 100755 (executable)
index 0000000..e0a28ca
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/python3
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+import os, socket
+from http.server import BaseHTTPRequestHandler, HTTPServer
+
+def sd_notify(state: str) -> bool:
+    notify_socket = os.environ.get("NOTIFY_SOCKET")
+    if not notify_socket:
+        return False
+    if notify_socket.startswith("@"):
+        notify_socket = "\0" + notify_socket[1:]
+    try:
+        with socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) as sock:
+            sock.sendto(state.encode(), notify_socket)
+    except OSError:
+        return False
+
+    return True
+
+class Handler(BaseHTTPRequestHandler):
+    def do_GET(self):
+        if self.path == "/userdata":
+            body = b"{\"systemd.credentials\":[{\"name\":\"acredtest\",\"text\":\"avalue\"}]}"
+            self.send_response(200)
+            self.send_header("Content-Type", "text/plain")
+            self.send_header("Content-Length", len(body))
+            self.end_headers()
+            self.wfile.write(body)
+        elif self.path == "/hostname":
+            body = b"piff"
+            self.send_response(200)
+            self.send_header("Content-Type", "text/plain")
+            self.send_header("Content-Length", len(body))
+            self.end_headers()
+            self.wfile.write(body)
+        else:
+            self.send_error(404)
+
+    def log_message(self, fmt, *args):
+        print(f"{self.address_string()} - {fmt % args}")
+
+PORT=8088
+
+server = HTTPServer(("", PORT), Handler)
+print(f"Serving on http://localhost:{PORT}/")
+try:
+    sd_notify("READY=1")
+    server.serve_forever()
+except KeyboardInterrupt:
+    print("\nStopped.")
diff --git a/test/units/TEST-74-AUX-UTILS.imds.sh b/test/units/TEST-74-AUX-UTILS.imds.sh
new file mode 100755 (executable)
index 0000000..2ee0c63
--- /dev/null
@@ -0,0 +1,62 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: LGPL-2.1-or-later
+set -eux
+set -o pipefail
+
+# shellcheck source=test/units/util.sh
+. "$(dirname "$0")"/util.sh
+
+
+if ! test -x /usr/lib/systemd/systemd-imdsd ; then
+    echo "No imdsd installed, skipping test."
+    exit 0
+fi
+
+at_exit() {
+    set +e
+    systemctl stop fake-imds systemd-imdsd.socket ||:
+    ip link del dummy0 ||:
+    rm -f /run/credstore/firstboot.hostname /run/credstore/acredtest /run/systemd/system/systemd-imdsd@.service.d/50-env.conf
+    rmdir /run/systemd/system/systemd-imdsd@.service.d ||:
+}
+
+trap at_exit EXIT
+
+systemd-run -p Type=notify --unit=fake-imds /usr/lib/systemd/tests/integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units/fake-imds.py
+systemctl status fake-imds
+
+# Add a fake network interface so that IMDS gets going
+ip link add dummy0 type dummy
+ip link set dummy0 up
+ip addr add 192.168.47.11/24 dev dummy0
+
+USERDATA='{"systemd.credentials":[{"name":"acredtest","text":"avalue"}]}'
+
+# First try imdsd directly
+IMDSD="/usr/lib/systemd/systemd-imdsd --vendor=test --data-url=http://192.168.47.11:8088 --well-known-key=userdata:/userdata --well-known-key=hostname:/hostname"
+assert_eq "$($IMDSD --well-known=hostname)" "piff"
+assert_eq "$($IMDSD --well-known=userdata)" "$USERDATA"
+assert_eq "$($IMDSD /hostname)" "piff"
+assert_eq "$($IMDSD /userdata)" "$USERDATA"
+
+# Then, try it as Varlink service
+mkdir -p /run/systemd/system/systemd-imdsd@.service.d/
+cat >/run/systemd/system/systemd-imdsd@.service.d/50-env.conf <<EOF
+[Service]
+Environment=SYSTEMD_IMDS_VENDOR=test2
+Environment=SYSTEMD_IMDS_DATA_URL=http://192.168.47.11:8088
+Environment=SYSTEMD_IMDS_KEY_USERDATA=/userdata
+Environment=SYSTEMD_IMDS_KEY_HOSTNAME=/hostname
+EOF
+systemctl daemon-reload
+systemctl start systemd-imdsd.socket
+
+assert_eq "$(/usr/lib/systemd/systemd-imds --well-known=hostname)" "piff"
+assert_eq "$(/usr/lib/systemd/systemd-imds --well-known=userdata)" "$USERDATA"
+assert_eq "$(/usr/lib/systemd/systemd-imds -u)" "$USERDATA"
+
+/usr/lib/systemd/systemd-imds
+/usr/lib/systemd/systemd-imds --import
+
+assert_eq "$(cat /run/credstore/firstboot.hostname)" "piff"
+assert_eq "$(cat /run/credstore/acredtest)" "avalue"