Skip to content

Commit e1d71b1

Browse files
committed
implement some unit tests
1 parent 4c0584e commit e1d71b1

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

tracing/test/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,13 @@ def setup_tracing(monkeypatch: pytest.MonkeyPatch, juju_context: _JujuContext):
9090
# - forcibly reset the opentelemetry global state
9191
# - tracing provider span processor, as that holds the helper thread
9292
# - the tracing provider, perhaps?
93+
94+
95+
@pytest.fixture
96+
def sample_charm():
97+
extra = str(Path(__file__).parent / 'sample_charm/src')
98+
sys.path.append(extra)
99+
from charm import SampleCharm
100+
101+
yield SampleCharm
102+
sys.path.remove(extra)

tracing/test/sample_charm/src/charm.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import ops
1919

2020

21-
class Charm(ops.CharmBase):
21+
class SampleCharm(ops.CharmBase):
2222
def __init__(self, framework: ops.Framework):
2323
super().__init__(framework)
2424
self.tracing = ops.tracing.Tracing(
@@ -35,4 +35,4 @@ def _on_collect_status(self, event: ops.CollectStatusEvent):
3535

3636

3737
if __name__ == '__main__':
38-
ops.main(Charm)
38+
ops.main(SampleCharm)

tracing/test/test_backend.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,32 @@
1313
# limitations under the License.
1414
from __future__ import annotations
1515

16+
import pytest
1617

17-
def test_set_destination_again(): ...
18-
def test_set_destination_none(): ...
19-
def test_set_destination(): ...
18+
import ops_tracing
19+
from ops_tracing.const import Config
20+
21+
22+
def test_unset_destination(setup_tracing: None):
23+
assert ops_tracing.backend._exporter
24+
ops_tracing.set_destination(None, None)
25+
assert ops_tracing.backend._exporter.buffer.get_destination() == Config(None, None)
26+
27+
28+
def test_set_destination(setup_tracing: None):
29+
assert ops_tracing.backend._exporter
30+
ops_tracing.set_destination('http://a.com', None)
31+
assert ops_tracing.backend._exporter.buffer.get_destination() == Config('http://a.com', None)
32+
33+
34+
def test_set_destination_again(setup_tracing: None):
35+
assert ops_tracing.backend._exporter
36+
ops_tracing.set_destination('http://a.com', None)
37+
ops_tracing.set_destination('http://a.com', None)
38+
39+
40+
@pytest.mark.parametrize('url', ['file:///etc/passwd', 'gopher://aaa'])
41+
def test_set_destination_invalid_url(setup_tracing: None, url: str):
42+
assert ops_tracing.backend._exporter
43+
with pytest.raises(ValueError):
44+
ops_tracing.set_destination(url, None)

tracing/test/test_nothing.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
from typing import Type
1819
from unittest.mock import Mock
1920

2021
import pytest
@@ -23,13 +24,12 @@
2324
import ops.testing
2425

2526
_pydantic = pytest.importorskip('pydantic')
26-
from test.fixme_charm.src.charm import Charm # noqa: E402
2727

2828
pytestmark = pytest.mark.filterwarnings('ignore::pydantic.PydanticDeprecatedSince20')
2929

3030

31-
def test_charm_runs():
32-
ctx = ops.testing.Context(Charm)
31+
def test_charm_runs(sample_charm: Type[ops.CharmBase]):
32+
ctx = ops.testing.Context(sample_charm)
3333
state_in = ops.testing.State()
3434
state_out = ctx.run(ctx.on.start(), state_in)
3535
assert isinstance(state_out.unit_status, ops.ActiveStatus)
@@ -42,28 +42,31 @@ def mock_destination(monkeypatch: pytest.MonkeyPatch) -> Mock:
4242
return rv
4343

4444

45-
def test_no_tracing_destination(mock_destination: Mock):
46-
ctx = ops.testing.Context(Charm)
45+
def test_no_tracing_destination(sample_charm: Type[ops.CharmBase], mock_destination: Mock):
46+
ctx = ops.testing.Context(sample_charm)
4747
state = ops.testing.State()
4848
ctx.run(ctx.on.start(), state)
4949
mock_destination.assert_called_with(url=None, ca=None)
5050

5151

52-
def test_http_tracing_destination(mock_destination: Mock, http_relation: ops.testing.Relation):
53-
ctx = ops.testing.Context(Charm)
52+
def test_http_tracing_destination(
53+
sample_charm: Type[ops.CharmBase], mock_destination: Mock, http_relation: ops.testing.Relation
54+
):
55+
ctx = ops.testing.Context(sample_charm)
5456
state = ops.testing.State(relations={http_relation})
5557
ctx.run(ctx.on.relation_changed(http_relation), state)
5658
mock_destination.assert_called_with(url='http://tracing.example:4318/v1/traces', ca=None)
5759

5860

5961
@pytest.mark.parametrize('relation_to_poke', [0, 1])
6062
def test_https_tracing_destination(
63+
sample_charm: Type[ops.CharmBase],
6164
mock_destination: Mock,
6265
https_relation: ops.testing.Relation,
6366
ca_relation: ops.testing.Relation,
6467
relation_to_poke: int,
6568
):
66-
ctx = ops.testing.Context(Charm)
69+
ctx = ops.testing.Context(sample_charm)
6770
state = ops.testing.State(relations={https_relation, ca_relation})
6871
ctx.run(ctx.on.relation_changed([https_relation, ca_relation][relation_to_poke]), state)
6972
mock_destination.assert_called_with(url='https://tls.example/v1/traces', ca='FIRST\nSECOND')

0 commit comments

Comments
 (0)