1import os 2 3import infra.basetest 4 5 6class TestPythonBase(infra.basetest.BRTest): 7 config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ 8 """ 9 BR2_TARGET_ROOTFS_CPIO=y 10 # BR2_TARGET_ROOTFS_TAR is not set 11 """ 12 interpreter = "python" 13 14 def login(self): 15 cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") 16 self.emulator.boot(arch="armv5", 17 kernel="builtin", 18 options=["-initrd", cpio_file]) 19 self.emulator.login() 20 21 def version_test(self, version, timeout=-1): 22 cmd = self.interpreter + " --version 2>&1 | grep '^{}'".format(version) 23 self.assertRunOk(cmd, timeout) 24 25 def math_floor_test(self, timeout=-1): 26 cmd = self.interpreter + " -c 'import math; math.floor(12.3)'" 27 self.assertRunOk(cmd, timeout) 28 29 def libc_time_test(self, timeout=-1): 30 cmd = self.interpreter + " -c 'from __future__ import print_function;" 31 cmd += "import ctypes;" 32 cmd += "libc = ctypes.cdll.LoadLibrary(\"libc.so.1\");" 33 cmd += "print(libc.time(None))'" 34 self.assertRunOk(cmd, timeout) 35 36 def zlib_test(self, timeout=-1): 37 cmd = self.interpreter + " -c 'import zlib'" 38 _, exit_code = self.emulator.run(cmd, timeout) 39 self.assertEqual(exit_code, 1) 40 41 42class TestPython2(TestPythonBase): 43 config = TestPythonBase.config + \ 44 """ 45 BR2_PACKAGE_PYTHON=y 46 """ 47 48 def test_run(self): 49 self.login() 50 self.version_test("Python 2") 51 self.math_floor_test() 52 self.libc_time_test() 53 self.zlib_test() 54 55 56class TestPython3(TestPythonBase): 57 config = TestPythonBase.config + \ 58 """ 59 BR2_PACKAGE_PYTHON3=y 60 """ 61 62 def test_run(self): 63 self.login() 64 self.version_test("Python 3") 65 self.math_floor_test() 66 self.libc_time_test() 67 self.zlib_test() 68 69 70class TestPythonPackageBase(TestPythonBase): 71 """Common class to test a python package. 72 73 Build an image containing the scripts listed in sample_scripts, start the 74 emulator, login to it and for each sample script in the image run the python 75 interpreter passing the name of the script and check the status code is 0. 76 77 Each test case that inherits from this class must have: 78 __test__ = True - to let nose2 know that it is a test case 79 config - defconfig fragment with the packages to run the test 80 It also can have: 81 sample_scripts - list of scripts to add to the image and run on the target 82 timeout - timeout to the script to run when the default from the 83 test infra is not enough 84 When custom commands need be issued on the target the method 85 run_sample_scripts can be overridden. 86 """ 87 88 __test__ = False 89 config_sample_scripts = \ 90 """ 91 BR2_ROOTFS_POST_BUILD_SCRIPT="{}" 92 BR2_ROOTFS_POST_SCRIPT_ARGS="{}" 93 """.format(infra.filepath("tests/package/copy-sample-script-to-target.sh"), 94 "{sample_scripts}") 95 sample_scripts = None 96 timeout = -1 97 98 def __init__(self, names): 99 """Add the scripts to the target in build time.""" 100 super(TestPythonPackageBase, self).__init__(names) 101 if self.sample_scripts: 102 scripts = [infra.filepath(s) for s in self.sample_scripts] 103 self.config += self.config_sample_scripts.format(sample_scripts=" ".join(scripts)) 104 105 def check_sample_scripts_exist(self): 106 """Check the scripts were really added to the image.""" 107 scripts = [os.path.basename(s) for s in self.sample_scripts] 108 cmd = "md5sum " + " ".join(scripts) 109 _, exit_code = self.emulator.run(cmd) 110 self.assertEqual(exit_code, 0) 111 112 def run_sample_scripts(self): 113 """Run each script previously added to the image.""" 114 for script in self.sample_scripts: 115 cmd = self.interpreter + " " + os.path.basename(script) 116 self.assertRunOk(cmd, timeout=self.timeout) 117 118 def test_run(self): 119 self.login() 120 self.check_sample_scripts_exist() 121 self.run_sample_scripts() 122