Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop] First pass at adding mod_aggregate support to the pip_state module. #52221

Merged
merged 5 commits into from
Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions salt/states/pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,51 @@ def uptodate(name,
ret['comment'] = 'Upgrade failed.'

return ret


def mod_aggregate(low, chunks, running):
'''
The mod_aggregate function which looks up all packages in the available
low chunks and merges them into a single pkgs ref in the present low data
'''
twangboy marked this conversation as resolved.
Show resolved Hide resolved
pkgs = []
pkg_type = None
agg_enabled = [
'installed',
'removed',
]
if low.get('fun') not in agg_enabled:
return low
for chunk in chunks:
tag = __utils__['state.gen_tag'](chunk)
if tag in running:
# Already ran the pkg state, skip aggregation
continue
if chunk.get('state') == 'pip':
if '__agg__' in chunk:
continue
# Check for the same function
if chunk.get('fun') != low.get('fun'):
continue
# Check first if 'sources' was passed so we don't aggregate pkgs
# and sources together.
if pkg_type is None:
pkg_type = 'pkgs'
if pkg_type == 'pkgs':
# Pull out the pkg names!
if 'pkgs' in chunk:
pkgs.extend(chunk['pkgs'])
chunk['__agg__'] = True
elif 'name' in chunk:
version = chunk.pop('version', None)
if version is not None:
pkgs.append({chunk['name']: version})
else:
pkgs.append(chunk['name'])
chunk['__agg__'] = True
if pkg_type is not None and pkgs:
if pkg_type in low:
low[pkg_type].extend(pkgs)
else:
low[pkg_type] = pkgs
return low
43 changes: 43 additions & 0 deletions tests/unit/states/test_pip_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Import salt libs
import salt.states.pip_state as pip_state
import salt.utils.versions
from salt.utils.odict import OrderedDict

# Import 3rd-party libs
try:
Expand Down Expand Up @@ -304,3 +305,45 @@ def test_install_in_editable_mode(self):
'successfully installed',
{'test': ret}
)

def test_mod_aggregate(self):
'''
Test to mod_aggregate function
'''

low = OrderedDict([('state', 'pip'),
('name', 'ipython'),
('__sls__', 'test.test_pip'),
('__env__', 'base'),
('__id__', 'ipython'),
('order', 10000),
('fun', 'installed')])
chunks = [OrderedDict([('state', 'pip'),
('name', 'ipython'),
('__sls__', 'test.test_pip'),
('__env__', 'base'),
('__id__', 'ipython'),
('order', 10000),
('fun', 'installed')]),
OrderedDict([('state', 'pip'),
('name', 'pylint'),
('__sls__', 'test.test_pip'),
('__env__', 'base'),
('__id__', 'pylint'),
('order', 10001),
('fun', 'installed')])]
running = {}
expected_low = OrderedDict([('state', 'pip'),
('name', 'ipython'),
('__sls__', 'test.test_pip'),
('__env__', 'base'),
('__id__', 'ipython'),
('order', 10000),
('fun', 'installed'),
('pkgs', ['ipython', 'pylint'])])

mock_tag = MagicMock(side_effect=['pip_|-ipython_|-ipython_|-installed',
'pip_|-pylint_|-pylint_|-installed'])
with patch.dict(pip_state.__utils__, {'state.gen_tag': mock_tag}):
self.assertDictEqual(pip_state.mod_aggregate(low, chunks, running),
expected_low)