Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/bootloader_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def run_module():

rc, kernels_info, stderr = module.run_command("grubby --info=ALL")
if "Permission denied" in stderr:
module.fail_json("You must run this as sudo", **result)
module.fail_json(msg="You must run this as sudo", **result)
rc, default_kernel, stderr = module.run_command("grubby --default-index")
result["ansible_facts"]["bootloader_facts"] = get_facts(
kernels_info, default_kernel
Expand Down
24 changes: 12 additions & 12 deletions library/bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def validate_kernel_initrd(module, bootloader_setting_kernel, kernel_mod_keys):
and "initrd" in bootloader_setting_kernel.keys()
):
module.fail_json(
"You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of %s"
msg="You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of %s"
% ", ".join(kernel_mod_keys)
Comment on lines +121 to 122

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)

Suggested change
msg="You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of %s"
% ", ".join(kernel_mod_keys)
msg=f"""You can use 'initrd' as a kernel key only when you must create a kernel. To modify or remove an existing kernel, use one of {", ".join(kernel_mod_keys)}"""

)

Expand Down Expand Up @@ -165,7 +165,7 @@ def validate_default_kernel(module, bootloader_settings):
kernel = bootloader_setting["kernel"]
if isinstance(kernel, str):
module.fail_json(
"You cannot set a kernel as default when you are using a string kernel - %s"
msg="You cannot set a kernel as default when you are using a string kernel - %s"
% (kernel)
Comment on lines +168 to 169

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code-quality): Replace interpolated string formatting with f-string (replace-interpolation-with-fstring)

Suggested change
msg="You cannot set a kernel as default when you are using a string kernel - %s"
% (kernel)
msg=f"You cannot set a kernel as default when you are using a string kernel - {kernel}"

)
if isinstance(kernel, dict):
Expand All @@ -178,7 +178,7 @@ def validate_default_kernel(module, bootloader_settings):
default_kernels.append(kernel_id)
if default_count > 1:
module.fail_json(
"Only one kernel can be set as default. Found %d kernels with 'default: true' - %s"
msg="Only one kernel can be set as default. Found %d kernels with 'default: true' - %s"
% (default_count, ", ".join(default_kernels))
)

Expand All @@ -196,21 +196,21 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
state = bootloader_setting.get("state", "present")

if "state" in bootloader_setting and bootloader_setting["state"] not in states:
module.fail_json("State must be one of '%s'" % ", ".join(states))
module.fail_json(msg="State must be one of '%s'" % ", ".join(states))

if (not isinstance(bootloader_setting["kernel"], dict)) and (
not isinstance(bootloader_setting["kernel"], str)
):
module.fail_json(
"kernel value in %s must be of type str or dict"
msg="kernel value in %s must be of type str or dict"
% bootloader_setting["kernel"]
)
Comment on lines 201 to 207

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: The error message could be more explicit about the actual type received.

Suggest including the actual type of bootloader_setting["kernel"] in the error message for easier debugging, e.g., 'kernel value in %s (type %s) must be of type str or dict'.

Suggested change
if (not isinstance(bootloader_setting["kernel"], dict)) and (
not isinstance(bootloader_setting["kernel"], str)
):
module.fail_json(
"kernel value in %s must be of type str or dict"
msg="kernel value in %s must be of type str or dict"
% bootloader_setting["kernel"]
)
if (not isinstance(bootloader_setting["kernel"], dict)) and (
not isinstance(bootloader_setting["kernel"], str)
):
module.fail_json(
msg="kernel value in %s (type %s) must be of type str or dict"
% (bootloader_setting["kernel"], type(bootloader_setting["kernel"]).__name__)
)


