From b1e75fd248ce09d9a11766c9b893aaeb4c7589fa Mon Sep 17 00:00:00 2001 From: Neha Oudin <17551419+Gu1nness@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:07:43 +0200 Subject: [PATCH] feat(backup): Go to blocked if invalid integration (#508) --- lib/charms/mongodb/v1/mongodb_backups.py | 18 ++++++++++------- tests/unit/test_mongodb_backups.py | 25 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/charms/mongodb/v1/mongodb_backups.py b/lib/charms/mongodb/v1/mongodb_backups.py index 4b9c3e618..c15e5bafe 100644 --- a/lib/charms/mongodb/v1/mongodb_backups.py +++ b/lib/charms/mongodb/v1/mongodb_backups.py @@ -41,7 +41,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 4 +LIBPATCH = 5 logger = logging.getLogger(__name__) @@ -61,6 +61,10 @@ BACKUP_RESTORE_MAX_ATTEMPTS = 10 BACKUP_RESTORE_ATTEMPT_COOLDOWN = 15 +INVALID_INTEGRATION_STATUS = BlockedStatus( + "Relation to s3-integrator is not supported, config role must be config-server" +) + _StrOrBytes = Union[str, bytes] @@ -136,11 +140,7 @@ def on_s3_relation_joined(self, event: RelationJoinedEvent) -> None: logger.debug( "Shard does not support s3 relations, please relate s3-integrator to config-server only." ) - self.charm.status.set_and_share_status( - BlockedStatus( - "Relation to s3-integrator is not supported, config role must be config-server" - ) - ) + self.charm.status.set_and_share_status(INVALID_INTEGRATION_STATUS) def _on_s3_credential_changed(self, event: CredentialsChangedEvent): """Sets pbm credentials, resyncs if necessary and reports config errors.""" @@ -154,7 +154,11 @@ def _on_s3_credential_changed(self, event: CredentialsChangedEvent): event.defer() return - if not self._pass_sanity_checks(event, action): + if not self.is_valid_s3_integration(): + logger.debug( + "Shard does not support s3 relations, please relate s3-integrator to config-server only." + ) + self.charm.status.set_and_share_status(INVALID_INTEGRATION_STATUS) return if not self.charm.db_initialised: diff --git a/tests/unit/test_mongodb_backups.py b/tests/unit/test_mongodb_backups.py index c3da752df..e46f02225 100644 --- a/tests/unit/test_mongodb_backups.py +++ b/tests/unit/test_mongodb_backups.py @@ -8,6 +8,7 @@ import tenacity from charms.mongodb.v1.helpers import current_pbm_op from charms.mongodb.v1.mongodb_backups import ( + INVALID_INTEGRATION_STATUS, PBMBusyError, ResyncError, SetPBMConfigError, @@ -42,6 +43,30 @@ def setUp(self, *unused): self.charm = self.harness.charm self.addCleanup(self.harness.cleanup) + def test_relation_joined_to_blocked_if_shard( + self, + ): + def is_shard_mock_call(role_name: str): + return role_name == "shard" + + self.harness.charm.is_role = is_shard_mock_call + relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator") + self.harness.add_relation_unit(relation_id, "s3-integrator/0") + relation = self.harness.charm.model.get_relation(RELATION_NAME) + self.harness.charm.on[RELATION_NAME].relation_joined.emit(relation=relation) + assert self.harness.charm.unit.status == INVALID_INTEGRATION_STATUS + + def test_credentials_changed_to_blocked_if_shard(self): + def is_shard_mock_call(role_name: str): + return role_name == "shard" + + self.harness.charm.is_role = is_shard_mock_call + relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator") + self.harness.add_relation_unit(relation_id, "s3-integrator/0") + relation = self.harness.charm.model.get_relation(RELATION_NAME) + self.harness.charm.backups.s3_client.on.credentials_changed.emit(relation=relation) + assert self.harness.charm.unit.status == INVALID_INTEGRATION_STATUS + @patch("charm.MongodbOperatorCharm.has_backup_service") @patch("charm.MongodbOperatorCharm.run_pbm_command") def test_get_pbm_status_snap_not_present(self, pbm_command, service):