diff --git a/tests/scenario/helpers.py b/tests/scenario/helpers.py new file mode 100644 index 0000000..822f77c --- /dev/null +++ b/tests/scenario/helpers.py @@ -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) diff --git a/tests/scenario/test_charm.py b/tests/scenario/test_charm.py new file mode 100644 index 0000000..3410e01 --- /dev/null +++ b/tests/scenario/test_charm.py @@ -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 diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 482a6d8..fed1514 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -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)