Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Nov 8, 2023
1 parent c6b1b87 commit ca78563
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
6 changes: 3 additions & 3 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,19 @@ def test_update_status_pbm_and_mongodb_ready(
@patch_network_get(private_address="1.1.1.1")
@patch("charms.mongodb.v1.helpers.MongoDBConnection")
@patch("charm.MongoDBConnection")
@patch("charm.MongoDBBackups.get_pbm_status")
@patch("charm.build_unit_status")
@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm._connect_mongodb_exporter")
def test_update_status_no_s3(
self, _, get_mongodb_status, get_pbm_status, connection, status_connection
self, _, has_backup_service, get_mongodb_status, connection, status_connection
):
"""Tests when the s3 relation isn't present that the MongoDB status is reported."""
# assume leader has already initialised the replica set
self.harness.set_leader(True)
self.harness.charm.app_peer_data["db_initialised"] = "True"
connection.return_value.__enter__.return_value.is_ready = True
has_backup_service.return_value = True

get_pbm_status.return_value = BlockedStatus("pbm")
get_mongodb_status.return_value = ActiveStatus("mongodb")
self.harness.charm.on.update_status.emit()
self.assertEqual(self.harness.charm.unit.status, ActiveStatus("mongodb"))
Expand Down
36 changes: 30 additions & 6 deletions tests/unit/test_mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ def setUp(self):

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_snap_not_present(self, pbm_command, service):
def test_get_pbm_status_snap_not_present(self, pbm_command, service):
"""Tests that when the snap is not present pbm is in blocked state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

pbm_command.side_effect = ModelError("service pbm-agent not found")
self.assertTrue(isinstance(self.harness.charm.backups.get_pbm_status(), BlockedStatus))

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_resync(self, pbm_command, service):
def test_get_pbm_status_resync(self, pbm_command, service):
"""Tests that when pbm is resyncing that pbm is in waiting state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

service.return_value = True
pbm_command.return_value = (
'{"running":{"type":"resync","opID":"64f5cc22a73b330c3880e3b2"}}'
Expand All @@ -60,16 +66,22 @@ def testget_pbm_status_resync(self, pbm_command, service):

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_running(self, pbm_command, service):
def test_get_pbm_status_running(self, pbm_command, service):
"""Tests that when pbm not running an op that pbm is in active state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

service.return_value = True
pbm_command.return_value = '{"running":{}}'
self.assertTrue(isinstance(self.harness.charm.backups.get_pbm_status(), ActiveStatus))

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_incorrect_cred(self, pbm_command, service):
def test_get_pbm_status_incorrect_cred(self, pbm_command, service):
"""Tests that when pbm has incorrect credentials that pbm is in blocked state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

service.return_value = True
pbm_command.side_effect = ExecError(
command=["/usr/bin/pbm", "status"], exit_code=1, stdout="status code: 403", stderr=""
Expand All @@ -78,14 +90,23 @@ def testget_pbm_status_incorrect_cred(self, pbm_command, service):

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_incorrect_conf(self, pbm_command, service):
def test_get_pbm_status_incorrect_conf(self, pbm_command, service):
"""Tests that when pbm has incorrect configs that pbm is in blocked state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

service.return_value = True
pbm_command.side_effect = ExecError(
command=["/usr/bin/pbm", "status"], exit_code=1, stdout="status code: 404", stderr=""
)
self.assertTrue(isinstance(self.harness.charm.backups.get_pbm_status(), BlockedStatus))

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def test_get_pbm_status_no_config(self, pbm_command, service):
"""Tests when configurations for pbm are not given through S3 there is no status."""
self.assertTrue(self.harness.charm.backups.get_pbm_status() is None)

@patch("charm.snap.SnapCache")
@patch("charms.mongodb.v1.mongodb_backups.wait_fixed")
@patch("charms.mongodb.v1.mongodb_backups.stop_after_attempt")
Expand Down Expand Up @@ -643,8 +664,11 @@ def test_remap_replicaset_remap_necessary(self, run_pbm_command):

@patch("charm.MongodbOperatorCharm.has_backup_service")
@patch("charm.MongodbOperatorCharm.run_pbm_command")
def testget_pbm_status_backup(self, run_pbm_command, service):
def test_get_pbm_status_backup(self, run_pbm_command, service):
"""Tests that when pbm running a backup that pbm is in maintenance state."""
relation_id = self.harness.add_relation(RELATION_NAME, "s3-integrator")
self.harness.add_relation_unit(relation_id, "s3-integrator/0")

service.return_value = "pbm"
run_pbm_command.return_value = '{"running":{"type":"backup","name":"2023-09-04T12:15:58Z","startTS":1693829759,"status":"oplog backup","opID":"64f5ca7e777e294530289465"}}'
self.assertTrue(isinstance(self.harness.charm.backups.get_pbm_status(), MaintenanceStatus))
Expand Down

0 comments on commit ca78563

Please sign in to comment.