Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rodolfo Olivieri <[email protected]>
  • Loading branch information
r0x0d committed Feb 8, 2023
1 parent b8c1dc8 commit 8508a4a
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 0 deletions.
33 changes: 33 additions & 0 deletions plans/tier1.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,36 @@
- name: reboot after conversion
how: ansible
playbook: tests/ansible_collections/roles/reboot/main.yml

/kernel_boot_files_missing_boot_files:
discover+:
test: checks-after-conversion
prepare+:
- name: main conversion part
how: tmt
test: tests/integration/tier1/kernel-boot-files/test_handle_missing_boot_files.py
- name: reboot after conversion
how: ansible
playbook: tests/ansible_collections/roles/reboot/main.yml

/kernel_boot_files_corrupted_initramfs_file:
discover+:
test: checks-after-conversion
prepare+:
- name: main conversion part
how: tmt
test: tests/integration/tier1/kernel-boot-files/test_handle_corrupted_files.py
- name: reboot after conversion
how: ansible
playbook: tests/ansible_collections/roles/reboot/main.yml

/kernel_boot_files_check_boot_files_presence:
discover+:
test: checks-after-conversion
prepare+:
- name: main conversion part
how: tmt
test: tests/integration/tier1/kernel-boot-files/test_verify_file_presence.py
- name: reboot after conversion
how: ansible
playbook: tests/ansible_collections/roles/reboot/main.yml
15 changes: 15 additions & 0 deletions tests/integration/tier1/kernel-boot-files/main.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
summary: Handle kernel boot files

description: |
Verify that Convert2RHEL can detect and handle the kernel boot files in
case of failure.
Verify that, if present, the kernel boot files are not corrupted.

In any case, the conversion should not be inhibited. An warning will be
shown to the user with instructions on how to fix it.

tier: 1

tag+:
- kernel
- boot
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import shlex
import subprocess

from multiprocessing import Pool

import pytest

from envparse import env


def get_latest_installed_kernel(get_system_release):
"""Utility function to get the latest installed kernel."""

kernel_name = "kernel"
if get_system_release in ("centos-8", "oracle-8"):
kernel_name = "kernel-core"

output = subprocess.check_output(["rpm", "-q", "--last", kernel_name])
latest_installed_kernel = output.split("\n", maxsplit=1)[0].split(" ")[0]
latest_installed_kernel = latest_installed_kernel.split("%s-" % kernel_name)[-1]
return output.output.strip()


def corrupt_initramfs_file():
"""Utility function to corrupt the initramfs file."""
latest_installed_kernel = get_latest_installed_kernel()

while True:
if os.path.exists("/boot/initramfs-%s.img" % latest_installed_kernel):
cmd = "echo 'corrupted' > /boot/initramfs-%s.img" % latest_installed_kernel
subprocess.run(shlex.split(shlex.quote(cmd)), shell=True, check=True)
return


@pytest.mark.corrupted_initramfs_file
def test_corrupted_initramfs_file(convert2rhel):
"""
Verify if an output with a warning message is sent to the user in case of a
corrupted initramfs file.
This case can happen when the transaction ran successfully, but, for lack of
disk space in the /boot partition, the kernel scriptlet will fail to copy
the uncompressed initramfs file to /boot/initramfs-*.img, thus, leaving the
file in an partial state and corrupted.
Since this could be a real scenario, we prepared this test to assert that,
if it happens, Convert2RHEL can detect that partial file there and instruct
the user on how to fix the problem.
.. note::
@lnykryn made an reproducer for the `cp` issue that can be seen here: https://gist.github.com/r0x0d/5d6a93c5827bd365e934f3d612fdafae
"""
with convert2rhel(
"-y --no-rpm-va --serverurl {} --username {} --password {} --pool {} --debug".format(
env.str("RHSM_SERVER_URL"),
env.str("RHSM_USERNAME"),
env.str("RHSM_PASSWORD"),
env.str("RHSM_POOL"),
)
) as c2r:
# Start the watcher as soon as we hit this message.
c2r.expect("Convert: Replace system packages", timeout=600) == 0

with Pool(processes=1) as pool:
watcher = pool.apply_async(corrupt_initramfs_file, ())
assert watcher.successful() is True

assert c2r.expect("Couldn't verify the kernel boot files in the boot partition.", timeout=600) == 0

assert c2r.exitstatus == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import shlex
import subprocess

from multiprocessing import Pool

import pytest

from envparse import env


def get_booted_kernel_version():
"""Utility function to get the booted kernel version."""
kernel_version = subprocess.check_output(["uname", "-r"])
return kernel_version.decode("utf-8").strip()


def fill_disk_space():
"""
Utility function to fill-up disk space when a certain file is not present anymore.
"""
kernel_version = get_booted_kernel_version()

while True:
if not os.path.exists("/boot/initramfs-%s.img" % kernel_version):
cmd = "yes `dd if=/dev/urandom count=1 bs=1M|base64` > /boot/test"
process = subprocess.run(
shlex.split(cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True
)
if "No space left" in process.stdout.decode():
print("Done! The disk is now out of space.")
return


@pytest.mark.missing_kernel_boot_files
def test_missing_kernel_boot_files(convert2rhel):
"""
Verify if an output with a warning message is sent to the user in case of
the tool can't detect the initramfs and vmlinuz files in /boot.
This case can happen if the kernel scriptlet fails during the yum/dnf
transaction where Convert2RHEL tries to replace the packages. Even thought
the scriptlet can fail, the transaction will still continue and the
workflow will continue to be executed. The problem is that, with a
scriptlet failure when replacing/installing a kernel, the initramfs and
vmlinuz could not be available in the /boot partition, especially if there
is no sufficient disk space available in there. This test has the intention
to verify that the a warning with the correct steps are provided to the
user in order to overcome this case and fix it for them.
"""

with convert2rhel(
"-y --no-rpm-va --serverurl {} --username {} --password {} --pool {} --debug".format(
env.str("RHSM_SERVER_URL"),
env.str("RHSM_USERNAME"),
env.str("RHSM_PASSWORD"),
env.str("RHSM_POOL"),
)
) as c2r:
c2r.expect("Convert: Replace system packages", timeout=600) == 0
with Pool(processes=1) as pool:
watcher = pool.apply_async(fill_disk_space, ())
assert watcher.successful() is True

assert c2r.expect("Couldn't verify the kernel boot files in the boot partition.", timeout=600) == 0

assert c2r.exitstatus == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from envparse import env


@pytest.mark.check_boot_files_presence
def test_check_boot_files_presence(convert2rhel):
"""
Verify that the conversion was successfull and the kernel boot files are
present.
Our criteria of a successfull conversion, in this case, is related to the
`check_kernel_boot_files()` finding the two necessary boot files (initramfs
and vmlinuz), and validating that the initramfs file is not corrupted.
"""

with convert2rhel(
"-y --no-rpm-va --serverurl {} --username {} --password {} --pool {} --debug".format(
env.str("RHSM_SERVER_URL"),
env.str("RHSM_USERNAME"),
env.str("RHSM_PASSWORD"),
env.str("RHSM_POOL"),
)
) as c2r:
assert c2r.expect("Checking if initiramfs file exists on the system.") == 0
assert c2r.expect("Checking if vmlinuz file exists on the system.") == 0
assert c2r.expect("Initramfs and vmlinuz files exists and are valid.") == 0

assert c2r.exitstatus == 0

0 comments on commit 8508a4a

Please sign in to comment.