From e8de5a607f78ed18252d4113668687eb76c591de Mon Sep 17 00:00:00 2001 From: SquirrelDevelopper Date: Tue, 15 Oct 2024 16:35:35 +0200 Subject: [PATCH 1/2] Refactor package upgrade logic and add Molecule tests Refactor the package upgrade logic by moving distribution-specific tasks to roles. Introduce Molecule tests to ensure proper functionality across Debian and RHEL-based systems. This enhances maintainability and improves test coverage. --- .../device/_upgrade.yml | 34 ++----------------- .../roles/upgrade_host_package/debian.yml | 21 ++++++++++++ .../roles/upgrade_host_package/main.yml | 8 +++++ .../roles/upgrade_host_package/rhel.yml | 21 ++++++++++++ server/src/tests/molecule/default/run_all.yml | 16 +++++++++ .../molecule/upgrade-packages/Dockerfile.j2 | 15 ++++++++ .../molecule/upgrade-packages/converge.yml | 8 +++++ .../molecule/upgrade-packages/molecule.yml | 27 +++++++++++++++ .../molecule/upgrade-packages/verify.yml | 28 +++++++++++++++ 9 files changed, 147 insertions(+), 31 deletions(-) create mode 100644 server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/debian.yml create mode 100644 server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/main.yml create mode 100644 server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/rhel.yml create mode 100644 server/src/tests/molecule/upgrade-packages/Dockerfile.j2 create mode 100644 server/src/tests/molecule/upgrade-packages/converge.yml create mode 100644 server/src/tests/molecule/upgrade-packages/molecule.yml create mode 100644 server/src/tests/molecule/upgrade-packages/verify.yml diff --git a/server/src/ansible/00000000-0000-0000-0000-000000000000/device/_upgrade.yml b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/_upgrade.yml index 69bbb1fb..431fc62e 100644 --- a/server/src/ansible/00000000-0000-0000-0000-000000000000/device/_upgrade.yml +++ b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/_upgrade.yml @@ -18,34 +18,6 @@ become: true tasks: - - name: Perform a dist-upgrade. - ansible.builtin.package: - name: "*" - state: latest - - - name: Check if a reboot is required (Debian-based systems). - ansible.builtin.stat: - path: /var/run/reboot-required - get_checksum: no - register: reboot_required_file - when: ansible_distribution in ["Debian", "Ubuntu"] - - - name: Check if a reboot is required (CentOS/RHEL-based systems). - ansible.builtin.command: /usr/bin/needs-restarting -r - register: needs_restarting - failed_when: needs_restarting.rc not in [0,1,123] - changed_when: needs_restarting.rc == 1 - when: ansible_distribution in ["CentOS", "RedHat"] - - - name: Reboot the server (if required - Debian-based systems). - ansible.builtin.reboot: - when: reboot_required_file.stat.exists - - - name: Reboot the server (if required - CentOS/RHEL-based systems). - ansible.builtin.reboot: - when: ansible_distribution in ["CentOS", "RedHat"] and needs_restarting.rc == 1 - - - name: Remove dependencies that are no longer required. - ansible.builtin.package: - autoremove: true - when: ansible_distribution in ["Debian", "Ubuntu", "CentOS", "RedHat"] + - name: Perform an upgrade. + include_role: + name: upgrade_host_package diff --git a/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/debian.yml b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/debian.yml new file mode 100644 index 00000000..9d3c06d1 --- /dev/null +++ b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/debian.yml @@ -0,0 +1,21 @@ +--- +# tasks/debian.yml + +- name: Perform a dist-upgrade. + ansible.builtin.package: + name: "*" + state: latest + +- name: Check if a reboot is required. + ansible.builtin.stat: + path: /var/run/reboot-required + get_checksum: no + register: reboot_required_file + +- name: Reboot the server if required. + ansible.builtin.reboot: + when: reboot_required_file.stat.exists + +- name: Remove dependencies that are no longer required. + ansible.builtin.package: + autoremove: true diff --git a/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/main.yml b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/main.yml new file mode 100644 index 00000000..9bfadea3 --- /dev/null +++ b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/main.yml @@ -0,0 +1,8 @@ +--- +# tasks/main.yml + +- include_tasks: debian.yml + when: ansible_distribution in ["Debian", "Ubuntu"] + +- include_tasks: rhel.yml + when: ansible_distribution in ["CentOS", "RedHat"] diff --git a/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/rhel.yml b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/rhel.yml new file mode 100644 index 00000000..d1917f4a --- /dev/null +++ b/server/src/ansible/00000000-0000-0000-0000-000000000000/device/roles/upgrade_host_package/rhel.yml @@ -0,0 +1,21 @@ +--- +# tasks/rhel.yml + +- name: Perform a dist-upgrade. + ansible.builtin.package: + name: "*" + state: latest + +- name: Check if a reboot is required. + ansible.builtin.command: /usr/bin/needs-restarting -r + register: needs_restarting + failed_when: needs_restarting.rc not in [0,1,123] + changed_when: needs_restarting.rc == 1 + +- name: Reboot the server if required. + ansible.builtin.reboot: + when: needs_restarting.rc == 1 + +- name: Remove dependencies that are no longer required. + ansible.builtin.package: + autoremove: true diff --git a/server/src/tests/molecule/default/run_all.yml b/server/src/tests/molecule/default/run_all.yml index f423d620..1a4341eb 100644 --- a/server/src/tests/molecule/default/run_all.yml +++ b/server/src/tests/molecule/default/run_all.yml @@ -68,3 +68,19 @@ - name: Show stderr of "Install docker" debug: msg: "{{ result_install_docker.stderr }}" + + - name: Run "Upgrade packages" + block: + - name: Run Molecule test for "upgrade-packages" + command: molecule test -s upgrade-packages + args: + chdir: ../../ + register: result_upgrade_packages + + - name: Show result of "Upgrade packages" + debug: + msg: "{{ result_upgrade_packages.stdout }}" + always: + - name: Show stderr of "Upgrade packages" + debug: + msg: "{{ result_upgrade_packages.stderr }}" diff --git a/server/src/tests/molecule/upgrade-packages/Dockerfile.j2 b/server/src/tests/molecule/upgrade-packages/Dockerfile.j2 new file mode 100644 index 00000000..4467e802 --- /dev/null +++ b/server/src/tests/molecule/upgrade-packages/Dockerfile.j2 @@ -0,0 +1,15 @@ +# Dockerfile.j2 + +FROM {{ item.image }} + +# For Debian-based systems +{% if item.image.startswith('debian') or item.image.startswith('ubuntu') %} +RUN apt-get update && \ + apt-get install -y python3 python3-pip sshpass sudo +{% endif %} + +# For RHEL-based systems +{% if item.image.startswith('centos') or item.image.startswith('rhel') or item.image.startswith('rockylinux') %} +RUN yum update -y && \ + yum install -y python3 python3-pip sshpass sudo +{% endif %} diff --git a/server/src/tests/molecule/upgrade-packages/converge.yml b/server/src/tests/molecule/upgrade-packages/converge.yml new file mode 100644 index 00000000..40033466 --- /dev/null +++ b/server/src/tests/molecule/upgrade-packages/converge.yml @@ -0,0 +1,8 @@ +--- +- name: Converge + hosts: all + become: true + gather_facts: false + +- import_playbook: "../../../ansible/00000000-0000-0000-0000-000000000000/device/_upgrade.yml" + diff --git a/server/src/tests/molecule/upgrade-packages/molecule.yml b/server/src/tests/molecule/upgrade-packages/molecule.yml new file mode 100644 index 00000000..6519b8f3 --- /dev/null +++ b/server/src/tests/molecule/upgrade-packages/molecule.yml @@ -0,0 +1,27 @@ +--- +dependency: + name: galaxy + +driver: + name: docker + +platforms: + - name: debian-latest + image: debian:latest + command: "sleep infinity" # Keep container running for Molecule to work with + - name: rockylinux + image: rockylinux:9.3 + command: "sleep infinity" # Keep container running for Molecule to work with + +provisioner: + name: ansible + log: true + +scenario: + test_sequence: + - destroy + - create + - prepare + - converge + - verify + - destroy diff --git a/server/src/tests/molecule/upgrade-packages/verify.yml b/server/src/tests/molecule/upgrade-packages/verify.yml new file mode 100644 index 00000000..0f58e722 --- /dev/null +++ b/server/src/tests/molecule/upgrade-packages/verify.yml @@ -0,0 +1,28 @@ +- name: Verify + hosts: all + tasks: + - name: Ensure no packages are pending for upgrade (Debian-based) + ansible.builtin.command: apt list --upgradable + register: apt_upgrade_check + changed_when: false + failed_when: "apt_upgrade_check.stdout | trim != '' and not apt_upgrade_check.stdout is search('Listing...')" + when: ansible_os_family == "Debian" + + - name: Ensure no packages are pending for upgrade (RHEL-based) + ansible.builtin.command: yum check-update + register: yum_upgrade_check + changed_when: false + failed_when: > + yum_upgrade_check.stdout is search('^([a-zA-Z0-9_.+-]+)\s+([0-9][a-zA-Z0-9_:\-\.\+~]*)[^\r\n]*') + and not (yum_upgrade_check.stdout | trim is search('Last metadata expiration')) + when: ansible_os_family == "RedHat" + + - name: Debug the result (Debian-based) + ansible.builtin.debug: + msg: "APT check output: {{ apt_upgrade_check.stdout }}" + when: ansible_os_family == "Debian" + + - name: Debug the result (RHEL-based) + ansible.builtin.debug: + msg: "YUM check output: {{ yum_upgrade_check.stdout }}" + when: ansible_os_family == "RedHat" From e074c1b4dc646f0fc3896f20c6ee618c94e7b148 Mon Sep 17 00:00:00 2001 From: SquirrelDevelopper Date: Tue, 15 Oct 2024 16:50:07 +0200 Subject: [PATCH 2/2] Add dependency step and requirements file to upgrade tests Introduce a dependency step in the molecule test sequence to ensure all dependencies are met before running tests. Also, add a requirements.yml file specifying required Ansible collections for the upgrade packages scenario. --- server/src/tests/molecule/upgrade-packages/molecule.yml | 1 + server/src/tests/molecule/upgrade-packages/requirements.yml | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 server/src/tests/molecule/upgrade-packages/requirements.yml diff --git a/server/src/tests/molecule/upgrade-packages/molecule.yml b/server/src/tests/molecule/upgrade-packages/molecule.yml index 6519b8f3..e6877dba 100644 --- a/server/src/tests/molecule/upgrade-packages/molecule.yml +++ b/server/src/tests/molecule/upgrade-packages/molecule.yml @@ -19,6 +19,7 @@ provisioner: scenario: test_sequence: + - dependency - destroy - create - prepare diff --git a/server/src/tests/molecule/upgrade-packages/requirements.yml b/server/src/tests/molecule/upgrade-packages/requirements.yml new file mode 100644 index 00000000..a13b2cbf --- /dev/null +++ b/server/src/tests/molecule/upgrade-packages/requirements.yml @@ -0,0 +1,6 @@ +collections: + - name: community.docker + version: ">=3.10.2" + - name: community.general + version: ">=7.0.0" + source: https://galaxy.ansible.com