test_python.py 3.8 KB

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