Skip to content

Commit

Permalink
Handle is_installed for rpm package when rpm database is corrupted
Browse files Browse the repository at this point in the history
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 <phil@philpep.org>
  • Loading branch information
narmaku and philpep authored May 26, 2024
1 parent 8155242 commit 35d6dec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ def test_held_package(host):
assert python.version.startswith("3.11.")


@pytest.mark.destructive
@pytest.mark.testinfra_hosts("docker://rockylinux9")
def test_rpmdb_corrupted(host):
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 (
"Could not check if RPM package 'zsh' is installed. error: sqlite failure:"
) in str(excinfo.value)


@pytest.mark.testinfra_hosts("docker://rockylinux9")
def test_non_default_package_tool(host):
# Make non default pkg tool binary present
Expand Down
10 changes: 9 additions & 1 deletion testinfra/modules/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,15 @@ 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 == "":
return False
else:
raise RuntimeError(
f"Could not check if RPM package '{self.name}' is installed. {result.stdout}"
)

@property
def version(self):
Expand Down

0 comments on commit 35d6dec

Please # to comment.