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
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
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,
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,