10000 Merge pull request #52221 from garethgreenaway/pip_state_mod_aggregate · perlchild/salt@0384824 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0384824

Browse files
authored
Merge pull request saltstack#52221 from garethgreenaway/pip_state_mod_aggregate
[develop] First pass at adding mod_aggregate support to the pip_state module.
2 parents 2ee8671 + c938413 commit 0384824

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

salt/states/pip_state.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,3 +1065,51 @@ def uptodate(name,
10651065
ret['comment'] = 'Upgrade failed.'
10661066

10671067
return ret
1068+
1069+
1070+
def mod_aggregate(low, chunks, running):
1071+
'''
1072+
The mod_aggregate function which looks up all packages in the available
1073+
low chunks and merges them into a single pkgs ref in the present low data
1074+
'''
1075+
pkgs = []
1076+
pkg_type = None
1077+
agg_enabled = [
1078+
'installed',
1079+
'removed',
1080+
]
1081+
if low.get('fun') not in agg_enabled:
1082+
return low
1083+
for chunk in chunks:
1084+
tag = __utils__['state.gen_tag'](chunk)
1085+
if tag in running:
1086+
# Already ran the pkg state, skip aggregation
1087+
continue
1088+
if chunk.get('state') == 'pip':
1089+
if '__agg__' in chunk:
1090+
continue
1091+
# Check for the same function
1092+
if chunk.get('fun') != low.get('fun'):
1093+
continue
1094+
# Check first if 'sources' was passed so we don't aggregate pkgs
1095+
# and sources together.
1096+
if pkg_type is None:
1097+
pkg_type = 'pkgs'
1098+
if pkg_type == 'pkgs':
1099+
# Pull out the pkg names!
1100+
if 'pkgs' in chunk:
1101+
pkgs.extend(chunk['pkgs'])
1102+
chunk['__agg__'] = True
1103+
elif 'name' in chunk:
1104+
version = chunk.pop('version', None)
1105+
if version is not None:
1106+
pkgs.append({chunk['name']: version})
1107+
else:
1108+
pkgs.append(chunk['name'])
1109+
chunk['__agg__'] = True
1110+
if pkg_type is not None and pkgs:
1111+
if pkg_type in low:
1112+
low[pkg_type].extend(pkgs)
1113+
else:
1114+
low[pkg_type] = pkgs
1115+
return low

tests/unit/states/test_pip_state.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# Import salt libs
1919
import salt.states.pip_state as pip_state
2020
import salt.utils.versions
21+
from salt.utils.odict import OrderedDict
2122

2223
# Import 3rd-party libs
2324
try:
@@ -304,3 +305,45 @@ def test_install_in_editable_mode(self):
304305
'successfully installed',
305306
{'test': ret}
306307
)
308+
309+
def test_mod_aggregate(self):
310+
'''
311+
Test to mod_aggregate function
312+
'''
313+
314+
low = OrderedDict([('state', 'pip'),
315+
('name', 'ipython'),
316+
('__sls__', 'test.test_pip'),
317+
('__env__', 'base'),
318+
('__id__', 'ipython'),
319+
('order', 10000),
320+
('fun', 'installed')])
321+
chunks = [OrderedDict([('state', 'pip'),
322+
('name', 'ipython'),
323+
('__sls__', 'test.test_pip'),
324+
('__env__', 'base'),
325+
('__id__', 'ipython'),
326+
('order', 10000),
327+
('fun', 'installed')]),
328+
OrderedDict([('state', 'pip'),
329+
('name', 'pylint'),
330+
('__sls__', 'test.test_pip'),
331+
('__env__', 'base'),
332+
('__id__', 'pylint'),
333+
('order', 10001),
334+
('fun', 'installed')])]
335+
running = {}
336+
expected_low = OrderedDict([('state', 'pip'),
337+
('name', 'ipython'),
338+
('__sls__', 'test.test_pip'),
339+
('__env__', 'base'),
340+
('__id__', 'ipython'),
341+
('order', 10000),
342+
('fun', 'installed'),
343+
('pkgs', ['ipython', 'pylint'])])
344+
345+
mock_tag = MagicMock(side_effect=['pip_|-ipython_|-ipython_|-installed',
346+
'pip_|-pylint_|-pylint_|-installed'])
347+
with patch.dict(pip_state.__utils__, {'state.gen_tag': mock_tag}):
348+
self.assertDictEqual(pip_state.mod_aggregate(low, chunks, running),
349+
expected_low)

0 commit comments

Comments
 (0)
0