Skip to content

Commit

Permalink
Add unit tests for bootloader_settings and bootloader_facts
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrosi committed Dec 6, 2023
1 parent 4a2d2ca commit 6eb3b45
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 269 deletions.
31 changes: 19 additions & 12 deletions library/bootloader_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@
from ansible.module_utils.basic import AnsibleModule


def get_facts(stdout):
stdout_lines = stdout.strip().split("\n")
kernels = []
index_count = 0
for line in stdout_lines:
if re.search(r"index=\d+", line):
index_count += 1
kernels.append({})
search = re.search(r"(.*?)=(.*)", line)
key = search.group(1).strip('"')
value = search.group(2).strip('"')
kernels[index_count - 1].update({key: value})
return kernels


def run_module():
# define available arguments/parameters a user can pass to the module
module_args = dict()
Expand All @@ -51,18 +66,10 @@ def run_module():
rc, stdout, stderr = module.run_command("grubby --info=ALL")
if "Permission denied" in stderr:
module.fail_json(msg="You must run this as sudo", **result)
stdout_lines = stdout.strip().split("\n")
kernels = []
index_count = 0
for line in stdout_lines:
if re.search(r"index=\d+", line):
index_count += 1
kernels.append({})
search = re.search(r"(.*?)=(.*)", line)
key = search.group(1).strip('"')
value = search.group(2).strip('"')
kernels[index_count - 1].update({key: value})
result["ansible_facts"]["bootloader_facts"] = kernels
# result.append(["stdout"])
# result["ansible_facts"].append(["stdout"])
result["ansible_facts"]["stdout"] = get_facts(stdout)
result["ansible_facts"]["bootloader_facts"] = get_facts(stdout)

# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
Expand Down
16 changes: 8 additions & 8 deletions library/bootloader_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,15 @@ def escapeval(val):
return ansible_six_moves.shlex_quote(str(val))


def get_kernels(bootloader_setting_kernel):
kernels_keys = ["kernel_index", "kernel_path", "kernel_title", "DEFAULT", "ALL"]
def get_kernels(bootloader_setting_kernel, kernels_keys):
kernels = []
for kernel_key in kernels_keys:
if kernel_key in bootloader_setting_kernel and kernel_key != bootloader_setting_kernel:
kernel_key_prefix = ""
if kernel_key == "kernel_title":
kernel_key_prefix = "TITLE="
if not isinstance(bootloader_setting_kernel[kernel_key], list):
kernels.append(kernel_key_prefix + bootloader_setting_kernel[kernel_key])
kernels.append(kernel_key_prefix + str(bootloader_setting_kernel[kernel_key]))
else:
for kernel_entry in bootloader_setting_kernel[kernel_key]:
kernels.append(kernel_key_prefix + str(kernel_entry))
Expand Down Expand Up @@ -105,11 +104,11 @@ def get_mod_boot_args_cmd(bootloader_setting_options, kernel):
else:
boot_present_args += setting_name + " "
if boot_absent_args:
boot_mod_args = " --remove-args=" + escapeval(boot_absent_args)
boot_mod_args = " --remove-args=" + escapeval(boot_absent_args.strip())
if len(boot_present_args) > 0:
boot_mod_args += " --args=" + escapeval(boot_present_args)
boot_mod_args += " --args=" + escapeval(boot_present_args.strip())
if boot_mod_args:
return "grubby --update-kernel=" + kernel + boot_mod_args
return "grubby --update-kernel=" + escapeval(kernel) + boot_mod_args


def run_module():
Expand All @@ -132,11 +131,12 @@ def run_module():
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
result['rm_boot_args_cmd'] = []
result['mod_boot_args_cmd'] = []
kernels_keys = ["kernel_index", "kernel_path", "kernel_title", "DEFAULT", "ALL"]
for bootloader_setting in module.params["bootloader_settings"]:
kernels = get_kernels(bootloader_setting["kernel"])
kernels = get_kernels(bootloader_setting["kernel"], kernels_keys)
if not kernels:
module.fail_json(
msg = 'bootloader_settings.kernel must contain one of %s' % ', '.join(kernels_keys),
msg='bootloader_settings.kernel must contain one of %s' % ', '.join(kernels_keys),
**result
)
for kernel in kernels:
Expand Down
4 changes: 2 additions & 2 deletions pytest_extra_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

# Write extra requirements for running pytest here:
# If you need ansible then uncomment the following line:
#-ransible_pytest_extra_requirements.txt
-ransible_pytest_extra_requirements.txt
# If you need mock then uncomment the following line:
#mock ; python_version < "3.0"
mock ; python_version < "3.0"
Loading

0 comments on commit 6eb3b45

Please sign in to comment.