From b24661b3bc0b46f8da732557eb966fa0a0a8b4ab Mon Sep 17 00:00:00 2001 From: Judit Novak Date: Fri, 24 Nov 2023 17:46:20 +0100 Subject: [PATCH] Integration tests --- tests/integration/helpers_deployments.py | 3 +- tests/integration/plugins/test_plugins.py | 51 ++++++++++++++++++++++- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/tests/integration/helpers_deployments.py b/tests/integration/helpers_deployments.py index 37dc25e33..f68186194 100644 --- a/tests/integration/helpers_deployments.py +++ b/tests/integration/helpers_deployments.py @@ -5,6 +5,7 @@ import logging import subprocess from datetime import datetime, timedelta +from dateutil.parser import parse from typing import Any, Dict, List, Optional, Union from uuid import uuid4 @@ -22,7 +23,7 @@ class Status: def __init__(self, value: str, since: str, message: Optional[str] = None): self.value = value - self.since = datetime.strptime(since, "%d %b %Y %H:%M:%SZ") + self.since = parse(since) self.message = message diff --git a/tests/integration/plugins/test_plugins.py b/tests/integration/plugins/test_plugins.py index 0b9d86779..22b684cfa 100644 --- a/tests/integration/plugins/test_plugins.py +++ b/tests/integration/plugins/test_plugins.py @@ -16,6 +16,7 @@ MODEL_CONFIG, SERIES, check_cluster_formation_successful, + http_request, get_application_unit_ids_ips, get_application_unit_names, get_leader_unit_ip, @@ -30,6 +31,7 @@ ) from tests.integration.tls.test_tls import TLS_CERTIFICATES_APP_NAME +COS_APP_NAME = "grafana-agent" @pytest.mark.abort_on_fail @pytest.mark.skip_if_deployed @@ -46,7 +48,7 @@ async def test_build_and_deploy(ops_test: OpsTest) -> None: await asyncio.gather( ops_test.model.deploy(TLS_CERTIFICATES_APP_NAME, channel="stable", config=config), ops_test.model.deploy( - my_charm, num_units=3, series=SERIES, config={"plugin_opensearch_knn": True} + my_charm, num_units=1, series=SERIES, config={"plugin_opensearch_knn": True} ), ) @@ -58,7 +60,7 @@ async def test_build_and_deploy(ops_test: OpsTest) -> None: timeout=1400, idle_period=IDLE_PERIOD, ) - assert len(ops_test.model.applications[APP_NAME].units) == 3 + assert len(ops_test.model.applications[APP_NAME].units) == 1 @pytest.mark.abort_on_fail @@ -236,3 +238,48 @@ async def test_knn_training_search(ops_test: OpsTest) -> None: except RetryError: # The search should fail if knn_enabled is false assert not knn_enabled + + +async def test_prometheus_exporter_enabled(ops_test): + await ops_test.model.deploy(COS_APP_NAME, channel="edge"), + await ops_test.model.relate(APP_NAME, COS_APP_NAME) + # NOTE: As for now, COS agent remains blocked, as we only have a partial config + await ops_test.model.wait_for_idle( + apps=[APP_NAME], + status="active", + timeout=1400, + idle_period=IDLE_PERIOD, + ) + config = await ops_test.model.applications[APP_NAME].get_config() + assert config['prometheus_exporter_plugin_url']['default'].startswith("https://") + + +async def test_prometheus_exporter_works(ops_test): + leader_unit_ip = await get_leader_unit_ip(ops_test, app=APP_NAME) + endpoint = f"https://{leader_unit_ip}:9200/_prometheus/metrics" + response = await http_request(ops_test, "get", endpoint, app=APP_NAME, json_resp=False) + + response_str = response.content.decode('utf-8') + assert response_str.count("opensearch_") > 500 + assert len(response_str.split("\n")) > 500 + + +@pytest.mark.xfail(reason="Doesn't work yet") +async def test_prometheus_exporter_disabled(ops_test): + await ops_test.juju("remove-relation", APP_NAME, COS_APP_NAME) + await ops_test.model.wait_for_idle( + apps=[APP_NAME], + status="active", + timeout=1400, + idle_period=IDLE_PERIOD, + ) + + leader_unit_ip = await get_leader_unit_ip(ops_test, app=APP_NAME) + endpoint = f"https://{leader_unit_ip}:9200/_prometheus/metrics" + response = await http_request(ops_test, "get", endpoint, app=APP_NAME, json_resp=False) + assert response.content.decode('utf-8') == { + "error": "no handler found for uri [/_prometheus/metrics] and method [GET]" + } + + config = await ops_test.model.applications[APP_NAME].get_config() + assert not config['prometheus_exporter_plugin_url']