|
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 |
3 | 7 | import ops.testing
|
4 | 8 | from test.fixme_charm.src.charm import Charm
|
5 | 9 |
|
| 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 | +) |
6 | 40 |
|
7 | 41 | def test_charm_runs():
|
8 |
| - # Arrange: |
9 |
| - # Create a Context to specify what code we will be running, |
10 | 42 | 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() |
16 | 44 | 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 |
17 | 53 |
|
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