From 40cac2cb39bdea2a2aa141a10bdea46236896b98 Mon Sep 17 00:00:00 2001 From: yazansalti Date: Tue, 14 Nov 2023 13:58:14 +0400 Subject: [PATCH 1/2] Checks if certificate is already requested Adds unit tests cases --- src/charm.py | 4 +++- tests/unit/test_charm.py | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/charm.py b/src/charm.py index 74b1783..b2efbcd 100755 --- a/src/charm.py +++ b/src/charm.py @@ -170,7 +170,9 @@ def _on_certificates_relation_joined(self, event: EventBase) -> None: if not self._private_key_is_stored(): event.defer() return - self._request_new_certificate() + if not self._certificate_is_stored(): + self._request_new_certificate() + return def _on_certificate_available(self, event: CertificateAvailableEvent) -> None: """Pushes certificate to workload and configures workload. diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index bbab6df..da398cb 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -494,7 +494,7 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_csr_ csr = b"whatever csr content" patch_generate_csr.return_value = csr patch_pull.return_value = StringIO("private key content") - patch_exists.return_value = True + patch_exists.side_effect = [True, False] self.harness.set_can_connect(container="pcf", val=True) self.harness.charm._on_certificates_relation_joined(event=Mock) @@ -508,7 +508,7 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_csr_ @patch("charm.generate_csr") @patch("ops.model.Container.pull") @patch("ops.model.Container.exists") - def test_given_private_key_exists_when_on_certificates_relation_joined_then_cert_is_requested( + def test_given_private_key_exists_and_cert_not_yet_requested_when_on_certificates_relation_joined_then_cert_is_requested( # noqa: E501 self, patch_exists, patch_pull, @@ -518,13 +518,33 @@ def test_given_private_key_exists_when_on_certificates_relation_joined_then_cert csr = b"whatever csr content" patch_generate_csr.return_value = csr patch_pull.return_value = StringIO("private key content") - patch_exists.return_value = True + patch_exists.side_effect = [True, False] self.harness.set_can_connect(container="pcf", val=True) self.harness.charm._on_certificates_relation_joined(event=Mock) patch_request_certificate_creation.assert_called_with(certificate_signing_request=csr) + @patch( + "charms.tls_certificates_interface.v2.tls_certificates.TLSCertificatesRequiresV2.request_certificate_creation", # noqa: E501 + ) + @patch("ops.model.Container.push", new=Mock) + @patch("ops.model.Container.pull") + @patch("ops.model.Container.exists") + def test_given_cert_already_stored_when_on_certificates_relation_joined_then_cert_is_not_requested( # noqa: E501 + self, + patch_exists, + patch_pull, + patch_request_certificate_creation, + ): + patch_pull.return_value = StringIO("private key content") + patch_exists.return_value = True + self.harness.set_can_connect(container="pcf", val=True) + + self.harness.charm._on_certificates_relation_joined(event=Mock) + + patch_request_certificate_creation.assert_not_called + @patch("ops.model.Container.pull") @patch("ops.model.Container.exists") @patch("ops.model.Container.push") From 3352d2987fe78b16dc4685dc8ff150dde3a1058c Mon Sep 17 00:00:00 2001 From: yazansalti Date: Wed, 15 Nov 2023 10:28:32 +0400 Subject: [PATCH 2/2] Addresses review comments --- src/charm.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/charm.py b/src/charm.py index b2efbcd..a9730df 100755 --- a/src/charm.py +++ b/src/charm.py @@ -170,10 +170,11 @@ def _on_certificates_relation_joined(self, event: EventBase) -> None: if not self._private_key_is_stored(): event.defer() return - if not self._certificate_is_stored(): - self._request_new_certificate() + if self._certificate_is_stored(): return + self._request_new_certificate() + def _on_certificate_available(self, event: CertificateAvailableEvent) -> None: """Pushes certificate to workload and configures workload.