Skip to content

Commit

Permalink
feat(backup): Go to blocked if invalid integration (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu1nness authored Oct 11, 2024
1 parent 719040c commit b1e75fd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/charms/mongodb/v1/mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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]

Expand Down Expand Up @@ -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."""
Expand All @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b1e75fd

Please sign in to comment.