Skip to content

Commit

Permalink
Merge pull request #51900 from max-arnold/saltutil-sync
Browse files Browse the repository at this point in the history
Add missing saltutil.sync_* states
  • Loading branch information
twangboy authored May 13, 2019
2 parents 4d123ea + 5943aa3 commit fc02176
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/ref/states/all/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ state modules
rvm
salt_proxy
saltmod
saltutil
schedule
selinux
serverdensity_device
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/states/all/salt.states.saltutil.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====================
salt.states.saltutil
====================

.. automodule:: salt.states.saltutil
:members:
10 changes: 10 additions & 0 deletions doc/topics/development/modules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ dynamic modules when states are run. To disable this behavior set
When dynamic modules are autoloaded via states, only the modules defined in the
same saltenvs as the states currently being run.

Also it is possible to use the explicit ``saltutil.sync_*`` :py:mod:`state functions <salt.states.saltutil>`
to sync the modules (previously it was necessary to use the ``module.run`` state):

.. code-block::yaml
synchronize_modules:
saltutil.sync_modules:
- refresh: True
Sync Via the saltutil Module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
28 changes: 28 additions & 0 deletions salt/states/saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ def sync_proxymodules(name, **kwargs):
return _sync_single(name, "proxymodules", **kwargs)


def sync_matchers(name, **kwargs):
'''
Performs the same task as saltutil.sync_matchers module
See :mod:`saltutil module for full list of options <salt.modules.saltutil>`
.. code-block:: yaml
sync_everything:
saltutil.sync_matchers:
- refresh: True
'''
return _sync_single(name, "matchers", **kwargs)


def sync_renderers(name, **kwargs):
'''
Performs the same task as saltutil.sync_renderers module
Expand Down Expand Up @@ -323,3 +337,17 @@ def sync_utils(name, **kwargs):
- refresh: True
'''
return _sync_single(name, "utils", **kwargs)


def sync_serializers(name, **kwargs):
'''
Performs the same task as saltutil.sync_serializers module
See :mod:`saltutil module for full list of options <salt.modules.saltutil>`
.. code-block:: yaml
sync_everything:
saltutil.sync_serializers:
- refresh: True
'''
return _sync_single(name, "serializers", **kwargs)
43 changes: 34 additions & 9 deletions tests/unit/states/test_saltutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals

import inspect

# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
Expand All @@ -16,7 +18,8 @@
)

# Import Salt Libs
import salt.states.saltutil as saltutil
import salt.states.saltutil as saltutil_state
import salt.modules.saltutil as saltutil_module


@skipIf(NO_MOCK, NO_MOCK_REASON)
Expand All @@ -25,7 +28,7 @@ class Saltutil(TestCase, LoaderModuleMockMixin):
Test cases for salt.states.saltutil
'''
def setup_loader_modules(self):
return {saltutil: {'__opts__': {'test': False}}}
return {saltutil_state: {'__opts__': {'test': False}}}

def test_saltutil_sync_all_nochange(self):
sync_output = {
Expand All @@ -45,6 +48,8 @@ def test_saltutil_sync_all_nochange(self):
'proxymodules': [],
'output': [],
'pillar': [],
'matchers': [],
'serializers': [],
}
state_id = 'somename'
state_result = {'changes': {},
Expand All @@ -54,8 +59,8 @@ def test_saltutil_sync_all_nochange(self):
}

mock_moduleout = MagicMock(return_value=sync_output)
with patch.dict(saltutil.__salt__, {'saltutil.sync_all': mock_moduleout}):
result = saltutil.sync_all(state_id, refresh=True)
with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
result = saltutil_state.sync_all(state_id, refresh=True)
self.assertEqual(result, state_result)

def test_saltutil_sync_all_test(self):
Expand All @@ -76,6 +81,8 @@ def test_saltutil_sync_all_test(self):
'proxymodules': [],
'output': [],
'pillar': [],
'matchers': [],
'serializers': [],
}
state_id = 'somename'
state_result = {'changes': {},
Expand All @@ -85,9 +92,9 @@ def test_saltutil_sync_all_test(self):
}

mock_moduleout = MagicMock(return_value=sync_output)
with patch.dict(saltutil.__salt__, {'saltutil.sync_all': mock_moduleout}):
with patch.dict(saltutil.__opts__, {'test': True}):
result = saltutil.sync_all(state_id, refresh=True)
with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
with patch.dict(saltutil_state.__opts__, {'test': True}):
result = saltutil_state.sync_all(state_id, refresh=True)
self.assertEqual(result, state_result)

def test_saltutil_sync_all_change(self):
Expand All @@ -108,6 +115,8 @@ def test_saltutil_sync_all_change(self):
'proxymodules': [],
'output': [],
'pillar': [],
'matchers': [],
'serializers': [],
}
state_id = 'somename'
state_result = {'changes': {'modules': ['modules.file'],
Expand All @@ -118,6 +127,22 @@ def test_saltutil_sync_all_change(self):
}

mock_moduleout = MagicMock(return_value=sync_output)
with patch.dict(saltutil.__salt__, {'saltutil.sync_all': mock_moduleout}):
result = saltutil.sync_all(state_id, refresh=True)
with patch.dict(saltutil_state.__salt__, {'saltutil.sync_all': mock_moduleout}):
result = saltutil_state.sync_all(state_id, refresh=True)
self.assertEqual(result, state_result)

def test_saltutil_sync_states_should_match_saltutil_module(self):
module_functions = [
f[0] for f in inspect.getmembers(saltutil_module, inspect.isfunction)
if f[0].startswith('sync_')
]
state_functions = [
f[0] for f in inspect.getmembers(saltutil_state, inspect.isfunction)
if f[0].startswith('sync_')
]
for fn in module_functions:
self.assertIn(
fn,
state_functions,
msg='modules.saltutil.{} has no matching state in states.saltutil'.format(fn)
)

0 comments on commit fc02176

Please sign in to comment.