Skip to content

Commit

Permalink
update charm-relation-interfaces to use prometheus_scrape schema that…
Browse files Browse the repository at this point in the history
… is compatible with pydantic v2 (#141)

This:
* rebuilds the charm, pulling a new version of the charm-relation-interfaces that is compatible with pydantic v2
* rewrites the `test_scrape_jobs_are_forwarded_on_adding_prometheus_then_targets` test in scenario in order to replicate [this issue](#140).  The previous unit test conceptually should have caught this bug, but because the issue occurred when calling `._get_scrape_configs()`, which is only called in __init__, unit tests did not reproduce this bug because Harness reuses the Charm object (eg: __init__ is only called once during `harness.begin()`, not on every event like in real Juju).  By refactoring to scenario, we more closely reproduce true Juju behavior and capture the bug.
  • Loading branch information
ca-scribner authored Jun 20, 2024
1 parent cf36ccd commit 8174714
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 44 deletions.
12 changes: 12 additions & 0 deletions tests/scenario/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
from pathlib import Path

import yaml

CHARM_ROOT = Path(__file__).parent.parent.parent


def get_charm_meta() -> dict:
raw_meta = (CHARM_ROOT / "metadata").with_suffix(".yaml").read_text()
return yaml.safe_load(raw_meta)
77 changes: 77 additions & 0 deletions tests/scenario/test_charm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import json

import scenario
from charm import COSProxyCharm

from tests.scenario.helpers import get_charm_meta

RELABEL_INSTANCE_CONFIG = {
"source_labels": [
"juju_model",
"juju_model_uuid",
"juju_application",
"juju_unit",
],
"separator": "_",
"target_label": "instance",
"regex": "(.*)",
}


def test_scrape_jobs_are_forwarded_on_adding_prometheus_then_targets():
# Arrange
prometheus_scrape_relation = scenario.Relation(
"downstream-prometheus-scrape",
remote_app_name="cos-prometheus",
remote_units_data={
0: {},
},
)
prometheus_target_relation = scenario.Relation(
"prometheus-target",
remote_app_name="target-app",
remote_units_data={
0: {
"hostname": "scrape_target_0",
"port": "1234",
},
},
)

model_name = "testmodel"
model_uuid = "ae3c0b14-9c3a-4262-b560-7a6ad7d3642f"
model = scenario.Model(model_name, model_uuid)

ctx = scenario.Context(COSProxyCharm, meta=get_charm_meta())
state_in = scenario.State(
leader=True,
relations=[prometheus_scrape_relation, prometheus_target_relation],
model=model,
)

expected_jobs = [
{
"job_name": f"juju_{model_name}_{model_uuid[:7]}_target-app_prometheus_scrape",
"static_configs": [
{
"targets": ["scrape_target_0:1234"],
"labels": {
"juju_model": model_name,
"juju_model_uuid": model_uuid,
"juju_application": "target-app",
"juju_unit": "target-app/0",
"host": "scrape_target_0",
"dns_name": "scrape_target_0",
},
}
],
"relabel_configs": [RELABEL_INSTANCE_CONFIG],
}
]

# Act
out = ctx.run(prometheus_target_relation.changed_event, state_in)

# Assert
actual_jobs = json.loads(out.relations[0].local_app_data.get("scrape_jobs", []))
assert actual_jobs == expected_jobs
44 changes: 0 additions & 44 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,50 +201,6 @@ def test_dashboards_without_grafana_relations_blocks(self):
BlockedStatus("Missing one of (Grafana|dashboard|grafana-agent) relation(s)"),
)

def test_scrape_jobs_are_forwarded_on_adding_prometheus_then_targets(self):
self.harness.set_leader(True)

prometheus_rel_id = self.harness.add_relation(
"downstream-prometheus-scrape", "cos-prometheus"
)
self.harness.add_relation_unit(prometheus_rel_id, "cos-prometheus/0")

target_rel_id = self.harness.add_relation("prometheus-target", "target-app")
self.harness.add_relation_unit(target_rel_id, "target-app/0")
self.harness.update_relation_data(
target_rel_id,
"target-app/0",
{
"hostname": "scrape_target_0",
"port": "1234",
},
)

prometheus_rel_data = self.harness.get_relation_data(
prometheus_rel_id, self.harness.model.app.name
)
scrape_jobs = json.loads(prometheus_rel_data.get("scrape_jobs", "[]"))
expected_jobs = [
{
"job_name": "juju_testmodel_ae3c0b1_target-app_prometheus_scrape",
"static_configs": [
{
"targets": ["scrape_target_0:1234"],
"labels": {
"juju_model": "testmodel",
"juju_model_uuid": "ae3c0b14-9c3a-4262-b560-7a6ad7d3642f",
"juju_application": "target-app",
"juju_unit": "target-app/0",
"host": "scrape_target_0",
"dns_name": "scrape_target_0",
},
}
],
"relabel_configs": [RELABEL_INSTANCE_CONFIG],
}
]
self.assertListEqual(scrape_jobs, expected_jobs)

def test_scrape_jobs_are_forwarded_on_adding_targets_then_prometheus(self):
self.harness.set_leader(True)

Expand Down

0 comments on commit 8174714

Please sign in to comment.