Skip to content

Commit

Permalink
fix unit tests - sort dict keys to preserve order on py27
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
richm committed Jun 4, 2024
1 parent e3ff681 commit b9ae493
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions library/bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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"
)
Expand Down

0 comments on commit b9ae493

Please sign in to comment.