From c84fc77f12109d575ef543bd0029bc6f28f33be4 Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Wed, 8 Nov 2023 13:27:08 +0000 Subject: [PATCH] update unit tests --- tests/unit/test_mongodb_backups.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/unit/test_mongodb_backups.py b/tests/unit/test_mongodb_backups.py index 39f277bc7..9603e35fc 100644 --- a/tests/unit/test_mongodb_backups.py +++ b/tests/unit/test_mongodb_backups.py @@ -45,6 +45,9 @@ 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)) @@ -52,6 +55,9 @@ def test_get_pbm_status_snap_not_present(self, pbm_command, 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"}}' @@ -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)) @@ -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="" @@ -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") @@ -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(