Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Aug 7, 2024
1 parent 7332cec commit f9579ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
10 changes: 6 additions & 4 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from typing import Optional, Set, Tuple

import ops
from ops.pebble import APIError

from charms.data_platform_libs.v0.s3 import S3Requirer
from charms.grafana_k8s.v0.grafana_dashboard import GrafanaDashboardProvider
from charms.grafana_k8s.v0.grafana_source import GrafanaSourceProvider
Expand All @@ -36,6 +34,7 @@
)
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.pebble import APIError

from tempo import Tempo

Expand Down Expand Up @@ -374,13 +373,16 @@ def _on_tempo_pebble_custom_notice(self, event: PebbleNoticeEvent):

try:
self.tempo.container.stop("tempo-ready")
except APIError:
# ops will fire APIError but ops.testing._TestingPebbleClient will fire RuntimeError.
except (APIError, RuntimeError):
# see https://matrix.to/#/!xzmWHtGpPfVCXKivIh:ubuntu.com/
# $d42wOu61e5mqMhnDRUB6K8eV4kUAPQ_yhIQmqq5Q_cs?via=ubuntu.com&
# via=matrix.org&via=matrix.debian.social
# issue: on sleep/resume, we get this event but there's no tempo-ready
# service in pebble (somehow?)
pass
logger.debug(
"`tempo-ready` service cannot be stopped at this time (probably doesn't exist)."
)

def _on_tempo_pebble_ready(self, event: WorkloadEvent):
if not self.tempo.container.can_connect():
Expand Down
42 changes: 33 additions & 9 deletions tests/scenario/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pathlib import Path
from unittest.mock import MagicMock

Expand All @@ -8,6 +9,7 @@
from ops import pebble
from scenario import Container, Mount, Relation, State
from scenario.sequences import check_builtin_sequences
from scenario.state import Notice, _BoundNotice

from tempo import Tempo
from tests.scenario.helpers import get_tempo_config
Expand Down Expand Up @@ -65,7 +67,7 @@ def test_tempo_restart_on_ingress_v2_changed(context, tmp_path, requested_protoc
)


def _tempo_mock_with_initial_config(tmp_path):
def _tempo_mock_with_initial_config(tmp_path, tempo_ready_svc_exists: bool = True):
tempo_config = tmp_path / "tempo.yaml"
container = MagicMock()
container.can_connect = lambda: True
Expand All @@ -75,19 +77,22 @@ def _tempo_mock_with_initial_config(tmp_path):
)
initial_config = Tempo(container).generate_config(["otlp_http"])
tempo_config.write_text(yaml.safe_dump(initial_config))
layer_raw = {
"summary": "tempo layer",
"description": "foo",
"services": {
"tempo": {"startup": "enabled"},
},
}
if tempo_ready_svc_exists:
layer_raw["services"]["tempo-ready"] = {"startup": "disabled"}

tempo = Container(
"tempo",
can_connect=True,
layers={
"tempo": pebble.Layer(
{
"summary": "tempo layer",
"description": "foo",
"services": {
"tempo": {"startup": "enabled"},
"tempo-ready": {"startup": "disabled"},
},
},
layer_raw,
),
},
service_status={
Expand Down Expand Up @@ -179,3 +184,22 @@ def test_tracing_storage_is_configured_to_s3_if_s3_relation_filled(
new_config = get_tempo_config(tempo, context)
expected_config = Tempo(container).generate_config(["otlp_http"], relation_data)
assert new_config == expected_config


def test_ready_check_on_resume(context, tmp_path, caplog):
# GIVEN the charm has no tempo-ready service
container, tempo = _tempo_mock_with_initial_config(tmp_path, tempo_ready_svc_exists=False)

state = State(leader=True, containers=[tempo])

# WHEN we receive a custom-notice event
with caplog.at_level("DEBUG"):
os.environ["SCENARIO_SKIP_CONSISTENCY_CHECKS"] = "1"
# scenario doesn't play nice in this very edge case
context.run(_BoundNotice(Notice(Tempo.tempo_ready_notice_key), tempo).event, state)
del os.environ["SCENARIO_SKIP_CONSISTENCY_CHECKS"]

# THEN we get a debug-log but the charm doesn't error
assert "`tempo-ready` service cannot be stopped at this time (probably doesn't exist)." in {
r.message for r in caplog.records
}

0 comments on commit f9579ad

Please sign in to comment.