Skip to content

Commit b892d88

Browse files
narmakuphilpep
authored andcommitted
Simplified test case.
Also improved package check logic to be locales agnostic.
1 parent fe48176 commit b892d88

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

test/test_modules.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,16 @@ def test_held_package(host):
5858
assert python.is_installed
5959
assert python.version.startswith("3.11.")
6060

61-
@pytest.mark.destructive
6261
@pytest.mark.testinfra_hosts("docker://rockylinux9")
63-
def test_rpm_package_broken_db(host):
64-
with host.sudo():
65-
# Corrupt RPM database so that we make RpmPackage.is_installed throw an exception
66-
host.run_test('rpm --rebuilddb')
67-
host.run_test('dd if=/dev/zero of=$(rpm --dbpath)/Packages bs=1024 count=1024 seek=1024')
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"
6865
with pytest.raises(RuntimeError) as excinfo:
69-
host.package("zsh").is_installed
66+
host.package(pkg_name).is_installed
7067
assert (
71-
"DB_PAGE_NOT_FOUND"
72-
in str(excinfo.value)
73-
)
68+
f"Could not check if RPM package '{pkg_name}' is installed. "
69+
f"rpm: {pkg_name}: unknown option"
70+
) in str(excinfo.value)
7471

7572
@pytest.mark.testinfra_hosts("docker://rockylinux9")
7673
def test_non_default_package_tool(host):

testinfra/modules/package.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,13 @@ def version(self):
165165
class RpmPackage(Package):
166166
@property
167167
def is_installed(self):
168-
result = self.run("rpm -q %s", self.name)
168+
result = self.run("rpm -q --quiet %s 2>&1", self.name)
169169
if result.succeeded:
170170
return True
171-
172-
if "not installed" in result.stdout or not result.stdout.startswith(self.name):
171+
elif result.failed and result.stdout == '':
173172
return False
174-
175-
raise RuntimeError(f"Could not check if RPM package '{self.name}' is installed. {result}")
173+
else:
174+
raise RuntimeError(f"Could not check if RPM package '{self.name}' is installed. {result.stdout}")
176175

177176
@property
178177
def version(self):

0 commit comments

Comments
 (0)