Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate the availability of traefik container before updating CA cer… #267

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ def __init__(self, *args):

def _on_recv_ca_cert_available(self, event: CertificateTransferAvailableEvent):
# Assuming only one cert per relation (this is in line with the original lib design).
if not self.container.can_connect():
return
self._update_received_ca_certs(event)

def _update_received_ca_certs(self, event: Optional[CertificateTransferAvailableEvent] = None):
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,52 @@ def test_tcp_config(self):
assert yaml.safe_load(static_config)["entryPoints"][prefix] == expected_entrypoint


class TestTraefikCertTransferInterface(unittest.TestCase):
def setUp(self):
self.harness: Harness[TraefikIngressCharm] = Harness(TraefikIngressCharm)
self.harness.set_model_name("test-model")
self.addCleanup(self.harness.cleanup)
patcher = patch.object(TraefikIngressCharm, "version", property(lambda *_: "0.0.0"))
self.mock_version = patcher.start()
self.addCleanup(patcher.stop)
self.container_name = "traefik"

@patch("ops.model.Container.exec")
@patch("charm._get_loadbalancer_status", lambda **__: "10.0.0.1")
@patch("charm.KubernetesServicePatch", lambda *_, **__: None)
def test_transferred_ca_certs_are_updated(self, patch_exec):
# Given container is ready, when receive-ca-cert relation joins,
# then ca certs are updated.
provider_app = "self-signed-certificates"
self.harness.set_leader(True)
self.harness.begin_with_initial_hooks()
self.harness.set_can_connect(container=self.container_name, val=True)
certificate_transfer_rel_id = self.harness.add_relation(
relation_name="receive-ca-cert", remote_app=provider_app
)
self.harness.add_relation_unit(
relation_id=certificate_transfer_rel_id, remote_unit_name=f"{provider_app}/0"
)
patch_exec.assert_called_once_with(["update-ca-certificates", "--fresh"])

@patch("ops.model.Container.exec")
@patch("charm._get_loadbalancer_status", lambda **__: "10.0.0.1")
@patch("charm.KubernetesServicePatch", lambda *_, **__: None)
def test_transferred_ca_certs_are_not_updated(self, patch_exec):
# Given container is not ready, when receive-ca-cert relation joins,
# then not attempting to update ca certs.
provider_app = "self-signed-certificates"
self.harness.set_leader(True)
self.harness.set_can_connect(container=self.container_name, val=False)
certificate_transfer_rel_id = self.harness.add_relation(
relation_name="receive-ca-cert", remote_app=provider_app
)
self.harness.add_relation_unit(
relation_id=certificate_transfer_rel_id, remote_unit_name=f"{provider_app}/0"
)
patch_exec.assert_not_called()


class TestConfigOptionsValidation(unittest.TestCase):
@patch("charm._get_loadbalancer_status", lambda **_: "10.0.0.1")
@patch("charm.KubernetesServicePatch", lambda *_, **__: None)
Expand Down
Loading