Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Sep 9, 2024
1 parent 61811e4 commit 2b80dc2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def _on_config_changed(self, event: ConfigChangedEvent) -> None:
return

logger.error(
f"cluster migration currently not supported, cannot change from { self.model.config['role']} to {self.role}"
f"cluster migration currently not supported, cannot change from {self.model.config['role']} to {self.role}"
)
raise ShardingMigrationError(
f"Migration of sharding components not permitted, revert config role to {self.role}"
Expand Down Expand Up @@ -655,6 +655,7 @@ def _on_start(self, event) -> None:
self._initialise_users(event)
self.db_initialised = True
except RetryError:
print("YOOOOOO")
logger.error("Failed to initialise users. Deferring start event.")
event.defer()
return
Expand Down
20 changes: 10 additions & 10 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
import json
import logging
import re
import unittest
Expand Down Expand Up @@ -329,9 +330,8 @@ def test_start_already_initialised(self, connection, init_user, provider, defer)
mock_container.return_value.can_connect.return_value = True
mock_container.return_value.exists.return_value = True
self.harness.charm.unit.get_container = mock_container

self.harness.charm.app_peer_data["replica_set_initialised"] = "True"
self.harness.charm.app_peer_data["users_initialized"] = "True"
self.harness.charm.app_peer_data["replica_set_initialised"] = json.dumps(True)
self.harness.charm.app_peer_data["users_initialized"] = json.dumps(True)

self.harness.charm.on.start.emit()

Expand Down Expand Up @@ -524,7 +524,7 @@ def test_reconfigure_get_members_failure(self, client, connection, defer):
"""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
rel = self.harness.charm.model.get_relation("database-peers")

for exception, _ in PYMONGO_EXCEPTIONS:
Expand Down Expand Up @@ -559,7 +559,7 @@ def test_reconfigure_remove_member_failure(self, connection, defer):
"""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
connection.return_value.__enter__.return_value.get_replset_members.return_value = {
"mongodb-k8s-0.mongodb-k8s-endpoints",
"mongodb-k8s-1.mongodb-k8s-endpoints",
Expand Down Expand Up @@ -593,7 +593,7 @@ def test_reconfigure_peer_not_ready(self, connection, defer):
"""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
connection.return_value.__enter__.return_value.get_replset_members.return_value = {
"mongodb-k8s-0.mongodb-k8s-endpoints"
}
Expand All @@ -617,7 +617,7 @@ def test_reconfigure_add_member_failure(self, connection, defer):
"""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
connection.return_value.__enter__.return_value.get_replset_members.return_value = {
"mongodb-k8s-0.mongodb-k8s-endpoints"
}
Expand Down Expand Up @@ -662,7 +662,7 @@ def test_start_init_operator_user_after_second_call(self, connection, oversee_us

oversee_users.side_effect = PyMongoError()

self.harness.charm.app_peer_data["replica_set_initialised"] = "True"
self.harness.charm.app_peer_data["replica_set_initialised"] = json.dumps(True)
self.harness.charm.on.start.emit()
self.assertEqual("operator-user-created" in self.harness.charm.app_peer_data, True)
defer.assert_called()
Expand Down Expand Up @@ -957,7 +957,7 @@ def test__connect_mongodb_exporter_success(
container.make_dir("/etc/logrotate.d", make_parents=True)

self.harness.set_can_connect(container, True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
self.harness.charm.on.mongod_pebble_ready.emit(container)

password = self.harness.charm.get_secret("app", "monitor-password")
Expand Down Expand Up @@ -1022,7 +1022,7 @@ def test__backup_user_created(
"""Tests what backup user was created."""
self.harness.charm._initialise_users.retry.wait = wait_none()
container = self.harness.model.unit.get_container("mongod")
self.harness.charm.app_peer_data["replica_set_initialised"] = "True"
self.harness.charm.app_peer_data["replica_set_initialised"] = json.dumps(True)
self.harness.set_can_connect(container, True)
self.harness.charm.on.start.emit()
password = self.harness.charm.get_secret("app", "backup-password")
Expand Down
11 changes: 6 additions & 5 deletions tests/unit/test_mongodb_backups.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
import json
import unittest
from unittest import mock
from unittest.mock import patch
Expand Down Expand Up @@ -277,7 +278,7 @@ def test_s3_credentials_set_pbm_failure(self, _set_config_options, service):
service.return_value = "pbm"

_set_config_options.side_effect = SetPBMConfigError
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)

# triggering s3 event with correct fields
mock_s3_info = mock.Mock()
Expand Down Expand Up @@ -305,7 +306,7 @@ def test_s3_credentials_config_error(
"""Test charm defers when more time is needed to sync pbm."""
container = self.harness.model.unit.get_container("mongod")
self.harness.set_can_connect(container, True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
service.return_value = "pbm"
pbm_status.return_value = ActiveStatus()
resync.side_effect = SetPBMConfigError
Expand Down Expand Up @@ -333,7 +334,7 @@ def test_s3_credentials_syncing(self, pbm_status, service, defer, resync, _set_c
"""Test charm defers when more time is needed to sync pbm credentials."""
container = self.harness.model.unit.get_container("mongod")
self.harness.set_can_connect(container, True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
service.return_value = "pbm"
resync.side_effect = ResyncError

Expand Down Expand Up @@ -364,7 +365,7 @@ def test_s3_credentials_pbm_busy(
"""Test charm defers when more time is needed to sync pbm."""
container = self.harness.model.unit.get_container("mongod")
self.harness.set_can_connect(container, True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
service.return_value = "pbm"

resync.side_effect = PBMBusyError
Expand Down Expand Up @@ -397,7 +398,7 @@ def test_s3_credentials_pbm_error(
container = self.harness.model.unit.get_container("mongod")
self.harness.set_can_connect(container, True)
service.return_value = "pbm"
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
resync.side_effect = ExecError(
command=["/usr/bin/pbm status"], exit_code=1, stdout="status code: 403", stderr=""
)
Expand Down
32 changes: 18 additions & 14 deletions tests/unit/test_mongodb_provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.

import json
import unittest
from unittest import mock
from unittest.mock import patch
Expand Down Expand Up @@ -67,7 +68,7 @@ def test_relation_event_oversee_users_mongo_failure(self, oversee_users, defer):
"""Tests the errors related to pymongo when overseeing users result in a defer."""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
relation_id = self.harness.add_relation("database", "consumer")

for exception, expected_raise in PYMONGO_EXCEPTIONS:
Expand All @@ -91,7 +92,7 @@ def test_relation_event_oversee_users_fails_to_get_relation(self, oversee_users,
"""Verifies that when users are formatted incorrectly an assertion error is raised."""
# presets
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
self.harness.charm.app_peer_data["db_initialised"] = json.dumps(True)
relation_id = self.harness.add_relation("database", "consumer")

# AssertionError is raised when unable to attain users from relation (due to name
Expand All @@ -107,7 +108,7 @@ def test_relation_event_oversee_users_fails_to_get_relation(self, oversee_users,
self.harness.remove_relation_unit(relation_id, "consumer/0")

@patch_network_get(private_address="1.1.1.1")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_get_users_failure(self, connection):
"""Verifies that when unable to retrieve users from mongod an exception is raised."""
for dep_id in DEPARTED_IDS:
Expand All @@ -120,7 +121,7 @@ def test_oversee_users_get_users_failure(self, connection):

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_drop_user_failure(self, connection, relation_users):
"""Verifies that when unable to drop users from mongod an exception is raised."""
# presets, such that there is a need to drop users.
Expand All @@ -129,7 +130,9 @@ def test_oversee_users_drop_user_failure(self, connection, relation_users):
"relation-user1",
"relation-user2",
}

self.harness.charm.app_peer_data["managed-users-key"] = json.dumps(
["relation-user1", "relation-user2"]
)
for dep_id in DEPARTED_IDS:
for exception, expected_raise in PYMONGO_EXCEPTIONS:
connection.return_value.__enter__.return_value.drop_user.side_effect = exception
Expand All @@ -140,7 +143,7 @@ def test_oversee_users_drop_user_failure(self, connection, relation_users):

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_get_config_failure(self, connection, relation_users):
"""Verifies that when users do not match necessary schema an AssertionError is raised."""
# presets, such that the need to create user relations is triggered. Further presets
Expand All @@ -159,7 +162,7 @@ def test_oversee_users_get_config_failure(self, connection, relation_users):
@patch("charm.MongoDBProvider._set_relation")
@patch("charm.MongoDBProvider._get_config")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
@patch("charm.MongoDBProvider._diff")
def test_oversee_users_no_config_database(
self, diff, connection, relation_users, get_config, set_relation
Expand All @@ -182,7 +185,7 @@ def test_oversee_users_no_config_database(
@patch("charm.MongoDBProvider._set_relation")
@patch("charm.MongoDBProvider._get_config")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_create_user_failure(
self, connection, relation_users, get_config, set_relation
):
Expand All @@ -203,7 +206,7 @@ def test_oversee_users_create_user_failure(
@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_config")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_set_relation_failure(self, connection, relation_users, get_config):
"""Verifies that when adding a user with an invalid name that an exception is raised."""
# presets, such that the need to create user relations is triggered and user naming such
Expand All @@ -222,7 +225,7 @@ def test_oversee_users_set_relation_failure(self, connection, relation_users, ge

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_update_get_config_failure(self, connection, relation_users):
"""Verifies that when updating a user with an invalid name that an exception is raised."""
# presets, such that the need to update user relations is triggered and user naming such
Expand All @@ -240,12 +243,13 @@ def test_oversee_users_update_get_config_failure(self, connection, relation_user
@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_config")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_update_user_failure(self, connection, relation_users, get_config):
"""Verifies that when updating users fails an exception is raised."""
# presets, such that the need to update user relations is triggered
relation_users.return_value = {"relation-user1"}
connection.return_value.__enter__.return_value.get_users.return_value = {"relation-user1"}
self.harness.charm.app_peer_data["managed-users-key"] = json.dumps(["relation-user1"])

for dep_id in DEPARTED_IDS:
for exception, expected_raise in PYMONGO_EXCEPTIONS:
Expand All @@ -259,7 +263,7 @@ def test_oversee_users_update_user_failure(self, connection, relation_users, get
@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_databases_from_relations")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_no_auto_delete(
self, connection, relation_users, databases_from_relations
):
Expand All @@ -276,7 +280,7 @@ def test_oversee_users_no_auto_delete(

@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_mongo_databases_failure(self, connection, relation_users):
"""Verifies failures in checking for databases with mongod result in raised exceptions."""
self.harness.update_config({"auto-delete": True})
Expand All @@ -294,7 +298,7 @@ def test_oversee_users_mongo_databases_failure(self, connection, relation_users)
@patch_network_get(private_address="1.1.1.1")
@patch("charm.MongoDBProvider._get_databases_from_relations")
@patch("charm.MongoDBProvider._get_users_from_relations")
@patch("charms.mongodb.v1.mongodb_provider.MongoDBConnection")
@patch("charms.mongodb.v1.mongodb_provider.MongoConnection")
def test_oversee_users_drop_database_failure(
self, connection, relation_users, databases_from_relations
):
Expand Down

0 comments on commit 2b80dc2

Please sign in to comment.