Skip to content

Commit

Permalink
Add support for installing by origin name on FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
amendlik committed Jan 3, 2025
1 parent 097db55 commit c7b6784
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/67126.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed pkg.install in test mode would not detect FreeBSD packages installed by their origin name
10 changes: 9 additions & 1 deletion salt/states/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,15 @@ def _find_install_targets(
warnings = []
failed_verify = False
for package_name, version_string in desired.items():
cver = cur_pkgs.get(package_name, [])

# FreeBSD pkg supports `openjdk` and `java/openjdk7` package names
origin = bool(re.search('/', package_name))

if __grains__['os'] == 'FreeBSD' and origin:
cver = [k for k, v in cur_pkgs.items() if v['origin'] == package_name]
else:
cver = cur_pkgs.get(package_name, [])

if resolve_capabilities and not cver and package_name in cur_prov:
cver = cur_pkgs.get(cur_prov.get(package_name)[0], [])

Expand Down
52 changes: 52 additions & 0 deletions tests/pytests/unit/states/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import salt.modules.pacmanpkg as pacmanpkg
import salt.modules.pkg_resource as pkg_resource
import salt.modules.yumpkg as yumpkg
import salt.modules.pkgng as pkgng
import salt.states.beacon as beaconstate
import salt.states.pkg as pkg
import salt.utils.state as state_utils
Expand Down Expand Up @@ -48,6 +49,11 @@ def configure_loader_modules(minion_opts):
"__salt__": {},
"__grains__": {"os": "CentOS", "os_family": "RedHat"},
},
pkgng: {
"__salt__": {},
"__grains__": {"os": "FreeBSD", "osarch": "amd64", "osmajorrelease": 14},
"__opts__": minion_opts,
},
yumpkg: {
"__salt__": {},
"__grains__": {"osarch": "x86_64", "osmajorrelease": 7},
Expand Down Expand Up @@ -883,6 +889,52 @@ def test_installed_with_single_normalize():
assert ret["changes"] == expected


def test_installed_with_freebsd_origin():
"""
Test pkg.installed where the package name is specified as an origin name
"""

list_pkgs = MagicMock(
return_value={
"pkga": {
"origin": "test/pkga",
"version": ["1.0.1"],
}
}
)

install_mock = MagicMock(return_value={})

salt_dict = {
"pkg.install": install_mock,
"pkg.list_pkgs": list_pkgs,
"pkg_resource.version_clean": pkg_resource.version_clean,
"pkg_resource.parse_targets": pkg_resource.parse_targets,
"pkg_resource.check_extra_requirements": pkg_resource.check_extra_requirements,
}

with (
patch("salt.modules.pkgng.list_pkgs", list_pkgs),
patch("salt.modules.pkgng.version_cmp", MagicMock(return_value=0)),
patch.dict(pkg.__salt__, salt_dict),
patch.dict(pkg_resource.__salt__, salt_dict),
patch.dict(pkgng.__salt__, salt_dict),
patch.dict(
pkg.__grains__,
{
"os": "FreeBSD",
"osarch": "amd64",
"os_family": "FreeBSD",
"osmajorrelease": 14,
},
),
):

ret = pkg.installed("test/pkga")
install_mock.assert_not_called()
assert ret["result"]


def test_removed_with_single_normalize():
"""
Test pkg.removed with preventing multiple package name normalisation
Expand Down

0 comments on commit c7b6784

Please sign in to comment.