-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c48ef21
feat: add unit test that confirms prometheus_remote_write config is c…
ca-scribner 9f358e3
fix: bump grafana_cloud_integrator lib to fix issue with non-leader u…
ca-scribner f1c478e
fix: formatting in test_cloud_integration.py
ca-scribner a3db5c9
refactor: test_cloud_integration.py based on review feedback
ca-scribner 7696acb
fix: linting
ca-scribner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,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): | ||
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", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.There was a problem hiding this comment.
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...