diff options
author | William Manley <will@stb-tester.com> | 2020-07-14 13:32:30 +0100 |
---|---|---|
committer | William Manley <will@stb-tester.com> | 2020-10-26 23:54:19 +0000 |
commit | 2a6c0b21db072cf8f66d27ca6ca8a01a7c908b17 (patch) | |
tree | 6516321c46807369ec67f4ec9c70fe16ee4bf67e | |
parent | a8dce46b5fb7d19e554ecaec994f51f4269de9ea (diff) | |
download | ostree-2a6c0b21db072cf8f66d27ca6ca8a01a7c908b17.tar.gz |
Tests: Refactor bootloader-entries-crosscheck
I've made this use functions to make it easier to add support for more
bootloaders. Seeing as there will be a big diff anyway I've also adjusted
the formatting to make it pep8 compliant.
-rwxr-xr-x | tests/bootloader-entries-crosscheck.py | 179 |
1 files changed, 92 insertions, 87 deletions
diff --git a/tests/bootloader-entries-crosscheck.py b/tests/bootloader-entries-crosscheck.py index 605bd080..b5a02066 100755 --- a/tests/bootloader-entries-crosscheck.py +++ b/tests/bootloader-entries-crosscheck.py @@ -20,113 +20,118 @@ import os import sys -if len(sys.argv) == 1: - sysroot = '' -else: - sysroot = sys.argv[1] -bootloader = sys.argv[2] -loaderpath = sysroot + '/boot/loader/entries' -syslinuxpath = sysroot + '/boot/syslinux/syslinux.cfg' +def main(argv): + _, sysroot, bootloader = argv + + if bootloader == "grub2": + sys.stdout.write('GRUB2 configuration validation not implemented.\n') + return 0 + else: + return validate_syslinux(sysroot) -if bootloader == "grub2": - sys.stdout.write('GRUB2 configuration validation not implemented.\n') - sys.exit(0) def fatal(msg): sys.stderr.write(msg) sys.stderr.write('\n') sys.exit(1) -def entry_get_version(entry): - return int(entry['version']) def get_ostree_option(optionstring): for o in optionstring.split(): if o.startswith('ostree='): return o[8:] - raise ValueError('ostree= not found') - -entries = [] -syslinux_entries = [] - -# Parse loader configs -for fname in os.listdir(loaderpath): - path = os.path.join(loaderpath, fname) - with open(path) as f: - entry = {} - for line in f: - line = line.strip() - if (line == '' or line.startswith('#')): - continue - s = line.find(' ') - assert s > 0 - k = line[0:s] - v = line[s+1:] - entry[k] = v - entries.append(entry) - entries.sort(key=entry_get_version, reverse=True) - -# Parse SYSLINUX config -with open(syslinuxpath) as f: - in_ostree_config = False - syslinux_entry = None - syslinux_default = None - for line in f: - try: - k, v = line.strip().split(" ", 1) - except ValueError: - continue - if k == 'DEFAULT': - if syslinux_entry is not None: - syslinux_default = v - elif k == 'LABEL': - if syslinux_entry is not None: - syslinux_entries.append(syslinux_entry) - syslinux_entry = {} - syslinux_entry['title'] = v - elif k == 'KERNEL': - syslinux_entry['linux'] = v - elif k == 'INITRD': - syslinux_entry['initrd'] = v - elif k == 'APPEND': - syslinux_entry['options'] = v - if syslinux_entry is not None: - syslinux_entries.append(syslinux_entry) - -if len(entries) != len(syslinux_entries): - fatal("Found {0} loader entries, but {1} SYSLINUX entries\n".format(len(entries), len(syslinux_entries))) + raise ValueError('ostree= not found in %r' % (optionstring,)) -def assert_eq(a, b): - assert a == b, "%r == %r" % (a, b) +def parse_loader_configs(sysroot): + loaderpath = sysroot + '/boot/loader/entries' + entries = [] + # Parse loader configs + for fname in os.listdir(loaderpath): + path = os.path.join(loaderpath, fname) + entry = {} + with open(path) as f: + for line in f: + line = line.strip() + if (line == '' or line.startswith('#')): + continue + k, v = line.split(' ', 1) + entry[k] = v + entries.append(entry) + entries.sort(key=lambda e: int(e['version']), reverse=True) + return entries -def assert_key_same_file(a, b, key): - aval = a[key] - bval = b[key] - sys.stderr.write("aval: %r\nbval: %r\n" % (aval, bval)) - # Paths in entries are always relative to /boot - entry = os.stat(sysroot + "/boot" + aval) +def validate_syslinux(sysroot): + syslinuxpath = sysroot + '/boot/syslinux/syslinux.cfg' - # Syslinux entries can be relative to /boot (if it's on another filesystem) - # or relative to / if /boot is on /. - s1 = os.stat(sysroot + bval) - s2 = os.stat(sysroot + "/boot" + bval) + entries = parse_loader_configs(sysroot) + syslinux_entries = [] - # A symlink ensures that no matter what they point at the same file - assert_eq(entry, s1) - assert_eq(entry, s2) + # Parse SYSLINUX config + with open(syslinuxpath) as f: + syslinux_entry = None + for line in f: + try: + k, v = line.strip().split(" ", 1) + except ValueError: + continue + if k == 'DEFAULT': + if syslinux_entry is not None: + syslinux_default = v + elif k == 'LABEL': + if syslinux_entry is not None: + syslinux_entries.append(syslinux_entry) + syslinux_entry = {} + syslinux_entry['title'] = v + elif k == 'KERNEL': + syslinux_entry['linux'] = v + elif k == 'INITRD': + syslinux_entry['initrd'] = v + elif k == 'APPEND': + syslinux_entry['options'] = v + if syslinux_entry is not None: + syslinux_entries.append(syslinux_entry) + + if len(entries) != len(syslinux_entries): + fatal("Found {0} loader entries, but {1} SYSLINUX entries\n".format( + len(entries), len(syslinux_entries))) + + def assert_key_same_file(a, b, key): + aval = a[key] + bval = b[key] + sys.stderr.write("aval: %r\nbval: %r\n" % (aval, bval)) + + # Paths in entries are always relative to /boot + entry = os.stat(sysroot + "/boot" + aval) + + # Syslinux entries can be relative to /boot (if it's on another filesystem) + # or relative to / if /boot is on /. + s1 = os.stat(sysroot + bval) + s2 = os.stat(sysroot + "/boot" + bval) + + # A symlink ensures that no matter what they point at the same file + assert_eq(entry, s1) + assert_eq(entry, s2) + + for i, (entry, syslinuxentry) in enumerate(zip(entries, syslinux_entries)): + assert_key_same_file(entry, syslinuxentry, 'linux') + assert_key_same_file(entry, syslinuxentry, 'initrd') + entry_ostree = get_ostree_option(entry['options']) + syslinux_ostree = get_ostree_option(syslinuxentry['options']) + if entry_ostree != syslinux_ostree: + fatal("Mismatch on ostree option: {0} != {1}".format( + entry_ostree, syslinux_ostree)) + + sys.stdout.write('SYSLINUX configuration validated\n') + return 0 -for i,(entry,syslinuxentry) in enumerate(zip(entries, syslinux_entries)): - assert_key_same_file(entry, syslinuxentry, 'linux') - assert_key_same_file(entry, syslinuxentry, 'initrd') - entry_ostree = get_ostree_option(entry['options']) - syslinux_ostree = get_ostree_option(syslinuxentry['options']) - if entry_ostree != syslinux_ostree: - fatal("Mismatch on ostree option: {0} != {1}".format(entry_ostree, syslinux_ostree)) +def assert_eq(a, b): + assert a == b, "%r == %r" % (a, b) + -sys.stdout.write('SYSLINUX configuration validated\n') -sys.exit(0) +if __name__ == '__main__': + sys.exit(main(sys.argv)) |