Skip to content

Commit 8d0773a

Browse files
authored
Allow integration tests to be run from IDE
1 parent f6a28ac commit 8d0773a

File tree

9 files changed

+260
-73
lines changed

9 files changed

+260
-73
lines changed

.ci/run-elasticsearch.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Export the TEST_SUITE variable, eg. 'free' or 'platinum' defaults to 'free'.
88
# Export the NUMBER_OF_NODES variable to start more than 1 node
99

10-
# Version 1.3.0
10+
# Version 1.4.0
1111
# - Initial version of the run-elasticsearch.sh script
1212
# - Deleting the volume should not dependent on the container still running
1313
# - Fixed `ES_JAVA_OPTS` config
@@ -17,6 +17,7 @@
1717
# - Added 5 retries on docker pull for fixing transient network errors
1818
# - Added flags to make local CCR configurations work
1919
# - Added action.destructive_requires_name=false as the default will be true in v8
20+
# - Added ingest.geoip.downloader.enabled=false as it causes false positives in testing
2021

2122
script_path=$(dirname $(realpath -s $0))
2223
source $script_path/functions/imports.sh
@@ -40,6 +41,7 @@ environment=($(cat <<-END
4041
--env path.repo=/tmp
4142
--env repositories.url.allowed_urls=http://snapshot.test*
4243
--env action.destructive_requires_name=false
44+
--env ingest.geoip.downloader.enabled=false
4345
END
4446
))
4547
if [[ "$TEST_SUITE" == "platinum" ]]; then
@@ -48,6 +50,7 @@ if [[ "$TEST_SUITE" == "platinum" ]]; then
4850
--env xpack.license.self_generated.type=trial
4951
--env xpack.security.enabled=true
5052
--env xpack.security.http.ssl.enabled=true
53+
--env xpack.security.http.ssl.verification_mode=certificate
5154
--env xpack.security.http.ssl.key=certs/testnode.key
5255
--env xpack.security.http.ssl.certificate=certs/testnode.crt
5356
--env xpack.security.http.ssl.certificate_authorities=certs/ca.crt

elasticsearch/_async/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async def map_actions():
190190
raise_on_error,
191191
ignore_status,
192192
*args,
193-
**kwargs
193+
**kwargs,
194194
),
195195
):
196196

@@ -458,5 +458,5 @@ async def _change_doc_index(hits, index):
458458
target_client,
459459
_change_doc_index(docs, target_index),
460460
chunk_size=chunk_size,
461-
**kwargs
461+
**kwargs,
462462
)

elasticsearch/helpers/test.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import os
2121
import time
22+
from os.path import abspath, dirname, join
2223
from unittest import SkipTest, TestCase
2324

2425
from elasticsearch import Elasticsearch
@@ -29,10 +30,12 @@
2930
else:
3031
ELASTICSEARCH_URL = "https://elastic:changeme@localhost:9200"
3132

33+
CA_CERTS = join(dirname(dirname(dirname(abspath(__file__)))), ".ci/certs/ca.pem")
34+
3235

3336
def get_test_client(nowait=False, **kwargs):
3437
# construct kwargs from the environment
35-
kw = {"timeout": 30, "ca_certs": ".ci/certs/ca.pem"}
38+
kw = {"timeout": 30, "ca_certs": CA_CERTS}
3639
if "PYTHON_CONNECTION_CLASS" in os.environ:
3740
from elasticsearch import connection
3841

@@ -55,13 +58,6 @@ def get_test_client(nowait=False, **kwargs):
5558
raise SkipTest("Elasticsearch failed to start.")
5659

5760

58-
def _get_version(version_string):
59-
if "." not in version_string:
60-
return ()
61-
version = version_string.strip().split(".")
62-
return tuple(int(v) if v.isdigit() else 999 for v in version)
63-
64-
6561
class ElasticsearchTestCase(TestCase):
6662
@staticmethod
6763
def _get_client():
@@ -85,6 +81,16 @@ def teardown_method(self, _):
8581

8682
def es_version(self):
8783
if not hasattr(self, "_es_version"):
88-
version_string = self.client.info()["version"]["number"]
89-
self._es_version = _get_version(version_string)
84+
self._es_version = es_version(self.client)
9085
return self._es_version
86+
87+
88+
def _get_version(version_string):
89+
if "." not in version_string:
90+
return ()
91+
version = version_string.strip().split(".")
92+
return tuple(int(v) if v.isdigit() else 999 for v in version)
93+
94+
95+
def es_version(client):
96+
return _get_version(client.info()["version"]["number"])

