Fix Basic_22 robot test 00/12800/4
authorPatricia Reinoso <patricia.reinoso@canonical.com>
Wed, 4 Jan 2023 10:40:10 +0000 (10:40 +0000)
committerPatricia Reinoso <patricia.reinoso@canonical.com>
Thu, 5 Jan 2023 12:22:54 +0000 (12:22 +0000)
When adding a juju relation that already exists
a JujuAPIError exception is raised.

The "already exists" message is contained in
JujuAPIError.error_code field.

Exception is handle in an except block and
is not propagated.

Change-Id: I0ac0d875cb9580959a474e486595144473873d24
Signed-off-by: Patricia Reinoso <patricia.reinoso@canonical.com>
n2vc/libjuju.py
n2vc/tests/unit/test_libjuju.py

index 7492acc..053aaa8 100644 (file)
@@ -1267,10 +1267,10 @@ class Libjuju:
         try:
             await model.add_relation(endpoint_1, endpoint_2)
         except juju.errors.JujuAPIError as e:
-            if "not found" in e.message:
+            if self._relation_is_not_found(e):
                 self.log.warning("Relation not found: {}".format(e.message))
                 return
-            if "already exists" in e.message:
+            if self._relation_already_exist(e):
                 self.log.warning("Relation already exists: {}".format(e.message))
                 return
             # another exception, raise it
@@ -1279,6 +1279,18 @@ class Libjuju:
             await self.disconnect_model(model)
             await self.disconnect_controller(controller)
 
+    def _relation_is_not_found(self, juju_error):
+        text = "not found"
+        return (text in juju_error.message) or (
+            juju_error.error_code and text in juju_error.error_code
+        )
+
+    def _relation_already_exist(self, juju_error):
+        text = "already exists"
+        return (text in juju_error.message) or (
+            juju_error.error_code and text in juju_error.error_code
+        )
+
     async def offer(self, endpoint: RelationEndpoint) -> Offer:
         """
         Create an offer from a RelationEndpoint
index e7f43ab..1bbe556 100644 (file)
@@ -1007,6 +1007,38 @@ class AddRelationTest(LibjujuTestCase):
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
+    @asynctest.mock.patch("logging.Logger.warning")
+    def test_not_found_in_error_code(
+        self,
+        mock_warning,
+        mock_add_relation,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        result = {
+            "error": "relation cannot be added",
+            "error-code": "not found",
+            "response": "response",
+            "request-id": 1,
+        }
+
+        mock_get_model.return_value = juju.model.Model()
+        mock_add_relation.side_effect = JujuAPIError(result)
+
+        self.loop.run_until_complete(
+            self.libjuju.add_relation(
+                "model",
+                "app1:relation1",
+                "app2:relation2",
+            )
+        )
+
+        mock_warning.assert_called_with("Relation not found: relation cannot be added")
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
+
     @asynctest.mock.patch("logging.Logger.warning")
     def test_already_exists(
         self,
@@ -1035,6 +1067,40 @@ class AddRelationTest(LibjujuTestCase):
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()
 
+    @asynctest.mock.patch("logging.Logger.warning")
+    def test_already_exists_error_code(
+        self,
+        mock_warning,
+        mock_add_relation,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        result = {
+            "error": "relation cannot be added",
+            "error-code": "already exists",
+            "response": "response",
+            "request-id": 1,
+        }
+
+        mock_get_model.return_value = juju.model.Model()
+        mock_add_relation.side_effect = JujuAPIError(result)
+
+        self.loop.run_until_complete(
+            self.libjuju.add_relation(
+                "model",
+                "app1:relation1",
+                "app2:relation2",
+            )
+        )
+
+        mock_warning.assert_called_with(
+            "Relation already exists: relation cannot be added"
+        )
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
+
     def test_exception(
         self,
         mock_add_relation,