From ed8a9e4e6a79285f9e7c50e86c3835ffe2f0fd9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Cami?= Date: Thu, 10 Dec 2020 08:35:12 +0200 Subject: [PATCH] ipatests: tasks: handle uninstalling packages with nodeps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Handle package removal without taking dependencies into account. E.g. add frontends for rpm -e --nodeps. Related: ipatests/pytest_ipa/integration/tasks.py Signed-off-by: François Cami Reviewed-By: Alexander Bokovoy Reviewed-By: Armando Neto Reviewed-By: Michal Polovka --- ipatests/pytest_ipa/integration/tasks.py | 51 +++++++++++++++++++----- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/ipatests/pytest_ipa/integration/tasks.py b/ipatests/pytest_ipa/integration/tasks.py index b91859816..2fe78367f 100755 --- a/ipatests/pytest_ipa/integration/tasks.py +++ b/ipatests/pytest_ipa/integration/tasks.py @@ -29,6 +29,7 @@ import re import collections import itertools import shutil +import shlex import copy import subprocess import tempfile @@ -2381,20 +2382,33 @@ def download_packages(host, pkgs): return tmpdir -def uninstall_packages(host, pkgs): +def uninstall_packages(host, pkgs, nodeps=False): """Uninstall packages on a remote host. - :param host: the host where the uninstallation takes place - :param pkgs: packages to uninstall, provided as a list of strings + :param host: the host where the uninstallation takes place. + :param pkgs: packages to uninstall, provided as a list of strings. + :param nodeps: ignore dependencies (dangerous!). """ platform = get_platform(host) - # Only supports RHEL 8+ and Fedora for now - if platform in ('rhel', 'fedora'): - install_cmd = ['/usr/bin/dnf', 'remove', '-y'] - elif platform in ('ubuntu'): - install_cmd = ['apt-get', 'remove', '-y'] + if platform not in ('rhel', 'fedora', 'ubuntu'): + raise ValueError('uninstall_packages: unknown platform %s' % platform) + if nodeps: + if platform in ('rhel', 'fedora'): + cmd = "rpm -e --nodeps" + elif platform in ('ubuntu'): + cmd = "dpkg -P --force-depends" + for package in pkgs: + uninstall_cmd = shlex.split(cmd) + uninstall_cmd.append(package) + # keep raiseonerr=True here. --fcami + host.run_command(uninstall_cmd) else: - raise ValueError('install_packages: unknown platform %s' % platform) - host.run_command(install_cmd + pkgs, raiseonerr=False) + if platform in ('rhel', 'fedora'): + cmd = "/usr/bin/dnf remove -y" + elif platform in ('ubuntu'): + cmd = "apt-get remove -y" + uninstall_cmd = shlex.split(cmd) + uninstall_cmd.extend(pkgs) + host.run_command(uninstall_cmd, raiseonerr=False) def wait_for_request(host, request_id, timeout=120): @@ -2649,3 +2663,20 @@ def run_ssh_cmd( assert "Authentication succeeded" not in stderr assert "No more authentication methods to try." in stderr return (return_code, stdout, stderr) + + +def is_package_installed(host, pkg): + platform = get_platform(host) + if platform in ('rhel', 'fedora'): + result = host.run_command( + ['rpm', '-q', pkg], raiseonerr=False + ) + elif platform in ['ubuntu']: + result = host.run_command( + ['dpkg', '-s', pkg], raiseonerr=False + ) + else: + raise ValueError( + 'is_package_installed: unknown platform %s' % platform + ) + return result.returncode == 0