1import os 2import time 3 4from tests.package.test_python import TestPythonPackageBase 5 6 7class TestPythonPy3Fastapi(TestPythonPackageBase): 8 """Test fastapi, uvicorn and pydantic2. 9 10 fastapi needs an asgi server to run. Since we select uvicorn as 11 asgi server here, uvicorn is tested as well. 12 13 pydantic is an major dependency of fastapi so it is implicitly 14 tested here as well. 15 """ 16 __test__ = True 17 config = \ 18 """ 19 BR2_arm=y 20 BR2_cortex_a9=y 21 BR2_ARM_ENABLE_NEON=y 22 BR2_ARM_ENABLE_VFP=y 23 BR2_TOOLCHAIN_EXTERNAL=y 24 BR2_PACKAGE_PYTHON3=y 25 BR2_PACKAGE_PYTHON_FASTAPI=y 26 BR2_PACKAGE_PYTHON_UVICORN=y 27 BR2_TARGET_ROOTFS_CPIO=y 28 # BR2_TARGET_ROOTFS_TAR is not set 29 """ 30 sample_scripts = ["tests/package/sample_python_fastapi.py"] 31 timeout = 60 32 33 def test_run(self): 34 self.login() 35 self.check_sample_scripts_exist() 36 cmd = "uvicorn sample_python_fastapi:app > /dev/null 2>&1 &" 37 38 _, exit_code = self.emulator.run(cmd, timeout=self.timeout) 39 40 # Give enough time for the uvicorn server to start up 41 for attempt in range(30): 42 time.sleep(1) 43 44 cmd = "wget -q -O - http://127.0.0.1:8000/" 45 output, exit_code = self.emulator.run(cmd, timeout=self.timeout) 46 if exit_code == 0: 47 self.assertEqual(output[0], '{"message":"Hello World"}') 48 break 49 else: 50 self.assertTrue(False, "Timeout while waiting for fastapi server") 51 52 def login(self): 53 cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") 54 self.emulator.boot(arch="armv7", 55 kernel="builtin", 56 options=["-initrd", cpio_file]) 57 self.emulator.login() 58