diff --git a/DEVELOPERS b/DEVELOPERS index c18ae46054..7a943d1762 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -2064,6 +2064,10 @@ F: package/selinux-python/ F: utils/config F: utils/diffconfig +N: Marcus Hoffmann <bubu@bubu1.eu> +F: support/testing/tests/package/test_python_fastapi.py +F: support/testing/tests/package/sample_python_fastapi.py + N: Marek Belisko <marek.belisko@open-nandra.com> F: package/libatasmart/ F: package/polkit/ diff --git a/support/testing/tests/package/sample_python_fastapi.py b/support/testing/tests/package/sample_python_fastapi.py new file mode 100644 index 0000000000..ee60be1f96 --- /dev/null +++ b/support/testing/tests/package/sample_python_fastapi.py @@ -0,0 +1,8 @@ +from fastapi import FastAPI + +app = FastAPI() + + +@app.get("/") +async def root(): + return {"message": "Hello World"} diff --git a/support/testing/tests/package/test_python_fastapi.py b/support/testing/tests/package/test_python_fastapi.py new file mode 100644 index 0000000000..85cdd0b65a --- /dev/null +++ b/support/testing/tests/package/test_python_fastapi.py @@ -0,0 +1,57 @@ +import os +import time + +from tests.package.test_python import TestPythonPackageBase + + +class TestPythonPy3Fastapi(TestPythonPackageBase): + """Test fastapi, uvicorn and pydantic2. + + fastapi needs an asgi server to run. Since we select uvicorn as + asgi server here, uvicorn is tested as well. + + pydantic is an major dependency of fastapi so it is implicitly + tested here as well. + """ + __test__ = True + config = \ + """ + BR2_arm=y + BR2_cortex_a9=y + BR2_ARM_ENABLE_NEON=y + BR2_ARM_ENABLE_VFP=y + BR2_TOOLCHAIN_EXTERNAL=y + BR2_PACKAGE_PYTHON3=y + BR2_PACKAGE_PYTHON_FASTAPI=y + BR2_PACKAGE_PYTHON_UVICORN=y + BR2_TARGET_ROOTFS_CPIO=y + # BR2_TARGET_ROOTFS_TAR is not set + """ + sample_scripts = ["tests/package/sample_python_fastapi.py"] + timeout = 60 + + def test_run(self): + self.login() + self.check_sample_scripts_exist() + cmd = "uvicorn sample_python_fastapi:app > /dev/null 2>&1 &" + + _, exit_code = self.emulator.run(cmd, timeout=self.timeout) + + # Give enough time for the uvicorn server to start up + for attempt in range(30): + time.sleep(1) + + cmd = "wget -q -O - http://127.0.0.1:8000/" + output, exit_code = self.emulator.run(cmd, timeout=self.timeout) + if exit_code == 0: + self.assertEqual(output[0], '{"message":"Hello World"}') + break + else: + self.assertTrue(False, "Timeout while waiting for fastapi server") + + def login(self): + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") + self.emulator.boot(arch="armv7", + kernel="builtin", + options=["-initrd", cpio_file]) + self.emulator.login()