Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ratushnyy committed Nov 28, 2023
1 parent 52f5fec commit 5d118b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
ServiceInfo,
)
from pymongo.errors import PyMongoError
from tenacity import Retrying, before_log, retry, stop_after_attempt, wait_fixed
from tenacity import before_log, retry, stop_after_attempt, wait_fixed

from config import Config
from exceptions import AdminUserCreationError, MissingSecretError
Expand Down Expand Up @@ -604,6 +604,20 @@ def _on_secret_changed(self, event):
# END: actions

# BEGIN: user management

@retry(
stop=_user_creation_stop_condition,
wait=wait_fixed(USER_CREATION_COOLDOWN),
reraise=True,
before_sleep=_before_sleep_user_creation,
)
def _initialize_users(self, event: StartEvent) -> None:
logger.info("User initialization")
self._init_operator_user()
self._init_monitor_user()
self._init_backup_user()
self.client_relations.oversee_users(None, event)

@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(5),
Expand Down Expand Up @@ -775,22 +789,7 @@ def _initialise_replica_set(self, event: StartEvent) -> None:
# Check replica set status before creating users
while not direct_mongo.client.admin.command("hello")["isWritablePrimary"]:
time.sleep(REPLICA_SET_INIT_CHECK_TIMEOUT)
logger.info("User initialization")

for attempt in Retrying(
stop=_user_creation_stop_condition,
wait=wait_fixed(USER_CREATION_COOLDOWN),
reraise=True,
before_sleep=_before_sleep_user_creation,
):
with attempt:
logger.error(
"Initializing users. Attempt #%s", attempt.retry_state.attempt_number
)
self._init_operator_user()
self._init_monitor_user()
self._init_backup_user()
self.client_relations.oversee_users(None, event)
self._initialize_users(event)
except ExecError as e:
logger.error(
"Deferring on_start: exit code: %i, stderr: %s", e.exit_code, e.stderr
Expand All @@ -802,7 +801,7 @@ def _initialise_replica_set(self, event: StartEvent) -> None:
event.defer()
return

self.db_initialised = True
self.alised = True

def _add_units_from_replica_set(
self, event, mongo: MongoDBConnection, units_to_add: Set[str]
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def test_start_mongod_error_initalising_replica_set(
@patch("charm.MongoDBProvider")
@patch("charm.MongoDBCharm._init_operator_user")
@patch("charm.MongoDBConnection")
@patch("tenacity.nap.time.sleep", MagicMock())
def test_start_mongod_error_initalising_user(self, connection, init_user, provider, defer):
"""Tests that failure to initialise users set is properly handled.
Expand Down Expand Up @@ -400,6 +401,10 @@ def test_start_mongod_error_initalising_user(self, connection, init_user, provid
@patch("charm.MongoDBProvider")
@patch("charm.MongoDBCharm._init_operator_user")
@patch("charm.MongoDBConnection")
@patch("tenacity.nap.time.sleep", MagicMock())
@patch("charm.USER_CREATING_MAX_ATTEMPTS", 1)
@patch("charm.USER_CREATION_COOLDOWN", 1)
@patch("charm.REPLICA_SET_INIT_CHECK_TIMEOUT", 1)
def test_start_mongod_error_overseeing_users(self, connection, init_user, provider, defer):
"""Tests failures related to pymongo are properly handled when overseeing users.
Expand Down Expand Up @@ -593,6 +598,10 @@ def test_reconfigure_add_member_failure(self, connection, defer):
@patch("ops.framework.EventBase.defer")
@patch("charm.MongoDBProvider.oversee_users")
@patch("charm.MongoDBConnection")
@patch("tenacity.nap.time.sleep", MagicMock())
@patch("charm.USER_CREATING_MAX_ATTEMPTS", 1)
@patch("charm.USER_CREATION_COOLDOWN", 1)
@patch("charm.REPLICA_SET_INIT_CHECK_TIMEOUT", 1)
def test_start_init_operator_user_after_second_call(self, connection, oversee_users, defer):
"""Tests that the creation of the admin user is only performed once.
Expand Down Expand Up @@ -909,6 +918,10 @@ def test__connect_mongodb_exporter_success(
@patch("ops.framework.EventBase.defer")
@patch("charm.MongoDBCharm._set_data_dir_permissions")
@patch("charm.MongoDBConnection")
@patch("tenacity.nap.time.sleep", MagicMock())
@patch("charm.USER_CREATING_MAX_ATTEMPTS", 1)
@patch("charm.USER_CREATION_COOLDOWN", 1)
@patch("charm.REPLICA_SET_INIT_CHECK_TIMEOUT", 1)
def test__backup_user_created(
self,
connection,
Expand Down

0 comments on commit 5d118b3

Please sign in to comment.