Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Nov 8, 2023
1 parent c0e31ac commit c84fc77
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/unit/test_mongodb_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ def setUp(self):
@patch("charm.MongodbOperatorCharm.run_pbm_command")
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 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 @@ -62,6 +68,9 @@ def test_get_pbm_status_resync(self, pbm_command, service):
@patch("charm.MongodbOperatorCharm.run_pbm_command")
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))
Expand All @@ -70,6 +79,9 @@ def test_get_pbm_status_running(self, pbm_command, service):
@patch("charm.MongodbOperatorCharm.run_pbm_command")
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 @@ -80,12 +92,21 @@ def test_get_pbm_status_incorrect_cred(self, pbm_command, service):
@patch("charm.MongodbOperatorCharm.run_pbm_command")
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.v0.mongodb_backups.wait_fixed")
@patch("charms.mongodb.v0.mongodb_backups.stop_after_attempt")
Expand Down Expand Up @@ -645,6 +666,9 @@ def test_remap_replicaset_remap_necessary(self, run_pbm_command):
@patch("charm.MongodbOperatorCharm.run_pbm_command")
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(
Expand Down

0 comments on commit c84fc77

Please sign in to comment.