Skip to content

Commit 1e09c4e

Browse files
committed
update unit testss
1 parent f726e56 commit 1e09c4e

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

tests/unit/test_charm.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ def test_mongod_pebble_ready(self, connect_exporter, fix_data_dir, defer, pull_l
120120
# Ensure that _connect_mongodb_exporter was called
121121
connect_exporter.assert_called_once()
122122

123+
@patch("charm.MongoDBCharm.get_current_termination_period")
124+
@patch("charm.MongoDBCharm.update_termination_grace_period")
123125
@patch("ops.framework.EventBase.defer")
124126
@patch("charm.MongoDBCharm._push_keyfile_to_workload")
125-
def test_pebble_ready_cannot_retrieve_container(self, push_keyfile_to_workload, defer):
127+
def test_pebble_ready_cannot_retrieve_container(
128+
self, push_keyfile_to_workload, defer, *unused
129+
):
126130
"""Test verifies behavior when retrieving container results in ModelError in pebble ready.
127131
128132
Verifies that when a failure to get a container occurs, that that failure is raised and
@@ -142,9 +146,11 @@ def test_pebble_ready_cannot_retrieve_container(self, push_keyfile_to_workload,
142146
mock_container.replan.assert_not_called()
143147
defer.assert_not_called()
144148

149+
@patch("charm.MongoDBCharm.get_current_termination_period")
150+
@patch("charm.MongoDBCharm.update_termination_grace_period")
145151
@patch("ops.framework.EventBase.defer")
146152
@patch("charm.MongoDBCharm._push_keyfile_to_workload")
147-
def test_pebble_ready_container_cannot_connect(self, push_keyfile_to_workload, defer):
153+
def test_pebble_ready_container_cannot_connect(self, push_keyfile_to_workload, defer, *unused):
148154
"""Test verifies behavior when cannot connect to container in pebble ready function.
149155
150156
Verifies that when a failure to connect to container results in a deferral and that no
@@ -203,11 +209,15 @@ def test_pebble_ready_no_storage_yet(self, defer):
203209
mock_container.replan.assert_not_called()
204210
defer.assert_called()
205211

212+
@patch("charm.MongoDBCharm.get_current_termination_period")
213+
@patch("charm.MongoDBCharm.update_termination_grace_period")
206214
@patch("ops.framework.EventBase.defer")
207215
@patch("charm.MongoDBProvider")
208216
@patch("charm.MongoDBCharm._init_operator_user")
209217
@patch("charm.MongoDBConnection")
210-
def test_start_cannot_retrieve_container(self, connection, init_user, provider, defer):
218+
def test_start_cannot_retrieve_container(
219+
self, connection, init_user, provider, defer, *unused
220+
):
211221
"""Verifies that failures to get container result in a ModelError being raised.
212222
213223
Further this function verifies that on error no attempts to set up the replica set or
@@ -230,11 +240,13 @@ def test_start_cannot_retrieve_container(self, connection, init_user, provider,
230240
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
231241
defer.assert_not_called()
232242

243+
@patch("charm.MongoDBCharm.get_current_termination_period")
244+
@patch("charm.MongoDBCharm.update_termination_grace_period")
233245
@patch("ops.framework.EventBase.defer")
234246
@patch("charm.MongoDBProvider")
235247
@patch("charm.MongoDBCharm._init_operator_user")
236248
@patch("charm.MongoDBConnection")
237-
def test_start_container_cannot_connect(self, connection, init_user, provider, defer):
249+
def test_start_container_cannot_connect(self, connection, init_user, provider, defer, *unused):
238250
"""Tests inability to connect results in deferral.
239251
240252
Verifies that if connection is not possible, that there are no attempts to set up the
@@ -257,11 +269,13 @@ def test_start_container_cannot_connect(self, connection, init_user, provider, d
257269
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
258270
defer.assert_called()
259271

272+
@patch("charm.MongoDBCharm.get_current_termination_period")
273+
@patch("charm.MongoDBCharm.update_termination_grace_period")
260274
@patch("ops.framework.EventBase.defer")
261275
@patch("charm.MongoDBProvider")
262276
@patch("charm.MongoDBCharm._init_operator_user")
263277
@patch("charm.MongoDBConnection")
264-
def test_start_container_does_not_exist(self, connection, init_user, provider, defer):
278+
def test_start_container_does_not_exist(self, connection, init_user, provider, defer, *unused):
265279
"""Tests lack of existence of files on container results in deferral.
266280
267281
Verifies that if files do not exists, that there are no attempts to set up the replica set
@@ -285,11 +299,13 @@ def test_start_container_does_not_exist(self, connection, init_user, provider, d
285299
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
286300
defer.assert_called()
287301

302+
@patch("charm.MongoDBCharm.get_current_termination_period")
303+
@patch("charm.MongoDBCharm.update_termination_grace_period")
288304
@patch("ops.framework.EventBase.defer")
289305
@patch("charm.MongoDBProvider")
290306
@patch("charm.MongoDBCharm._init_operator_user")
291307
@patch("charm.MongoDBConnection")
292-
def test_start_container_exists_fails(self, connection, init_user, provider, defer):
308+
def test_start_container_exists_fails(self, connection, init_user, provider, defer, *unused):
293309
"""Tests failure in checking file existence on container raises an APIError.
294310
295311
Verifies that when checking container files raises an API Error, we raise that same error
@@ -314,11 +330,13 @@ def test_start_container_exists_fails(self, connection, init_user, provider, def
314330
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
315331
defer.assert_not_called()
316332

333+
@patch("charm.MongoDBCharm.get_current_termination_period")
334+
@patch("charm.MongoDBCharm.update_termination_grace_period")
317335
@patch("ops.framework.EventBase.defer")
318336
@patch("charm.MongoDBProvider")
319337
@patch("charm.MongoDBCharm._init_operator_user")
320338
@patch("charm.MongoDBConnection")
321-
def test_start_already_initialised(self, connection, init_user, provider, defer):
339+
def test_start_already_initialised(self, connection, init_user, provider, defer, *unused):
322340
"""Tests that if the replica set has already been set up that we return.
323341
324342
Verifies that if the replica set is already set up that no attempts to set it up again are
@@ -343,11 +361,13 @@ def test_start_already_initialised(self, connection, init_user, provider, defer)
343361
provider.return_value.oversee_users.assert_not_called()
344362
defer.assert_not_called()
345363

364+
@patch("charm.MongoDBCharm.get_current_termination_period")
365+
@patch("charm.MongoDBCharm.update_termination_grace_period")
346366
@patch("ops.framework.EventBase.defer")
347367
@patch("charm.MongoDBProvider")
348368
@patch("charm.MongoDBCharm._init_operator_user")
349369
@patch("charm.MongoDBConnection")
350-
def test_start_mongod_not_ready(self, connection, init_user, provider, defer):
370+
def test_start_mongod_not_ready(self, connection, init_user, provider, defer, *unused):
351371
"""Tests that if mongod is not ready that we defer and return.
352372
353373
Verifies that if mongod is not ready that no attempts to set up the replica set and set up
@@ -374,12 +394,14 @@ def test_start_mongod_not_ready(self, connection, init_user, provider, defer):
374394
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
375395
defer.assert_called()
376396

397+
@patch("charm.MongoDBCharm.get_current_termination_period")
398+
@patch("charm.MongoDBCharm.update_termination_grace_period")
377399
@patch("ops.framework.EventBase.defer")
378400
@patch("charm.MongoDBProvider")
379401
@patch("charm.MongoDBCharm._initialise_users")
380402
@patch("charm.MongoDBConnection")
381403
def test_start_mongod_error_initalising_replica_set(
382-
self, connection, init_users, provider, defer
404+
self, connection, init_users, provider, defer, *unused
383405
):
384406
"""Tests that failure to initialise replica set is properly handled.
385407
@@ -403,12 +425,14 @@ def test_start_mongod_error_initalising_replica_set(
403425
self.assertEqual("replica_set_initialised" in self.harness.charm.app_peer_data, False)
404426
defer.assert_called()
405427

428+
@patch("charm.MongoDBCharm.get_current_termination_period")
429+
@patch("charm.MongoDBCharm.update_termination_grace_period")
406430
@patch("ops.framework.EventBase.defer")
407431
@patch("charm.MongoDBProvider")
408432
@patch("charm.MongoDBCharm._init_operator_user")
409433
@patch("charm.MongoDBConnection")
410434
@patch("tenacity.nap.time.sleep", MagicMock())
411-
def test_error_initalising_users(self, connection, init_user, provider, defer):
435+
def test_error_initalising_users(self, connection, init_user, provider, defer, *unused):
412436
"""Tests that failure to initialise users set is properly handled.
413437
414438
Verifies that when there is a failure to initialise users that overseeing users is not
@@ -433,6 +457,8 @@ def test_error_initalising_users(self, connection, init_user, provider, defer):
433457
# verify app data
434458
self.assertEqual("db_initialised" in self.harness.charm.app_peer_data, False)
435459

460+
@patch("charm.MongoDBCharm.get_current_termination_period")
461+
@patch("charm.MongoDBCharm.update_termination_grace_period")
436462
@patch("ops.framework.EventBase.defer")
437463
@patch("charm.MongoDBProvider")
438464
@patch("charm.MongoDBCharm._init_operator_user")
@@ -444,7 +470,7 @@ def test_error_initalising_users(self, connection, init_user, provider, defer):
444470
@patch("charm.wait_fixed")
445471
@patch("charm.stop_after_attempt")
446472
def test_start_mongod_error_overseeing_users(
447-
self, retry_stop, retry_wait, connection, init_user, provider, defer
473+
self, retry_stop, retry_wait, connection, init_user, provider, defer, *unused
448474
):
449475
"""Tests failures related to pymongo are properly handled when overseeing users.
450476
@@ -640,10 +666,14 @@ def test_reconfigure_add_member_failure(self, connection, defer):
640666
connection.return_value.__enter__.return_value.add_replset_member.assert_called()
641667
defer.assert_called()
642668

669+
@patch("charm.MongoDBCharm.get_current_termination_period")
670+
@patch("charm.MongoDBCharm.update_termination_grace_period")
643671
@patch("ops.framework.EventBase.defer")
644672
@patch("charm.MongoDBProvider.oversee_users")
645673
@patch("charm.MongoDBConnection")
646-
def test_start_init_operator_user_after_second_call(self, connection, oversee_users, defer):
674+
def test_start_init_operator_user_after_second_call(
675+
self, connection, oversee_users, defer, *unused
676+
):
647677
"""Tests that the creation of the admin user is only performed once.
648678
649679
Verifies that if the user is already set up, that no attempts to set it up again are
@@ -999,6 +1029,8 @@ def test__connect_mongodb_exporter_success(
9991029
expected_uri = uri_template.format(password="mongo123")
10001030
self.assertEqual(expected_uri, new_uri)
10011031

1032+
@patch("charm.MongoDBCharm.get_current_termination_period")
1033+
@patch("charm.MongoDBCharm.update_termination_grace_period")
10021034
@patch("tenacity.nap.time.sleep", MagicMock())
10031035
@patch("charm.USER_CREATING_MAX_ATTEMPTS", 1)
10041036
@patch("charm.USER_CREATION_COOLDOWN", 1)
@@ -1021,6 +1053,7 @@ def test__backup_user_created(
10211053
_connect_mongodb_exporter,
10221054
_init_operator_user,
10231055
_init_monitor_user,
1056+
*unused,
10241057
):
10251058
"""Tests what backup user was created."""
10261059
self.harness.charm._initialise_users.retry.wait = wait_none()
@@ -1032,8 +1065,10 @@ def test__backup_user_created(
10321065
self.harness.charm._initialise_users.retry.wait = wait_none()
10331066
self.assertIsNotNone(password) # verify the password is set
10341067

1068+
@patch("charm.MongoDBCharm.get_current_termination_period")
1069+
@patch("charm.MongoDBCharm.update_termination_grace_period")
10351070
@patch("charm.MongoDBConnection")
1036-
def test_set_password_provided(self, connection):
1071+
def test_set_password_provided(self, connection, *unused):
10371072
"""Tests that a given password is set as the new mongodb password for backup user."""
10381073
container = self.harness.model.unit.get_container("mongod")
10391074
self.harness.set_leader(True)

0 commit comments

Comments
 (0)