From 925f4a85188d40e45439ad6bb6869143ae1922f2 Mon Sep 17 00:00:00 2001 From: 100gr Date: Sat, 2 Sep 2023 10:36:49 +0100 Subject: [PATCH 1/3] Missing RHEL distribution in package module Missing RHEL distribution in package module --- testinfra/modules/package.py | 1 + testinfra/modules/package.py.bak | 216 +++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 testinfra/modules/package.py.bak diff --git a/testinfra/modules/package.py b/testinfra/modules/package.py index 9eb19759..2c015595 100644 --- a/testinfra/modules/package.py +++ b/testinfra/modules/package.py @@ -80,6 +80,7 @@ def get_module_class(cls, host): "opensuse-leap", "opensuse-tumbleweed", "rocky", + "rhel", ) ): return RpmPackage diff --git a/testinfra/modules/package.py.bak b/testinfra/modules/package.py.bak new file mode 100644 index 00000000..9eb19759 --- /dev/null +++ b/testinfra/modules/package.py.bak @@ -0,0 +1,216 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from testinfra.modules.base import Module + + +class Package(Module): + """Test packages status and version""" + + def __init__(self, name): + self.name = name + super().__init__() + + @property + def is_installed(self): + """Test if the package is installed + + >>> host.package("nginx").is_installed + True + + Supported package systems: + + - apk (Alpine) + - apt (Debian, Ubuntu, ...) + - pacman (Arch, Manjaro ) + - pkg (FreeBSD) + - pkg_info (NetBSD) + - pkg_info (OpenBSD) + - rpm (RHEL, RockyLinux, Fedora, ...) + """ + raise NotImplementedError + + @property + def release(self): + """Return the release specific info from the package version + + >>> host.package("nginx").release + '1.el6' + """ + raise NotImplementedError + + @property + def version(self): + """Return package version as returned by the package system + + >>> host.package("nginx").version + '1.2.1-2.2+wheezy3' + """ + raise NotImplementedError + + def __repr__(self): + return "".format(self.name) + + @classmethod + def get_module_class(cls, host): + if host.system_info.type == "windows": + return ChocolateyPackage + if host.system_info.type == "freebsd": + return FreeBSDPackage + if host.system_info.type in ("openbsd", "netbsd"): + return OpenBSDPackage + if host.system_info.distribution in ("debian", "ubuntu"): + return DebianPackage + if host.system_info.distribution and ( + host.system_info.distribution.lower() + in ( + "almalinux", + "centos", + "cloudlinux", + "fedora", + "opensuse-leap", + "opensuse-tumbleweed", + "rocky", + ) + ): + return RpmPackage + if host.system_info.distribution in ("arch", "manjarolinux"): + return ArchPackage + if host.exists("apk"): + return AlpinePackage + # Fallback conditions + if host.exists("dpkg-query"): + return DebianPackage + if host.exists("rpm"): + return RpmPackage + raise NotImplementedError + + +class DebianPackage(Package): + @property + def is_installed(self): + result = self.run_test("dpkg-query -f '${Status}' -W %s", self.name) + if result.rc == 1: + return False + out = result.stdout.strip().split() + installed_status = ["ok", "installed"] + return out[0] in ["install", "hold"] and out[1:3] == installed_status + + @property + def release(self): + raise NotImplementedError + + @property + def version(self): + out = self.check_output("dpkg-query -f '${Status} ${Version}' -W %s", self.name) + splitted = out.split() + assert splitted[0].lower() in ( + "install", + "hold", + ), "The package {} is not installed, dpkg-query output: {}".format( + self.name, out + ) + return splitted[3] + + +class FreeBSDPackage(Package): + @property + def is_installed(self): + EX_UNAVAILABLE = 69 + return ( + self.run_expect([0, EX_UNAVAILABLE], "pkg query %%n %s", self.name).rc == 0 + ) + + @property + def release(self): + raise NotImplementedError + + @property + def version(self): + return self.check_output("pkg query %%v %s", self.name) + + +class OpenBSDPackage(Package): + @property + def is_installed(self): + return self.run_test("pkg_info -e %s", "{}-*".format(self.name)).rc == 0 + + @property + def release(self): + raise NotImplementedError + + @property + def version(self): + out = self.check_output("pkg_info -e %s", "{}-*".format(self.name)) + # OpenBSD: inst:zsh-5.0.5p0 + # NetBSD: zsh-5.0.7nb1 + return out.split(self.name + "-", 1)[1] + + +class RpmPackage(Package): + @property + def is_installed(self): + return self.run_test("rpm -q %s", self.name).rc == 0 + + @property + def version(self): + return self.check_output('rpm -q --queryformat="%%{VERSION}" %s', self.name) + + @property + def release(self): + return self.check_output('rpm -q --queryformat="%%{RELEASE}" %s', self.name) + + +class AlpinePackage(Package): + @property + def is_installed(self): + return self.run_test("apk -e info %s", self.name).rc == 0 + + @property + def version(self): + out = self.check_output("apk -e -v info %s", self.name).split("-") + return out[-2] + + @property + def release(self): + out = self.check_output("apk -e -v info %s", self.name).split("-") + return out[-1] + + +class ArchPackage(Package): + @property + def is_installed(self): + return self.run_test("pacman -Q %s", self.name).rc == 0 + + @property + def version(self): + out = self.check_output("pacman -Q %s", self.name).split(" ") + return out[1] + + @property + def release(self): + raise NotImplementedError + + +class ChocolateyPackage(Package): + @property + def is_installed(self): + return self.run_test("choco info -lo %s", self.name).rc == 0 + + @property + def version(self): + _, version = self.check_output("choco info -lo %s -r", self.name).split("|", 1) + return version + + @property + def release(self): + raise NotImplementedError From 4f714b4c8a6611c160291ec7e3959c25a5b3a6f1 Mon Sep 17 00:00:00 2001 From: 700grm <42538950+700grm@users.noreply.github.com> Date: Sat, 2 Sep 2023 10:44:05 +0100 Subject: [PATCH 2/3] Delete testinfra/modules/package.py.bak --- testinfra/modules/package.py.bak | 216 ------------------------------- 1 file changed, 216 deletions(-) delete mode 100644 testinfra/modules/package.py.bak diff --git a/testinfra/modules/package.py.bak b/testinfra/modules/package.py.bak deleted file mode 100644 index 9eb19759..00000000 --- a/testinfra/modules/package.py.bak +++ /dev/null @@ -1,216 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from testinfra.modules.base import Module - - -class Package(Module): - """Test packages status and version""" - - def __init__(self, name): - self.name = name - super().__init__() - - @property - def is_installed(self): - """Test if the package is installed - - >>> host.package("nginx").is_installed - True - - Supported package systems: - - - apk (Alpine) - - apt (Debian, Ubuntu, ...) - - pacman (Arch, Manjaro ) - - pkg (FreeBSD) - - pkg_info (NetBSD) - - pkg_info (OpenBSD) - - rpm (RHEL, RockyLinux, Fedora, ...) - """ - raise NotImplementedError - - @property - def release(self): - """Return the release specific info from the package version - - >>> host.package("nginx").release - '1.el6' - """ - raise NotImplementedError - - @property - def version(self): - """Return package version as returned by the package system - - >>> host.package("nginx").version - '1.2.1-2.2+wheezy3' - """ - raise NotImplementedError - - def __repr__(self): - return "".format(self.name) - - @classmethod - def get_module_class(cls, host): - if host.system_info.type == "windows": - return ChocolateyPackage - if host.system_info.type == "freebsd": - return FreeBSDPackage - if host.system_info.type in ("openbsd", "netbsd"): - return OpenBSDPackage - if host.system_info.distribution in ("debian", "ubuntu"): - return DebianPackage - if host.system_info.distribution and ( - host.system_info.distribution.lower() - in ( - "almalinux", - "centos", - "cloudlinux", - "fedora", - "opensuse-leap", - "opensuse-tumbleweed", - "rocky", - ) - ): - return RpmPackage - if host.system_info.distribution in ("arch", "manjarolinux"): - return ArchPackage - if host.exists("apk"): - return AlpinePackage - # Fallback conditions - if host.exists("dpkg-query"): - return DebianPackage - if host.exists("rpm"): - return RpmPackage - raise NotImplementedError - - -class DebianPackage(Package): - @property - def is_installed(self): - result = self.run_test("dpkg-query -f '${Status}' -W %s", self.name) - if result.rc == 1: - return False - out = result.stdout.strip().split() - installed_status = ["ok", "installed"] - return out[0] in ["install", "hold"] and out[1:3] == installed_status - - @property - def release(self): - raise NotImplementedError - - @property - def version(self): - out = self.check_output("dpkg-query -f '${Status} ${Version}' -W %s", self.name) - splitted = out.split() - assert splitted[0].lower() in ( - "install", - "hold", - ), "The package {} is not installed, dpkg-query output: {}".format( - self.name, out - ) - return splitted[3] - - -class FreeBSDPackage(Package): - @property - def is_installed(self): - EX_UNAVAILABLE = 69 - return ( - self.run_expect([0, EX_UNAVAILABLE], "pkg query %%n %s", self.name).rc == 0 - ) - - @property - def release(self): - raise NotImplementedError - - @property - def version(self): - return self.check_output("pkg query %%v %s", self.name) - - -class OpenBSDPackage(Package): - @property - def is_installed(self): - return self.run_test("pkg_info -e %s", "{}-*".format(self.name)).rc == 0 - - @property - def release(self): - raise NotImplementedError - - @property - def version(self): - out = self.check_output("pkg_info -e %s", "{}-*".format(self.name)) - # OpenBSD: inst:zsh-5.0.5p0 - # NetBSD: zsh-5.0.7nb1 - return out.split(self.name + "-", 1)[1] - - -class RpmPackage(Package): - @property - def is_installed(self): - return self.run_test("rpm -q %s", self.name).rc == 0 - - @property - def version(self): - return self.check_output('rpm -q --queryformat="%%{VERSION}" %s', self.name) - - @property - def release(self): - return self.check_output('rpm -q --queryformat="%%{RELEASE}" %s', self.name) - - -class AlpinePackage(Package): - @property - def is_installed(self): - return self.run_test("apk -e info %s", self.name).rc == 0 - - @property - def version(self): - out = self.check_output("apk -e -v info %s", self.name).split("-") - return out[-2] - - @property - def release(self): - out = self.check_output("apk -e -v info %s", self.name).split("-") - return out[-1] - - -class ArchPackage(Package): - @property - def is_installed(self): - return self.run_test("pacman -Q %s", self.name).rc == 0 - - @property - def version(self): - out = self.check_output("pacman -Q %s", self.name).split(" ") - return out[1] - - @property - def release(self): - raise NotImplementedError - - -class ChocolateyPackage(Package): - @property - def is_installed(self): - return self.run_test("choco info -lo %s", self.name).rc == 0 - - @property - def version(self): - _, version = self.check_output("choco info -lo %s -r", self.name).split("|", 1) - return version - - @property - def release(self): - raise NotImplementedError From 586a87eb3cdb4495454a693cbdc8534d6e5942f6 Mon Sep 17 00:00:00 2001 From: 100gr <42538950+700grm@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:38:09 +0100 Subject: [PATCH 3/3] Updated order in packages Added "ol" Oracle Linux and moved "rhel" accordingly --- testinfra/modules/package.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/testinfra/modules/package.py b/testinfra/modules/package.py index 2c015595..ed113d81 100644 --- a/testinfra/modules/package.py +++ b/testinfra/modules/package.py @@ -77,10 +77,11 @@ def get_module_class(cls, host): "centos", "cloudlinux", "fedora", + "ol", "opensuse-leap", "opensuse-tumbleweed", - "rocky", "rhel", + "rocky", ) ): return RpmPackage