salt/tests/unit/states/test_elasticsearch.py
Erik Johnson 3184168365 Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.

A summary of the changes in this PR is as follows:

* Moves the following functions from `salt.utils` to new locations
  (including a deprecation warning if invoked from `salt.utils`):
  `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
  `dequote`, `is_hex`, `is_bin_str`, `rand_string`,
  `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
  `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
  `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
  `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
  `is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
  `salt/utils/__init__.py` for better organization, so we can keep the
  deprecated ones separate from the ones yet to be deprecated as we
  continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
  unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
  import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
  accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
  such that it returns a string matching the passed size. Previously
  this function would get `size` bytes from `os.urandom()`,
  base64-encode it, and return the result, which would in most cases not
  be equal to the passed size.
2017-08-08 13:33:43 -05:00

445 lines
22 KiB
Python

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Lukas Raska <lukas@raska.me>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch)
from salt.exceptions import CommandExecutionError
# Import Salt Libs
import salt.utils.dictdiffer as dictdiffer
from salt.states import elasticsearch
@skipIf(NO_MOCK, NO_MOCK_REASON)
class ElasticsearchTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.states.elasticsearch
'''
def setup_loader_modules(self):
return {
elasticsearch: {
'__opts__': {'test': False},
'__utils__': {'dictdiffer.deep_diff': dictdiffer.deep_diff}
}
}
# 'index_absent' function tests: 1
def test_index_absent(self):
'''
Test to manage a elasticsearch index.
'''
name = 'foo'
ret = {'name': name,
'result': True,
'comment': 'Index foo is already absent',
'changes': {}}
mock_get = MagicMock(side_effect=[None, {name: {"test": "key"}}, {name: {}}, {name: {"test": "key"}}, CommandExecutionError, {name: {"test": "key"}}])
mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_get': mock_get,
'elasticsearch.index_delete': mock_delete}):
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': 'Successfully removed index foo', 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': 'Failed to remove index foo for unknown reasons', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Index foo will be removed", 'result': None, 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
# 'index_present' function tests: 1
def test_index_present(self):
'''
Test to manage a elasticsearch index.
'''
name = 'foo'
ret = {'name': name,
'result': True,
'comment': 'Index foo is already present',
'changes': {}}
mock_exists = MagicMock(side_effect=[True, False, False, False, CommandExecutionError, False, False])
mock_get = MagicMock(side_effect=[{name: {"test": "key"}}, CommandExecutionError])
mock_create = MagicMock(side_effect=[True, False, CommandExecutionError, True])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_get': mock_get,
'elasticsearch.index_exists': mock_exists,
'elasticsearch.index_create': mock_create}):
self.assertDictEqual(elasticsearch.index_present(name), ret)
ret.update({'comment': 'Successfully created index foo', 'changes': {"new": {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_present(name), ret)
ret.update({'comment': 'Cannot create index foo, False', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_present(name), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Index foo does not exist and will be created", 'result': None, 'changes': {"new": {"test2": "key"}}})
self.assertDictEqual(elasticsearch.index_present(name, {"test2": "key"}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_absent(name), ret)
# 'alias_absent' function tests: 1
def test_alias_absent(self):
'''
Test to manage a elasticsearch alias.
'''
name = 'foo'
index = 'bar'
alias = {index: {"aliases": {name: {"test": "key"}}}}
ret = {'name': name,
'result': True,
'comment': 'Alias foo for index bar is already absent',
'changes': {}}
mock_get = MagicMock(side_effect=[None, {"foo2": {}}, alias, alias, alias, CommandExecutionError, alias])
mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.alias_get': mock_get,
'elasticsearch.alias_delete': mock_delete}):
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
ret.update({'comment': 'Successfully removed alias foo for index bar', 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
ret.update({'comment': 'Failed to remove alias foo for index bar for unknown reasons', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Alias foo for index bar will be removed", 'result': None, 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.alias_absent(name, index), ret)
# 'alias_present' function tests: 1
def test_alias_present(self):
'''
Test to manage a elasticsearch alias.
'''
name = 'foo'
index = 'bar'
alias = {index: {"aliases": {name: {"test": "key"}}}}
ret = {'name': name,
'result': True,
'comment': 'Alias foo for index bar is already present',
'changes': {}}
mock_get = MagicMock(side_effect=[alias, alias, None, None, None, alias, CommandExecutionError, None])
mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.alias_get': mock_get,
'elasticsearch.alias_create': mock_create}):
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test": "key"}), ret)
ret.update({'comment': "Successfully replaced alias foo for index bar", 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test2": "key"}), ret)
ret.update({'comment': "Successfully created alias foo for index bar", 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test2": "key"}), ret)
ret.update({'comment': 'Cannot create alias foo for index bar, False', 'result': False})
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test2": "key"}), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Alias foo for index bar does not exist and will be created", 'result': None, 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test2": "key"}), ret)
ret.update({'comment': "Alias foo for index bar exists with wrong configuration and will be overriden", 'result': None, 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.alias_present(name, index, {"test2": "key"}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.alias_present(name, index), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.alias_present(name, index), ret)
# 'index_template_absent' function tests: 1
def test_index_template_absent(self):
'''
Test to manage a elasticsearch index template.
'''
name = 'foo'
index_template = {name: {"test": "key"}}
ret = {'name': name,
'result': True,
'comment': 'Index template foo is already absent',
'changes': {}}
mock_get = MagicMock(side_effect=[None, {"bar": {}}, index_template, index_template, index_template, CommandExecutionError, index_template])
mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_template_get': mock_get,
'elasticsearch.index_template_delete': mock_delete}):
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
ret.update({'comment': 'Successfully removed index template foo', 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
ret.update({'comment': 'Failed to remove index template foo for unknown reasons', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Index template foo will be removed", 'result': None, 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_absent(name), ret)
# 'index_template_present' function tests: 1
def test_index_template_present(self):
'''
Test to manage a elasticsearch index template.
'''
name = 'foo'
index_template = {name: {"test": "key"}}
ret = {'name': name,
'result': True,
'comment': 'Index template foo is already present',
'changes': {}}
mock_exists = MagicMock(side_effect=[True, False, False, False, CommandExecutionError, False, False])
mock_create = MagicMock(side_effect=[True, False, CommandExecutionError, True])
mock_get = MagicMock(side_effect=[index_template, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.index_template_get': mock_get,
'elasticsearch.index_template_create': mock_create,
'elasticsearch.index_template_exists': mock_exists}):
self.assertDictEqual(elasticsearch.index_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': "Successfully created index template foo", 'changes': {'new': {"test": "key"}}})
self.assertDictEqual(elasticsearch.index_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': 'Cannot create index template foo, False', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_present(name, {"test2": "key"}), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Index template foo does not exist and will be created", 'result': None, 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.index_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.index_template_present(name, {}), ret)
# 'pipeline_absent' function tests: 1
def test_pipeline_absent(self):
'''
Test to manage a elasticsearch pipeline.
'''
name = 'foo'
pipeline = {name: {"test": "key"}}
ret = {'name': name,
'result': True,
'comment': 'Pipeline foo is already absent',
'changes': {}}
mock_get = MagicMock(side_effect=[None, {"foo2": {}}, pipeline, pipeline, pipeline, CommandExecutionError, pipeline])
mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.pipeline_get': mock_get,
'elasticsearch.pipeline_delete': mock_delete}):
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
ret.update({'comment': 'Successfully removed pipeline foo', 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
ret.update({'comment': 'Failed to remove pipeline foo for unknown reasons', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Pipeline foo will be removed", 'result': None, 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.pipeline_absent(name), ret)
# 'pipeline_present' function tests: 1
def test_pipeline_present(self):
'''
Test to manage a elasticsearch pipeline.
'''
name = 'foo'
pipeline = {name: {"test": "key"}}
ret = {'name': name,
'result': True,
'comment': 'Pipeline foo is already present',
'changes': {}}
mock_get = MagicMock(side_effect=[pipeline, pipeline, None, None, None, pipeline, CommandExecutionError, None])
mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.pipeline_get': mock_get,
'elasticsearch.pipeline_create': mock_create}):
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test": "key"}), ret)
ret.update({'comment': "Successfully replaced pipeline foo", 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test2": "key"}), ret)
ret.update({'comment': "Successfully created pipeline foo", 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test2": "key"}), ret)
ret.update({'comment': 'Cannot create pipeline foo, False', 'result': False})
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test2": "key"}), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Pipeline foo does not exist and will be created", 'result': None, 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test2": "key"}), ret)
ret.update({'comment': "Pipeline foo exists with wrong configuration and will be overriden", 'result': None, 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {"test2": "key"}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.pipeline_present(name, {}), ret)
# 'search_template_absent' function tests: 1
def test_search_template_absent(self):
'''
Test to manage a elasticsearch search template.
'''
name = 'foo'
template = {"template": '{"test": "key"}'}
ret = {'name': name,
'result': True,
'comment': 'Search template foo is already absent',
'changes': {}}
mock_get = MagicMock(side_effect=[None, template, template, template, CommandExecutionError, template])
mock_delete = MagicMock(side_effect=[True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.search_template_get': mock_get,
'elasticsearch.search_template_delete': mock_delete}):
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
ret.update({'comment': 'Successfully removed search template foo', 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
ret.update({'comment': 'Failed to remove search template foo for unknown reasons', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Search template foo will be removed", 'result': None, 'changes': {"old": {"test": "key"}}})
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.search_template_absent(name), ret)
# 'pipeline_present' function tests: 1
def test_search_template_present(self):
'''
Test to manage a elasticsearch search template.
'''
name = 'foo'
template = {"template": '{"test": "key"}'}
ret = {'name': name,
'result': True,
'comment': 'Search template foo is already present',
'changes': {}}
mock_get = MagicMock(side_effect=[template, template, None, None, None, template, CommandExecutionError, None])
mock_create = MagicMock(side_effect=[True, True, False, CommandExecutionError])
with patch.dict(elasticsearch.__salt__, {'elasticsearch.search_template_get': mock_get,
'elasticsearch.search_template_create': mock_create}):
self.assertDictEqual(elasticsearch.search_template_present(name, {"test": "key"}), ret)
ret.update({'comment': "Successfully replaced search template foo", 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': "Successfully created search template foo", 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': 'Cannot create search template foo, False', 'result': False})
self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)
with patch.dict(elasticsearch.__opts__, {'test': True}):
ret.update({'comment': "Search template foo does not exist and will be created", 'result': None, 'changes': {'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': "Search template foo exists with wrong configuration and will be overriden", 'result': None, 'changes': {'old': {"test": "key"}, 'new': {"test2": "key"}}})
self.assertDictEqual(elasticsearch.search_template_present(name, {"test2": "key"}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)
ret.update({'comment': '', 'result': False, 'changes': {}})
self.assertDictEqual(elasticsearch.search_template_present(name, {}), ret)