test_python.py 4.1 KB

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