Skip to content

Improved RpmPackage is_installed property logic. #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import crypt
import datetime
import os
from unittest import mock
import re
import time
from ipaddress import IPv4Address, IPv6Address, ip_address
Expand Down Expand Up @@ -58,6 +59,19 @@ def test_held_package(host):
assert python.is_installed
assert python.version.startswith("3.11.")

@pytest.mark.testinfra_hosts("docker://rockylinux9")
def test_rpmdb_corrupted(host):
host.check_output("mv /var/lib/rpm/rpmdb.sqlite /var/lib/rpm/rpmdb.sqlite.bck")
try:
host.check_output("dd if=/dev/zero of=/var/lib/rpm/rpmdb.sqlite bs=1024 count=1")
with pytest.raises(RuntimeError) as excinfo:
host.package("zsh").is_installed
assert (
f"Could not check if RPM package 'zsh' is installed. "
"error: sqlite failure:"
) in str(excinfo.value)
finally:
host.check_output("mv /var/lib/rpm/rpmdb.sqlite.bck /var/lib/rpm/rpmdb.sqlite")

@pytest.mark.testinfra_hosts("docker://rockylinux9")
def test_non_default_package_tool(host):
Expand Down
8 changes: 7 additions & 1 deletion testinfra/modules/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,13 @@ def version(self):
class RpmPackage(Package):
@property
def is_installed(self):
return self.run_test("rpm -q %s", self.name).rc == 0
result = self.run_test("rpm -q --quiet %s 2>&1", self.name)
if result.succeeded:
return True
elif result.failed and result.stdout == '':
Copy link
Contributor Author

@narmaku narmaku May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: If return code is 1 but there is no output/error, it means the package is not installed
(for that we used --quiet option).

return False
else:
raise RuntimeError(f"Could not check if RPM package '{self.name}' is installed. {result.stdout}")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: If return code is 1 and there is stdout, that means there was an unexpected error, so we throw a RuntimeError here. (as we use --quiet there should not be any output when failing).


@property
def version(self):
Expand Down
Loading