Rename module to opensearchpy

To avoid conflict with an existing package by name 'opensearch' being
present

Signed-off-by: Rushi Agrawal <[email protected]>
This commit is contained in:
Rushi Agrawal
2021-09-16 21:23:38 +05:30
parent f4891be3c3
commit ef0c23c0e4
129 changed files with 237 additions and 237 deletions
+164
View File
@@ -0,0 +1,164 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import unicode_literals
import warnings
from opensearchpy.client import OpenSearch, _normalize_hosts
from ..test_cases import OpenSearchTestCase, TestCase
class TestNormalizeHosts(TestCase):
def test_none_uses_defaults(self):
self.assertEqual([{}], _normalize_hosts(None))
def test_strings_are_used_as_hostnames(self):
self.assertEqual([{"host": "elastic.co"}], _normalize_hosts(["elastic.co"]))
def test_strings_are_parsed_for_port_and_user(self):
self.assertEqual(
[
{"host": "elastic.co", "port": 42},
{"host": "elastic.co", "http_auth": "user:secre]"},
],
_normalize_hosts(["elastic.co:42", "user:secre%[email protected]"]),
)
def test_strings_are_parsed_for_scheme(self):
self.assertEqual(
[
{"host": "elastic.co", "port": 42, "use_ssl": True},
{
"host": "elastic.co",
"http_auth": "user:secret",
"use_ssl": True,
"port": 443,
"url_prefix": "/prefix",
},
],
_normalize_hosts(
["https://elastic.co:42", "https://user:[email protected]/prefix"]
),
)
def test_dicts_are_left_unchanged(self):
self.assertEqual(
[{"host": "local", "extra": 123}],
_normalize_hosts([{"host": "local", "extra": 123}]),
)
def test_single_string_is_wrapped_in_list(self):
self.assertEqual([{"host": "elastic.co"}], _normalize_hosts("elastic.co"))
class TestClient(OpenSearchTestCase):
def test_request_timeout_is_passed_through_unescaped(self):
self.client.ping(request_timeout=0.1)
calls = self.assert_url_called("HEAD", "/")
self.assertEqual([({"request_timeout": 0.1}, {}, None)], calls)
def test_params_is_copied_when(self):
rt = object()
params = dict(request_timeout=rt)
self.client.ping(params=params)
self.client.ping(params=params)
calls = self.assert_url_called("HEAD", "/", 2)
self.assertEqual(
[({"request_timeout": rt}, {}, None), ({"request_timeout": rt}, {}, None)],
calls,
)
self.assertFalse(calls[0][0] is calls[1][0])
def test_headers_is_copied_when(self):
hv = "value"
headers = dict(Authentication=hv)
self.client.ping(headers=headers)
self.client.ping(headers=headers)
calls = self.assert_url_called("HEAD", "/", 2)
self.assertEqual(
[({}, {"authentication": hv}, None), ({}, {"authentication": hv}, None)],
calls,
)
self.assertFalse(calls[0][0] is calls[1][0])
def test_from_in_search(self):
self.client.search(index="i", from_=10)
calls = self.assert_url_called("POST", "/i/_search")
self.assertEqual([({"from": "10"}, {}, None)], calls)
def test_repr_contains_hosts(self):
self.assertEqual("<OpenSearch([{}])>", repr(self.client))
def test_repr_subclass(self):
class OtherOpenSearch(OpenSearch):
pass
self.assertEqual("<OtherOpenSearch([{}])>", repr(OtherOpenSearch()))
def test_repr_contains_hosts_passed_in(self):
self.assertIn("opensearchpy.org", repr(OpenSearch(["opensearch.org:123"])))
def test_repr_truncates_host_to_5(self):
hosts = [{"host": "opensearch" + str(i)} for i in range(10)]
client = OpenSearch(hosts)
self.assertNotIn("opensearch5", repr(client))
self.assertIn("...", repr(client))
def test_index_uses_post_if_id_is_empty(self):
self.client.index(index="my-index", id="", body={})
self.assert_url_called("POST", "/my-index/_doc")
def test_index_uses_put_if_id_is_not_empty(self):
self.client.index(index="my-index", id=0, body={})
self.assert_url_called("PUT", "/my-index/_doc/0")
def test_tasks_get_without_task_id_deprecated(self):
warnings.simplefilter("always", DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
self.client.tasks.get()
self.assert_url_called("GET", "/_tasks")
self.assertEqual(len(w), 1)
self.assertIs(w[0].category, DeprecationWarning)
self.assertEqual(
str(w[0].message),
"Calling client.tasks.get() without a task_id is deprecated "
"and will be removed in v8.0. Use client.tasks.list() instead.",
)
def test_tasks_get_with_task_id_not_deprecated(self):
warnings.simplefilter("always", DeprecationWarning)
with warnings.catch_warnings(record=True) as w:
self.client.tasks.get("task-1")
self.client.tasks.get(task_id="task-2")
self.assert_url_called("GET", "/_tasks/task-1")
self.assert_url_called("GET", "/_tasks/task-2")
self.assertEqual(len(w), 0)
@@ -0,0 +1,53 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from test_opensearchpy.test_cases import OpenSearchTestCase
class TestCluster(OpenSearchTestCase):
def test_stats_without_node_id(self):
self.client.cluster.stats()
self.assert_url_called("GET", "/_cluster/stats")
def test_stats_with_node_id(self):
self.client.cluster.stats("node-1")
self.assert_url_called("GET", "/_cluster/stats/nodes/node-1")
self.client.cluster.stats(node_id="node-2")
self.assert_url_called("GET", "/_cluster/stats/nodes/node-2")
def test_state_with_index_without_metric_defaults_to_all(self):
self.client.cluster.state()
self.assert_url_called("GET", "/_cluster/state")
self.client.cluster.state(metric="cluster_name")
self.assert_url_called("GET", "/_cluster/state/cluster_name")
self.client.cluster.state(index="index-1")
self.assert_url_called("GET", "/_cluster/state/_all/index-1")
self.client.cluster.state(index="index-1", metric="cluster_name")
self.assert_url_called("GET", "/_cluster/state/cluster_name/index-1")
@@ -0,0 +1,50 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from test_opensearchpy.test_cases import OpenSearchTestCase
class TestIndices(OpenSearchTestCase):
def test_create_one_index(self):
self.client.indices.create("test-index")
self.assert_url_called("PUT", "/test-index")
def test_delete_multiple_indices(self):
self.client.indices.delete(["test-index", "second.index", "third/index"])
self.assert_url_called("DELETE", "/test-index,second.index,third%2Findex")
def test_exists_index(self):
self.client.indices.exists("second.index,third/index")
self.assert_url_called("HEAD", "/second.index,third%2Findex")
def test_passing_empty_value_for_required_param_raises_exception(self):
self.assertRaises(ValueError, self.client.indices.exists, index=None)
self.assertRaises(ValueError, self.client.indices.exists, index=[])
self.assertRaises(ValueError, self.client.indices.exists, index="")
def test_put_mapping_without_index(self):
self.client.indices.put_mapping(doc_type="doc-type", body={})
self.assert_url_called("PUT", "/_all/doc-type/_mapping")
@@ -0,0 +1,173 @@
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import pytest
from test_opensearchpy.test_cases import OpenSearchTestCase
class TestOverriddenUrlTargets(OpenSearchTestCase):
def test_create(self):
self.client.create(index="test-index", id="test-id", body={})
self.assert_url_called("PUT", "/test-index/_create/test-id")
self.client.create(
index="test-index", doc_type="test-type", id="test-id", body={}
)
self.assert_url_called("PUT", "/test-index/test-type/test-id/_create")
def test_delete(self):
self.client.delete(index="test-index", id="test-id")
self.assert_url_called("DELETE", "/test-index/_doc/test-id")
self.client.delete(index="test-index", doc_type="test-type", id="test-id")
self.assert_url_called("DELETE", "/test-index/test-type/test-id")
def test_exists(self):
self.client.exists(index="test-index", id="test-id")
self.assert_url_called("HEAD", "/test-index/_doc/test-id")
self.client.exists(index="test-index", doc_type="test-type", id="test-id")
self.assert_url_called("HEAD", "/test-index/test-type/test-id")
def test_explain(self):
self.client.explain(index="test-index", id="test-id")
self.assert_url_called("POST", "/test-index/_explain/test-id")
self.client.explain(index="test-index", doc_type="test-type", id="test-id")
self.assert_url_called("POST", "/test-index/test-type/test-id/_explain")
def test_get(self):
self.client.get(index="test-index", id="test-id")
self.assert_url_called("GET", "/test-index/_doc/test-id")
self.client.get(index="test-index", doc_type="test-type", id="test-id")
self.assert_url_called("GET", "/test-index/test-type/test-id")
def test_get_source(self):
self.client.get_source(index="test-index", id="test-id")
self.assert_url_called("GET", "/test-index/_source/test-id")
self.client.get_source(index="test-index", doc_type="test-type", id="test-id")
self.assert_url_called("GET", "/test-index/test-type/test-id/_source")
def test_exists_source(self):
self.client.exists_source(index="test-index", id="test-id")
self.assert_url_called("HEAD", "/test-index/_source/test-id")
self.client.exists_source(
index="test-index", doc_type="test-type", id="test-id"
)
self.assert_url_called("HEAD", "/test-index/test-type/test-id/_source")
def test_index(self):
self.client.index(index="test-index", body={})
self.assert_url_called("POST", "/test-index/_doc")
self.client.index(index="test-index", id="test-id", body={})
self.assert_url_called("PUT", "/test-index/_doc/test-id")
self.client.index(index="test-index", doc_type="test-type", body={})
self.assert_url_called("POST", "/test-index/test-type")
self.client.index(
index="test-index", doc_type="test-type", id="test-id", body={}
)
self.assert_url_called("PUT", "/test-index/test-type/test-id")
self.client.index(index="test-index", doc_type="_doc", body={})
self.assert_url_called("POST", "/test-index/_doc", count=2)
self.client.index(index="test-index", doc_type="_doc", id="test-id", body={})
self.assert_url_called("PUT", "/test-index/_doc/test-id", count=2)
def test_termvectors(self):
self.client.termvectors(index="test-index", body={})
self.assert_url_called("POST", "/test-index/_termvectors")
self.client.termvectors(index="test-index", id="test-id", body={})
self.assert_url_called("POST", "/test-index/_termvectors/test-id")
self.client.termvectors(index="test-index", doc_type="test-type", body={})
self.assert_url_called("POST", "/test-index/test-type/_termvectors")
self.client.termvectors(
index="test-index", doc_type="test-type", id="test-id", body={}
)
self.assert_url_called("POST", "/test-index/test-type/test-id/_termvectors")
def test_mtermvectors(self):
self.client.mtermvectors(index="test-index", body={})
self.assert_url_called("POST", "/test-index/_mtermvectors")
self.client.mtermvectors(index="test-index", doc_type="test-type", body={})
self.assert_url_called("POST", "/test-index/test-type/_mtermvectors")
def test_update(self):
self.client.update(index="test-index", id="test-id", body={})
self.assert_url_called("POST", "/test-index/_update/test-id")
self.client.update(
index="test-index", doc_type="test-type", id="test-id", body={}
)
self.assert_url_called("POST", "/test-index/test-type/test-id/_update")
def test_cluster_state(self):
self.client.cluster.state()
self.assert_url_called("GET", "/_cluster/state")
self.client.cluster.state(index="test-index")
self.assert_url_called("GET", "/_cluster/state/_all/test-index")
self.client.cluster.state(index="test-index", metric="test-metric")
self.assert_url_called("GET", "/_cluster/state/test-metric/test-index")
def test_cluster_stats(self):
self.client.cluster.stats()
self.assert_url_called("GET", "/_cluster/stats")
self.client.cluster.stats(node_id="test-node")
self.assert_url_called("GET", "/_cluster/stats/nodes/test-node")
def test_indices_put_mapping(self):
self.client.indices.put_mapping(body={})
self.assert_url_called("PUT", "/_mapping")
self.client.indices.put_mapping(index="test-index", body={})
self.assert_url_called("PUT", "/test-index/_mapping")
self.client.indices.put_mapping(
index="test-index", doc_type="test-type", body={}
)
self.assert_url_called("PUT", "/test-index/test-type/_mapping")
self.client.indices.put_mapping(doc_type="test-type", body={})
self.assert_url_called("PUT", "/_all/test-type/_mapping")
def test_tasks_get(self):
with pytest.warns(DeprecationWarning):
self.client.tasks.get()
+182
View File
@@ -0,0 +1,182 @@
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import unicode_literals
from opensearchpy.client.utils import _bulk_body, _escape, _make_path, query_params
from opensearchpy.compat import PY2
from ..test_cases import SkipTest, TestCase
class TestQueryParams(TestCase):
def setup_method(self, _):
self.calls = []
@query_params("simple_param")
def func_to_wrap(self, *args, **kwargs):
self.calls.append((args, kwargs))
def test_handles_params(self):
self.func_to_wrap(params={"simple_param_2": "2"}, simple_param="3")
self.assertEqual(
self.calls,
[
(
(),
{
"params": {"simple_param": b"3", "simple_param_2": "2"},
"headers": {},
},
)
],
)
def test_handles_headers(self):
self.func_to_wrap(headers={"X-Opaque-Id": "app-1"})
self.assertEqual(
self.calls, [((), {"params": {}, "headers": {"x-opaque-id": "app-1"}})]
)
def test_handles_opaque_id(self):
self.func_to_wrap(opaque_id="request-id")
self.assertEqual(
self.calls, [((), {"params": {}, "headers": {"x-opaque-id": "request-id"}})]
)
def test_handles_empty_none_and_normalization(self):
self.func_to_wrap(params=None)
self.assertEqual(self.calls[-1], ((), {"params": {}, "headers": {}}))
self.func_to_wrap(headers=None)
self.assertEqual(self.calls[-1], ((), {"params": {}, "headers": {}}))
self.func_to_wrap(headers=None, params=None)
self.assertEqual(self.calls[-1], ((), {"params": {}, "headers": {}}))
self.func_to_wrap(headers={}, params={})
self.assertEqual(self.calls[-1], ((), {"params": {}, "headers": {}}))
self.func_to_wrap(headers={"X": "y"})
self.assertEqual(self.calls[-1], ((), {"params": {}, "headers": {"x": "y"}}))
def test_per_call_authentication(self):
self.func_to_wrap(api_key=("name", "key"))
self.assertEqual(
self.calls[-1],
((), {"headers": {"authorization": "ApiKey bmFtZTprZXk="}, "params": {}}),
)
self.func_to_wrap(http_auth=("user", "password"))
self.assertEqual(
self.calls[-1],
(
(),
{
"headers": {"authorization": "Basic dXNlcjpwYXNzd29yZA=="},
"params": {},
},
),
)
self.func_to_wrap(http_auth="abcdef")
self.assertEqual(
self.calls[-1],
((), {"headers": {"authorization": "Basic abcdef"}, "params": {}}),
)
# If one or the other is 'None' it's all good!
self.func_to_wrap(http_auth=None, api_key=None)
self.assertEqual(self.calls[-1], ((), {"headers": {}, "params": {}}))
self.func_to_wrap(http_auth="abcdef", api_key=None)
self.assertEqual(
self.calls[-1],
((), {"headers": {"authorization": "Basic abcdef"}, "params": {}}),
)
# If both are given values an error is raised.
with self.assertRaises(ValueError) as e:
self.func_to_wrap(http_auth="key", api_key=("1", "2"))
self.assertEqual(
str(e.exception),
"Only one of 'http_auth' and 'api_key' may be passed at a time",
)
class TestMakePath(TestCase):
def test_handles_unicode(self):
id = "中文"
self.assertEqual(
"/some-index/type/%E4%B8%AD%E6%96%87", _make_path("some-index", "type", id)
)
def test_handles_utf_encoded_string(self):
if not PY2:
raise SkipTest("Only relevant for py2")
id = "中文".encode("utf-8")
self.assertEqual(
"/some-index/type/%E4%B8%AD%E6%96%87", _make_path("some-index", "type", id)
)
class TestEscape(TestCase):
def test_handles_ascii(self):
string = "abc123"
self.assertEqual(b"abc123", _escape(string))
def test_handles_unicode(self):
string = "中文"
self.assertEqual(b"\xe4\xb8\xad\xe6\x96\x87", _escape(string))
def test_handles_bytestring(self):
string = b"celery-task-meta-c4f1201f-eb7b-41d5-9318-a75a8cfbdaa0"
self.assertEqual(string, _escape(string))
class TestBulkBody(TestCase):
def test_proper_bulk_body_as_string_is_not_modified(self):
string_body = '"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"\n'
self.assertEqual(string_body, _bulk_body(None, string_body))
def test_proper_bulk_body_as_bytestring_is_not_modified(self):
bytestring_body = b'"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"\n'
self.assertEqual(bytestring_body, _bulk_body(None, bytestring_body))
def test_bulk_body_as_string_adds_trailing_newline(self):
string_body = '"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"'
self.assertEqual(
'"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"\n',
_bulk_body(None, string_body),
)
def test_bulk_body_as_bytestring_adds_trailing_newline(self):
bytestring_body = b'"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"'
self.assertEqual(
b'"{"index":{ "_index" : "test"}}\n{"field1": "value1"}"\n',
_bulk_body(None, bytestring_body),
)