From 2c32813d8c0a0e85e9ba49f95188a18870766fad Mon Sep 17 00:00:00 2001 From: akumari Date: Mon, 30 Jun 2025 04:36:09 +0530 Subject: [PATCH] Add IPv6 preflight check --- src/playbooks/checks/check.yaml | 10 +++++++ src/roles/ipv6_check/files/foreman_ipv6_check | 13 +++++++++ tests/unit/test_foreman_ipv6_check.py | 28 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/playbooks/checks/check.yaml create mode 100755 src/roles/ipv6_check/files/foreman_ipv6_check create mode 100644 tests/unit/test_foreman_ipv6_check.py diff --git a/src/playbooks/checks/check.yaml b/src/playbooks/checks/check.yaml new file mode 100644 index 00000000..69b914d2 --- /dev/null +++ b/src/playbooks/checks/check.yaml @@ -0,0 +1,10 @@ +--- +- name: Run preflight checks + hosts: localhost + gather_facts: false + tasks: + - name: Run IPv6 check script + command: "{{ playbook_dir }}/../../roles/ipv6_check/files/foreman_ipv6_check" + register: ipv6_check + failed_when: ipv6_check.rc != 0 + diff --git a/src/roles/ipv6_check/files/foreman_ipv6_check b/src/roles/ipv6_check/files/foreman_ipv6_check new file mode 100755 index 00000000..237a5052 --- /dev/null +++ b/src/roles/ipv6_check/files/foreman_ipv6_check @@ -0,0 +1,13 @@ +#!/bin/sh + +RED=$(tput setaf 1 2>/dev/null) +RESET=$(tput sgr0 2>/dev/null) + +if grep -q "ipv6.disable=1" /proc/cmdline ; then + echo "${RED}FAIL${RESET}: The kernel contains ipv6.disable=1 which breaks installation and upgrade." + echo "Remove this boot parameter and reboot before continuing." + exit 2 +fi + +echo "IPv6 check: OK" +exit 0 diff --git a/tests/unit/test_foreman_ipv6_check.py b/tests/unit/test_foreman_ipv6_check.py new file mode 100644 index 00000000..c024a037 --- /dev/null +++ b/tests/unit/test_foreman_ipv6_check.py @@ -0,0 +1,28 @@ +import subprocess +import pytest +import os + +TEST_DIR = os.path.dirname(os.path.abspath(__file__)) +SCRIPT_PATH = os.path.abspath(os.path.join(TEST_DIR, "..", "..", "src", "roles", "ipv6_check", "files", "foreman-ipv6-check")) + +if not os.path.exists(SCRIPT_PATH): + pytest.fail(f"Script not found at: {SCRIPT_PATH}") + +def run_script_with_mocked_cmdline(cmdline_content): + """Helper to mock /proc/cmdline using subprocess input.""" + return subprocess.run( + ["bash", "-c", f'echo "{cmdline_content}" | {SCRIPT_PATH}'], + text=True, + capture_output=True, + shell=False, + ) + +def test_ipv6_check_passes_on_valid_cmdline(): + result = run_script_with_mocked_cmdline("quiet splash") + assert result.returncode == 0 + assert "IPv6 check: OK" in result.stdout + +def test_ipv6_check_fails_on_disabled_ipv6(): + result = run_script_with_mocked_cmdline("quiet splash ipv6.disable=1") + assert result.returncode == 2 + assert "FAIL" in result.stdout or result.stderr