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