Skip to content

Commit

Permalink
Merge pull request #52943 from Ch3LL/fix_elastisearch
Browse files Browse the repository at this point in the history
Fix elasticsearch state module: allow user to define empty aliases
  • Loading branch information
dwoz committed May 9, 2019
2 parents 9b290b6 + 451fb7e commit 4437764
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion salt/states/elasticsearch.py
Expand Up @@ -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:
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/states/test_elasticsearch.py
Expand Up @@ -278,6 +278,68 @@ def test_index_template_present(self):
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):
Expand Down

0 comments on commit 4437764

Please sign in to comment.