Skip to content

Commit 94cb4d5

Browse files
committed
Use a mock test to check rpmdb corrupted vs package not installed
1 parent b892d88 commit 94cb4d5

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

test/test_modules.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import crypt
1414
import datetime
1515
import os
16+
from unittest import mock
1617
import re
1718
import time
1819
from ipaddress import IPv4Address, IPv6Address, ip_address
@@ -59,15 +60,18 @@ def test_held_package(host):
5960
assert python.version.startswith("3.11.")
6061

6162
@pytest.mark.testinfra_hosts("docker://rockylinux9")
62-
def test_rpm_package_not_installed(host):
63-
# Pass an invalid value as package name, it returns exit code 1
64-
pkg_name = "-3"
65-
with pytest.raises(RuntimeError) as excinfo:
66-
host.package(pkg_name).is_installed
67-
assert (
68-
f"Could not check if RPM package '{pkg_name}' is installed. "
69-
f"rpm: {pkg_name}: unknown option"
70-
) in str(excinfo.value)
63+
def test_rpmdb_corrupted(host):
64+
host.check_output("mv /var/lib/rpm/rpmdb.sqlite /var/lib/rpm/rpmdb.sqlite.bck")
65+
try:
66+
host.check_output("dd if=/dev/zero of=/var/lib/rpm/rpmdb.sqlite bs=1024 count=1")
67+
with pytest.raises(RuntimeError) as excinfo:
68+
host.package("zsh").is_installed
69+
assert (
70+
f"Could not check if RPM package 'zsh' is installed. "
71+
"error: sqlite failure:"
72+
) in str(excinfo.value)
73+
finally:
74+
host.check_output("mv /var/lib/rpm/rpmdb.sqlite.bck /var/lib/rpm/rpmdb.sqlite")
7175

7276
@pytest.mark.testinfra_hosts("docker://rockylinux9")
7377
def test_non_default_package_tool(host):

testinfra/modules/package.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def version(self):
165165
class RpmPackage(Package):
166166
@property
167167
def is_installed(self):
168-
result = self.run("rpm -q --quiet %s 2>&1", self.name)
168+
result = self.run_test("rpm -q --quiet %s 2>&1", self.name)
169169
if result.succeeded:
170170
return True
171171
elif result.failed and result.stdout == '':

0 commit comments

Comments
 (0)