elasticsearch/helpers/test.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from unittest import TestCase
2121
from ..client import Elasticsearch
2222

2323
ELASTICSEARCH_URL: str
24+
CA_CERTS: str
2425

2526
def get_test_client(nowait: bool = ..., **kwargs: Any) -> Elasticsearch: ...
2627
def _get_version(version_string: str) -> Tuple[int, ...]: ...

test_elasticsearch/test_async/test_server/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pytest
2121

2222
import elasticsearch
23-
from elasticsearch.helpers.test import ELASTICSEARCH_URL
23+
from elasticsearch.helpers.test import CA_CERTS, ELASTICSEARCH_URL
2424

2525
from ...utils import wipe_cluster
2626

@@ -34,7 +34,7 @@ async def async_client():
3434
if not hasattr(elasticsearch, "AsyncElasticsearch"):
3535
pytest.skip("test requires 'AsyncElasticsearch'")
3636

37-
kw = {"timeout": 3, "ca_certs": ".ci/certs/ca.pem"}
37+
kw = {"timeout": 3, "ca_certs": CA_CERTS}
3838
client = elasticsearch.AsyncElasticsearch(ELASTICSEARCH_URL, **kw)
3939

4040
# wait for yellow status

test_elasticsearch/test_async/test_server/test_rest_api_spec.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,26 @@ async def await_if_coro(x):
5050

5151
class AsyncYamlRunner(YamlRunner):
5252
async def setup(self):
53+
# Pull skips from individual tests to not do unnecessary setup.
54+
skip_code = []
55+
for action in self._run_code:
56+
assert len(action) == 1
57+
action_type, _ = list(action.items())[0]
58+
if action_type == "skip":
59+
skip_code.append(action)
60+
else:
61+
break
62+
63+
if self._setup_code or skip_code:
64+
self.section("setup")
65+
if skip_code:
66+
await self.run_code(skip_code)
5367
if self._setup_code:
5468
await self.run_code(self._setup_code)
5569

5670
async def teardown(self):
5771
if self._teardown_code:
72+
self.section("teardown")
5873
await self.run_code(self._teardown_code)
5974

6075
async def es_version(self):
@@ -67,19 +82,26 @@ async def es_version(self):
6782
ES_VERSION = tuple(int(v) if v.isdigit() else 999 for v in version)
6883
return ES_VERSION
6984

85+
def section(self, name):
86+
print(("=" * 10) + " " + name + " " + ("=" * 10))
87+
7088
async def run(self):
7189
try:
7290
await self.setup()
91+
self.section("test")
7392
await self.run_code(self._run_code)
7493
finally:
75-
await self.teardown()
94+
try:
95+
await self.teardown()
96+
except Exception:
97+
pass
7698

7799
async def run_code(self, test):
78100
"""Execute an instruction based on it's type."""
79-
print(test)
80101
for action in test:
81102
assert len(action) == 1
82103
action_type, action = list(action.items())[0]
104+
print(action_type, action)
83105

84106
if hasattr(self, "run_" + action_type):
85107
await await_if_coro(getattr(self, "run_" + action_type)(action))

test_elasticsearch/test_server/conftest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pytest
2222

2323
import elasticsearch
24-
from elasticsearch.helpers.test import ELASTICSEARCH_URL
24+
from elasticsearch.helpers.test import CA_CERTS, ELASTICSEARCH_URL
2525

2626
from ..utils import wipe_cluster
2727

@@ -38,15 +38,23 @@ def sync_client_factory():
3838
try:
3939
# Configure the client with certificates and optionally
4040
# an HTTP conn class depending on 'PYTHON_CONNECTION_CLASS' envvar
41-
kw = {"timeout": 3, "ca_certs": ".ci/certs/ca.pem"}
41+
kw = {
42+
"timeout": 3,
43+
"ca_certs": CA_CERTS,
44+
"headers": {"Authorization": "Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="},
45+
}
4246
if "PYTHON_CONNECTION_CLASS" in os.environ:
4347
from elasticsearch import connection
4448

4549
kw["connection_class"] = getattr(
4650
connection, os.environ["PYTHON_CONNECTION_CLASS"]
4751
)
4852

49-
client = elasticsearch.Elasticsearch(ELASTICSEARCH_URL, **kw)
53+
# We do this little dance with the URL to force
54+
# Requests to respect 'headers: None' within rest API spec tests.
55+
client = elasticsearch.Elasticsearch(
56+
ELASTICSEARCH_URL.replace("elastic:changeme@", ""), **kw
57+
)
5058

5159
# Wait for the cluster to report a status of 'yellow'
5260
for _ in range(100):

0 commit comments

Comments
 (0)