From fe3bb30f4c82a1a4bb9f0bb141812d0498abf010 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 23 Sep 2021 20:37:00 +0000 Subject: [PATCH] test/host: add check for call with fds Create a new test that checks method calls that pass file descriptors in both directions. --- test/mod/test_host.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/mod/test_host.py b/test/mod/test_host.py index 72ab36f4..bf36b51b 100755 --- a/test/mod/test_host.py +++ b/test/mod/test_host.py @@ -4,7 +4,9 @@ # Runtime Tests for Host Services # +import os import sys +import tempfile from typing import Any import pytest @@ -16,12 +18,20 @@ from osbuild.util.jsoncomm import FdSet class ServiceTest(host.Service): def dispatch(self, method: str, args: Any, fds: FdSet): - ret, fds = None, None + ret = None if method == "exception": raise ValueError("Remote Exception") if method == "echo": ret = args + elif method == "echo-fd": + ret = args + with tempfile.TemporaryFile("w+") as f: + with os.fdopen(fds.steal(0)) as d: + f.write(d.read()) + f.seek(0) + fds = [os.dup(f.fileno())] + elif method == "identify": ret = self.id else: @@ -51,6 +61,35 @@ def test_basic(): client.stop() +def test_pass_fd(): + with host.ServiceManager() as mgr: + for i in range(3): + client = mgr.start(str(i), __file__) + + args = ["an", "argument"] + data = "osbuild\n" + + with tempfile.TemporaryFile("w+") as f: + f.write(data) + f.seek(0) + + res, fds = client.call_with_fds("echo-fd", args, fds=[f.fileno()]) + + assert args == res + with os.fdopen(fds.steal(0)) as d: + assert data == d.read() + + remote_id = client.call("identify") + assert remote_id == str(i) + + with pytest.raises(ValueError, match=f"{str(i)}"): + _ = mgr.start(str(i), __file__) + + for i in range(3): + client = mgr.services[str(i)] + client.stop() + + def test_exception(): with host.ServiceManager() as mgr: client = mgr.start("exception", __file__)