Merge pull request #62019 from meaksh/master-fix-issue-targeting-duplicated-package-names

Make Salt to return an error on "pkg" modules and states when targeting duplicated package names
This commit is contained in:
Megan Wilhite 2022-11-16 12:36:07 -07:00 committed by GitHub
commit c678a1bb85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

1
changelog/62019.fixed Normal file
View file

@ -0,0 +1 @@
Make Salt to return an error on "pkg" modules and states when targeting duplicated package names

View file

@ -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):
"""

View file

@ -8,6 +8,7 @@ import yaml
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
@ -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