From e022932ed19c5bf295362ad753b363d4a45a33fb Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Wed, 26 Jul 2023 12:18:45 -0500 Subject: [PATCH] Add the ability to run test patterns. (#454) Signed-off-by: dblock --- .ci/functions/imports.sh | 2 +- .ci/make.sh | 2 +- .ci/run-opensearch.sh | 2 +- .ci/run-repository.sh | 3 ++ .ci/run-tests | 3 +- CHANGELOG.md | 1 + DEVELOPER_GUIDE.md | 12 +++++ dev-requirements.txt | 1 + test_opensearchpy/run_tests.py | 95 ++++++++++++++++++---------------- 9 files changed, 72 insertions(+), 49 deletions(-) diff --git a/.ci/functions/imports.sh b/.ci/functions/imports.sh index 0146a121..0984fba2 100755 --- a/.ci/functions/imports.sh +++ b/.ci/functions/imports.sh @@ -28,7 +28,7 @@ if [[ -z $opensearch_node_name ]]; then fi - export script_path=$(dirname $(realpath -s $0)) + export script_path=$(dirname $(realpath $0)) source $script_path/functions/cleanup.sh source $script_path/functions/wait-for-container.sh trap "cleanup_trap ${network_name}" EXIT diff --git a/.ci/make.sh b/.ci/make.sh index 1e8985dd..c1ab9fba 100755 --- a/.ci/make.sh +++ b/.ci/make.sh @@ -24,7 +24,7 @@ # Bootstrap # ------------------------------------------------------- # -script_path=$(dirname "$(realpath -s "$0")") +script_path=$(dirname "$(realpath "$0")") repo=$(realpath "$script_path/../") # shellcheck disable=SC1090 diff --git a/.ci/run-opensearch.sh b/.ci/run-opensearch.sh index 308a37aa..87ad1afe 100755 --- a/.ci/run-opensearch.sh +++ b/.ci/run-opensearch.sh @@ -5,7 +5,7 @@ # # Export the NUMBER_OF_NODES variable to start more than 1 node -script_path=$(dirname $(realpath -s $0)) +script_path=$(dirname $(realpath $0)) source $script_path/functions/imports.sh set -euo pipefail diff --git a/.ci/run-repository.sh b/.ci/run-repository.sh index fecf6e4b..df9daae2 100755 --- a/.ci/run-repository.sh +++ b/.ci/run-repository.sh @@ -38,7 +38,10 @@ docker run \ --env "TEST_SUITE=${TEST_SUITE}" \ --env "PYTHON_CONNECTION_CLASS=${PYTHON_CONNECTION_CLASS}" \ --env "TEST_TYPE=server" \ + --env "TEST_PATTERN=${TEST_PATTERN}" \ --name opensearch-py \ --rm \ opensearch-project/opensearch-py \ python setup.py test + +unset TEST_PATTERN diff --git a/.ci/run-tests b/.ci/run-tests index 9b606485..4ac6f0b0 100755 --- a/.ci/run-tests +++ b/.ci/run-tests @@ -9,6 +9,7 @@ export PYTHON_CONNECTION_CLASS="${PYTHON_CONNECTION_CLASS:=Urllib3HttpConnection export CLUSTER="${CLUSTER:-opensearch}" export SECURE_INTEGRATION="${1:-false}" export OPENSEARCH_VERSION="${2:-latest}" +export TEST_PATTERN="${3:-}" if [[ "$SECURE_INTEGRATION" == "true" ]]; then export OPENSEARCH_URL_EXTENSION="https" else @@ -21,7 +22,7 @@ if [[ "$OPENSEARCH_VERSION" == *"SNAPSHOT" ]]; then fi echo -e "\033[1m>>>>> Unreleased is $IS_UNRELEASED >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\033[0m" -script_path=$(dirname $(realpath -s $0)) +script_path=$(dirname $(realpath $0)) source $script_path/functions/imports.sh set -euo pipefail diff --git a/CHANGELOG.md b/CHANGELOG.md index d1546fad..38e970cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added samples ([#447](https://github.com/opensearch-project/opensearch-py/pull/447)) - Improved CI performance of integration with unreleased OpenSearch ([#318](https://github.com/opensearch-project/opensearch-py/pull/318)) - Added k-NN guide and samples ([#449](https://github.com/opensearch-project/opensearch-py/pull/449)) +- Added the ability to run tests matching a pattern to `.ci/run-tests` ([#454](https://github.com/opensearch-project/opensearch-py/pull/454)) ### Changed - Moved security from `plugins` to `clients` ([#442](https://github.com/opensearch-project/opensearch-py/pull/442)) ### Deprecated diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md index fad93de0..963a11ad 100644 --- a/DEVELOPER_GUIDE.md +++ b/DEVELOPER_GUIDE.md @@ -17,6 +17,8 @@ $ python --version Python 3.11.1 ``` +You can install dev requirements with `pip install -r dev-requirements.txt`, but it's better to use the docker setup described below. + Install [Nox](https://nox.thea.codes/en/stable/) for task management. ``` @@ -69,6 +71,16 @@ The following example runs tests against the latest SNAPSHOT build of OpenSearch ./.ci/run-tests opensearch false SNAPSHOT ``` +You can also run individual tests matching a pattern (`pytest -k [pattern]`). + +``` +./.ci/run-tests true 1.3.0 test_no_http_compression + +test_opensearchpy/test_connection.py::TestUrllib3Connection::test_no_http_compression PASSED [ 33%] +test_opensearchpy/test_connection.py::TestRequestsConnection::test_no_http_compression PASSED [ 66%] +test_opensearchpy/test_async/test_connection.py::TestAIOHttpConnection::test_no_http_compression PASSED [100%] +``` + Note that integration tests require docker to be installed and running, and downloads quite a bit of data from over the internet and hence take few minutes to complete. ## Linter diff --git a/dev-requirements.txt b/dev-requirements.txt index 412797c5..1714dd74 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -6,6 +6,7 @@ mock sphinx<7.1 sphinx_rtd_theme jinja2 +pytz # No wheels for Python 3.10 yet! numpy; python_version<"3.10" diff --git a/test_opensearchpy/run_tests.py b/test_opensearchpy/run_tests.py index 269144c4..e0461af7 100755 --- a/test_opensearchpy/run_tests.py +++ b/test_opensearchpy/run_tests.py @@ -111,58 +111,63 @@ def run_all(argv=None): if environ.get("OPENSEARCH_URL", "").startswith("https://"): secured = True - ignores = [] - # Python 3.6+ is required for async - if sys.version_info < (3, 6): - ignores.append("test_opensearchpy/test_async/") - - ignores.extend( - [ - "test_opensearchpy/test_server/", - "test_opensearchpy/test_server_secured/", - "test_opensearchpy/test_async/test_server/", - "test_opensearchpy/test_async/test_server_secured/", - ] - ) - - # Jenkins/Github actions, only run server tests - if environ.get("TEST_TYPE") == "server": - test_dir = abspath(dirname(__file__)) - if secured: - argv.append(join(test_dir, "test_server_secured")) - if sys.version_info >= (3, 6): - argv.append(join(test_dir, "test_async/test_server_secured")) - ignores.extend( - [ - "test_opensearchpy/test_server/", - "test_opensearchpy/test_async/test_server/", - ] - ) - else: - argv.append(join(test_dir, "test_server")) - if sys.version_info >= (3, 6): - argv.append(join(test_dir, "test_async/test_server")) - ignores.extend( - [ - "test_opensearchpy/test_server_secured/", - ] - ) + # check TEST_PATTERN env var for specific test to run + test_pattern = environ.get("TEST_PATTERN") + if test_pattern: + argv.append("-k %s" % test_pattern) + else: + ignores = [] + # Python 3.6+ is required for async + if sys.version_info < (3, 6): + ignores.append("test_opensearchpy/test_async/") - # There are no plugins for unreleased versions of opensearch - if environ.get("OPENSEARCH_VERSION") == "SNAPSHOT": ignores.extend( [ - "test_opensearchpy/test_server/test_plugins/", - "test_opensearchpy/test_async/test_server/test_plugins/", + "test_opensearchpy/test_server/", + "test_opensearchpy/test_server_secured/", + "test_opensearchpy/test_async/test_server/", + "test_opensearchpy/test_async/test_server_secured/", ] ) - if ignores: - argv.extend(["--ignore=%s" % ignore for ignore in ignores]) + # Jenkins/Github actions, only run server tests + if environ.get("TEST_TYPE") == "server": + test_dir = abspath(dirname(__file__)) + if secured: + argv.append(join(test_dir, "test_server_secured")) + if sys.version_info >= (3, 6): + argv.append(join(test_dir, "test_async/test_server_secured")) + ignores.extend( + [ + "test_opensearchpy/test_server/", + "test_opensearchpy/test_async/test_server/", + ] + ) + else: + argv.append(join(test_dir, "test_server")) + if sys.version_info >= (3, 6): + argv.append(join(test_dir, "test_async/test_server")) + ignores.extend( + [ + "test_opensearchpy/test_server_secured/", + ] + ) + + # There are no plugins for unreleased versions of opensearch + if environ.get("OPENSEARCH_VERSION") == "SNAPSHOT": + ignores.extend( + [ + "test_opensearchpy/test_server/test_plugins/", + "test_opensearchpy/test_async/test_server/test_plugins/", + ] + ) - # Not in CI, run all tests specified. - else: - argv.append(abspath(dirname(__file__))) + if ignores: + argv.extend(["--ignore=%s" % ignore for ignore in ignores]) + + # Not in CI, run all tests specified. + else: + argv.append(abspath(dirname(__file__))) exit_code = 0 try: