test_python.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. self.assertRunOk(cmd, timeout)
  19. def math_floor_test(self, timeout=-1):
  20. cmd = self.interpreter + " -c 'import math; math.floor(12.3)'"
  21. self.assertRunOk(cmd, timeout)
  22. def libc_time_test(self, timeout=-1):
  23. cmd = self.interpreter + " -c '"
  24. cmd += "import ctypes;"
  25. cmd += "libc = ctypes.cdll.LoadLibrary(\"libc.so.6\");"
  26. cmd += "print(libc.time(None))'"
  27. self.assertRunOk(cmd, timeout)
  28. def zlib_test(self, timeout=-1):
  29. cmd = self.interpreter + " -c 'import zlib'"
  30. _, exit_code = self.emulator.run(cmd, timeout)
  31. self.assertEqual(exit_code, 1)
  32. class TestPython3Pyc(TestPythonBase):
  33. config = TestPythonBase.config + \
  34. """
  35. BR2_PACKAGE_PYTHON3=y
  36. """
  37. def test_run(self):
  38. self.login()
  39. self.version_test("Python 3")
  40. self.math_floor_test()
  41. self.libc_time_test()
  42. self.zlib_test()
  43. class TestPython3Py(TestPythonBase):
  44. config = TestPythonBase.config + \
  45. """
  46. BR2_PACKAGE_PYTHON3=y
  47. BR2_PACKAGE_PYTHON3_PY_ONLY=y
  48. """
  49. def test_run(self):
  50. self.login()
  51. self.version_test("Python 3")
  52. self.math_floor_test()
  53. self.libc_time_test()
  54. self.zlib_test()
  55. class TestPython3PyPyc(TestPythonBase):
  56. config = TestPythonBase.config + \
  57. """
  58. BR2_PACKAGE_PYTHON3=y
  59. BR2_PACKAGE_PYTHON3_PY_PYC=y
  60. """
  61. def test_run(self):
  62. self.login()
  63. self.version_test("Python 3")
  64. self.math_floor_test()
  65. self.libc_time_test()
  66. self.zlib_test()
  67. class TestPythonPackageBase(TestPythonBase):
  68. """Common class to test a python package.
  69. Build an image containing the scripts listed in sample_scripts, start the
  70. emulator, login to it and for each sample script in the image run the python
  71. interpreter passing the name of the script and check the status code is 0.
  72. Each test case that inherits from this class must have:
  73. __test__ = True - to let nose2 know that it is a test case
  74. config - defconfig fragment with the packages to run the test
  75. It also can have:
  76. sample_scripts - list of scripts to add to the image and run on the target
  77. timeout - timeout to the script to run when the default from the
  78. test infra is not enough
  79. When custom commands need be issued on the target the method
  80. run_sample_scripts can be overridden.
  81. """
  82. __test__ = False
  83. config_sample_scripts = \
  84. """
  85. BR2_ROOTFS_POST_BUILD_SCRIPT="{}"
  86. BR2_ROOTFS_POST_SCRIPT_ARGS="{}"
  87. """.format(infra.filepath("tests/package/copy-sample-script-to-target.sh"),
  88. "{sample_scripts}")
  89. sample_scripts = None
  90. timeout = -1
  91. def __init__(self, names):
  92. """Add the scripts to the target in build time."""
  93. super(TestPythonPackageBase, self).__init__(names)
  94. if self.sample_scripts:
  95. scripts = [infra.filepath(s) for s in self.sample_scripts]
  96. self.config += self.config_sample_scripts.format(sample_scripts=" ".join(scripts))
  97. def check_sample_scripts_exist(self):
  98. """Check the scripts were really added to the image."""
  99. scripts = [os.path.basename(s) for s in self.sample_scripts]
  100. cmd = "md5sum " + " ".join(scripts)
  101. _, exit_code = self.emulator.run(cmd)
  102. self.assertEqual(exit_code, 0)
  103. def run_sample_scripts(self):
  104. """Run each script previously added to the image."""
  105. for script in self.sample_scripts:
  106. cmd = self.interpreter + " " + os.path.basename(script)
  107. self.assertRunOk(cmd, timeout=self.timeout)
  108. def test_run(self):
  109. self.login()
  110. self.check_sample_scripts_exist()
  111. self.run_sample_scripts()