test_python.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os
  2. import infra.basetest
  3. class TestPythonBase(infra.basetest.BRTest):
  4. config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \
  5. """
  6. BR2_TARGET_ROOTFS_CPIO=y
  7. # BR2_TARGET_ROOTFS_TAR is not set
  8. """
  9. interpreter = "python"
  10. def login(self):
  11. cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio")
  12. self.emulator.boot(arch="armv5",
  13. kernel="builtin",
  14. options=["-initrd", cpio_file])
  15. self.emulator.login()
  16. def version_test(self, version, timeout=-1):
  17. cmd = self.interpreter + " --version 2>&1 | grep '^{}'".format(version)
  18. _, exit_code = self.emulator.run(cmd, timeout)
  19. self.assertEqual(exit_code, 0)
  20. def math_floor_test(self, timeout=-1):
  21. cmd = self.interpreter + " -c 'import math; math.floor(12.3)'"
  22. _, exit_code = self.emulator.run(cmd, timeout)
  23. self.assertEqual(exit_code, 0)
  24. def libc_time_test(self, timeout=-1):
  25. cmd = self.interpreter + " -c 'from __future__ import print_function;"
  26. cmd += "import ctypes;"
  27. cmd += "libc = ctypes.cdll.LoadLibrary(\"libc.so.1\");"
  28. cmd += "print(libc.time(None))'"
  29. _, exit_code = self.emulator.run(cmd, timeout)
  30. self.assertEqual(exit_code, 0)
  31. def zlib_test(self, timeout=-1):
  32. cmd = self.interpreter + " -c 'import zlib'"
  33. _, exit_code = self.emulator.run(cmd, timeout)
  34. self.assertEqual(exit_code, 1)
  35. class TestPython2(TestPythonBase):
  36. config = TestPythonBase.config + \
  37. """
  38. BR2_PACKAGE_PYTHON=y
  39. """
  40. def test_run(self):
  41. self.login()
  42. self.version_test("Python 2")
  43. self.math_floor_test()
  44. self.libc_time_test()
  45. self.zlib_test()
  46. class TestPython3(TestPythonBase):
  47. config = TestPythonBase.config + \
  48. """
  49. BR2_PACKAGE_PYTHON3=y
  50. """
  51. def test_run(self):
  52. self.login()
  53. self.version_test("Python 3")
  54. self.math_floor_test()
  55. self.libc_time_test()
  56. self.zlib_test()