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)
def test_index_present(): """ 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, }, ): assert elasticsearch.index_present(name) == ret ret.update({ "comment": "Successfully created index foo", "changes": { "new": { "test": "key" } }, }) assert elasticsearch.index_present(name) == ret ret.update({ "comment": "Cannot create index foo, False", "result": False, "changes": {}, }) assert 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" } }, }) assert elasticsearch.index_present(name, {"test2": "key"}) == ret ret.update({"comment": "", "result": False, "changes": {}}) assert elasticsearch.index_absent(name) == ret ret.update({"comment": "", "result": False, "changes": {}}) assert elasticsearch.index_absent(name) == ret ret.update({"comment": "", "result": False, "changes": {}}) assert elasticsearch.index_absent(name) == ret