-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update charm-relation-interfaces to use prometheus_scrape schema that…
… 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
1 parent
cf36ccd
commit 8174714
Showing
3 changed files
with
89 additions
and
44 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
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) |
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,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 |
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