2016-06-02 13:26:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Nicole Thomas <nicole@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python libs
|
2017-12-15 18:14:18 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2016-08-26 20:46:40 +00:00
|
|
|
import copy
|
2016-08-11 16:48:23 +00:00
|
|
|
import os
|
2016-08-25 18:43:53 +00:00
|
|
|
import tempfile
|
2016-06-02 13:26:39 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-02-27 15:59:04 +00:00
|
|
|
import tests.integration as integration
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
2017-11-23 12:48:54 +00:00
|
|
|
from tests.support.mock import (
|
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON,
|
|
|
|
MagicMock,
|
|
|
|
patch)
|
2017-04-10 13:00:57 +00:00
|
|
|
from tests.support.mixins import AdaptedConfigurationTestCaseMixin
|
2016-08-11 16:48:23 +00:00
|
|
|
|
2016-06-02 13:26:39 +00:00
|
|
|
# Import Salt libs
|
2016-08-11 16:48:23 +00:00
|
|
|
import salt.exceptions
|
2017-08-19 00:06:15 +00:00
|
|
|
from salt.ext import six
|
|
|
|
import salt.state
|
2016-08-25 19:15:23 +00:00
|
|
|
from salt.utils.odict import OrderedDict, DefaultOrderedDict
|
2017-11-22 17:16:31 +00:00
|
|
|
from salt.utils.decorators import state as statedecorators
|
2016-06-02 13:26:39 +00:00
|
|
|
|
2017-11-22 17:16:31 +00:00
|
|
|
try:
|
|
|
|
import pytest
|
|
|
|
except ImportError as err:
|
|
|
|
pytest = None
|
2016-06-02 13:26:39 +00:00
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-04-10 13:00:57 +00:00
|
|
|
class StateCompilerTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
|
2016-06-02 13:26:39 +00:00
|
|
|
'''
|
|
|
|
TestCase for the state compiler.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def test_format_log_non_ascii_character(self):
|
|
|
|
'''
|
|
|
|
Tests running a non-ascii character through the state.format_log
|
|
|
|
function. See Issue #33605.
|
|
|
|
'''
|
|
|
|
# There is no return to test against as the format_log
|
|
|
|
# function doesn't return anything. However, we do want
|
|
|
|
# to make sure that the function doesn't stacktrace when
|
|
|
|
# called.
|
Update file state/execution modules and associated files with unicode_literals
This updates the file state and execution modules to use
unicode_literals. Since the serializers and the cmd module are touched
by the file state/exec module, those are also updated here, as well as
the cmd state module, for good measure.
Additionally, I found that salt.utils.data.decode_dict (and decode_list)
are misnamed for what they actually do. Since they *encode* the
contents, the functions should be named encode_dict and encode_list,
respectively. And we also should have counterparts which actually
decode, so I've added them. The compatibility functions from salt.utils
still use the old "decode" names to preserve backward compatibility, but
they now invoke the renamed "encode" functions in salt.utils.data. Note
that this means that the compatibility functions
salt.utils.decode_dict/list, and their cognates in salt.utils.data now
do different things, but since the move to salt.utils.data is also
happening in the Oxygen release this is as good a time as any to correct
this oversight.
I've updated the jinja filter docs to include information on the renamed
jinja filters, and also added a section on jinja filter renaming to the
Oxygen release notes. There was another filter that I renamed during the
process of moving functions from salt.utils which I did not properly
document in the release notes, so this is now included along with the
others.
2017-12-12 16:30:33 +00:00
|
|
|
ret = {'changes': {'Français': {'old': 'something old',
|
2016-06-02 18:32:21 +00:00
|
|
|
'new': 'something new'}},
|
|
|
|
'result': True}
|
2016-08-11 16:48:23 +00:00
|
|
|
salt.state.format_log(ret)
|
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
def test_render_error_on_invalid_requisite(self):
|
2016-08-11 16:48:23 +00:00
|
|
|
'''
|
|
|
|
Test that the state compiler correctly deliver a rendering
|
|
|
|
exception when a requisite cannot be resolved
|
|
|
|
'''
|
2017-04-10 13:00:57 +00:00
|
|
|
with patch('salt.state.State._gather_pillar') as state_patch:
|
|
|
|
high_data = {
|
|
|
|
'git': OrderedDict([
|
|
|
|
('pkg', [
|
|
|
|
OrderedDict([
|
|
|
|
('require', [
|
|
|
|
OrderedDict([
|
|
|
|
('file', OrderedDict(
|
|
|
|
[('test1', 'test')]))])])]),
|
Update file state/execution modules and associated files with unicode_literals
This updates the file state and execution modules to use
unicode_literals. Since the serializers and the cmd module are touched
by the file state/exec module, those are also updated here, as well as
the cmd state module, for good measure.
Additionally, I found that salt.utils.data.decode_dict (and decode_list)
are misnamed for what they actually do. Since they *encode* the
contents, the functions should be named encode_dict and encode_list,
respectively. And we also should have counterparts which actually
decode, so I've added them. The compatibility functions from salt.utils
still use the old "decode" names to preserve backward compatibility, but
they now invoke the renamed "encode" functions in salt.utils.data. Note
that this means that the compatibility functions
salt.utils.decode_dict/list, and their cognates in salt.utils.data now
do different things, but since the move to salt.utils.data is also
happening in the Oxygen release this is as good a time as any to correct
this oversight.
I've updated the jinja filter docs to include information on the renamed
jinja filters, and also added a section on jinja filter renaming to the
Oxygen release notes. There was another filter that I renamed during the
process of moving functions from salt.utils which I did not properly
document in the release notes, so this is now included along with the
others.
2017-12-12 16:30:33 +00:00
|
|
|
'installed', {'order': 10000}]), ('__sls__', 'issue_35226'), ('__env__', 'base')])}
|
2017-04-10 13:00:57 +00:00
|
|
|
minion_opts = self.get_temp_config('minion')
|
|
|
|
minion_opts['pillar'] = {'git': OrderedDict([('test1', 'test')])}
|
|
|
|
state_obj = salt.state.State(minion_opts)
|
|
|
|
with self.assertRaises(salt.exceptions.SaltRenderError):
|
|
|
|
state_obj.call_high(high_data)
|
|
|
|
|
|
|
|
|
|
|
|
class HighStateTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
|
2016-08-25 18:43:53 +00:00
|
|
|
def setUp(self):
|
2017-04-10 13:00:57 +00:00
|
|
|
root_dir = tempfile.mkdtemp(dir=integration.TMP)
|
|
|
|
state_tree_dir = os.path.join(root_dir, 'state_tree')
|
|
|
|
cache_dir = os.path.join(root_dir, 'cachedir')
|
|
|
|
for dpath in (root_dir, state_tree_dir, cache_dir):
|
|
|
|
if not os.path.isdir(dpath):
|
|
|
|
os.makedirs(dpath)
|
|
|
|
|
|
|
|
overrides = {}
|
|
|
|
overrides['root_dir'] = root_dir
|
|
|
|
overrides['state_events'] = False
|
|
|
|
overrides['id'] = 'match'
|
|
|
|
overrides['file_client'] = 'local'
|
|
|
|
overrides['file_roots'] = dict(base=[state_tree_dir])
|
|
|
|
overrides['cachedir'] = cache_dir
|
|
|
|
overrides['test'] = False
|
|
|
|
self.config = self.get_temp_config('minion', **overrides)
|
|
|
|
self.addCleanup(delattr, self, 'config')
|
2016-08-25 19:09:07 +00:00
|
|
|
self.highstate = salt.state.HighState(self.config)
|
2017-04-10 13:00:57 +00:00
|
|
|
self.addCleanup(delattr, self, 'highstate')
|
2016-08-25 18:43:53 +00:00
|
|
|
self.highstate.push_active()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.highstate.pop_active()
|
|
|
|
|
|
|
|
def test_top_matches_with_list(self):
|
|
|
|
top = {'env': {'match': ['state1', 'state2'], 'nomatch': ['state3']}}
|
|
|
|
matches = self.highstate.top_matches(top)
|
|
|
|
self.assertEqual(matches, {'env': ['state1', 'state2']})
|
|
|
|
|
|
|
|
def test_top_matches_with_string(self):
|
|
|
|
top = {'env': {'match': 'state1', 'nomatch': 'state2'}}
|
|
|
|
matches = self.highstate.top_matches(top)
|
|
|
|
self.assertEqual(matches, {'env': ['state1']})
|
|
|
|
|
|
|
|
def test_matches_whitelist(self):
|
|
|
|
matches = {'env': ['state1', 'state2', 'state3']}
|
|
|
|
matches = self.highstate.matches_whitelist(matches, ['state2'])
|
|
|
|
self.assertEqual(matches, {'env': ['state2']})
|
|
|
|
|
|
|
|
def test_matches_whitelist_with_string(self):
|
|
|
|
matches = {'env': ['state1', 'state2', 'state3']}
|
|
|
|
matches = self.highstate.matches_whitelist(matches,
|
|
|
|
'state2,state3')
|
|
|
|
self.assertEqual(matches, {'env': ['state2', 'state3']})
|
|
|
|
|
2016-09-29 16:06:45 +00:00
|
|
|
def test_show_state_usage(self):
|
|
|
|
# monkey patch sub methods
|
|
|
|
self.highstate.avail = {
|
2016-09-30 09:07:52 +00:00
|
|
|
'base': ['state.a', 'state.b', 'state.c']
|
2016-09-29 16:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def verify_tops(*args, **kwargs):
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_top(*args, **kwargs):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def top_matches(*args, **kwargs):
|
|
|
|
return {'base': ['state.a', 'state.b']}
|
|
|
|
|
|
|
|
self.highstate.verify_tops = verify_tops
|
|
|
|
self.highstate.get_top = get_top
|
|
|
|
self.highstate.top_matches = top_matches
|
|
|
|
|
|
|
|
# get compile_state_usage() result
|
|
|
|
state_usage_dict = self.highstate.compile_state_usage()
|
|
|
|
|
|
|
|
self.assertEqual(state_usage_dict['base']['count_unused'], 1)
|
|
|
|
self.assertEqual(state_usage_dict['base']['count_used'], 2)
|
|
|
|
self.assertEqual(state_usage_dict['base']['count_all'], 3)
|
|
|
|
self.assertEqual(state_usage_dict['base']['used'], ['state.a', 'state.b'])
|
|
|
|
self.assertEqual(state_usage_dict['base']['unused'], ['state.c'])
|
|
|
|
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
class TopFileMergeTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
|
|
|
Test various merge strategies for multiple tops files collected from
|
|
|
|
multiple environments. Various options correspond to merge strategies
|
|
|
|
which can be set by the user with the top_file_merging_strategy config
|
|
|
|
option.
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
Create multiple top files for use in each test. Envs within self.tops
|
|
|
|
should be defined in the same order as this ordering will affect
|
|
|
|
ordering in merge_tops. The envs in each top file are defined in the
|
|
|
|
same order as self.env_order. This is no accident; it was done this way
|
|
|
|
in order to produce the proper deterministic results to match the
|
|
|
|
tests. Changing anything created in this func will affect the tests, as
|
|
|
|
they would affect ordering in states in real life. So, don't change any
|
|
|
|
of this unless you know what you're doing. If a test is failing, it is
|
|
|
|
likely due to incorrect logic in merge_tops.
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
self.env_order = ['base', 'foo', 'bar', 'baz']
|
2017-04-10 13:00:57 +00:00
|
|
|
self.addCleanup(delattr, self, 'env_order')
|
2016-08-26 20:46:40 +00:00
|
|
|
self.tops = {
|
|
|
|
'base': OrderedDict([
|
|
|
|
('base', OrderedDict([('*', ['base_base'])])),
|
|
|
|
('foo', OrderedDict([('*', ['base_foo'])])),
|
|
|
|
('bar', OrderedDict([('*', ['base_bar'])])),
|
|
|
|
('baz', OrderedDict([('*', ['base_baz'])])),
|
|
|
|
]),
|
|
|
|
'foo': OrderedDict([
|
|
|
|
('base', OrderedDict([('*', ['foo_base'])])),
|
|
|
|
('foo', OrderedDict([('*', ['foo_foo'])])),
|
|
|
|
('bar', OrderedDict([('*', ['foo_bar'])])),
|
|
|
|
('baz', OrderedDict([('*', ['foo_baz'])])),
|
|
|
|
]),
|
|
|
|
'bar': OrderedDict([
|
|
|
|
('base', OrderedDict([('*', ['bar_base'])])),
|
|
|
|
('foo', OrderedDict([('*', ['bar_foo'])])),
|
|
|
|
('bar', OrderedDict([('*', ['bar_bar'])])),
|
|
|
|
('baz', OrderedDict([('*', ['bar_baz'])])),
|
|
|
|
]),
|
|
|
|
# Empty environment
|
|
|
|
'baz': OrderedDict()
|
|
|
|
}
|
2017-04-10 13:00:57 +00:00
|
|
|
self.addCleanup(delattr, self, 'tops')
|
2016-08-26 20:46:40 +00:00
|
|
|
|
|
|
|
# Version without the other envs defined in the base top file
|
|
|
|
self.tops_limited_base = copy.deepcopy(self.tops)
|
|
|
|
self.tops_limited_base['base'] = OrderedDict([
|
|
|
|
('base', OrderedDict([('*', ['base_base'])])),
|
|
|
|
])
|
2017-04-10 13:00:57 +00:00
|
|
|
self.addCleanup(delattr, self, 'tops_limited_base')
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2017-04-10 13:00:57 +00:00
|
|
|
def highstate(self, **opts):
|
2016-08-25 18:43:53 +00:00
|
|
|
root_dir = tempfile.mkdtemp(dir=integration.TMP)
|
|
|
|
state_tree_dir = os.path.join(root_dir, 'state_tree')
|
|
|
|
cache_dir = os.path.join(root_dir, 'cachedir')
|
2017-04-10 13:00:57 +00:00
|
|
|
overrides = {}
|
|
|
|
overrides['root_dir'] = root_dir
|
|
|
|
overrides['state_events'] = False
|
|
|
|
overrides['id'] = 'match'
|
|
|
|
overrides['file_client'] = 'local'
|
|
|
|
overrides['file_roots'] = dict(base=[state_tree_dir])
|
|
|
|
overrides['cachedir'] = cache_dir
|
|
|
|
overrides['test'] = False
|
|
|
|
overrides['default_top'] = 'base'
|
|
|
|
overrides.update(opts)
|
|
|
|
return salt.state.HighState(self.get_temp_config('minion', **overrides))
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2016-08-26 20:46:40 +00:00
|
|
|
def get_tops(self, tops=None, env_order=None, state_top_saltenv=None):
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-25 19:09:07 +00:00
|
|
|
A test helper to emulate salt.state.HighState.get_tops() but just to
|
|
|
|
construct an appropriate data structure for top files from multiple
|
|
|
|
environments
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
if tops is None:
|
|
|
|
tops = self.tops
|
|
|
|
|
|
|
|
if state_top_saltenv:
|
|
|
|
append_order = [state_top_saltenv]
|
|
|
|
elif env_order:
|
|
|
|
append_order = env_order
|
|
|
|
else:
|
|
|
|
append_order = self.env_order
|
|
|
|
|
|
|
|
ret = DefaultOrderedDict(list)
|
|
|
|
for env in append_order:
|
|
|
|
item = tops[env]
|
|
|
|
if env_order:
|
|
|
|
for remove in [x for x in self.env_order if x not in env_order]:
|
|
|
|
# Remove this env from the tops from the tops since this
|
|
|
|
# env is not part of env_order.
|
|
|
|
item.pop(remove)
|
|
|
|
ret[env].append(tops[env])
|
|
|
|
return ret
|
|
|
|
|
2016-10-11 22:33:23 +00:00
|
|
|
def test_merge_tops_merge(self):
|
2016-08-26 20:46:40 +00:00
|
|
|
'''
|
|
|
|
Test the default merge strategy for top files, in an instance where the
|
|
|
|
base top file contains sections for all envs and the other envs' top
|
|
|
|
files are therefore ignored.
|
|
|
|
'''
|
|
|
|
merged_tops = self.highstate().merge_tops(self.get_tops())
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order:
|
|
|
|
expected_merge[env]['*'] = ['base_{0}'.format(env)]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
2016-10-11 22:33:23 +00:00
|
|
|
def test_merge_tops_merge_limited_base(self):
|
2016-08-26 20:46:40 +00:00
|
|
|
'''
|
2016-10-11 22:33:23 +00:00
|
|
|
Test the default merge strategy for top files when the base environment
|
|
|
|
only defines states for itself.
|
2016-08-26 20:46:40 +00:00
|
|
|
'''
|
|
|
|
tops = self.get_tops(tops=self.tops_limited_base)
|
|
|
|
merged_tops = self.highstate().merge_tops(tops)
|
|
|
|
|
|
|
|
# No baz in the expected results because baz has no top file
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order[:-1]:
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
2016-10-11 22:33:23 +00:00
|
|
|
def test_merge_tops_merge_state_top_saltenv_base(self):
|
2016-08-26 20:46:40 +00:00
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'base' saltenv, with the default merging strategy. This should
|
|
|
|
result in all states from the 'base' top file being in the merged
|
|
|
|
result.
|
|
|
|
'''
|
|
|
|
env = 'base'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate().merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env2 in self.env_order:
|
|
|
|
expected_merge[env2]['*'] = ['_'.join((env, env2))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
2016-10-11 22:33:23 +00:00
|
|
|
def test_merge_tops_merge_state_top_saltenv_foo(self):
|
2016-08-26 20:46:40 +00:00
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'foo' saltenv, with the default merging strategy. This should
|
|
|
|
result in just the 'foo' environment's states from the 'foo' top file
|
|
|
|
being in the merged result.
|
|
|
|
'''
|
|
|
|
env = 'foo'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate().merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_merge_all(self):
|
|
|
|
'''
|
|
|
|
Test the merge_all strategy
|
|
|
|
'''
|
|
|
|
tops = self.get_tops()
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='merge_all').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order:
|
|
|
|
states = []
|
|
|
|
for top_env in self.env_order:
|
|
|
|
if top_env in tops[top_env][0]:
|
|
|
|
states.extend(tops[top_env][0][env]['*'])
|
|
|
|
expected_merge[env]['*'] = states
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_merge_all_with_env_order(self):
|
|
|
|
'''
|
|
|
|
Test an altered env_order with the 'merge_all' strategy.
|
|
|
|
'''
|
|
|
|
env_order = ['bar', 'foo', 'base']
|
|
|
|
tops = self.get_tops(env_order=env_order)
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='merge_all',
|
|
|
|
env_order=env_order).merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in [x for x in self.env_order if x in env_order]:
|
|
|
|
states = []
|
|
|
|
for top_env in env_order:
|
|
|
|
states.extend(tops[top_env][0][env]['*'])
|
|
|
|
expected_merge[env]['*'] = states
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_merge_all_state_top_saltenv_base(self):
|
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'base' saltenv, with the 'merge_all' merging strategy. This should
|
|
|
|
result in all states from the 'base' top file being in the merged
|
|
|
|
result.
|
|
|
|
'''
|
|
|
|
env = 'base'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='merge_all').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env2 in self.env_order:
|
|
|
|
expected_merge[env2]['*'] = ['_'.join((env, env2))]
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2016-08-26 20:46:40 +00:00
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2016-08-26 20:46:40 +00:00
|
|
|
def test_merge_tops_merge_all_state_top_saltenv_foo(self):
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'foo' saltenv, with the 'merge_all' merging strategy. This should
|
|
|
|
result in all the states from the 'foo' top file being in the merged
|
|
|
|
result.
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
env = 'foo'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='merge_all').merge_tops(tops)
|
2016-08-25 18:43:53 +00:00
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
2016-08-26 20:46:40 +00:00
|
|
|
for env2 in self.env_order:
|
|
|
|
expected_merge[env2]['*'] = ['_'.join((env, env2))]
|
|
|
|
|
2016-08-25 18:43:53 +00:00
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
2016-08-26 20:46:40 +00:00
|
|
|
def test_merge_tops_same_with_default_top(self):
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
Test to see if the top file that corresponds to the requested env is
|
|
|
|
the one that is used by the state system. Also test the 'default_top'
|
|
|
|
option for env 'baz', which has no top file and should pull its states
|
|
|
|
from the 'foo' top file.
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='same',
|
|
|
|
default_top='foo').merge_tops(self.get_tops())
|
2016-08-25 18:43:53 +00:00
|
|
|
|
2016-08-26 20:46:40 +00:00
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order[:-1]:
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
# The 'baz' env should be using the foo top file because baz lacks a
|
|
|
|
# top file, and default_top has been seet to 'foo'
|
|
|
|
expected_merge['baz']['*'] = ['foo_baz']
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_same_without_default_top(self):
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
Test to see if the top file that corresponds to the requested env is
|
|
|
|
the one that is used by the state system. default_top will not be set
|
|
|
|
(falling back to 'base'), so the 'baz' environment should pull its
|
|
|
|
states from the 'base' top file.
|
2016-08-25 18:43:53 +00:00
|
|
|
'''
|
2016-08-26 20:46:40 +00:00
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='same').merge_tops(self.get_tops())
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order[:-1]:
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
# The 'baz' env should be using the foo top file because baz lacks a
|
|
|
|
# top file, and default_top == 'base'
|
|
|
|
expected_merge['baz']['*'] = ['base_baz']
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_same_limited_base_without_default_top(self):
|
|
|
|
'''
|
|
|
|
Test to see if the top file that corresponds to the requested env is
|
|
|
|
the one that is used by the state system. default_top will not be set
|
|
|
|
(falling back to 'base'), and since we are using a limited base top
|
|
|
|
file, the 'baz' environment should not appear in the merged tops.
|
|
|
|
'''
|
|
|
|
tops = self.get_tops(tops=self.tops_limited_base)
|
|
|
|
merged_tops = \
|
|
|
|
self.highstate(top_file_merging_strategy='same').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
for env in self.env_order[:-1]:
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_same_state_top_saltenv_base(self):
|
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'base' saltenv, with the 'same' merging strategy. This should
|
|
|
|
result in just the 'base' environment's states from the 'base' top file
|
|
|
|
being in the merged result.
|
|
|
|
'''
|
|
|
|
env = 'base'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='same').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_same_state_top_saltenv_foo(self):
|
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'foo' saltenv, with the 'same' merging strategy. This should
|
|
|
|
result in just the 'foo' environment's states from the 'foo' top file
|
|
|
|
being in the merged result.
|
|
|
|
'''
|
|
|
|
env = 'foo'
|
|
|
|
tops = self.get_tops(state_top_saltenv=env)
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='same').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
expected_merge[env]['*'] = ['_'.join((env, env))]
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
|
|
|
|
|
|
|
def test_merge_tops_same_state_top_saltenv_baz(self):
|
|
|
|
'''
|
|
|
|
Test the 'state_top_saltenv' parameter to load states exclusively from
|
|
|
|
the 'baz' saltenv, with the 'same' merging strategy. This should
|
|
|
|
result in an empty dictionary since this environment has not top file.
|
|
|
|
'''
|
|
|
|
tops = self.get_tops(state_top_saltenv='baz')
|
|
|
|
merged_tops = self.highstate(
|
|
|
|
top_file_merging_strategy='same').merge_tops(tops)
|
|
|
|
|
|
|
|
expected_merge = DefaultOrderedDict(OrderedDict)
|
|
|
|
|
|
|
|
self.assertEqual(merged_tops, expected_merge)
|
2017-08-19 00:06:15 +00:00
|
|
|
|
|
|
|
|
2017-11-22 17:16:31 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
|
|
|
@skipIf(pytest is None, 'PyTest is missing')
|
2017-08-19 00:06:15 +00:00
|
|
|
class StateReturnsTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
TestCase for code handling state returns.
|
|
|
|
'''
|
|
|
|
|
2017-11-22 17:17:27 +00:00
|
|
|
def test_state_output_check_changes_is_dict(self):
|
|
|
|
'''
|
|
|
|
Test that changes key contains a dictionary.
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
data = {'changes': []}
|
|
|
|
with pytest.raises(salt.exceptions.SaltException) as err:
|
|
|
|
statedecorators.state_output_check(lambda: data)()
|
|
|
|
assert "'Changes' should be a dictionary" in str(err)
|
|
|
|
|
2017-11-22 17:17:53 +00:00
|
|
|
def test_state_output_check_return_is_dict(self):
|
|
|
|
'''
|
2017-11-22 17:30:48 +00:00
|
|
|
Test for the entire return is a dictionary
|
2017-11-22 17:17:53 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
data = ['whatever']
|
|
|
|
with pytest.raises(salt.exceptions.SaltException) as err:
|
|
|
|
statedecorators.state_output_check(lambda: data)()
|
|
|
|
assert 'Malformed state return, return must be a dict' in str(err)
|
|
|
|
|
2017-11-22 17:20:21 +00:00
|
|
|
def test_state_output_check_return_is_dict(self):
|
|
|
|
'''
|
2017-11-22 17:30:48 +00:00
|
|
|
Test for name/result/comment keys are inside the return.
|
2017-11-22 17:20:21 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
data = {'arbitrary': 'data', 'changes': {}}
|
|
|
|
with pytest.raises(salt.exceptions.SaltException) as err:
|
|
|
|
statedecorators.state_output_check(lambda: data)()
|
|
|
|
assert ' The following keys were not present in the state return: name, result, comment' in str(err)
|
|
|
|
|
2017-11-22 17:26:57 +00:00
|
|
|
def test_state_output_unificator_comment_is_not_list(self):
|
|
|
|
'''
|
2017-11-22 17:30:48 +00:00
|
|
|
Test for output is unified so the comment is converted to a multi-line string
|
2017-11-22 17:26:57 +00:00
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
data = {'comment': ['data', 'in', 'the', 'list'], 'changes': {}, 'name': None, 'result': 'fantastic!'}
|
|
|
|
expected = {'comment': 'data\nin\nthe\nlist', 'changes': {}, 'name': None, 'result': True}
|
|
|
|
assert statedecorators.state_output_unificator(lambda: data)() == expected
|
|
|
|
|
2017-11-22 17:30:39 +00:00
|
|
|
data = {'comment': ['data', 'in', 'the', 'list'], 'changes': {}, 'name': None, 'result': None}
|
|
|
|
expected = 'data\nin\nthe\nlist'
|
|
|
|
assert statedecorators.state_output_unificator(lambda: data)()['comment'] == expected
|
|
|
|
|
2017-11-23 12:48:54 +00:00
|
|
|
|
|
|
|
class StateFormatSlotsTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
|
|
|
|
'''
|
|
|
|
TestCase for code handling slots
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
with patch('salt.state.State._gather_pillar'):
|
|
|
|
minion_opts = self.get_temp_config('minion')
|
|
|
|
self.state_obj = salt.state.State(minion_opts)
|
|
|
|
|
|
|
|
def test_format_slots_no_slots(self):
|
|
|
|
'''
|
|
|
|
Test the format slots keeps data without slots untouched.
|
|
|
|
'''
|
|
|
|
cdata = {
|
|
|
|
'args': [
|
|
|
|
'arg',
|
|
|
|
],
|
|
|
|
'kwargs': {
|
|
|
|
'key': 'val',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.state_obj.format_slots(cdata)
|
|
|
|
self.assertEqual(cdata, {'args': ['arg'], 'kwargs': {'key': 'val'}})
|
|
|
|
|
|
|
|
def test_format_slots_arg(self):
|
|
|
|
'''
|
|
|
|
Test the format slots is calling a slot specified in args with corresponding arguments.
|
|
|
|
'''
|
|
|
|
cdata = {
|
|
|
|
'args': [
|
|
|
|
'__slot__:salt:mod.fun(fun_arg, fun_key=fun_val)',
|
|
|
|
],
|
|
|
|
'kwargs': {
|
|
|
|
'key': 'val',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mock = MagicMock(return_value='fun_return')
|
|
|
|
with patch.dict(self.state_obj.functions, {'mod.fun': mock}):
|
|
|
|
self.state_obj.format_slots(cdata)
|
|
|
|
mock.assert_called_once_with('fun_arg', fun_key='fun_val')
|
|
|
|
self.assertEqual(cdata, {'args': ['fun_return'], 'kwargs': {'key': 'val'}})
|
|
|
|
|
|
|
|
def test_format_slots_kwarg(self):
|
|
|
|
'''
|
|
|
|
Test the format slots is calling a slot specified in kwargs with corresponding arguments.
|
|
|
|
'''
|
|
|
|
cdata = {
|
|
|
|
'args': [
|
|
|
|
'arg',
|
|
|
|
],
|
|
|
|
'kwargs': {
|
|
|
|
'key': '__slot__:salt:mod.fun(fun_arg, fun_key=fun_val)',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mock = MagicMock(return_value='fun_return')
|
|
|
|
with patch.dict(self.state_obj.functions, {'mod.fun': mock}):
|
|
|
|
self.state_obj.format_slots(cdata)
|
|
|
|
mock.assert_called_once_with('fun_arg', fun_key='fun_val')
|
|
|
|
self.assertEqual(cdata, {'args': ['arg'], 'kwargs': {'key': 'fun_return'}})
|
|
|
|
|
|
|
|
def test_format_slots_multi(self):
|
|
|
|
'''
|
|
|
|
Test the format slots is calling all slots with corresponding arguments when multiple slots
|
|
|
|
specified.
|
|
|
|
'''
|
|
|
|
cdata = {
|
|
|
|
'args': [
|
|
|
|
'__slot__:salt:test_mod.fun_a(a_arg, a_key=a_kwarg)',
|
|
|
|
'__slot__:salt:test_mod.fun_b(b_arg, b_key=b_kwarg)',
|
|
|
|
],
|
|
|
|
'kwargs': {
|
|
|
|
'kw_key_1': '__slot__:salt:test_mod.fun_c(c_arg, c_key=c_kwarg)',
|
|
|
|
'kw_key_2': '__slot__:salt:test_mod.fun_d(d_arg, d_key=d_kwarg)',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mock_a = MagicMock(return_value='fun_a_return')
|
|
|
|
mock_b = MagicMock(return_value='fun_b_return')
|
|
|
|
mock_c = MagicMock(return_value='fun_c_return')
|
|
|
|
mock_d = MagicMock(return_value='fun_d_return')
|
|
|
|
with patch.dict(self.state_obj.functions, {'test_mod.fun_a': mock_a,
|
|
|
|
'test_mod.fun_b': mock_b,
|
|
|
|
'test_mod.fun_c': mock_c,
|
|
|
|
'test_mod.fun_d': mock_d}):
|
|
|
|
self.state_obj.format_slots(cdata)
|
|
|
|
mock_a.assert_called_once_with('a_arg', a_key='a_kwarg')
|
|
|
|
mock_b.assert_called_once_with('b_arg', b_key='b_kwarg')
|
|
|
|
mock_c.assert_called_once_with('c_arg', c_key='c_kwarg')
|
|
|
|
mock_d.assert_called_once_with('d_arg', d_key='d_kwarg')
|
|
|
|
self.assertEqual(cdata, {'args': ['fun_a_return',
|
|
|
|
'fun_b_return'],
|
|
|
|
'kwargs': {'kw_key_1': 'fun_c_return',
|
|
|
|
'kw_key_2': 'fun_d_return'}})
|
|
|
|
|
|
|
|
def test_format_slots_malformed(self):
|
|
|
|
'''
|
|
|
|
Test the format slots keeps malformed slots untouched.
|
|
|
|
'''
|
|
|
|
sls_data = {
|
|
|
|
'args': [
|
|
|
|
'__slot__:NOT_SUPPORTED:not.called()',
|
|
|
|
'__slot__:salt:not.called(',
|
|
|
|
'__slot__:salt:',
|
|
|
|
'__slot__:salt',
|
|
|
|
'__slot__:',
|
|
|
|
'__slot__',
|
|
|
|
],
|
|
|
|
'kwargs': {
|
|
|
|
'key3': '__slot__:NOT_SUPPORTED:not.called()',
|
|
|
|
'key4': '__slot__:salt:not.called(',
|
|
|
|
'key5': '__slot__:salt:',
|
|
|
|
'key6': '__slot__:salt',
|
|
|
|
'key7': '__slot__:',
|
|
|
|
'key8': '__slot__',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cdata = sls_data.copy()
|
|
|
|
mock = MagicMock(return_value='return')
|
|
|
|
with patch.dict(self.state_obj.functions, {'not.called': mock}):
|
|
|
|
self.state_obj.format_slots(cdata)
|
|
|
|
mock.assert_not_called()
|
|
|
|
self.assertEqual(cdata, sls_data)
|