Skip to content

Commit

Permalink
Fix unit tests (#57)
Browse files Browse the repository at this point in the history
* Fix unit tests

* Update postgresql_tls lib

* Readding test_on_relation_broken_extensions_keep_block

* Code review improvements
  • Loading branch information
dragomirp authored Jan 25, 2023
1 parent 6d4b41d commit 3f09edc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
7 changes: 5 additions & 2 deletions lib/charms/postgresql_k8s/v0/postgresql_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def _on_tls_relation_broken(self, _) -> None:

def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
"""Enable TLS when TLS certificate available."""
if event.certificate_signing_request.strip() != self.charm.get_secret(SCOPE, "csr").strip():
if (
event.certificate_signing_request.strip()
!= str(self.charm.get_secret(SCOPE, "csr")).strip()
):
logger.error("An unknown certificate available.")
return

Expand All @@ -144,7 +147,7 @@ def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:

def _on_certificate_expiring(self, event: CertificateExpiringEvent) -> None:
"""Request the new certificate when old certificate is expiring."""
if event.certificate.strip() != self.charm.get_secret(SCOPE, "cert").strip():
if event.certificate.strip() != str(self.charm.get_secret(SCOPE, "cert")).strip():
logger.error("An unknown certificate expiring.")
return

Expand Down
7 changes: 2 additions & 5 deletions src/relations/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,8 @@ def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
self.charm._has_blocked_status
and self.charm.unit.status.message == EXTENSIONS_BLOCKING_MESSAGE
):
if "extensions" in event.relation.data.get(
event.app, {}
) or "extensions" in event.relation.data.get(event.unit, {}):
if not self._check_for_blocking_relations(event.relation.id):
self.charm.unit.status = ActiveStatus()
if not self._check_for_blocking_relations(event.relation.id):
self.charm.unit.status = ActiveStatus()

def update_endpoints(self, event: RelationChangedEvent = None) -> None:
"""Set the read/write and read-only endpoints."""
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def setUp(self):
# Define some relations.
self.rel_id = self.harness.add_relation(RELATION_NAME, "application")
self.harness.add_relation_unit(self.rel_id, "application/0")
self.harness.add_relation_unit(self.rel_id, self.unit)
self.peer_rel_id = self.harness.add_relation(PEER, self.app)
self.harness.add_relation_unit(self.peer_rel_id, self.unit)
self.harness.update_relation_data(
Expand Down Expand Up @@ -229,14 +228,13 @@ def test_on_relation_broken_extensions_unblock(
postgresql_mock.delete_user = PropertyMock(return_value=None)
self.harness.model.unit.status = BlockedStatus("extensions requested through relation")
with self.harness.hooks_disabled():
self.rel_id = self.harness.add_relation(RELATION_NAME, "application")
self.harness.update_relation_data(
self.rel_id,
"application",
{"database": DATABASE, "extensions": ["test"]},
{"database": DATABASE, "extensions": "test"},
)

# Break the relation before the database is ready.
# Break the relation that blocked the charm.
self.harness.remove_relation(self.rel_id)
self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))

Expand All @@ -258,21 +256,23 @@ def test_on_relation_broken_extensions_keep_block(
postgresql_mock.delete_user = PropertyMock(return_value=None)
self.harness.model.unit.status = BlockedStatus("extensions requested through relation")
with self.harness.hooks_disabled():
self.rel_id = self.harness.add_relation(RELATION_NAME, "application")
first_rel_id = self.harness.add_relation(RELATION_NAME, "application1")
self.harness.update_relation_data(
self.rel_id,
"application",
{"database": DATABASE, "extensions": ["test"]},
first_rel_id,
"application1",
{"database": DATABASE, "extensions": "test"},
)
second_rel_id = self.harness.add_relation(RELATION_NAME, "application2")
self.harness.update_relation_data(
second_rel_id,
"application2",
{"database": DATABASE, "extensions": ["test"]},
{"database": DATABASE, "extensions": "test"},
)

# Break the relation before the database is ready.
self.harness.remove_relation(self.rel_id)
event = Mock()
event.relation.id = first_rel_id
# Break one of the relations that block the charm.
self.harness.charm.legacy_db_relation._on_relation_broken(event)
self.assertTrue(isinstance(self.harness.model.unit.status, BlockedStatus))

@patch(
Expand Down

0 comments on commit 3f09edc

Please sign in to comment.