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)
def test_alias_present(): """ 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, }, ): assert 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" } }, }) assert elasticsearch.alias_present(name, index, {"test2": "key"}) == ret ret.update({ "comment": "Successfully created alias foo for index bar", "changes": { "new": { "test2": "key" } }, }) assert elasticsearch.alias_present(name, index, {"test2": "key"}) == ret ret.update({ "comment": "Cannot create alias foo for index bar, False", "result": False, }) assert 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" } }, }) assert elasticsearch.alias_present(name, index, {"test2": "key"}) == ret ret.update({ "comment": ("Alias foo for index bar exists with wrong configuration and" " will be overridden"), "result": None, "changes": { "old": { "test": "key" }, "new": { "test2": "key" } }, }) assert elasticsearch.alias_present(name, index, {"test2": "key"}) == ret ret.update({"comment": "", "result": False, "changes": {}}) assert elasticsearch.alias_present(name, index) == ret ret.update({"comment": "", "result": False, "changes": {}}) assert elasticsearch.alias_present(name, index) == ret