test_python.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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()
  57. class TestPythonPackageBase(TestPythonBase):
  58. """Common class to test a python package.
  59. Build an image containing the scripts listed in sample_scripts, start the
  60. emulator, login to it and for each sample script in the image run the python
  61. interpreter passing the name of the script and check the status code is 0.
  62. Each test case that inherits from this class must have:
  63. __test__ = True - to let nose2 know that it is a test case
  64. config - defconfig fragment with the packages to run the test
  65. It also can have:
  66. sample_scripts - list of scripts to add to the image and run on the target
  67. timeout - timeout to the script to run when the default from the
  68. test infra is not enough
  69. When custom commands need be issued on the target the method
  70. run_sample_scripts can be overridden.
  71. """
  72. __test__ = False
  73. config_sample_scripts = \
  74. """
  75. BR2_ROOTFS_POST_BUILD_SCRIPT="{}"
  76. BR2_ROOTFS_POST_SCRIPT_ARGS="{}"
  77. """.format(infra.filepath("tests/package/copy-sample-script-to-target.sh"),
  78. "{sample_scripts}")
  79. sample_scripts = None
  80. timeout = -1
  81. def __init__(self, names):
  82. """Add the scripts to the target in build time."""
  83. super(TestPythonPackageBase, self).__init__(names)
  84. if self.sample_scripts:
  85. scripts = [infra.filepath(s) for s in self.sample_scripts]
  86. self.config += self.config_sample_scripts.format(sample_scripts=" ".join(scripts))
  87. def check_sample_scripts_exist(self):
  88. """Check the scripts were really added to the image."""
  89. scripts = [os.path.basename(s) for s in self.sample_scripts]
  90. cmd = "md5sum " + " ".join(scripts)
  91. _, exit_code = self.emulator.run(cmd)
  92. self.assertEqual(exit_code, 0)
  93. def run_sample_scripts(self):
  94. """Run each script previously added to the image."""
  95. for script in self.sample_scripts:
  96. cmd = self.interpreter + " " + os.path.basename(script)
  97. _, exit_code = self.emulator.run(cmd, timeout=self.timeout)
  98. self.assertEqual(exit_code, 0)
  99. def test_run(self):
  100. self.login()
  101. self.check_sample_scripts_exist()
  102. self.run_sample_scripts()