From 0c6ffa55138e8d70953029a544308560101b55a6 Mon Sep 17 00:00:00 2001 From: Julien Olivain Date: Tue, 16 Jan 2024 22:05:56 +0100 Subject: [PATCH] support/testing: add micropython runtime test Signed-off-by: Julien Olivain [me: rework the test mostly for eye-candyness] Signed-off-by: Yann E. MORIN --- DEVELOPERS | 2 + .../testing/tests/package/test_micropython.py | 68 +++++++++++++++++++ .../rootfs-overlay/root/mandel.py | 25 +++++++ 3 files changed, 95 insertions(+) create mode 100644 support/testing/tests/package/test_micropython.py create mode 100755 support/testing/tests/package/test_micropython/rootfs-overlay/root/mandel.py diff --git a/DEVELOPERS b/DEVELOPERS index a6e4b7cdb7..9d2b9a11ea 100644 --- a/DEVELOPERS +++ b/DEVELOPERS @@ -1791,6 +1791,8 @@ F: support/testing/tests/package/test_lzip.py F: support/testing/tests/package/test_lsof.py F: support/testing/tests/package/test_lz4.py F: support/testing/tests/package/test_lzop.py +F: support/testing/tests/package/test_micropython.py +F: support/testing/tests/package/test_micropython/ F: support/testing/tests/package/test_mtools.py F: support/testing/tests/package/test_mtr.py F: support/testing/tests/package/test_ncdu.py diff --git a/support/testing/tests/package/test_micropython.py b/support/testing/tests/package/test_micropython.py new file mode 100644 index 0000000000..0ecd4790bd --- /dev/null +++ b/support/testing/tests/package/test_micropython.py @@ -0,0 +1,68 @@ +import os + +import infra.basetest + + +class TestMicroPython(infra.basetest.BRTest): + config = infra.basetest.BASIC_TOOLCHAIN_CONFIG + \ + f""" + BR2_PACKAGE_MICROPYTHON=y + BR2_ROOTFS_OVERLAY="{infra.filepath("tests/package/test_micropython/rootfs-overlay")}" + BR2_TARGET_ROOTFS_CPIO=y + # BR2_TARGET_ROOTFS_TAR is not set + """ + + def run_upy_code(self, python_code, opts=""): + cmd = f'micropython {opts} -c "{python_code}"' + output, ret = self.emulator.run(cmd) + self.assertEqual(ret, 0, f"could not run '{cmd}', returnd {ret}: '{output}'") + return output + + def test_run(self): + cpio_file = os.path.join(self.builddir, "images", "rootfs.cpio") + self.emulator.boot(arch="armv5", + kernel="builtin", + options=["-initrd", cpio_file]) + self.emulator.login() + + # The micropython binary can execute. + self.assertRunOk("micropython -h") + + # Query interpreter version and implementation. + py_code = "import sys ; " + py_code += "print('Version:', sys.version) ; " + py_code += "print('Implementation:', sys.implementation)" + self.run_upy_code(py_code) + + # Check implementation is 'micropython'. + py_code = "import sys ; print(sys.implementation.name)" + output = self.run_upy_code(py_code) + self.assertEqual(output[0], "micropython") + + # Check micropython optimization are correctly reported. + py_code = "import micropython ; print(micropython.opt_level())" + for opt_level in range(4): + output = self.run_upy_code(py_code, f"-O{opt_level}") + self.assertEqual( + int(output[0]), + opt_level, + f"Running '{py_code}' at -O{opt_level} returned '{output}'" + ) + + # Check micropython can return a non-zero exit code. + expected_code = 123 + py_code = "import sys ; " + py_code += f"sys.exit({expected_code})" + cmd = f'micropython -c "{py_code}"' + _, exit_code = self.emulator.run(cmd) + self.assertEqual(exit_code, expected_code) + + # Check micropython computes correctly. + input_value = 1234 + expected_output = str(sum(range(input_value))) + py_code = f"print(sum(range(({input_value}))))" + output = self.run_upy_code(py_code) + self.assertEqual(output[0], expected_output) + + # Finally, Check a small script can execute. + self.assertRunOk("/root/mandel.py", timeout=10) diff --git a/support/testing/tests/package/test_micropython/rootfs-overlay/root/mandel.py b/support/testing/tests/package/test_micropython/rootfs-overlay/root/mandel.py new file mode 100755 index 0000000000..0552f6894c --- /dev/null +++ b/support/testing/tests/package/test_micropython/rootfs-overlay/root/mandel.py @@ -0,0 +1,25 @@ +#! /usr/bin/env micropython + +from micropython import mem_info + +POINTS = list(",.:-;!/>)|&IH%*Z") + + +def mandel(): + for y in range(-15, 16): + for x in range(1, 85): + i = 0 + r = 0 + for k in range(112): + j = (r*r) - (i*i) - 2 + (x/25) + i = 2 * r * i + (y/10) + if j*j + i*i >= 11: + break + r = j + print(POINTS[k & 0xF], end='') + print() + + +if __name__ == '__main__': + mandel() + mem_info()