Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make unit tests pass again with elasticsearch 8.x client #223

Conversation

lvaylet
Copy link
Collaborator

@lvaylet lvaylet commented Mar 24, 2022

No description provided.

@lvaylet lvaylet changed the title fix: prevent unit tests from failing by using new calling convention from elasticsearch 8.x client fix: make unit tests pass again with elasticsearch 8.x client Mar 25, 2022
@lvaylet lvaylet requested a review from ocervell March 25, 2022 08:11
Copy link
Collaborator

@ocervell ocervell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents to pass additional config params, which can be required for some setups such as connecting with ElasticCloud or to a local Elasticsearch instance and pass basic auth for example:

ElastiCloud:

client = Elasticsearch(
    cloud_id=CLOUD_ID,
    basic_auth=("elastic", ELASTIC_PASSWORD)
)

Basic auth

client = Elasticsearch(
    "https://localhost:9200",
    ca_certs="/path/to/http_ca.crt",
    basic_auth=("elastic", ELASTIC_PASSWORD)
)

Multiple nodes

client = Elasticsearch(
    ["https://localhost:9200", "https://localhost:9201"] ,
    ca_certs="/path/to/http_ca.crt",
    basic_auth=("elastic", ELASTIC_PASSWORD)
)

API Token

client = Elasticsearch(
    "http://localhost:9200",
    api_key=("id", "api_key")
)

The input params of the client are pretty awkard (see here), for instance the basic auth takes a tuple as input as well as the API key, or the URL that can be optional or not makes it a bit more ennoying so I would suggest something like:

import copy
def __init__(self, client=None, **es_config):
    self.client = client
    if self.client is None:
        conf = copy.deepcopy(es_config)
        url = conf.pop('url')
        basic_auth = conf.pop('basic_auth')
        api_key = conf.pop('api_key')
        if url:
          conf['hosts'] = url
        if basic_auth:
          conf['basic_auth'] = (basic_auth['username'], basic_auth['password'])
        if api_key:
          conf['api_key'] = (api_key['id'], api_key['value'])
        self.client = Elasticsearch(**conf)
        

@lvaylet
Copy link
Collaborator Author

lvaylet commented Mar 26, 2022

Thanks for catching this. I reused your suggested code, slightly updated it to handle missing keys on dict.pop() and added comments to make maintenance easier later on.

Now does that mean that we should replace url with hosts in the configuration files? Version 8.x.x of the client requires either hosts or cloud_id to be specified.

Finally, should I write unit tests to cover all these scenarios (Elastic Cloud, basic auth, multiple nodes, API token...)?

@lvaylet lvaylet marked this pull request as draft March 26, 2022 14:45
@ocervell
Copy link
Collaborator

ocervell commented Mar 27, 2022

I think we should maintain the backward compat for this one, so let's keep url for now and we pass it as hosts to the client. We should update our Elasticsearch docs a bit (can be done in a further PR probably) to specify that url can be a single host (string) or a set of hosts (list).

About the unit tests, that's probably a good idea :) maybe split them in a further PR so that we fix the broken unit tests with this PR asap.

@lvaylet
Copy link
Collaborator Author

lvaylet commented Mar 27, 2022

I'd rather not accumulate technical debt so I updated the docs in this PR. I created a separate issue for the unit tests: #227.

@lvaylet lvaylet marked this pull request as ready for review March 27, 2022 14:48
@lvaylet lvaylet requested a review from ocervell March 27, 2022 19:48
@lvaylet
Copy link
Collaborator Author

lvaylet commented Mar 28, 2022

Thanks @ocervell for approving the changes. Do you expect me to go ahead and merge the PR myself? If so, which method should I use: "merge commit" or "squash and merge" (my preference)? If "squash and merge", are you happy with the commit message (i.e. the PR title)?

@lvaylet lvaylet merged commit 39dd26c into master Mar 28, 2022
@lvaylet lvaylet deleted the 222-running-make-unit-in-a-fresh-cloned-repo-fails-on-test_compute_elasticsearch branch March 28, 2022 11:50
@lvaylet lvaylet self-assigned this Oct 28, 2022
@lvaylet lvaylet added p/elasticsearch Elasticsearch provider issue tests Testing improvements / bugs ci labels Oct 28, 2022
lvaylet added a commit that referenced this pull request Nov 25, 2022
* use new convention from elasticsearch 8.x client

* handle multiple connection setups

* typo

* update docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci p/elasticsearch Elasticsearch provider issue tests Testing improvements / bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running make unit in a fresh cloned repo fails on test_compute_elasticsearch
2 participants