From 340ee23a107a47bc13a72f0a65e71ea7671570b2 Mon Sep 17 00:00:00 2001 From: twangboy Date: Tue, 30 Apr 2019 15:55:08 -0600 Subject: [PATCH 1/2] Pass encoding to _validate_str_list --- salt/states/file.py | 9 ++++----- tests/unit/states/test_file.py | 1 + 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/salt/states/file.py b/salt/states/file.py index 08afadb82a..69baa0053f 100644 --- a/salt/states/file.py +++ b/salt/states/file.py @@ -308,7 +308,6 @@ import salt.utils.stringutils import salt.utils.templates import salt.utils.url import salt.utils.versions -from salt.utils.locales import sdecode from salt.exceptions import CommandExecutionError from salt.serializers import DeserializationError from salt.state import get_accumulator_dir as _get_accumulator_dir @@ -1176,12 +1175,12 @@ def _get_template_texts(source_list=None, return ret -def _validate_str_list(arg): +def _validate_str_list(arg, encoding=None): ''' ensure ``arg`` is a list of strings ''' if isinstance(arg, six.binary_type): - ret = [salt.utils.stringutils.to_unicode(arg)] + ret = [salt.utils.stringutils.to_unicode(arg, encoding=encoding)] elif isinstance(arg, six.string_types): ret = [arg] elif isinstance(arg, Iterable) and not isinstance(arg, Mapping): @@ -2623,7 +2622,7 @@ def managed(name, ) try: - validated_contents = _validate_str_list(use_contents) + validated_contents = _validate_str_list(use_contents, encoding=encoding) if not validated_contents: return _error( ret, @@ -3756,7 +3755,7 @@ def recurse(name, # "env" is not supported; Use "saltenv". kwargs.pop('env') - name = os.path.expanduser(sdecode(name)) + name = os.path.expanduser(salt.utils.data.decode(name)) user = _test_owner(kwargs, user=user) if salt.utils.platform.is_windows(): diff --git a/tests/unit/states/test_file.py b/tests/unit/states/test_file.py index 2c6cf320fe..71c2f63270 100644 --- a/tests/unit/states/test_file.py +++ b/tests/unit/states/test_file.py @@ -612,6 +612,7 @@ class TestFileState(TestCase, LoaderModuleMockMixin): ret = filestate.managed( filename, contents_pillar='fnord', + encoding='utf-8' ) actual_contents = mock_manage.call_args[0][14] self.assertEqual(actual_contents, expected_contents) From ebff9b94e0b0298761929b62a5ad314ea84771b4 Mon Sep 17 00:00:00 2001 From: Ch3LL Date: Wed, 8 May 2019 15:50:35 -0400 Subject: [PATCH 2/2] Fix elasticsearch state module: allow user to define empty aliases --- salt/states/elasticsearch.py | 2 +- tests/unit/states/test_elasticsearch.py | 62 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/salt/states/elasticsearch.py b/salt/states/elasticsearch.py index 313372fc2b..e52897b9eb 100644 --- a/salt/states/elasticsearch.py +++ b/salt/states/elasticsearch.py @@ -282,7 +282,7 @@ def index_template_present(name, definition, check_definition=False): current_template = __salt__['elasticsearch.index_template_get'](name=name)[name] # Prune empty keys (avoid false positive diff) for key in ("mappings", "aliases", "settings"): - if current_template[key] == {}: + if current_template[key] == {} and key not in definition_parsed: del current_template[key] diff = __utils__['dictdiffer.deep_diff'](current_template, definition_parsed) if len(diff) != 0: diff --git a/tests/unit/states/test_elasticsearch.py b/tests/unit/states/test_elasticsearch.py index 19882c23b8..243fb0ca75 100644 --- a/tests/unit/states/test_elasticsearch.py +++ b/tests/unit/states/test_elasticsearch.py @@ -278,6 +278,68 @@ class ElasticsearchTestCase(TestCase, LoaderModuleMockMixin): ret.update({'comment': '', 'result': False, 'changes': {}}) self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret) + def test_index_template_present_check_definition(self): + ''' + Test to manage a elasticsearch index template. + with check_definition set + ''' + name = 'foo' + + index_template = {name: {"test2": "key", + "aliases": {}, + "mappings": {}, + "settings": {}}} + + expected = {'name': name, + 'result': True, + 'comment': 'Index template foo is already present and up to date', + 'changes': {}} + + mock_exists = MagicMock(side_effect=[True]) + mock_create = MagicMock(side_effect=[True]) + mock_get = MagicMock(side_effect=[index_template]) + + with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_template_get': mock_get, + 'elasticsearch.index_template_create': mock_create, + 'elasticsearch.index_template_exists': mock_exists}): + + ret = elasticsearch.index_template_present(name, + {"test2": "key", + "aliases": {}}, + check_definition=True) + self.assertDictEqual(expected, ret) + + def test_index_template_present_check_definition_alias_not_empty(self): + ''' + Test to manage a elasticsearch index template. + with check_definition set and alias is not empty + ''' + name = 'foo' + + index_template = {name: {"test2": "key", + "aliases": {}, + "mappings": {}, + "settings": {}}} + + expected = {'name': name, + 'result': True, + 'comment': 'Successfully updated index template foo', + 'changes': {'new': {'aliases': {'alias1': {}}}, 'old': {'aliases': {}}}} + + mock_exists = MagicMock(side_effect=[True]) + mock_create = MagicMock(side_effect=[True]) + mock_get = MagicMock(side_effect=[index_template]) + + with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_template_get': mock_get, + 'elasticsearch.index_template_create': mock_create, + 'elasticsearch.index_template_exists': mock_exists}): + + ret = elasticsearch.index_template_present(name, + {"test2": "key", + "aliases": {'alias1': {}}}, + check_definition=True) + self.assertDictEqual(expected, ret) + # 'pipeline_absent' function tests: 1 def test_pipeline_absent(self):