From 573b43f3a6af4cf2d0b8745cdc0ea7cb23e0af70 Mon Sep 17 00:00:00 2001 From: Daniela Plascencia Date: Mon, 15 Apr 2024 09:32:18 +0200 Subject: [PATCH] skip: remove constants --- tests/integration/constants.py | 33 -------- tests/integration/test_charm.py | 128 +++++++++++++++++++------------- 2 files changed, 76 insertions(+), 85 deletions(-) delete mode 100644 tests/integration/constants.py diff --git a/tests/integration/constants.py b/tests/integration/constants.py deleted file mode 100644 index de2b55a..0000000 --- a/tests/integration/constants.py +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2024 Canonical Ltd. -# See LICENSE file for licensing details. -"""Constants module including constants used in tests.""" -from pathlib import Path - -import yaml - -METADATA = yaml.safe_load(Path("./metadata.yaml").read_text()) -CHARM_ROOT = "." -ENVOY_APP_NAME = "envoy" - -MLMD = "mlmd" -MLMD_CHANNEL = "1.14/stable" -MLMD_TRUST = False - -ISTIO_OPERATORS_CHANNEL = "1.17/stable" -ISTIO_PILOT = "istio-pilot" -ISTIO_PILOT_TRUST = True -ISTIO_PILOT_CONFIG = {"default-gateway": "kubeflow-gateway"} -ISTIO_GATEWAY = "istio-gateway" -ISTIO_GATEWAY_APP_NAME = "istio-ingressgateway" -ISTIO_GATEWAY_TRUST = True -ISTIO_GATEWAY_CONFIG = {"kind": "ingress"} - -PROMETHEUS_K8S = "prometheus-k8s" -PROMETHEUS_K8S_CHANNEL = "1.0/stable" -PROMETHEUS_K8S_TRUST = True -GRAFANA_K8S = "grafana-k8s" -GRAFANA_K8S_CHANNEL = "1.0/stable" -GRAFANA_K8S_TRUST = True -PROMETHEUS_SCRAPE_K8S = "prometheus-scrape-config-k8s" -PROMETHEUS_SCRAPE_K8S_CHANNEL = "1.0/stable" -PROMETHEUS_SCRAPE_CONFIG = {"scrape_interval": "30s"} diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index 58b6233..1d4c7c0 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -3,47 +3,71 @@ import json import logging +from pathlib import Path import aiohttp import pytest import requests import tenacity +import yaml from lightkube import Client from lightkube.generic_resource import create_namespaced_resource from lightkube.resources.core_v1 import Service -from . import constants - +METADATA = yaml.safe_load(Path("./metadata.yaml").read_text()) +CHARM_ROOT = "." +ENVOY_APP_NAME = "envoy" + +MLMD = "mlmd" +MLMD_CHANNEL = "1.14/stable" +MLMD_TRUST = False + +ISTIO_OPERATORS_CHANNEL = "1.17/stable" +ISTIO_PILOT = "istio-pilot" +ISTIO_PILOT_TRUST = True +ISTIO_PILOT_CONFIG = {"default-gateway": "kubeflow-gateway"} +ISTIO_GATEWAY = "istio-gateway" +ISTIO_GATEWAY_APP_NAME = "istio-ingressgateway" +ISTIO_GATEWAY_TRUST = True +ISTIO_GATEWAY_CONFIG = {"kind": "ingress"} + +PROMETHEUS_K8S = "prometheus-k8s" +PROMETHEUS_K8S_CHANNEL = "1.0/stable" +PROMETHEUS_K8S_TRUST = True +GRAFANA_K8S = "grafana-k8s" +GRAFANA_K8S_CHANNEL = "1.0/stable" +GRAFANA_K8S_TRUST = True +PROMETHEUS_SCRAPE_K8S = "prometheus-scrape-config-k8s" +PROMETHEUS_SCRAPE_K8S_CHANNEL = "1.0/stable" +PROMETHEUS_SCRAPE_CONFIG = {"scrape_interval": "30s"} log = logging.getLogger(__name__) @pytest.fixture(scope="session") def lightkube_client() -> Client: - client = Client(field_manager=constants.ENVOY_APP_NAME) + client = Client(field_manager=ENVOY_APP_NAME) return client @pytest.mark.abort_on_fail async def test_build_and_deploy(ops_test): - await ops_test.model.deploy( - constants.MLMD, channel=constants.MLMD_CHANNEL, trust=constants.MLMD_TRUST - ) - charm = await ops_test.build_charm(constants.CHARM_ROOT) - image_path = constants.METADATA["resources"]["oci-image"]["upstream-source"] + await ops_test.model.deploy(MLMD, channel=MLMD_CHANNEL, trust=MLMD_TRUST) + charm = await ops_test.build_charm(CHARM_ROOT) + image_path = METADATA["resources"]["oci-image"]["upstream-source"] resources = {"oci-image": image_path} await ops_test.model.deploy(charm, resources=resources) - await ops_test.model.add_relation(constants.ENVOY_APP_NAME, constants.MLMD) + await ops_test.model.add_relation(ENVOY_APP_NAME, MLMD) await ops_test.model.wait_for_idle( - apps=[constants.MLMD], status="active", raise_on_blocked=False, idle_period=30 + apps=[MLMD], status="active", raise_on_blocked=False, idle_period=30 ) await ops_test.model.wait_for_idle( - apps=[constants.ENVOY_APP_NAME], status="blocked", raise_on_blocked=False, idle_period=30 + apps=[ENVOY_APP_NAME], status="blocked", raise_on_blocked=False, idle_period=30 ) relation = ops_test.model.relations[0] assert [app.entity_id for app in relation.applications] == [ - constants.ENVOY_APP_NAME, - constants.MLMD, + ENVOY_APP_NAME, + MLMD, ] assert all([endpoint.name == "grpc" for endpoint in relation.endpoints]) @@ -51,24 +75,24 @@ async def test_build_and_deploy(ops_test): @pytest.mark.abort_on_fail async def test_virtual_service(ops_test, lightkube_client): await ops_test.model.deploy( - entity_url=constants.ISTIO_PILOT, - channel=constants.ISTIO_OPERATORS_CHANNEL, - config=constants.ISTIO_PILOT_CONFIG, - trust=constants.ISTIO_PILOT_TRUST, + entity_url=ISTIO_PILOT, + channel=ISTIO_OPERATORS_CHANNEL, + config=ISTIO_PILOT_CONFIG, + trust=ISTIO_PILOT_TRUST, ) await ops_test.model.deploy( - entity_url=constants.ISTIO_GATEWAY, - application_name=constants.ISTIO_GATEWAY_APP_NAME, - channel=constants.ISTIO_OPERATORS_CHANNEL, - config=constants.ISTIO_GATEWAY_CONFIG, - trust=constants.ISTIO_GATEWAY_TRUST, + entity_url=ISTIO_GATEWAY, + application_name=ISTIO_GATEWAY_APP_NAME, + channel=ISTIO_OPERATORS_CHANNEL, + config=ISTIO_GATEWAY_CONFIG, + trust=ISTIO_GATEWAY_TRUST, ) await ops_test.model.add_relation( - constants.ISTIO_PILOT, - constants.ISTIO_GATEWAY_APP_NAME, + ISTIO_PILOT, + ISTIO_GATEWAY_APP_NAME, ) - await ops_test.model.add_relation(constants.ISTIO_PILOT, constants.ENVOY_APP_NAME) + await ops_test.model.add_relation(ISTIO_PILOT, ENVOY_APP_NAME) await ops_test.model.wait_for_idle( status="active", @@ -79,7 +103,7 @@ async def test_virtual_service(ops_test, lightkube_client): # Verify that virtualService is as expected assert_virtualservice_exists( - name=constants.ENVOY_APP_NAME, + name=ENVOY_APP_NAME, namespace=ops_test.model.name, lightkube_client=lightkube_client, ) @@ -94,39 +118,39 @@ async def test_virtual_service(ops_test, lightkube_client): async def test_deploy_with_prometheus_and_grafana(ops_test): # Deploy and relate prometheus await ops_test.model.deploy( - constants.PROMETHEUS_K8S, - channel=constants.PROMETHEUS_K8S_CHANNEL, - trust=constants.PROMETHEUS_K8S_TRUST, + PROMETHEUS_K8S, + channel=PROMETHEUS_K8S_CHANNEL, + trust=PROMETHEUS_K8S_TRUST, ) await ops_test.model.deploy( - constants.GRAFANA_K8S, - channel=constants.GRAFANA_K8S_CHANNEL, - trust=constants.GRAFANA_K8S_TRUST, + GRAFANA_K8S, + channel=GRAFANA_K8S_CHANNEL, + trust=GRAFANA_K8S_TRUST, ) await ops_test.model.deploy( - constants.PROMETHEUS_SCRAPE_K8S, - channel=constants.PROMETHEUS_SCRAPE_K8S_CHANNEL, - config=constants.PROMETHEUS_SCRAPE_CONFIG, + PROMETHEUS_SCRAPE_K8S, + channel=PROMETHEUS_SCRAPE_K8S_CHANNEL, + config=PROMETHEUS_SCRAPE_CONFIG, ) await ops_test.model.add_relation( - f"{constants.PROMETHEUS_K8S}:grafana-dashboard", - f"{constants.GRAFANA_K8S}:grafana-dashboard", + f"{PROMETHEUS_K8S}:grafana-dashboard", + f"{GRAFANA_K8S}:grafana-dashboard", ) await ops_test.model.add_relation( - f"{constants.PROMETHEUS_K8S}:metrics-endpoint", - f"{constants.PROMETHEUS_SCRAPE_K8S}:metrics-endpoint", + f"{PROMETHEUS_K8S}:metrics-endpoint", + f"{PROMETHEUS_SCRAPE_K8S}:metrics-endpoint", ) - await ops_test.model.add_relation(constants.ENVOY_APP_NAME, constants.GRAFANA_K8S) - await ops_test.model.add_relation(constants.PROMETHEUS_K8S, constants.ENVOY_APP_NAME) + await ops_test.model.add_relation(ENVOY_APP_NAME, GRAFANA_K8S) + await ops_test.model.add_relation(PROMETHEUS_K8S, ENVOY_APP_NAME) await ops_test.model.wait_for_idle( [ - constants.ENVOY_APP_NAME, - constants.PROMETHEUS_K8S, - constants.GRAFANA_K8S, - constants.PROMETHEUS_SCRAPE_K8S, + ENVOY_APP_NAME, + PROMETHEUS_K8S, + GRAFANA_K8S, + PROMETHEUS_SCRAPE_K8S, ], status="active", ) @@ -134,20 +158,20 @@ async def test_deploy_with_prometheus_and_grafana(ops_test): async def test_correct_observability_setup(ops_test): status = await ops_test.model.get_status() - prometheus_unit_ip = status["applications"][constants.PROMETHEUS_K8S]["units"][ - f"{constants.PROMETHEUS_K8S}/0" - ]["address"] + prometheus_unit_ip = status["applications"][PROMETHEUS_K8S]["units"][f"{PROMETHEUS_K8S}/0"][ + "address" + ] r = requests.get( - f'http://{prometheus_unit_ip}:9090/api/v1/query?query=up{{juju_application="{constants.ENVOY_APP_NAME}"}}' # noqa + f'http://{prometheus_unit_ip}:9090/api/v1/query?query=up{{juju_application="{ENVOY_APP_NAME}"}}' # noqa ) response = json.loads(r.content.decode("utf-8")) assert response["status"] == "success" response_metric = response["data"]["result"][0]["metric"] - assert response_metric["juju_application"] == constants.ENVOY_APP_NAME - assert response_metric["juju_charm"] == constants.ENVOY_APP_NAME + assert response_metric["juju_application"] == ENVOY_APP_NAME + assert response_metric["juju_charm"] == ENVOY_APP_NAME assert response_metric["juju_model"] == ops_test.model_name - assert response_metric["juju_unit"] == f"{constants.ENVOY_APP_NAME}/0" + assert response_metric["juju_unit"] == f"{ENVOY_APP_NAME}/0" def assert_virtualservice_exists(name: str, namespace: str, lightkube_client):