Skip to content

Commit 7669749

Browse files
guimalufphilpep
andauthored
Fix module class for RPM and Debian distributions #570 (#575)
* Fix module class for RPM and Debian distributions #570 A CentOS system with dpkg package installed fails all packages is_installed checks. This happens because the decision to use module class is based on the presence of `dpkg-query` binary. Co-authored-by: Philippe Pepiot <[email protected]>
1 parent 205d208 commit 7669749

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

test/test_modules.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
)
3232
])
3333

34+
centos_images = pytest.mark.testinfra_hosts(*[
35+
"docker://{}".format(image)
36+
for image in (
37+
"centos_6", "centos_7",
38+
)
39+
])
40+
3441

3542
@all_images
3643
def test_package(host, docker_image):
@@ -72,6 +79,14 @@ def test_held_package(host):
7279
assert python.version.startswith("2.7.")
7380

7481

82+
@pytest.mark.destructive
83+
@centos_images
84+
def test_non_default_package_tool(host):
85+
# Make non default pkg tool binary present
86+
host.run("install -m a+rx /bin/true /usr/bin/dpkg-query")
87+
assert host.package("openssh").is_installed
88+
89+
7590
@pytest.mark.destructive
7691
def test_uninstalled_package_version(host):
7792
with pytest.raises(AssertionError) as excinfo:

testinfra/modules/package.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ def get_module_class(cls, host):
6868
return FreeBSDPackage
6969
if host.system_info.type in ("openbsd", "netbsd"):
7070
return OpenBSDPackage
71-
if host.exists("dpkg-query"):
71+
if host.system_info.distribution in ("debian", "ubuntu"):
7272
return DebianPackage
73-
if host.exists("rpm"):
73+
if (
74+
host.system_info.distribution
75+
and host.system_info.distribution.lower() == "centos"
76+
):
7477
return RpmPackage
75-
if host.exists("apk"):
76-
return AlpinePackage
7778
if host.system_info.distribution == "arch":
7879
return ArchPackage
80+
if host.exists("apk"):
81+
return AlpinePackage
82+
# Fallback conditions
83+
if host.exists("dpkg-query"):
84+
return DebianPackage
85+
if host.exists("rpm"):
86+
return RpmPackage
7987
raise NotImplementedError
8088

8189

0 commit comments

Comments
 (0)