Skip to content

Commit

Permalink
skip: remove constants
Browse files Browse the repository at this point in the history
  • Loading branch information
DnPlas committed Apr 15, 2024
1 parent 108e529 commit 573b43f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 85 deletions.
33 changes: 0 additions & 33 deletions tests/integration/constants.py

This file was deleted.

128 changes: 76 additions & 52 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,96 @@

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])


@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",
Expand All @@ -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,
)
Expand All @@ -94,60 +118,60 @@ 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",
)


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):
Expand Down

0 comments on commit 573b43f

Please sign in to comment.