Skip to content

Commit

Permalink
Merge pull request #62019 from meaksh/master-fix-issue-targeting-dupl…
Browse files Browse the repository at this point in the history
…icated-package-names

Make Salt to return an error on "pkg" modules and states when targeting duplicated package names
  • Loading branch information
Megan Wilhite authored Nov 16, 2022
2 parents 239bb06 + 43398ae commit c678a1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/62019.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make Salt to return an error on "pkg" modules and states when targeting duplicated package names
13 changes: 12 additions & 1 deletion salt/modules/pkg_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ def _repack_pkgs(pkgs, normalize=True):
_normalize_name = __salt__["pkg.normalize_name"]
else:
_normalize_name = lambda pkgname: pkgname
return {

repacked_pkgs = {
_normalize_name(str(x)): str(y) if y is not None else y
for x, y in salt.utils.data.repack_dictlist(pkgs).items()
}

# Check if there were collisions in names
if len(pkgs) != len(repacked_pkgs):
raise SaltInvocationError(
"You are passing a list of packages that contains duplicated packages names: {}. This cannot be processed. In case you are targeting different versions of the same package, please target them individually".format(
pkgs
)
)

return repacked_pkgs


def pack_sources(sources, normalize=True):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/pytests/unit/modules/test_pkg_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import salt.modules.pkg_resource as pkg_resource
import salt.utils.data
import salt.utils.yaml
from salt.exceptions import SaltInvocationError
from tests.support.mock import MagicMock, patch


Expand Down Expand Up @@ -224,6 +225,17 @@ def test_format_pkg_list_with_attr():
assert sorted(pkgs) == sorted(expected_pkg_list)


def test_repack_pkgs():
"""
Test to check that repack function is raising error in case of
package name collisions
"""
assert pkg_resource._repack_pkgs([{"A": "a"}])
assert pkg_resource._repack_pkgs([{"A": "a"}, {"B": "b"}])
with pytest.raises(SaltInvocationError):
assert pkg_resource._repack_pkgs([{"A": "a"}, {"A": "c"}])


def test_stringify():
"""
Test to takes a dict of package name/version information
Expand Down

0 comments on commit c678a1b

Please sign in to comment.