diff --git a/test/test_modules.py b/test/test_modules.py index 3106479f..23a1f2f2 100644 --- a/test/test_modules.py +++ b/test/test_modules.py @@ -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 diff --git a/testinfra/modules/package.py b/testinfra/modules/package.py index 39ef248d..f9f93515 100644 --- a/testinfra/modules/package.py +++ b/testinfra/modules/package.py @@ -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):