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

Add refresh_events to COSAgentProvider to update relation data #151

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions src/apt_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Apt helper module for missing features in operator_libs_linux."""

import re
from subprocess import PIPE, CalledProcessError, check_output
from typing import Optional
Expand Down
4 changes: 4 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ def __init__(self, *args: Any) -> None:
super().__init__(*args)
self.hw_tool_helper = HWToolHelper()

# Add refresh_events to COSAgentProvider to update relation data when
# config changed (default behavior) and upgrade charm. This is useful
# for updating alert rules.
self.cos_agent_provider = COSAgentProvider(
self,
refresh_events=[self.on.config_changed, self.on.upgrade_charm],
chanchiwai-ray marked this conversation as resolved.
Show resolved Hide resolved
metrics_endpoints=[
{"path": "/metrics", "port": int(self.model.config["exporter-port"])}
],
Expand Down
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config."""

import typing as t
from enum import Enum
from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions src/hardware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Hardware support config and command helper."""

import json
import logging
import subprocess
Expand Down
1 change: 1 addition & 0 deletions src/hw_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Define strategy for install, remove and verifier for different hardwares.
"""

import logging
import os
import shutil
Expand Down
1 change: 1 addition & 0 deletions src/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Exporter service helper."""

from functools import wraps
from logging import getLogger
from pathlib import Path
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
# Learn more about testing at: https://juju.is/docs/sdk/testing

import json
import unittest
from unittest import mock

Expand Down Expand Up @@ -268,3 +269,75 @@ def test_13_update_status_config_invalid(self, mock_hw_tool_helper, mock_exporte

self.harness.charm.on.update_status.emit()
self.assertEqual(self.harness.charm.unit.status, BlockedStatus("config fail message"))

@mock.patch("charm.Exporter", return_value=mock.MagicMock())
@mock.patch("charm.HWToolHelper", return_value=mock.MagicMock())
def test_14_config_changed_update_alert_rules(self, mock_hw_tool_helper, mock_exporter):
"""Test config changed will update alert rule."""
self.mock_bmc_hw_verifier.return_value = [
HWTool.IPMI_SENSOR,
HWTool.IPMI_SEL,
HWTool.IPMI_DCMI,
]
mock_hw_tool_helper.return_value.install.return_value = (True, "")
mock_hw_tool_helper.return_value.check_installed.return_value = (True, "")
mock_exporter.return_value.install.return_value = True
rid = self.harness.add_relation("cos-agent", "grafana-agent")
self.harness.begin()
self.harness.charm.on.install.emit()
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.harness.charm.on.update_status.emit()
self.assertEqual(self.harness.charm.unit.status, ActiveStatus("Unit is ready"))

relation_data = self.harness.get_relation_data(rid, "hardware-observer/0")
metrics_alert_rules = json.loads(relation_data["config"]).get("metrics_alert_rules")

with mock.patch(
"charm.COSAgentProvider._metrics_alert_rules", new_callable=mock.PropertyMock
) as mock_alert_rules:
fake_metrics_alert_rules = {} # just make it empty
dashmage marked this conversation as resolved.
Show resolved Hide resolved
mock_alert_rules.return_value = fake_metrics_alert_rules
self.harness.charm.on.config_changed.emit()

relation_data = self.harness.get_relation_data(rid, "hardware-observer/0")
updated_metrics_alert_rules = json.loads(relation_data["config"]).get(
"metrics_alert_rules"
)
self.assertEqual(updated_metrics_alert_rules, fake_metrics_alert_rules)
self.assertNotEqual(updated_metrics_alert_rules, metrics_alert_rules)

@mock.patch("charm.Exporter", return_value=mock.MagicMock())
@mock.patch("charm.HWToolHelper", return_value=mock.MagicMock())
def test_15_upgrade_charm_update__alert_rules(self, mock_hw_tool_helper, mock_exporter):
dashmage marked this conversation as resolved.
Show resolved Hide resolved
"""Test upgrade charm will update alert rule."""
self.mock_bmc_hw_verifier.return_value = [
HWTool.IPMI_SENSOR,
HWTool.IPMI_SEL,
HWTool.IPMI_DCMI,
]
mock_hw_tool_helper.return_value.install.return_value = (True, "")
mock_hw_tool_helper.return_value.check_installed.return_value = (True, "")
mock_exporter.return_value.install.return_value = True
rid = self.harness.add_relation("cos-agent", "grafana-agent")
self.harness.begin()
self.harness.charm.on.install.emit()
self.harness.add_relation_unit(rid, "grafana-agent/0")
self.harness.charm.on.update_status.emit()
self.assertEqual(self.harness.charm.unit.status, ActiveStatus("Unit is ready"))

relation_data = self.harness.get_relation_data(rid, "hardware-observer/0")
metrics_alert_rules = json.loads(relation_data["config"]).get("metrics_alert_rules")

with mock.patch(
"charm.COSAgentProvider._metrics_alert_rules", new_callable=mock.PropertyMock
) as mock_alert_rules:
fake_metrics_alert_rules = {} # just make it empty
dashmage marked this conversation as resolved.
Show resolved Hide resolved
mock_alert_rules.return_value = fake_metrics_alert_rules
self.harness.charm.on.upgrade_charm.emit()

relation_data = self.harness.get_relation_data(rid, "hardware-observer/0")
updated_metrics_alert_rules = json.loads(relation_data["config"]).get(
"metrics_alert_rules"
)
self.assertEqual(updated_metrics_alert_rules, fake_metrics_alert_rules)
self.assertNotEqual(updated_metrics_alert_rules, metrics_alert_rules)
Loading