diff options
Diffstat (limited to 'test/py/tests/test_ut.py')
-rw-r--r-- | test/py/tests/test_ut.py | 235 |
1 files changed, 123 insertions, 112 deletions
diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py index d2d8ce10755..b8adb597e11 100644 --- a/test/py/tests/test_ut.py +++ b/test/py/tests/test_ut.py @@ -13,7 +13,7 @@ import os import os.path import pytest -import u_boot_utils +import utils # pylint: disable=E0611 from tests import fs_helper from test_android import test_abootimg @@ -27,12 +27,12 @@ def mkdir_cond(dirname): if not os.path.exists(dirname): os.mkdir(dirname) -def setup_image(cons, devnum, part_type, img_size=20, second_part=False, +def setup_image(ubman, devnum, part_type, img_size=20, second_part=False, basename='mmc'): """Create a disk image with a single partition Args: - cons (ConsoleBase): Console to use + ubman (ConsoleBase): Console to use devnum (int): Device number to use, e.g. 1 part_type (int): Partition type, e.g. 0xc for FAT32 img_size (int): Image size in MiB @@ -44,26 +44,37 @@ def setup_image(cons, devnum, part_type, img_size=20, second_part=False, str: Filename of MMC image str: Directory name of scratch directory """ - fname = os.path.join(cons.config.source_dir, f'{basename}{devnum}.img') - mnt = os.path.join(cons.config.persistent_data_dir, 'scratch') + fname = os.path.join(ubman.config.source_dir, f'{basename}{devnum}.img') + mnt = os.path.join(ubman.config.persistent_data_dir, 'scratch') mkdir_cond(mnt) spec = f'type={part_type:x}, size={img_size - 2}M, start=1M, bootable' if second_part: spec += '\ntype=c' - u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') - u_boot_utils.run_and_log(cons, f'sfdisk {fname}', + utils.run_and_log(ubman, f'qemu-img create {fname} 20M') + utils.run_and_log(ubman, f'sfdisk {fname}', stdin=spec.encode('utf-8')) return fname, mnt -def setup_bootmenu_image(cons): +def copy_partition(ubman, fsfile, outname): + """Copy a partition into a disk iamge + + Args: + ubman (ConsoleBase): U-Boot fixture + fsfile (str): Name of partition file + outname (str): Name of full-disk file to update + """ + utils.run_and_log(ubman, + f'dd if={fsfile} of={outname} bs=1M seek=1 conv=notrunc') + +def setup_bootmenu_image(ubman): """Create a 20MB disk image with a single ext4 partition This is modelled on Armbian 22.08 Jammy """ mmc_dev = 4 - fname, mnt = setup_image(cons, mmc_dev, 0x83) + fname, mnt = setup_image(ubman, mmc_dev, 0x83) script = '''# DO NOT EDIT THIS FILE # @@ -146,16 +157,16 @@ booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r} with open(cmd_fname, 'w', encoding='ascii') as outf: print(script, file=outf) - infname = os.path.join(cons.config.source_dir, + infname = os.path.join(ubman.config.source_dir, 'test/py/tests/bootstd/armbian.bmp.xz') bmp_file = os.path.join(bootdir, 'boot.bmp') - u_boot_utils.run_and_log( - cons, + utils.run_and_log( + ubman, ['sh', '-c', f'xz -dc {infname} >{bmp_file}']) - mkimage = cons.config.build_dir + '/tools/mkimage' - u_boot_utils.run_and_log( - cons, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}') + mkimage = ubman.config.build_dir + '/tools/mkimage' + utils.run_and_log( + ubman, f'{mkimage} -C none -A arm -T script -d {cmd_fname} {scr_fname}') kernel = 'vmlinuz-5.15.63-rockchip64' target = os.path.join(bootdir, kernel) @@ -165,21 +176,21 @@ booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r} symlink = os.path.join(bootdir, 'Image') if os.path.exists(symlink): os.remove(symlink) - u_boot_utils.run_and_log( - cons, f'echo here {kernel} {symlink}') + utils.run_and_log( + ubman, f'echo here {kernel} {symlink}') os.symlink(kernel, symlink) fsfile = 'ext18M.img' - u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') - u_boot_utils.run_and_log(cons, f'mkfs.ext4 {fsfile} -d {mnt}') - u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') - u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') - u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') + utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}') + utils.run_and_log(ubman, f'mkfs.ext4 {fsfile} -d {mnt}') + copy_partition(ubman, fsfile, fname) + utils.run_and_log(ubman, f'rm -rf {mnt}') + utils.run_and_log(ubman, f'rm -f {fsfile}') -def setup_bootflow_image(cons): +def setup_bootflow_image(ubman): """Create a 20MB disk image with a single FAT partition""" mmc_dev = 1 - fname, mnt = setup_image(cons, mmc_dev, 0xc, second_part=True) + fname, mnt = setup_image(ubman, mmc_dev, 0xc, second_part=True) vmlinux = 'vmlinuz-5.3.7-301.fc31.armv7hl' initrd = 'initramfs-5.3.7-301.fc31.armv7hl.img' @@ -204,12 +215,12 @@ label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) with open(conf, 'w', encoding='ascii') as fd: print(script, file=fd) - inf = os.path.join(cons.config.persistent_data_dir, 'inf') + inf = os.path.join(ubman.config.persistent_data_dir, 'inf') with open(inf, 'wb') as fd: fd.write(gzip.compress(b'vmlinux')) - mkimage = cons.config.build_dir + '/tools/mkimage' - u_boot_utils.run_and_log( - cons, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}') + mkimage = ubman.config.build_dir + '/tools/mkimage' + utils.run_and_log( + ubman, f'{mkimage} -f auto -d {inf} {os.path.join(mnt, vmlinux)}') with open(os.path.join(mnt, initrd), 'w', encoding='ascii') as fd: print('initrd', file=fd) @@ -217,28 +228,28 @@ label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) mkdir_cond(os.path.join(mnt, dtbdir)) dtb_file = os.path.join(mnt, f'{dtbdir}/sandbox.dtb') - u_boot_utils.run_and_log( - cons, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};') + utils.run_and_log( + ubman, f'dtc -o {dtb_file}', stdin=b'/dts-v1/; / {};') fsfile = 'vfat18M.img' - u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') - u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}') - u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/']) - u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') - u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') - u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') - -def setup_cros_image(cons): + utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}') + utils.run_and_log(ubman, f'mkfs.vfat {fsfile}') + utils.run_and_log(ubman, ['sh', '-c', f'mcopy -i {fsfile} {mnt}/* ::/']) + copy_partition(ubman, fsfile, fname) + utils.run_and_log(ubman, f'rm -rf {mnt}') + utils.run_and_log(ubman, f'rm -f {fsfile}') + +def setup_cros_image(ubman): """Create a 20MB disk image with ChromiumOS partitions""" Partition = collections.namedtuple('part', 'start,size,name') parts = {} disk_data = None - def pack_kernel(cons, arch, kern, dummy): + def pack_kernel(ubman, arch, kern, dummy): """Pack a kernel containing some fake data Args: - cons (ConsoleBase): Console to use + ubman (ConsoleBase): Console to use arch (str): Architecture to use ('x86' or 'arm') kern (str): Filename containing kernel dummy (str): Dummy filename to use for config and bootloader @@ -246,10 +257,10 @@ def setup_cros_image(cons): Return: bytes: Packed-kernel data """ - kern_part = os.path.join(cons.config.result_dir, + kern_part = os.path.join(ubman.config.result_dir, f'kern-part-{arch}.bin') - u_boot_utils.run_and_log( - cons, + utils.run_and_log( + ubman, f'futility vbutil_kernel --pack {kern_part} ' '--keyblock doc/chromium/files/devkeys/kernel.keyblock ' '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk ' @@ -275,9 +286,9 @@ def setup_cros_image(cons): disk_data = disk_data[:start] + data + disk_data[start + len(data):] mmc_dev = 5 - fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') - u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') - u_boot_utils.run_and_log(cons, f'cgpt create {fname}') + fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img') + utils.run_and_log(ubman, f'qemu-img create {fname} 20M') + utils.run_and_log(ubman, f'cgpt create {fname}') uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7' uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309' @@ -316,13 +327,13 @@ def setup_cros_image(cons): size = int(size_str[:-1]) * sect_1mb else: size = int(size_str) - u_boot_utils.run_and_log( - cons, + utils.run_and_log( + ubman, f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}") ptr += size - u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') - out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') + utils.run_and_log(ubman, f'cgpt boot -p {fname}') + out = utils.run_and_log(ubman, f'cgpt show -q {fname}') # We expect something like this: # 8239 2048 1 Basic data @@ -344,14 +355,14 @@ def setup_cros_image(cons): parts[int(num)] = Partition(int(start), int(size), name) # Set up the kernel command-line - dummy = os.path.join(cons.config.result_dir, 'dummy.txt') + dummy = os.path.join(ubman.config.result_dir, 'dummy.txt') with open(dummy, 'wb') as outf: outf.write(b'BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=/dev/nvme0n1p1 ro quiet splash vt.handoff=7') # For now we just use dummy kernels. This limits testing to just detecting # a signed kernel. We could add support for the x86 data structures so that # testing could cover getting the cmdline, setup.bin and other pieces. - kern = os.path.join(cons.config.result_dir, 'kern.bin') + kern = os.path.join(ubman.config.result_dir, 'kern.bin') with open(kern, 'wb') as outf: outf.write(b'kernel\n') @@ -359,15 +370,15 @@ def setup_cros_image(cons): disk_data = inf.read() # put x86 kernel in partition 2 and arm one in partition 4 - set_part_data(2, pack_kernel(cons, 'x86', kern, dummy)) - set_part_data(4, pack_kernel(cons, 'arm', kern, dummy)) + set_part_data(2, pack_kernel(ubman, 'x86', kern, dummy)) + set_part_data(4, pack_kernel(ubman, 'arm', kern, dummy)) with open(fname, 'wb') as outf: outf.write(disk_data) return fname -def setup_android_image(cons): +def setup_android_image(ubman): """Create a 20MB disk image with Android partitions""" Partition = collections.namedtuple('part', 'start,size,name') parts = {} @@ -388,9 +399,9 @@ def setup_android_image(cons): disk_data = disk_data[:start] + data + disk_data[start + len(data):] mmc_dev = 7 - fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') - u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') - u_boot_utils.run_and_log(cons, f'cgpt create {fname}') + fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img') + utils.run_and_log(ubman, f'qemu-img create {fname} 20M') + utils.run_and_log(ubman, f'cgpt create {fname}') ptr = 40 @@ -412,13 +423,13 @@ def setup_android_image(cons): size = int(size_str[:-1]) * sect_1mb else: size = int(size_str) - u_boot_utils.run_and_log( - cons, + utils.run_and_log( + ubman, f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}") ptr += size - u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') - out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') + utils.run_and_log(ubman, f'cgpt boot -p {fname}') + out = utils.run_and_log(ubman, f'cgpt show -q {fname}') # Create a dict (indexed by partition number) containing the above info for line in out.splitlines(): @@ -428,13 +439,13 @@ def setup_android_image(cons): with open(fname, 'rb') as inf: disk_data = inf.read() - test_abootimg.AbootimgTestDiskImage(cons, 'bootv4.img', test_abootimg.boot_img_hex) - boot_img = os.path.join(cons.config.result_dir, 'bootv4.img') + test_abootimg.AbootimgTestDiskImage(ubman, 'bootv4.img', test_abootimg.boot_img_hex) + boot_img = os.path.join(ubman.config.result_dir, 'bootv4.img') with open(boot_img, 'rb') as inf: set_part_data(2, inf.read()) - test_abootimg.AbootimgTestDiskImage(cons, 'vendor_boot.img', test_abootimg.vboot_img_hex) - vendor_boot_img = os.path.join(cons.config.result_dir, 'vendor_boot.img') + test_abootimg.AbootimgTestDiskImage(ubman, 'vendor_boot.img', test_abootimg.vboot_img_hex) + vendor_boot_img = os.path.join(ubman.config.result_dir, 'vendor_boot.img') with open(vendor_boot_img, 'rb') as inf: set_part_data(4, inf.read()) @@ -444,9 +455,9 @@ def setup_android_image(cons): print(f'wrote to {fname}') mmc_dev = 8 - fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') - u_boot_utils.run_and_log(cons, f'qemu-img create {fname} 20M') - u_boot_utils.run_and_log(cons, f'cgpt create {fname}') + fname = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img') + utils.run_and_log(ubman, f'qemu-img create {fname} 20M') + utils.run_and_log(ubman, f'cgpt create {fname}') ptr = 40 @@ -466,13 +477,13 @@ def setup_android_image(cons): size = int(size_str[:-1]) * sect_1mb else: size = int(size_str) - u_boot_utils.run_and_log( - cons, + utils.run_and_log( + ubman, f"cgpt add -i {part['num']} -b {ptr} -s {size} -l {part['label']} -t basicdata {fname}") ptr += size - u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') - out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') + utils.run_and_log(ubman, f'cgpt boot -p {fname}') + out = utils.run_and_log(ubman, f'cgpt show -q {fname}') # Create a dict (indexed by partition number) containing the above info for line in out.splitlines(): @@ -482,8 +493,8 @@ def setup_android_image(cons): with open(fname, 'rb') as inf: disk_data = inf.read() - test_abootimg.AbootimgTestDiskImage(cons, 'boot.img', test_abootimg.img_hex) - boot_img = os.path.join(cons.config.result_dir, 'boot.img') + test_abootimg.AbootimgTestDiskImage(ubman, 'boot.img', test_abootimg.img_hex) + boot_img = os.path.join(ubman.config.result_dir, 'boot.img') with open(boot_img, 'rb') as inf: set_part_data(2, inf.read()) @@ -494,95 +505,95 @@ def setup_android_image(cons): return fname -def setup_cedit_file(cons): +def setup_cedit_file(ubman): """Set up a .dtb file for use with testing expo and configuration editor""" - infname = os.path.join(cons.config.source_dir, + infname = os.path.join(ubman.config.source_dir, 'test/boot/files/expo_layout.dts') - inhname = os.path.join(cons.config.source_dir, + inhname = os.path.join(ubman.config.source_dir, 'test/boot/files/expo_ids.h') - expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py') + expo_tool = os.path.join(ubman.config.source_dir, 'tools/expo.py') outfname = 'cedit.dtb' - u_boot_utils.run_and_log( - cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}') + utils.run_and_log( + ubman, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}') @pytest.mark.buildconfigspec('ut_dm') -def test_ut_dm_init(u_boot_console): +def test_ut_dm_init(ubman): """Initialize data for ut dm tests.""" - fn = u_boot_console.config.source_dir + '/testflash.bin' + fn = ubman.config.source_dir + '/testflash.bin' if not os.path.exists(fn): data = b'this is a test' data += b'\x00' * ((4 * 1024 * 1024) - len(data)) with open(fn, 'wb') as fh: fh.write(data) - fn = u_boot_console.config.source_dir + '/spi.bin' + fn = ubman.config.source_dir + '/spi.bin' if not os.path.exists(fn): data = b'\x00' * (2 * 1024 * 1024) with open(fn, 'wb') as fh: fh.write(data) # Create a file with a single partition - fn = u_boot_console.config.source_dir + '/scsi.img' + fn = ubman.config.source_dir + '/scsi.img' if not os.path.exists(fn): data = b'\x00' * (2 * 1024 * 1024) with open(fn, 'wb') as fh: fh.write(data) - u_boot_utils.run_and_log( - u_boot_console, f'sfdisk {fn}', stdin=b'type=83') + utils.run_and_log( + ubman, f'sfdisk {fn}', stdin=b'type=83') - fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', None) - fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', None) + fs_helper.mk_fs(ubman.config, 'ext2', 0x200000, '2MB', None) + fs_helper.mk_fs(ubman.config, 'fat32', 0x100000, '1MB', None) mmc_dev = 6 - fn = os.path.join(u_boot_console.config.source_dir, f'mmc{mmc_dev}.img') + fn = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img') data = b'\x00' * (12 * 1024 * 1024) with open(fn, 'wb') as fh: fh.write(data) -def setup_efi_image(cons): +def setup_efi_image(ubman): """Create a 20MB disk image with an EFI app on it""" devnum = 1 basename = 'flash' - fname, mnt = setup_image(cons, devnum, 0xc, second_part=True, + fname, mnt = setup_image(ubman, devnum, 0xc, second_part=True, basename=basename) efi_dir = os.path.join(mnt, 'EFI') mkdir_cond(efi_dir) bootdir = os.path.join(efi_dir, 'BOOT') mkdir_cond(bootdir) - efi_src = os.path.join(cons.config.build_dir, + efi_src = os.path.join(ubman.config.build_dir, 'lib/efi_loader/testapp.efi') efi_dst = os.path.join(bootdir, 'BOOTSBOX.EFI') with open(efi_src, 'rb') as inf: with open(efi_dst, 'wb') as outf: outf.write(inf.read()) fsfile = 'vfat18M.img' - u_boot_utils.run_and_log(cons, f'fallocate -l 18M {fsfile}') - u_boot_utils.run_and_log(cons, f'mkfs.vfat {fsfile}') - u_boot_utils.run_and_log(cons, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/']) - u_boot_utils.run_and_log(cons, f'dd if={fsfile} of={fname} bs=1M seek=1') - u_boot_utils.run_and_log(cons, f'rm -rf {mnt}') - u_boot_utils.run_and_log(cons, f'rm -f {fsfile}') + utils.run_and_log(ubman, f'fallocate -l 18M {fsfile}') + utils.run_and_log(ubman, f'mkfs.vfat {fsfile}') + utils.run_and_log(ubman, ['sh', '-c', f'mcopy -vs -i {fsfile} {mnt}/* ::/']) + copy_partition(ubman, fsfile, fname) + utils.run_and_log(ubman, f'rm -rf {mnt}') + utils.run_and_log(ubman, f'rm -f {fsfile}') @pytest.mark.buildconfigspec('cmd_bootflow') @pytest.mark.buildconfigspec('sandbox') -def test_ut_dm_init_bootstd(u_boot_console): +def test_ut_dm_init_bootstd(ubman): """Initialise data for bootflow tests""" - setup_bootflow_image(u_boot_console) - setup_bootmenu_image(u_boot_console) - setup_cedit_file(u_boot_console) - setup_cros_image(u_boot_console) - setup_android_image(u_boot_console) - setup_efi_image(u_boot_console) + setup_bootflow_image(ubman) + setup_bootmenu_image(ubman) + setup_cedit_file(ubman) + setup_cros_image(ubman) + setup_android_image(ubman) + setup_efi_image(ubman) # Restart so that the new mmc1.img is picked up - u_boot_console.restart_uboot() + ubman.restart_uboot() -def test_ut(u_boot_console, ut_subtest): +def test_ut(ubman, ut_subtest): """Execute a "ut" subtest. The subtests are collected in function generate_ut_subtest() from linker @@ -595,16 +606,16 @@ def test_ut(u_boot_console, ut_subtest): implemented in C function foo_test_bar(). Args: - u_boot_console (ConsoleBase): U-Boot console + ubman (ConsoleBase): U-Boot console ut_subtest (str): test to be executed via command ut, e.g 'foo bar' to execute command 'ut foo bar' """ if ut_subtest == 'hush hush_test_simple_dollar': # ut hush hush_test_simple_dollar prints "Unknown command" on purpose. - with u_boot_console.disable_check('unknown_command'): - output = u_boot_console.run_command('ut ' + ut_subtest) + with ubman.disable_check('unknown_command'): + output = ubman.run_command('ut ' + ut_subtest) assert 'Unknown command \'quux\' - try \'help\'' in output else: - output = u_boot_console.run_command('ut ' + ut_subtest) + output = ubman.run_command('ut ' + ut_subtest) assert output.endswith('failures: 0') |