Skip to content

Commit

Permalink
Add unit test for package manager installing a file system path
Browse files Browse the repository at this point in the history
  • Loading branch information
happz authored and psss committed Apr 10, 2024
1 parent c9f5f0e commit ed099b9
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 1 deletion.
131 changes: 131 additions & 0 deletions tests/unit/test_package_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import tmt.log
import tmt.package_managers
import tmt.package_managers.apk
import tmt.package_managers.apt
import tmt.package_managers.dnf
import tmt.package_managers.rpm_ostree
Expand Down Expand Up @@ -1086,3 +1087,133 @@ def test_check_presence(

if expected_stderr:
assert_log(caplog, remove_colors=True, message=MATCH(expected_stderr))


def _parametrize_test_install_filesystempath() -> Iterator[
tuple[Container, PackageManagerClass, FileSystemPath, Optional[str], Optional[str]]]:

for container, package_manager_class in CONTAINER_BASE_MATRIX:
if package_manager_class is tmt.package_managers.dnf.Dnf5:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| dnf5 install -y /usr/bin/dos2unix", \
'[1/1] dos2unix', \
None # noqa: E501

elif package_manager_class is tmt.package_managers.dnf.Dnf:
if 'centos:stream8' in container.url or 'fedora:rawhide' in container.url:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| dnf install -y /usr/bin/dos2unix", \
'Installed:\n dos2unix-', \
None # noqa: E501

else:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| dnf install -y /usr/bin/dos2unix", \
'Installed:\n dos2unix-', \
None # noqa: E501

elif package_manager_class is tmt.package_managers.dnf.Yum:
if 'centos:stream8' in container.url:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| yum install -y /usr/bin/dos2unix && rpm -q --whatprovides /usr/bin/dos2unix", \
'Installed:\n dos2unix-', \
None # noqa: E501

elif 'centos:7' in container.url:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| yum install -y /usr/bin/dos2unix && rpm -q --whatprovides /usr/bin/dos2unix", \
'Installed:\n dos2unix.', \
None # noqa: E501

elif 'fedora:rawhide' in container.url:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| yum install -y /usr/bin/dos2unix && rpm -q --whatprovides /usr/bin/dos2unix", \
'Installed:\n dos2unix-', \
None # noqa: E501

else:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -q --whatprovides /usr/bin/dos2unix \|\| yum install -y /usr/bin/dos2unix && rpm -q --whatprovides /usr/bin/dos2unix", \
'Installed:\n dos2unix-', \
None # noqa: E501

elif package_manager_class is tmt.package_managers.apt.Apt:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"export DEBIAN_FRONTEND=noninteractive; dpkg-query --show dos2unix \|\| apt install -y dos2unix", \
"Setting up dos2unix", \
None # noqa: E501

elif package_manager_class is tmt.package_managers.rpm_ostree.RpmOstree:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"rpm -qf /usr/bin/dos2unix \|\| rpm-ostree install --apply-live --idempotent --allow-inactive /usr/bin/dos2unix", \
"Installing 1 packages:\n dos2unix-", \
None # noqa: E501

elif package_manager_class is tmt.package_managers.apk.Apk:
yield container, \
package_manager_class, \
FileSystemPath('/usr/bin/dos2unix'), \
r"apk info -e dos2unix \|\| apk add dos2unix", \
'Installing dos2unix', \
None

else:
pytest.fail(f"Unhandled package manager class '{package_manager_class}'.")


@pytest.mark.containers()
@pytest.mark.parametrize(('container_per_test',
'package_manager_class',
'installable',
'expected_command',
'expected_stdout',
'expected_stderr'),
list(_parametrize_test_install_filesystempath()),
indirect=["container_per_test"],
ids=CONTAINER_MATRIX_IDS)
def test_install_filesystempath(
container_per_test: ContainerData,
guest_per_test: GuestContainer,
package_manager_class: PackageManagerClass,
installable: FileSystemPath,
expected_command: str,
expected_stdout: Optional[str],
expected_stderr: Optional[str],
root_logger: tmt.log.Logger,
caplog: _pytest.logging.LogCaptureFixture) -> None:
package_manager = create_package_manager(
container_per_test,
guest_per_test,
package_manager_class,
root_logger)

output = package_manager.install(installable)

assert_log(caplog, message=MATCH(
rf"Run command: podman exec .+? /bin/bash -c '{expected_command}'"))

if expected_stdout:
assert output.stdout is not None
assert expected_stdout in output.stdout

if expected_stderr:
assert output.stderr is not None
assert expected_stderr in output.stderr
5 changes: 4 additions & 1 deletion tmt/package_managers/apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@

PACKAGE_PATH: dict[FileSystemPath, str] = {
FileSystemPath('/usr/bin/arch'): 'busybox',
FileSystemPath('/usr/bin/flock'): 'flock'
FileSystemPath('/usr/bin/flock'): 'flock',
# Note: not used for anything serious, serves for unit tests as
# an installable path.
FileSystemPath('/usr/bin/dos2unix'): 'dos2unix'
}


Expand Down

0 comments on commit ed099b9

Please sign in to comment.