if (isinstance(bootloader_setting["kernel"], str)) and (
bootloader_setting["kernel"] not in kernel_str_values
):
module.fail_json(
"kernel %s is of type str, it must be one of '%s'"
msg="kernel %s is of type str, it must be one of '%s'"
% (
bootloader_setting["kernel"],
", ".join(kernel_str_values),
Expand All @@ -227,7 +227,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
for key, value in bootloader_setting["kernel"].items():
if key not in kernel_keys:
module.fail_json(
"kernel key in '%s: %s' must be one of '%s'"
msg="kernel key in '%s: %s' must be one of '%s'"
% (
key,
value,
Expand All @@ -236,7 +236,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
)
if (not isinstance(value, str)) and (not isinstance(value, int)):
module.fail_json(
"kernel value in '%s: %s' must be of type str or int" % (key, value)
msg="kernel value in '%s: %s' must be of type str or int" % (key, value)
)

# Validate with len(bootloader_setting["kernel"]) == 1
Expand All @@ -255,7 +255,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
diff, same = compare_dicts(bootloader_setting["kernel"], fact_trunc)
if diff and same:
module.fail_json(
"A kernel with provided %s already exists and its other fields are different %s"
msg="A kernel with provided %s already exists and its other fields are different %s"
% (same, diff)
)
elif not diff and same:
Expand All @@ -268,7 +268,7 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
sorted(bootloader_setting["kernel"].keys()) != sorted(kernel_create_keys)
):
module.fail_json(
"To create a kernel, you must provide 3 kernel keys - '%s'"
msg="To create a kernel, you must provide 3 kernel keys - '%s'"
% ", ".join(kernel_create_keys)
)
kernel_action = "create" if state == "present" else "remove"
Expand Down Expand Up @@ -407,7 +407,7 @@ def rm_kernel(module, result, kernel):

def get_default_kernel(module, type):
if type not in ["kernel", "title", "index"]:
module.fail_json("Type must be one of 'kernel', 'title', or 'index'")
module.fail_json(msg="Type must be one of 'kernel', 'title', or 'index'")
cmd = "grubby --default-" + type
_unused, stdout, _unused = module.run_command(cmd)
return stdout.strip()
Expand Down Expand Up @@ -437,7 +437,7 @@ def run_module():
for bootloader_setting in module.params["bootloader_settings"]:
_unused, kernels_info, stderr = module.run_command("grubby --info=ALL")
if "Permission denied" in stderr:
module.fail_json("You must run this as sudo")
module.fail_json(msg="You must run this as sudo")

default_kernel_index = get_default_kernel(module, "index")
bootloader_facts = get_facts(kernels_info, default_kernel_index)
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def assert_error_msg(self, err, *cmd_args):
self.mock_module, *cmd_args
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(err)
self.mock_module.fail_json.assert_called_once_with(msg=err)

def test_validate_kernels(self):
self.reset_vars()
Expand Down Expand Up @@ -578,7 +578,7 @@ def test_validate_default_kernel(self):
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(
"You cannot set a kernel as default when you are using a string kernel - ALL"
msg="You cannot set a kernel as default when you are using a string kernel - ALL"
)
self.reset_vars()

Expand All @@ -592,7 +592,7 @@ def test_validate_default_kernel(self):
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(
"You cannot set a kernel as default when you are using a string kernel - DEFAULT"
msg="You cannot set a kernel as default when you are using a string kernel - DEFAULT"
)
self.reset_vars()

Expand Down Expand Up @@ -650,7 +650,7 @@ def test_validate_default_kernel(self):
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(
"Only one kernel can be set as default. Found 2 kernels with 'default: true' - /boot/vmlinuz-test1, /boot/vmlinuz-test2"
msg="Only one kernel can be set as default. Found 2 kernels with 'default: true' - /boot/vmlinuz-test1, /boot/vmlinuz-test2"
)
self.reset_vars()

Expand All @@ -665,7 +665,7 @@ def test_validate_default_kernel(self):
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(
"Only one kernel can be set as default. Found 2 kernels with 'default: true' - Test Kernel, 2"
msg="Only one kernel can be set as default. Found 2 kernels with 'default: true' - Test Kernel, 2"
)
self.reset_vars()

Expand Down Expand Up @@ -787,6 +787,6 @@ def test_mod_default_kernel(self):
bootloader_settings.get_default_kernel(self.mock_module, "invalid")
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(
"Type must be one of 'kernel', 'title', or 'index'"
msg="Type must be one of 'kernel', 'title', or 'index'"
)
self.reset_vars()
Loading