Skip to content

Commit dd17314

Browse files
committed
usable Scenario tests for 1p charm lib
1 parent 1b397dd commit dd17314

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

ops/_tracing/api.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ def __init__(self, framework: ops.Framework):
9292

9393
def _reconcile(self, _event: Any):
9494
url, ca = self._get_config()
95-
print(["FIXME", url, ca])
96-
9795
ops._tracing.set_tracing_destination(url=url, ca=ca)
9896

9997
def _get_config(self) -> tuple[str | None, str | None]:
@@ -115,6 +113,6 @@ def _get_config(self) -> tuple[str | None, str | None]:
115113
ca_rel_id = ca_rel.id if ca_rel else None
116114

117115
if ca_rel and self._certificate_transfer.is_ready(ca_rel):
118-
return url, "\n".join(self._certificate_transfer.get_all_certificates(ca_rel_id))
116+
return url, "\n".join(sorted(self._certificate_transfer.get_all_certificates(ca_rel_id)))
119117
else:
120118
return None, None

test/fixme_charm/metadata.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
name: fixme_charm
2+
3+
requires:
4+
charm-tracing:
5+
interface: tracing
6+
limit: 1
7+
send-ca-cert:
8+
interface: certificate_transfer
9+
limit: 1

test/fixme_charm/src/charm.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import ops
2+
import ops._tracing.api
23

34

45
class Charm(ops.CharmBase):
5-
def init(self, framework: ops.Framework):
6+
def __init__(self, framework: ops.Framework):
67
super().__init__(framework)
7-
self.framework.observe(self.on.collect_app_status, self._on_collect_status)
8-
self.framework.observe(self.on.collect_unit_status, self._on_collect_status)
9-
return
108
self._tracing = ops._tracing.api.Tracing(
119
self,
1210
tracing_relation_name="charm-tracing",
1311
ca_relation_name="send-ca-cert",
1412
)
13+
self.framework.observe(self.on.collect_app_status, self._on_collect_status)
14+
self.framework.observe(self.on.collect_unit_status, self._on_collect_status)
1515

16+
# FIXME: not strictly necessary, only testing that charm didn't crash
1617
def _on_collect_status(self, event: ops.CollectStatusEvent):
1718
event.add_status(ops.ActiveStatus())
1819

test/fixme_charm/test/test_nothing.py

+66-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,74 @@
1-
import sys
2-
print(sys.path)
1+
import json
2+
from unittest.mock import Mock
3+
4+
import pytest
5+
6+
import ops._tracing
37
import ops.testing
48
from test.fixme_charm.src.charm import Charm
59

10+
pytestmark = pytest.mark.filterwarnings("ignore::pydantic.PydanticDeprecatedSince20")
11+
12+
13+
HTTP_RELATION = ops.testing.Relation(
14+
"charm-tracing",
15+
remote_app_data={
16+
"receivers": json.dumps([{
17+
"protocol": {"name": "otlp_http", "type": "http"},
18+
"url": "http://tracing.example:4318/v1/traces"}]),
19+
},
20+
id=1,
21+
)
22+
23+
HTTPS_RELATION = ops.testing.Relation(
24+
"charm-tracing",
25+
remote_app_data={
26+
"receivers": json.dumps([{
27+
"protocol": {"name": "otlp_http", "type": "http"},
28+
"url": "https://tls.example/v1/traces"}]),
29+
},
30+
id=2,
31+
)
32+
33+
CA_RELATION = ops.testing.Relation(
34+
"send-ca-cert",
35+
remote_app_data={
36+
"certificates": json.dumps(["BEGIN FAKE CA", "ONE MORE"]),
37+
},
38+
id=3,
39+
)
640

741
def test_charm_runs():
8-
# Arrange:
9-
# Create a Context to specify what code we will be running,
1042
ctx = ops.testing.Context(Charm)
11-
# and create a State to specify what simulated data the charm being run will access.
12-
state_in = ops.testing.State(leader=True)
13-
14-
# Act:
15-
# Ask the context to run an event, e.g. 'start', with the state we have previously created.
43+
state_in = ops.testing.State()
1644
state_out = ctx.run(ctx.on.start(), state_in)
45+
assert isinstance(state_out.unit_status, ops.ActiveStatus)
46+
47+
48+
@pytest.fixture
49+
def mock_destination(monkeypatch: pytest.MonkeyPatch) -> Mock:
50+
rv = Mock()
51+
monkeypatch.setattr(ops._tracing, "set_tracing_destination", rv)
52+
return rv
1753

18-
# Assert:
19-
# Verify that the output state looks like you expect it to.
20-
assert state_out.unit_status == 33
54+
55+
def test_no_tracing_destination(mock_destination: Mock):
56+
ctx = ops.testing.Context(Charm)
57+
state = ops.testing.State()
58+
ctx.run(ctx.on.start(), state)
59+
mock_destination.assert_called_with(url=None, ca=None)
60+
61+
62+
def test_http_tracing_destination(mock_destination: Mock):
63+
ctx = ops.testing.Context(Charm)
64+
state = ops.testing.State(relations={HTTP_RELATION})
65+
ctx.run(ctx.on.relation_changed(HTTP_RELATION), state)
66+
mock_destination.assert_called_with(url="http://tracing.example:4318/v1/traces", ca=None)
67+
68+
69+
@pytest.mark.parametrize("relation", [HTTPS_RELATION, CA_RELATION])
70+
def test_https_tracing_destination(mock_destination: Mock, relation: ops.testing.Relation):
71+
ctx = ops.testing.Context(Charm)
72+
state = ops.testing.State(relations={HTTPS_RELATION, CA_RELATION})
73+
ctx.run(ctx.on.relation_changed(relation), state)
74+
mock_destination.assert_called_with(url="https://tls.example/v1/traces", ca="BEGIN FAKE CA\nONE MORE")

0 commit comments

Comments
 (0)