From 30f740ec9fc73279aadeef1268168f4886bd90b7 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 26 Feb 2024 10:14:05 +0100 Subject: [PATCH] stages: add test for `coreos.platform` generate_console_settings_file And a tiny tweak to avoid an empty line at the start of the `boot/grub2/console.cfg` --- stages/org.osbuild.coreos.platform | 2 +- stages/test/test_coreos_platform.py | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/stages/org.osbuild.coreos.platform b/stages/org.osbuild.coreos.platform index 51fcc824..59833070 100755 --- a/stages/org.osbuild.coreos.platform +++ b/stages/org.osbuild.coreos.platform @@ -59,7 +59,7 @@ def generate_console_settings_file(console_settings, file_path): settings_content = "" if console_settings is not None: settings_content = "\n".join(console_settings) - file_content = f""" + file_content = f"""\ # Any non-default console settings will be inserted here. # CONSOLE-SETTINGS-START {settings_content} diff --git a/stages/test/test_coreos_platform.py b/stages/test/test_coreos_platform.py index f9bdeed0..0e8f9032 100644 --- a/stages/test/test_coreos_platform.py +++ b/stages/test/test_coreos_platform.py @@ -53,3 +53,36 @@ def test_process_platforms_json(tmp_path, stage_module, platform, expected_grub, grub_cmds, kernel_args = stage_module.process_platforms_json(fake_platforms_path, platform) assert grub_cmds == expected_grub assert kernel_args == expected_kernel + + +def test_generate_console_settings_none(tmp_path, stage_module): + fake_console_settings_path = tmp_path / "boot/grub2/console.cfg" + fake_console_settings_path.parent.mkdir(parents=True) + console_settings = None + + stage_module.generate_console_settings_file(console_settings, fake_console_settings_path) + # ensure we generate a template even if no settings are provided + assert fake_console_settings_path.read_text(encoding="utf8") == textwrap.dedent("""\ + # Any non-default console settings will be inserted here. + # CONSOLE-SETTINGS-START + + # CONSOLE-SETTINGS-END + """) + + +def test_generate_console_settings_multiple(tmp_path, stage_module): + fake_console_settings_path = tmp_path / "boot/grub2/console.cfg" + fake_console_settings_path.parent.mkdir(parents=True) + # console settings are the grub commands from the platforms.json + console_settings = ["serial --speed=115200", "terminal_input serial console", "terminal_output serial console"] + + stage_module.generate_console_settings_file(console_settings, fake_console_settings_path) + # ensure we generate a template even if no settings are provided + assert fake_console_settings_path.read_text(encoding="utf8") == textwrap.dedent("""\ + # Any non-default console settings will be inserted here. + # CONSOLE-SETTINGS-START + serial --speed=115200 + terminal_input serial console + terminal_output serial console + # CONSOLE-SETTINGS-END + """)