Skip to content

Commit

Permalink
Fix linters
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrosi committed Apr 18, 2024
1 parent f97bfa2 commit 4abeb73
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 61 deletions.
13 changes: 10 additions & 3 deletions library/bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def validate_kernels(module, bootloader_setting, bootloader_facts):
bootloader_setting["kernel"] not in kernel_str_values
):
module.fail_json(
"kernel %s is of type str, it must be one of '%s'" % (
"kernel %s is of type str, it must be one of '%s'"
% (
bootloader_setting["kernel"],
", ".join(kernel_str_values),
)
Expand All @@ -192,7 +193,8 @@ 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'" % (
"kernel key in '%s: %s' must be one of '%s'"
% (
key,
value,
", ".join(kernel_keys),
Expand Down Expand Up @@ -268,7 +270,12 @@ def get_boot_args(kernel_info):
def rm_boot_args(module, result, kernel_info, kernel):
"""Remove all existing args for a kernel"""
bootloader_args = get_boot_args(kernel_info)
cmd = "grubby --update-kernel=" + kernel + " --remove-args=" + escapeval(bootloader_args)
cmd = (
"grubby --update-kernel="
+ kernel
+ " --remove-args="
+ escapeval(bootloader_args)
)
_unused, stdout, _unused = module.run_command(cmd)
result["changed"] = True
result["actions"].append(cmd)
Expand Down
95 changes: 37 additions & 58 deletions tests/unit/test_bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import unittest

try:
from unittest.mock import call, MagicMock, Mock, patch
from unittest.mock import MagicMock
except ImportError:
from mock import call, MagicMock, Mock, patch
from mock import MagicMock

import bootloader_settings

Expand Down Expand Up @@ -163,9 +163,10 @@

class InputValidator(unittest.TestCase):
"""test functions that process bootloader_settings argument"""

mock_module = MagicMock(
run_command=MagicMock(return_value=("test_rc", "test_stdout", "test_err")),
fail_json=MagicMock(side_effect=SystemExit)
fail_json=MagicMock(side_effect=SystemExit),
)
result = dict(changed=False, actions=list())
kernel = None
Expand All @@ -186,7 +187,9 @@ def reset_vars(self):

def assert_error_msg(self, err, *cmd_args):
try:
self.kernel_action, self.kernel = bootloader_settings.validate_kernels(self.mock_module, *cmd_args)
self.kernel_action, self.kernel = bootloader_settings.validate_kernels(
self.mock_module, *cmd_args
)
except SystemExit:
self.mock_module.fail_json.assert_called_once_with(err)

Expand Down Expand Up @@ -248,7 +251,9 @@ def test_validate_kernels(self):
self.assertIsNone(self.kernel)
self.reset_vars()

err = "To create a kernel, you must provide 3 kernel keys - 'path, title, initrd'"
err = (
"To create a kernel, you must provide 3 kernel keys - 'path, title, initrd'"
)
cmd_args = SETTINGS[7], FACTS
self.assert_error_msg(err, *cmd_args)
self.assertIsNone(self.kernel_action)
Expand Down Expand Up @@ -306,12 +311,8 @@ def test_add_kernel(self):
+ "arg_with_int_value_absent=1 arg_without_val_absent' --copy-default"
)
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

def test_rm_kernel(self):
Expand All @@ -325,12 +326,8 @@ def test_rm_kernel(self):
bootloader_settings.rm_kernel(self.mock_module, self.result, self.kernel)
expected_cmd = "grubby --remove-kernel=1"
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

def test_get_boot_args(self):
Expand All @@ -349,20 +346,14 @@ def test_get_boot_args(self):

def test_rm_boot_args(self):
self.reset_vars()
bootloader_settings.rm_boot_args(
self.mock_module, self.result, INFO, "0"
)
bootloader_settings.rm_boot_args(self.mock_module, self.result, INFO, "0")
expected_cmd = (
"grubby --update-kernel=0 --remove-args="
+ "'arg_with_str_value_absent=test_value arg_with_int_value_absent=1 arg_without_val_absent'"
)
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

def test_mod_boot_args(self):
Expand All @@ -373,29 +364,29 @@ def test_mod_boot_args(self):
)

bootloader_settings.mod_boot_args(
self.mock_module, self.result, OPTIONS, str(KERNELS[1]["kernel"]["kernel_index"]), INFO
self.mock_module,
self.result,
OPTIONS,
str(KERNELS[1]["kernel"]["kernel_index"]),
INFO,
)
expected_cmd = "grubby --update-kernel=2 " + args
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

bootloader_settings.mod_boot_args(
self.mock_module, self.result, OPTIONS, KERNELS[3]["kernel"]["kernel_path"], INFO
self.mock_module,
self.result,
OPTIONS,
KERNELS[3]["kernel"]["kernel_path"],
INFO,
)
expected_cmd = "grubby --update-kernel=/path/3 " + args
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

bootloader_settings.mod_boot_args(
Expand All @@ -412,12 +403,8 @@ def test_mod_boot_args(self):
+ args
)
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

bootloader_settings.mod_boot_args(
Expand All @@ -429,12 +416,8 @@ def test_mod_boot_args(self):
)
expected_cmd = "grubby --update-kernel=DEFAULT " + args
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

bootloader_settings.mod_boot_args(
Expand All @@ -446,10 +429,6 @@ def test_mod_boot_args(self):
)
expected_cmd = "grubby --update-kernel=ALL " + args
self.mock_module.run_command.assert_called_once_with(expected_cmd)
self.assertEqual(
self.result["changed"], True
)
self.assertEqual(
self.result["actions"][0], expected_cmd
)
self.assertEqual(self.result["changed"], True)
self.assertEqual(self.result["actions"][0], expected_cmd)
self.reset_vars()

0 comments on commit 4abeb73

Please sign in to comment.