From b9ae49319642a0eee049b3b106599902554d5e2e Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Mon, 3 Jun 2024 12:28:06 -0600 Subject: [PATCH] fix unit tests - sort dict keys to preserve order on py27 In python3, dict key order is preserved by default. Not on py27. Sort dict keys so that order is predictable. Convert `set` to `list` because py2 and py3 render a `set` object differently. --- library/bootloader_settings.py | 7 ++++--- tests/unit/test_bootloader_settings.py | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index f5feba7..e0d8acf 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -102,7 +102,7 @@ def compare_dicts(dict1, dict2): dict2_keys = set(dict2.keys()) shared_keys = dict1_keys.intersection(dict2_keys) diff = {o: (dict1[o], dict2[o]) for o in shared_keys if dict1[o] != dict2[o]} - same = set(o for o in shared_keys if dict1[o] == dict2[o]) + same = list(set(o for o in shared_keys if dict1[o] == dict2[o])) return diff, same @@ -139,7 +139,8 @@ def get_single_kernel(bootloader_setting_kernel): def get_create_kernel(bootloader_setting_kernel): """Get kernel in the format expected by 'grubby --add-kernel=' from a multiple-element dict""" kernel = "" - for key, value in bootloader_setting_kernel.items(): + for key in sorted(bootloader_setting_kernel.keys()): + value = bootloader_setting_kernel[key] if key == "path": kernel += " --add-kernel=" + escapeval(value) elif key == "title": @@ -222,7 +223,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts): # diff, same = compare_dicts(bootloader_setting["kernel"], fact) if diff and same: module.fail_json( - "A kernel with provided %s already exists and it's other fields are different %s" + "A kernel with provided %s already exists and its other fields are different %s" % (same, diff) ) elif not diff and same: diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 34b2119..0a6e149 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -263,7 +263,7 @@ def test_validate_kernels(self): self.reset_vars() err = ( - "A kernel with provided {'path'} already exists and it's other fields are different " + "A kernel with provided ['path'] already exists and its other fields are different " + "{'title': ('Fedora Linux', 'Fedora Linux (6.5.12-100.fc37.x86_64) 37 (Workstation Edition)')}" ) cmd_args = SETTINGS[6], FACTS @@ -288,7 +288,7 @@ def test_validate_kernels(self): self.assertEqual(self.kernel_action, "create") self.assertEqual( self.kernel, - "--title='Fedora Linux' --add-kernel=/boot/vmlinuz-6 --initrd=/boot/initramfs-6.6.img", + "--initrd=/boot/initramfs-6.6.img --add-kernel=/boot/vmlinuz-6 --title='Fedora Linux'", ) self.reset_vars() @@ -327,7 +327,7 @@ def test_add_kernel(self): self.mock_module, self.result, SETTINGS[8]["options"], self.kernel ) expected_cmd = ( - "grubby --title='Fedora Linux' --add-kernel=/boot/vmlinuz-6 --initrd=/boot/initramfs-6.6.img " + "grubby --initrd=/boot/initramfs-6.6.img --add-kernel=/boot/vmlinuz-6 --title='Fedora Linux' " + "--args='arg_with_str_value=test_value arg_with_int_value=1 arg_without_val arg_with_str_value_absent=test_value " + "arg_with_int_value_absent=1 arg_without_val_absent' --copy-default" )