Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configure prometheus_remote_write config for non-leader #168

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _on_relation_changed(self, event):

def _on_relation_broken(self, event):
self.on.cloud_config_revoked.emit() # pyright: ignore

def _is_not_empty(self, s):
return bool(s and not s.isspace())

Expand Down Expand Up @@ -124,7 +124,7 @@ def prometheus_endpoint(self) -> dict:
"""Return the prometheus endpoint dict."""
if not self.prometheus_ready:
return {}

endpoint = {}
endpoint["url"] = self.prometheus_url
if self.credentials:
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/test_cloud_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

import yaml
from ops import ActiveStatus, BlockedStatus
from ops.testing import Harness

from charm import GrafanaAgentMachineCharm as GrafanaAgentCharm


class TestUpdateStatus(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how much of this test should reside in canonical/grafana-cloud-integrator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the only part that's testable in grafana-cloud-integrator is added already here. The rest of this test is grafana-agent business logic (confirm the settings appear in the place they need to be). Although tbh this unit test is a pretty large unit test, more an integration test that just doesn't need juju...

def setUp(self, *unused):
patcher = patch.object(GrafanaAgentCharm, "_agent_version", property(lambda *_: "0.0.0"))
self.mock_version = patcher.start()
self.addCleanup(patcher.stop)

temp_config_path = tempfile.mkdtemp() + "/grafana-agent.yaml"
# otherwise will attempt to write to /etc/grafana-agent.yaml
patcher = patch("grafana_agent.CONFIG_PATH", temp_config_path)
self.config_path_mock = patcher.start()
self.addCleanup(patcher.stop)

patcher = patch("charm.snap")
self.mock_snap = patcher.start()
self.addCleanup(patcher.stop)

patcher = patch.object(GrafanaAgentCharm, "_install")
self.mock_install = patcher.start()
self.addCleanup(patcher.stop)

def test_prometheus_remote_write_config_with_grafana_cloud_integrator(self):
"""Asserts that the prometheus remote write config is written correctly for leaders and non-leaders."""
for leader in (True, False):
with self.subTest(leader=leader):
harness = Harness(GrafanaAgentCharm)
harness.set_model_name(self.__class__.__name__)
harness.set_leader(True)
harness.begin_with_initial_hooks()

# WHEN an incoming relation is added
rel_id = harness.add_relation("juju-info", "grafana-agent")
harness.add_relation_unit(rel_id, "grafana-agent/0")

# THEN the charm goes into blocked status
assert isinstance(harness.charm.unit.status, BlockedStatus)

# AND WHEN the necessary outgoing relations are added
harness.add_relation(
"grafana-cloud-config",
"grafana-cloud-integrator",
app_data={"prometheus_url": "http://some.domain.name:9090/api/v1/write"},
)

# THEN the charm goes into active status
assert isinstance(harness.charm.unit.status, ActiveStatus)

# THEN with the expected prometheus endpoint settings are written to the config file
config = yaml.safe_load(Path(self.config_path_mock).read_text())
assert len(config["integrations"]["prometheus_remote_write"]) == 1
self.assertEqual(
config["integrations"]["prometheus_remote_write"][0]["url"],
"http://some.domain.name:9090/api/v1/write",
)
Loading