[7.x] Surface deprecation warnings from Elasticsearch
This commit is contained in:
@@ -4,6 +4,7 @@ import gzip
|
||||
import io
|
||||
from mock import Mock, patch
|
||||
import urllib3
|
||||
from urllib3._collections import HTTPHeaderDict
|
||||
import warnings
|
||||
from requests.auth import AuthBase
|
||||
from platform import python_version
|
||||
@@ -87,6 +88,48 @@ class TestBaseConnection(TestCase):
|
||||
"8af7ee35420f458e903026b4064081f2.westeurope.azure.elastic-cloud.com",
|
||||
)
|
||||
|
||||
def test_empty_warnings(self):
|
||||
con = Connection()
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
con._raise_warnings(())
|
||||
con._raise_warnings([])
|
||||
|
||||
self.assertEquals(w, [])
|
||||
|
||||
def test_raises_warnings(self):
|
||||
con = Connection()
|
||||
|
||||
with warnings.catch_warnings(record=True) as warn:
|
||||
con._raise_warnings(['299 Elasticsearch-7.6.1-aa751 "this is deprecated"'])
|
||||
|
||||
self.assertEquals([str(w.message) for w in warn], ["this is deprecated"])
|
||||
|
||||
with warnings.catch_warnings(record=True) as warn:
|
||||
con._raise_warnings(
|
||||
[
|
||||
'299 Elasticsearch-7.6.1-aa751 "this is also deprecated"',
|
||||
'299 Elasticsearch-7.6.1-aa751 "this is also deprecated"',
|
||||
'299 Elasticsearch-7.6.1-aa751 "guess what? deprecated"',
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEquals(
|
||||
[str(w.message) for w in warn],
|
||||
["this is also deprecated", "guess what? deprecated"],
|
||||
)
|
||||
|
||||
def test_raises_warnings_when_folded(self):
|
||||
con = Connection()
|
||||
with warnings.catch_warnings(record=True) as warn:
|
||||
con._raise_warnings(
|
||||
[
|
||||
'299 Elasticsearch-7.6.1-aa751 "warning",'
|
||||
'299 Elasticsearch-7.6.1-aa751 "folded"',
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEquals([str(w.message) for w in warn], ["warning", "folded"])
|
||||
|
||||
|
||||
class TestUrllib3Connection(TestCase):
|
||||
def _get_mock_connection(self, connection_params={}, response_body=b"{}"):
|
||||
@@ -94,7 +137,7 @@ class TestUrllib3Connection(TestCase):
|
||||
|
||||
def _dummy_urlopen(*args, **kwargs):
|
||||
dummy_response = Mock()
|
||||
dummy_response.headers = {}
|
||||
dummy_response.headers = HTTPHeaderDict({})
|
||||
dummy_response.status = 200
|
||||
dummy_response.data = response_body
|
||||
_dummy_urlopen.call_args = (args, kwargs)
|
||||
|
||||
@@ -8,8 +8,9 @@ from os import walk, environ
|
||||
from os.path import exists, join, dirname, pardir
|
||||
import yaml
|
||||
from shutil import rmtree
|
||||
import warnings
|
||||
|
||||
from elasticsearch import TransportError, RequestError
|
||||
from elasticsearch import TransportError, RequestError, ElasticsearchDeprecationWarning
|
||||
from elasticsearch.compat import string_types
|
||||
from elasticsearch.helpers.test import _get_version
|
||||
|
||||
@@ -30,6 +31,7 @@ IMPLEMENTED_FEATURES = {
|
||||
"headers",
|
||||
"catch_unauthorized",
|
||||
"default_shards",
|
||||
"warnings",
|
||||
}
|
||||
|
||||
# broken YAML tests on some releases
|
||||
@@ -40,6 +42,8 @@ SKIP_TESTS = {
|
||||
# Disallowing expensive queries is 7.7+
|
||||
"TestSearch320DisallowQueries",
|
||||
"TestIndicesPutIndexTemplate10Basic",
|
||||
"TestIndicesGetIndexTemplate10Basic",
|
||||
"TestIndicesGetIndexTemplate20GetMissing",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +154,7 @@ class YamlTestCase(ElasticsearchTestCase):
|
||||
|
||||
def run_code(self, test):
|
||||
""" Execute an instruction based on it's type. """
|
||||
print(test)
|
||||
for action in test:
|
||||
self.assertEquals(1, len(action))
|
||||
action_type, action = list(action.items())[0]
|
||||
@@ -164,6 +169,7 @@ class YamlTestCase(ElasticsearchTestCase):
|
||||
api = self.client
|
||||
headers = action.pop("headers", None)
|
||||
catch = action.pop("catch", None)
|
||||
warn = action.pop("warnings", None)
|
||||
self.assertEquals(1, len(action))
|
||||
|
||||
method, args = list(action.items())[0]
|
||||
@@ -184,17 +190,34 @@ class YamlTestCase(ElasticsearchTestCase):
|
||||
for k in args:
|
||||
args[k] = self._resolve(args[k])
|
||||
|
||||
try:
|
||||
self.last_response = api(**args)
|
||||
except Exception as e:
|
||||
if not catch:
|
||||
raise
|
||||
self.run_catch(catch, e)
|
||||
else:
|
||||
if catch:
|
||||
raise AssertionError(
|
||||
"Failed to catch %r in %r." % (catch, self.last_response)
|
||||
)
|
||||
warnings.simplefilter("always", category=ElasticsearchDeprecationWarning)
|
||||
with warnings.catch_warnings(record=True) as caught_warnings:
|
||||
try:
|
||||
self.last_response = api(**args)
|
||||
except Exception as e:
|
||||
if not catch:
|
||||
raise
|
||||
self.run_catch(catch, e)
|
||||
else:
|
||||
if catch:
|
||||
raise AssertionError(
|
||||
"Failed to catch %r in %r." % (catch, self.last_response)
|
||||
)
|
||||
|
||||
# Filter out warnings raised by other components.
|
||||
caught_warnings = [
|
||||
str(w.message)
|
||||
for w in caught_warnings
|
||||
if w.category == ElasticsearchDeprecationWarning
|
||||
]
|
||||
|
||||
# Sorting removes the issue with order raised. We only care about
|
||||
# if all warnings are raised in the single API call.
|
||||
if warn is not None and sorted(warn) != sorted(caught_warnings):
|
||||
raise AssertionError(
|
||||
"Expected warnings not equal to actual warnings: expected=%r actual=%r"
|
||||
% (warn, caught_warnings)
|
||||
)
|
||||
|
||||
def _get_nodes(self):
|
||||
if not hasattr(self, "_node_info"):
|
||||
|
||||
Reference in New Issue
Block a user