Skip to content

Commit 35d6dec

Browse files
narmakuphilpep
andauthored
Handle is_installed for rpm package when rpm database is corrupted
When rpm database is corrupted `rpm -q $pkg` return exit code 1 and testinfra was reporting the package as not installed. Use --quiet option so the "package $pkg is (not) installed" is not printed and in case of corrupted database error will display to stdout even with --quiet. Closes #758 Co-authored-by: Philippe Pepiot <[email protected]>
1 parent 8155242 commit 35d6dec

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

test/test_modules.py

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ def test_held_package(host):
5959
assert python.version.startswith("3.11.")
6060

6161

62+
@pytest.mark.destructive
63+
@pytest.mark.testinfra_hosts("docker://rockylinux9")
64+
def test_rpmdb_corrupted(host):
65+
host.check_output("dd if=/dev/zero of=/var/lib/rpm/rpmdb.sqlite bs=1024 count=1")
66+
with pytest.raises(RuntimeError) as excinfo:
67+
host.package("zsh").is_installed
68+
assert (
69+
"Could not check if RPM package 'zsh' is installed. error: sqlite failure:"
70+
) in str(excinfo.value)
71+
72+
6273
@pytest.mark.testinfra_hosts("docker://rockylinux9")
6374
def test_non_default_package_tool(host):
6475
# Make non default pkg tool binary present

testinfra/modules/package.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ def version(self):
165165
class RpmPackage(Package):
166166
@property
167167
def is_installed(self):
168-
return self.run_test("rpm -q %s", self.name).rc == 0
168+
result = self.run_test("rpm -q --quiet %s 2>&1", self.name)
169+
if result.succeeded:
170+
return True
171+
elif result.failed and result.stdout == "":
172+
return False
173+
else:
174+
raise RuntimeError(
175+
f"Could not check if RPM package '{self.name}' is installed. {result.stdout}"
176+
)
169177

170178
@property
171179
def version(self):

0 commit comments

Comments
 (0)