From 33479613d437a99dd51f9ac53095d168371f0cbd Mon Sep 17 00:00:00 2001 From: Marcus Hoffmann Date: Mon, 24 Nov 2025 15:05:16 +0100 Subject: [PATCH] support/testing: python-varlink: new runtime test Add new runtime test for python-varlink. As always when we need to start a separate server process inside a test case this gets slightly fiddly. We override the test_run() function to first start the varlink example server, then call the packages cli interface to do a varlink call against the server. The cli defaults to pretty printing the result, which makes it more annoying to compare to the expected result in the test case, so we un-prettyprint it with python's builtin json.tool module. Signed-off-by: Marcus Hoffmann Signed-off-by: Thomas Petazzoni --- DEVELOPERS | 1 + .../tests/package/test_python_varlink.py | 30 +++++++++++++++++++ .../org.example.test.varlink | 5 ++++ .../sample_python_varlink.py | 24 +++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 support/testing/tests/package/test_python_varlink.py create mode 100644 support/testing/tests/package/test_python_varlink/org.example.test.varlink create mode 100644 support/testing/tests/package/test_python_varlink/sample_python_varlink.py diff --git a/DEVELOPERS b/DEVELOPERS index 0451d8341c..5ffd0e5398 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -2295,6 +2295,7 @@ F: support/testing/tests/package/test_python_pydantic_settings.py F: support/testing/tests/package/test_python_ruamel_yaml.py F: support/testing/tests/package/test_python_sdbus_modemmanager.py F: support/testing/tests/package/test_python_tzlocal.py +F: support/testing/tests/package/test_python_varlink/ F: support/testing/tests/package/test_python_waitress.py F: support/testing/tests/package/test_python_whitenoise.py F: support/testing/tests/package/sample_python_apscheduler.py diff --git a/support/testing/tests/package/test_python_varlink.py b/support/testing/tests/package/test_python_varlink.py new file mode 100644 index 0000000000..f8595582f5 --- /dev/null +++ b/support/testing/tests/package/test_python_varlink.py @@ -0,0 +1,30 @@ +import os + +from tests.package.test_python import TestPythonPackageBase + + +class TestPythonPy3Varlink(TestPythonPackageBase): + __test__ = True + config = TestPythonPackageBase.config + \ + """ + BR2_PACKAGE_PYTHON3=y + BR2_PACKAGE_PYTHON_VARLINK=y + """ + sample_scripts = ["tests/package/test_python_varlink/sample_python_varlink.py", + "tests/package/test_python_varlink/org.example.test.varlink"] + + def test_run(self): + self.login() + self.check_sample_scripts_exist() + # we need to supress any output here as otherwise the commnd output parsing + # gets confused for the second command + cmd = "%s %s > /dev/null 2>&1 &" % (self.interpreter, os.path.basename(self.sample_scripts[0])) + + _, exit_code = self.emulator.run(cmd, timeout=self.timeout) + + varlink_cli_call = "-m varlink.cli call 'unix:@test/org.example.test.Ping' '{\"ping\": \"hello\"}'" + unprettyprint = "| python -m json.tool --compact" + cmd = "%s %s %s" % (self.interpreter, varlink_cli_call, unprettyprint) + output, exit_code = self.emulator.run(cmd, timeout=self.timeout) + self.assertEqual(exit_code, 0) + self.assertEqual(output[0], '{"pong":"hello"}') diff --git a/support/testing/tests/package/test_python_varlink/org.example.test.varlink b/support/testing/tests/package/test_python_varlink/org.example.test.varlink new file mode 100644 index 0000000000..c199824e6e --- /dev/null +++ b/support/testing/tests/package/test_python_varlink/org.example.test.varlink @@ -0,0 +1,5 @@ +# Example Varlink service +interface org.example.test + +# Returns the same string +method Ping(ping: string) -> (pong: string) diff --git a/support/testing/tests/package/test_python_varlink/sample_python_varlink.py b/support/testing/tests/package/test_python_varlink/sample_python_varlink.py new file mode 100644 index 0000000000..567d95da80 --- /dev/null +++ b/support/testing/tests/package/test_python_varlink/sample_python_varlink.py @@ -0,0 +1,24 @@ +import os + +import varlink + +service = varlink.Service(interface_dir=os.path.dirname(__file__)) + + +class ServiceRequestHandler(varlink.RequestHandler): + service = service + + +@service.interface("org.example.test") +class Example: + def Ping(self, ping): + return {"pong": ping} + + +def run_server(address): + with varlink.ThreadingServer(address, ServiceRequestHandler) as server: + print("Listening on", server.server_address) + server.serve_forever() + + +run_server("unix:@test")