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

Commit

Permalink
Add using is_ready safeguard to get_endpoint docstring (#170)
Browse files Browse the repository at this point in the history
* add more comments

* make docstring less exposing
  • Loading branch information
michaeldmitry authored Sep 17, 2024
1 parent 49eb25d commit 82090bd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/charms/tempo_k8s/v2/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 9
LIBPATCH = 10

PYDEPS = ["pydantic"]

Expand Down Expand Up @@ -902,7 +902,16 @@ def _get_endpoint(
def get_endpoint(
self, protocol: ReceiverProtocol, relation: Optional[Relation] = None
) -> Optional[str]:
"""Receiver endpoint for the given protocol."""
"""Receiver endpoint for the given protocol.
It could happen that this function gets called before the provider publishes the endpoints.
In such a scenario, if a non-leader unit calls this function, a permission denied exception will be raised due to
restricted access. To prevent this, this function needs to be guarded by the `is_ready` check.
Raises:
ProtocolNotRequestedError:
If the charm unit is the leader unit and attempts to obtain an endpoint for a protocol it did not request.
"""
endpoint = self._get_endpoint(relation or self._relation, protocol=protocol)
if not endpoint:
requested_protocols = set()
Expand Down
4 changes: 3 additions & 1 deletion tests/scenario/test_charm_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,10 @@ def test_tracing_requirer_remote_charm_no_request_no_response(leader, relation):
relations = []

# THEN self.tempo() will raise on init
# FIXME: non-leader units should get a permission denied exception,
# but it won't fire due to https://github.com/canonical/operator/issues/1378
with pytest.raises(UncaughtCharmError, match=r"ProtocolNotRequestedError"):
ctx.run("start", State(relations=relations))
ctx.run("start", State(relations=relations, leader=leader))


class MyRemoteBorkyCharm(CharmBase):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ description = Run scenario tests
deps =
pytest<8.2.0 # https://github.com/pytest-dev/pytest/issues/12263
coverage[toml]
ops-scenario>=4.0.3
ops-scenario>=4.0.3,<7.0.0
.[lib_pydeps]
-r{toxinidir}/requirements.txt
commands =
Expand Down

0 comments on commit 82090bd

Please sign in to comment.