forked from opensearch-project/opensearch-py
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add async tests for plugins (opensearch-project#425)
* test(plugins): add async tests for plugins Signed-off-by: florian <florian@harfanglab.fr> * chore(USER_GUIDE): remove ** Signed-off-by: florian <florian@harfanglab.fr> * test(async): remove the event loop session scoped fixture Signed-off-by: florian <florian@harfanglab.fr> --------- Signed-off-by: florian <florian@harfanglab.fr>
- Loading branch information
1 parent
5fa6d99
commit 5866880
Showing
7 changed files
with
374 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test_opensearchpy/test_async/test_server/test_plugins/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# 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. |
187 changes: 187 additions & 0 deletions
187
test_opensearchpy/test_async/test_server/test_plugins/test_alerting.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# -*- 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. | ||
|
||
|
||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
import pytest | ||
|
||
from opensearchpy.helpers.test import OPENSEARCH_VERSION | ||
|
||
from .. import AsyncOpenSearchTestCase | ||
|
||
pytestmark = pytest.mark.asyncio | ||
|
||
|
||
class TestAlertingPlugin(AsyncOpenSearchTestCase): | ||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_create_destination(self): | ||
# Test to create alert destination | ||
dummy_destination = { | ||
"name": "my-destination", | ||
"type": "slack", | ||
"slack": {"url": "http://www.example.com"}, | ||
} | ||
response = await self.client.alerting.create_destination(dummy_destination) | ||
|
||
self.assertNotIn("errors", response) | ||
self.assertIn("_id", response) | ||
|
||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_get_destination(self): | ||
# Create a dummy destination | ||
await self.test_create_destination() | ||
|
||
# Try fetching the destination | ||
response = await self.client.alerting.get_destination() | ||
|
||
self.assertNotIn("errors", response) | ||
self.assertGreaterEqual(response["totalDestinations"], 1) | ||
self.assertEqual(response["totalDestinations"], len(response["destinations"])) | ||
|
||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_create_monitor(self): | ||
# Create a dummy destination | ||
await self.test_create_destination() | ||
|
||
# Try fetching the destination | ||
destination = await self.client.alerting.get_destination() | ||
self.assertGreaterEqual( | ||
destination["totalDestinations"], | ||
1, | ||
"No destination entries found in the database.", | ||
) | ||
|
||
# Select the first destination available | ||
destination = destination["destinations"][0] | ||
|
||
# A dummy schedule for 1 minute interval | ||
schedule = {"period": {"interval": 1, "unit": "MINUTES"}} | ||
|
||
# A dummy query fetching everything | ||
query = {"query": {"query_string": {"query": "*"}}} | ||
|
||
# A dummy action with the dummy destination | ||
action = { | ||
"name": "test-action", | ||
"destination_id": destination["id"], | ||
"message_template": {"source": "This is my message body."}, | ||
"throttle_enabled": True, | ||
"throttle": {"value": 27, "unit": "MINUTES"}, | ||
"subject_template": {"source": "TheSubject"}, | ||
} | ||
|
||
# A dummy trigger with the dummy action | ||
triggers = { | ||
"name": "test-trigger", | ||
"severity": "1", | ||
"condition": { | ||
"script": { | ||
"source": "ctx.results[0].hits.total.value > 0", | ||
"lang": "painless", | ||
} | ||
}, | ||
"actions": [action], | ||
} | ||
|
||
# A dummy monitor with the dummy schedule, dummy query, dummy trigger | ||
monitor = { | ||
"type": "monitor", | ||
"name": "test-monitor", | ||
"monitor_type": "query_level_monitor", | ||
"enabled": True, | ||
"schedule": schedule, | ||
"inputs": [{"search": {"indices": ["*"], "query": query}}], | ||
"triggers": [triggers], | ||
} | ||
|
||
response = await self.client.alerting.create_monitor(monitor) | ||
|
||
self.assertNotIn("errors", response) | ||
self.assertIn("_id", response) | ||
self.assertIn("monitor", response) | ||
|
||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_search_monitor(self): | ||
# Create a dummy monitor | ||
await self.test_create_monitor() | ||
|
||
# Create a monitor search query by it's name | ||
query = {"query": {"match": {"monitor.name": "test-monitor"}}} | ||
|
||
# Perform the search with the above query | ||
response = await self.client.alerting.search_monitor(query) | ||
|
||
self.assertNotIn("errors", response) | ||
self.assertIn("hits", response) | ||
self.assertEqual(response["hits"]["total"]["value"], 1, "No monitor found.") | ||
|
||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_get_monitor(self): | ||
# Create a dummy monitor | ||
await self.test_create_monitor() | ||
|
||
# Create a monitor search query by it's name | ||
query = {"query": {"match": {"monitor.name": "test-monitor"}}} | ||
|
||
# Perform the search with the above query | ||
response = await self.client.alerting.search_monitor(query) | ||
|
||
# Select the first monitor | ||
monitor = response["hits"]["hits"][0] | ||
|
||
# Fetch the monitor by id | ||
response = await self.client.alerting.get_monitor(monitor["_id"]) | ||
|
||
self.assertNotIn("errors", response) | ||
self.assertIn("_id", response) | ||
self.assertIn("monitor", response) | ||
|
||
@unittest.skipUnless( | ||
(OPENSEARCH_VERSION) and (OPENSEARCH_VERSION < (2, 0, 0)), | ||
"Plugin not supported for opensearch version", | ||
) | ||
async def test_run_monitor(self): | ||
# Create a dummy monitor | ||
await self.test_create_monitor() | ||
|
||
# Create a monitor search query by it's name | ||
query = {"query": {"match": {"monitor.name": "test-monitor"}}} | ||
|
||
# Perform the search with the above query | ||
response = await self.client.alerting.search_monitor(query) | ||
|
||
# Select the first monitor | ||
monitor = response["hits"]["hits"][0] | ||
|
||
# Run the monitor by id | ||
response = await self.client.alerting.run_monitor(monitor["_id"]) | ||
|
||
self.assertEqual(response["error"], None) | ||
self.assertIn("monitor_name", response) | ||
self.assertIn("period_start", response) | ||
self.assertIn("period_end", response) |
Oops, something went wrong.