Fix: make ceph_metrics test more robust
Instead of messing with the harness' construction patch the missing network-get in place Change-Id: I162a0b73d76a3ed18689c2baf258372efe5f2ec4
This commit is contained in:
parent
efcd98746b
commit
e36a1890b4
|
@ -0,0 +1,23 @@
|
||||||
|
# Copyright 2020 Canonical Ltd.
|
||||||
|
# See LICENSE file for licensing details.
|
||||||
|
|
||||||
|
from typing import Callable
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
|
||||||
|
def patch_network_get(private_address="10.0.0.10") -> Callable:
|
||||||
|
def network_get(*args, **kwargs) -> dict:
|
||||||
|
"""Patch for the not-yet-implemented testing backend needed for `bind_address`.
|
||||||
|
|
||||||
|
This patch decorator can be used for cases such as:
|
||||||
|
self.model.get_binding(event.relation).network.bind_address
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
"bind-addresses": [
|
||||||
|
{
|
||||||
|
"addresses": [{"value": private_address}],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
return patch("ops.testing._TestingModelBackend.network_get", network_get)
|
|
@ -1,22 +1,23 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
# Copyright 2022 Canonical Ltd.
|
||||||
|
# See LICENSE file for licensing details.
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
import tempfile
|
import tempfile
|
||||||
import textwrap
|
import textwrap
|
||||||
|
|
||||||
# Copyright 2022 Canonical Ltd.
|
|
||||||
# See LICENSE file for licensing details.
|
|
||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from ops import storage, model, framework
|
from ops.testing import Harness
|
||||||
from ops.testing import Harness, _TestingModelBackend
|
|
||||||
|
|
||||||
import ceph_metrics # noqa: avoid circ. import
|
import ceph_metrics # noqa: avoid circ. import
|
||||||
import charm
|
import charm
|
||||||
|
import helpers
|
||||||
|
|
||||||
|
|
||||||
|
@helpers.patch_network_get()
|
||||||
class TestCephMetrics(unittest.TestCase):
|
class TestCephMetrics(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls):
|
def setUpClass(cls):
|
||||||
|
@ -40,35 +41,7 @@ class TestCephMetrics(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.harness = Harness(charm.CephMonCharm)
|
self.harness = Harness(charm.CephMonCharm)
|
||||||
|
|
||||||
# BEGIN: Workaround until network_get is implemented
|
|
||||||
class _TestingOPSModelBackend(_TestingModelBackend):
|
|
||||||
def network_get(self, endpoint_name, relation_id=None):
|
|
||||||
network_data = {
|
|
||||||
"bind-addresses": [
|
|
||||||
{
|
|
||||||
"addresses": [{"value": "10.0.0.10"}],
|
|
||||||
}
|
|
||||||
],
|
|
||||||
}
|
|
||||||
return network_data
|
|
||||||
|
|
||||||
self.harness._backend = _TestingOPSModelBackend(
|
|
||||||
self.harness._unit_name, self.harness._meta
|
|
||||||
)
|
|
||||||
self.harness._model = model.Model(
|
|
||||||
self.harness._meta, self.harness._backend
|
|
||||||
)
|
|
||||||
self.harness._framework = framework.Framework(
|
|
||||||
storage.SQLiteStorage(":memory:"),
|
|
||||||
self.harness._charm_dir,
|
|
||||||
self.harness._meta,
|
|
||||||
self.harness._model,
|
|
||||||
)
|
|
||||||
# END Workaround
|
|
||||||
|
|
||||||
self.addCleanup(self.harness.cleanup)
|
self.addCleanup(self.harness.cleanup)
|
||||||
|
|
||||||
self.harness.begin()
|
self.harness.begin()
|
||||||
self.harness.set_leader(True)
|
self.harness.set_leader(True)
|
||||||
self.harness.charm.metrics_endpoint._alert_rules_path = self.rules_dir
|
self.harness.charm.metrics_endpoint._alert_rules_path = self.rules_dir
|
||||||
|
@ -131,7 +104,9 @@ class TestCephMetrics(unittest.TestCase):
|
||||||
@patch("ceph_metrics.ceph_utils.is_bootstrapped", return_value=True)
|
@patch("ceph_metrics.ceph_utils.is_bootstrapped", return_value=True)
|
||||||
@patch("ceph_metrics.CephMetricsEndpointProvider._set_alert_rules")
|
@patch("ceph_metrics.CephMetricsEndpointProvider._set_alert_rules")
|
||||||
def test_update_alert_rules_empty(
|
def test_update_alert_rules_empty(
|
||||||
self, set_alert_rules, _is_bootstrapped,
|
self,
|
||||||
|
set_alert_rules,
|
||||||
|
_is_bootstrapped,
|
||||||
):
|
):
|
||||||
"""Test: no alert rules created with empty alert rules file."""
|
"""Test: no alert rules created with empty alert rules file."""
|
||||||
rel_id = self.harness.add_relation("metrics-endpoint", "prometheus")
|
rel_id = self.harness.add_relation("metrics-endpoint", "prometheus")
|
||||||
|
|
Loading…
Reference in New Issue