-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config option for log_level (#217)
* Add config option for log_level * Migrate to ops.testing (Scenario v7) * Merge the test_machine_charm dir with scenario dir root * Set BlockedStatus on invalid config * Add status.config_error
- Loading branch information
1 parent
c0382b7
commit 5ea3a57
Showing
18 changed files
with
310 additions
and
528 deletions.
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
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 |
---|---|---|
@@ -1,19 +1,48 @@ | ||
import shutil | ||
from pathlib import Path, PosixPath | ||
# Copyright 2022 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
from unittest.mock import PropertyMock, patch | ||
|
||
import pytest | ||
from charms.tempo_coordinator_k8s.v0.charm_tracing import charm_tracing_disabled | ||
|
||
from tests.scenario.helpers import CHARM_ROOT | ||
|
||
@pytest.fixture | ||
def placeholder_cfg_path(tmp_path): | ||
return tmp_path / "foo.yaml" | ||
|
||
class Vroot(PosixPath): | ||
def clean(self) -> None: | ||
shutil.rmtree(self) | ||
shutil.copytree(CHARM_ROOT / "src", self / "src") | ||
|
||
@pytest.fixture() | ||
def mock_config_path(placeholder_cfg_path): | ||
with patch("grafana_agent.CONFIG_PATH", placeholder_cfg_path): | ||
yield | ||
|
||
@pytest.fixture | ||
def vroot(tmp_path) -> Path: | ||
vroot = Vroot(str(tmp_path.absolute())) | ||
vroot.clean() | ||
return vroot | ||
|
||
@pytest.fixture(autouse=True) | ||
def mock_snap(): | ||
"""Mock the charm's snap property so we don't access the host.""" | ||
with patch("charm.GrafanaAgentMachineCharm.snap", new_callable=PropertyMock): | ||
yield | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_refresh(): | ||
"""Mock the refresh call so we don't access the host.""" | ||
with patch("snap_management._install_snap", new_callable=PropertyMock): | ||
yield | ||
|
||
|
||
CONFIG_MATRIX = [ | ||
{"classic_snap": True}, | ||
{"classic_snap": False}, | ||
] | ||
|
||
|
||
@pytest.fixture(params=CONFIG_MATRIX) | ||
def charm_config(request): | ||
return request.param | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_charm_tracing(): | ||
with charm_tracing_disabled(): | ||
yield |
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
# 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(charm_type) -> dict: | ||
raw_meta = (CHARM_ROOT / "charmcraft").with_suffix(".yaml").read_text() | ||
return yaml.safe_load(raw_meta) | ||
from unittest.mock import MagicMock | ||
|
||
|
||
def set_run_out(mock_run, returncode: int = 0, stdout: str = "", stderr: str = ""): | ||
mock_stdout = MagicMock() | ||
mock_stdout.configure_mock( | ||
**{ | ||
"returncode": returncode, | ||
"stdout.decode.return_value": stdout, | ||
"stderr.decode.return_value": stderr, | ||
} | ||
) | ||
mock_run.return_value = mock_stdout |
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,50 @@ | ||
# Copyright 2023 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
import yaml | ||
from ops import BlockedStatus | ||
from ops.testing import Context, State | ||
|
||
import charm | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def patch_all(placeholder_cfg_path): | ||
with patch("grafana_agent.CONFIG_PATH", placeholder_cfg_path): | ||
yield | ||
|
||
|
||
@pytest.mark.parametrize("log_level", ("debug", "info", "warn", "error")) | ||
def test_valid_config_log_level(placeholder_cfg_path, log_level): | ||
"""Asserts that all valid log_levels set the correct config.""" | ||
# GIVEN a GrafanaAgentMachineCharm | ||
with patch("charm.GrafanaAgentMachineCharm.is_ready", True): | ||
ctx = Context(charm_type=charm.GrafanaAgentMachineCharm) | ||
# WHEN the config option for log_level is set to a VALID option | ||
ctx.run(ctx.on.start(), State(config={"log_level": log_level})) | ||
|
||
# THEN the config file has the correct server:log_level field | ||
yaml_cfg = yaml.safe_load(placeholder_cfg_path.read_text()) | ||
assert yaml_cfg["server"]["log_level"] == log_level | ||
|
||
|
||
@patch("charm.GrafanaAgentMachineCharm.is_ready", True) | ||
def test_invalid_config_log_level(placeholder_cfg_path): | ||
"""Asserts that an invalid log_level sets Blocked status.""" | ||
# GIVEN a GrafanaAgentMachineCharm | ||
ctx = Context(charm_type=charm.GrafanaAgentMachineCharm) | ||
with ctx(ctx.on.start(), State(config={"log_level": "foo"})) as mgr: | ||
# WHEN the config option for log_level is set to an invalid option | ||
mgr.run() | ||
# THEN a warning Juju debug-log is created | ||
assert any( | ||
log.level == "WARNING" and "log_level must be one of" in log.message | ||
for log in ctx.juju_log | ||
) | ||
# AND the charm goes into blocked status | ||
assert isinstance(mgr.charm.unit.status, BlockedStatus) | ||
# AND the config file defaults the server:log_level field to "info" | ||
yaml_cfg = yaml.safe_load(placeholder_cfg_path.read_text()) | ||
assert yaml_cfg["server"]["log_level"] == "info" |
Oops, something went wrong.