Skip to content

Commit

Permalink
Use relation_created instead of relation_joined
Browse files Browse the repository at this point in the history
The handler is supposed to run when the relation is created, not when a unit joins the relation

See canonical/mysql-router-k8s-operator#101
  • Loading branch information
carlcsaposs-canonical committed Apr 15, 2024
1 parent 09252b7 commit fee9c37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
9 changes: 4 additions & 5 deletions lib/charms/opensearch/v0/opensearch_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
generate_csr,
generate_private_key,
)
from ops.charm import ActionEvent, RelationBrokenEvent, RelationJoinedEvent
from ops.charm import ActionEvent, RelationBrokenEvent, RelationCreatedEvent
from ops.framework import Object

# The unique Charmhub library identifier, never change it
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(self, charm, peer_relation: str):
)

self.framework.observe(
self.charm.on[TLS_RELATION].relation_joined, self._on_tls_relation_joined
self.charm.on[TLS_RELATION].relation_created, self._on_tls_relation_created
)
self.framework.observe(
self.charm.on[TLS_RELATION].relation_broken, self._on_tls_relation_broken
Expand Down Expand Up @@ -108,12 +108,11 @@ def request_new_unit_certificates(self) -> None:
secrets = self.charm.secrets.get_object(Scope.UNIT, cert_type.val)
self._request_certificate_renewal(Scope.UNIT, cert_type, secrets)

def _on_tls_relation_joined(self, event: RelationJoinedEvent) -> None:
"""Request certificate when TLS relation joined."""
def _on_tls_relation_created(self, event: RelationCreatedEvent) -> None:
"""Request certificate when TLS relation created."""
if not (deployment_desc := self.charm.opensearch_peer_cm.deployment_desc()):
event.defer()
return

admin_cert = self.charm.secrets.get_object(Scope.APP, CertType.APP_ADMIN.val)
if (
self.charm.unit.is_leader()
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/lib/test_opensearch_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ def test_find_secret(self):
@patch("charms.opensearch.v0.opensearch_tls.OpenSearchTLS._request_certificate")
@patch("charm.OpenSearchOperatorCharm._put_admin_user")
@patch("charm.OpenSearchOperatorCharm._purge_users")
def test_on_relation_joined_admin(
def test_on_relation_created_admin(
self, _, _put_admin_user, _request_certificate, deployment_desc
):
"""Test on certificate relation joined event."""
"""Test on certificate relation created event."""
deployment_desc.return_value = DeploymentDescription(
config=PeerClusterConfig(cluster_name="", init_hold=False, roles=[]),
start=StartMode.WITH_GENERATED_ROLES,
Expand All @@ -114,11 +114,10 @@ def test_on_relation_joined_admin(
app=self.charm.app.name,
state=DeploymentState(value=State.ACTIVE),
)

event_mock = MagicMock()

self.harness.set_leader(is_leader=True)
self.charm.tls._on_tls_relation_joined(event_mock)
self.charm.tls._on_tls_relation_created(event_mock)
self.assertEqual(
_request_certificate.mock_calls,
[
Expand All @@ -134,10 +133,10 @@ def test_on_relation_joined_admin(
@patch("charms.opensearch.v0.opensearch_tls.OpenSearchTLS._request_certificate")
@patch("charm.OpenSearchOperatorCharm._put_admin_user")
@patch("charm.OpenSearchOperatorCharm._purge_users")
def test_on_relation_joined_non_admin(
def test_on_relation_created_non_admin(
self, _, _put_admin_user, _request_certificate, deployment_desc
):
"""Test on certificate relation joined event."""
"""Test on certificate relation created event."""
deployment_desc.return_value = DeploymentDescription(
config=PeerClusterConfig(cluster_name="", init_hold=False, roles=[]),
start=StartMode.WITH_GENERATED_ROLES,
Expand All @@ -146,11 +145,10 @@ def test_on_relation_joined_non_admin(
app=self.charm.app.name,
state=DeploymentState(value=State.ACTIVE),
)

event_mock = MagicMock()

self.harness.set_leader(is_leader=False)
self.charm.tls._on_tls_relation_joined(event_mock)
self.charm.tls._on_tls_relation_created(event_mock)
self.assertEqual(
_request_certificate.mock_calls,
[
Expand Down

0 comments on commit fee9c37

Please sign in to comment.