Repository: ClearcodeHQ/pytest-elasticsearch (language: python)

Issue:
4.0.0 is not compatible with elasticsearch<8
There is an issue running 4.0.0 with `elasticsearch<8`, as shown by:
```python
test setup failed
request = <SubRequest 'es_client' for <Function test_main>>

    @pytest.fixture
    def elasticsearch_fixture(request: FixtureRequest) -> Iterator[Elasticsearch]:
        """Elasticsearch client fixture."""
        process = request.getfixturevalue(process_fixture_name)
        if not process.running():
            process.start()
        client = Elasticsearch(
            hosts=[{"host": process.host, "port": process.port, "scheme": "http"}],
            request_timeout=30,
            verify_certs=False,
        )
>       client.options(ignore_status=400)
E       AttributeError: 'Elasticsearch' object has no attribute 'options'
```
The `options` attribute was added in version 8 of `elasticsearch`.


Discussion / hints:
Code line:
https://github.com/ClearcodeHQ/pytest-elasticsearch/pull/384/files#diff-f5db3c7f7a1894e48c35c7de3b9bb15ec1fb4b43573844b72b59557d847e900fR178

`elasticsearch=>8` allows to run tests only against Elasticsearch 7.17 if I recall correctly.

But an additional test run with elasticsearch<8 for that run on CI would be good to have.


Fix patch:
diff --git a/.github/actions/elastic_7/action.yml b/.github/actions/elastic_7/action.yml
new file mode 100644
index 00000000..8c4dbf8b
--- /dev/null
+++ b/.github/actions/elastic_7/action.yml
@@ -0,0 +1,42 @@
+name: 'Pytest run'
+description: 'Greet someone'
+inputs:
+  python-version:  # id of input
+    description: 'Python version to use'
+    required: true
+  elasticsearch:
+    description: 'Elasticsearch version'
+    required: true
+runs:
+  using: "composite"
+  steps:
+  - name: Set up Python ${{ inputs.python-version }}
+    uses: actions/setup-python@v3
+    with:
+      python-version: ${{ inputs.python-version }}
+  - uses: actions/cache@v3
+    with:
+      path: ~/elasticsearch
+      key: elasticsearch-${{ inputs.elasticsearch }}
+  - name: Setup elasticsearch
+    uses: ankane/setup-elasticsearch@v1
+    with:
+      elasticsearch-version: ${{ inputs.elasticsearch }}
+  - name: Prep environment
+    shell: bash
+    run: pip install pipenv && pipenv install --dev --skip-lock
+  - name: Downgrade elasticsearch
+    shell: bash
+    run: pipenv run pip install "elasticsearch<8"
+  - name: Run test
+    shell: bash
+    run: pipenv run pytest --elasticsearch-executable=$ES_HOME/bin/elasticsearch -n 0 --cov-report=xml
+  - name: Run xdist test
+    shell: bash
+    run: pipenv run pytest --elasticsearch-executable=$ES_HOME/bin/elasticsearch -n 1 --cov-report=xml:coverage-xdist.xml
+  - name: Upload coverage to Codecov
+    uses: codecov/codecov-action@v3.0.0
+    with:
+      flags: linux,elastic-${{ inputs.elasticsearch }}
+      env_vars: OS, PYTHON
+      fail_ci_if_error: false
\ No newline at end of file
diff --git a/newsfragments/512.bugfix.rst b/newsfragments/512.bugfix.rst
new file mode 100644
index 00000000..bad8b2a4
--- /dev/null
+++ b/newsfragments/512.bugfix.rst
@@ -0,0 +1,1 @@
+Fixed compatibility with elasticsearch client 7.x
\ No newline at end of file
diff --git a/pyproject.toml b/pyproject.toml
index dc8d9be9..378d70df 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -30,7 +30,7 @@ dependencies = [
     "pytest >= 6.2",
     "port-for >= 0.7.1",
     "mirakuru",
-    "elasticsearch",
+    "elasticsearch >= 7",
 ]
 requires-python = ">= 3.8"
 


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 84997516..a924a239 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -157,3 +157,19 @@ jobs:
         with:
           python-version: ${{ matrix.python-version }}
           elasticsearch: "7.17"
+  tests_7_17_lib_7:
+    needs: [tests_7_17]
+    runs-on: ubuntu-latest
+    strategy:
+      fail-fast: true
+      matrix:
+        python-version: ["3.11"]
+    env:
+      OS: ubuntu-latest
+      PYTHON: ${{ matrix.python-version }}
+    steps:
+      - uses: actions/checkout@v3
+      - uses: ./.github/actions/elastic_7
+        with:
+          python-version: ${{ matrix.python-version }}
+          elasticsearch: "7.17"
diff --git a/pytest_elasticsearch/factories.py b/pytest_elasticsearch/factories.py
index 115330cb..4b2accc3 100644
--- a/pytest_elasticsearch/factories.py
+++ b/pytest_elasticsearch/factories.py
@@ -22,6 +22,7 @@
 
 import pytest
 from elasticsearch import Elasticsearch
+from elasticsearch import __version__ as elastic_version
 from mirakuru import ProcessExitedWithError
 from port_for import get_port
 from port_for.api import PortType
@@ -101,7 +102,6 @@ def elasticsearch_proc_fixture(
             elasticsearch_index_store_type,
             timeout=60,
         )
-        print(elasticsearch_executor.command)
 
         elasticsearch_executor.start()
         yield elasticsearch_executor
@@ -161,7 +161,8 @@ def elasticsearch_fixture(request: FixtureRequest) -> Iterator[Elasticsearch]:
             request_timeout=30,
             verify_certs=False,
         )
-        client.options(ignore_status=400)
+        if elastic_version >= (8, 0, 0):
+            client.options(ignore_status=400)
 
         yield client
         for index in client.indices.get_alias():