diff --git a/library/bootloader_settings.py b/library/bootloader_settings.py index 422ac96..37b1324 100644 --- a/library/bootloader_settings.py +++ b/library/bootloader_settings.py @@ -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), ) @@ -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), @@ -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) diff --git a/tests/unit/test_bootloader_settings.py b/tests/unit/test_bootloader_settings.py index 34af2a4..055d866 100644 --- a/tests/unit/test_bootloader_settings.py +++ b/tests/unit/test_bootloader_settings.py @@ -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 @@ -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 @@ -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) @@ -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) @@ -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): @@ -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): @@ -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): @@ -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( @@ -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( @@ -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( @@ -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()