From: David Garcia Date: Fri, 11 Sep 2020 13:05:00 +0000 (+0200) Subject: Add consume to libjuju.py X-Git-Tag: release-v9.0-start~23 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F00%2F9700%2F1;p=osm%2FN2VC.git Add consume to libjuju.py This function allows to consume an offered endpoint in another model Change-Id: I320978aac654fbb8199fdfbf4e928b0e2ba5458b Signed-off-by: David Garcia --- diff --git a/n2vc/libjuju.py b/n2vc/libjuju.py index ceb5e02..c62b11c 100644 --- a/n2vc/libjuju.py +++ b/n2vc/libjuju.py @@ -740,6 +740,28 @@ class Libjuju: await self.disconnect_model(model) await self.disconnect_controller(controller) + async def consume( + self, offer_url: str, model_name: str, + ): + """ + Adds a remote offer to the model. Relations can be created later using "juju relate". + + :param: offer_url: Offer Url + :param: model_name: Model name + + :raises ParseError if there's a problem parsing the offer_url + :raises JujuError if remote offer includes and endpoint + :raises JujuAPIError if the operation is not successful + """ + controller = await self.get_controller() + model = await controller.get_model(model_name) + + try: + await model.consume(offer_url) + finally: + await self.disconnect_model(model) + await self.disconnect_controller(controller) + async def destroy_model(self, model_name: str, total_timeout: float): """ Destroy model diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index a789bc8..41b32d1 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -954,3 +954,96 @@ class ListOffers(LibjujuTestCase): offers = self.loop.run_until_complete(self.libjuju.list_offers("model")) self.assertEqual(offers, ["offer"]) mock_disconnect_controller.assert_called_once() + + +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller") +@asynctest.mock.patch("juju.controller.Controller.get_model") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_model") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller") +@asynctest.mock.patch("juju.model.Model.consume") +class ConsumeTest(LibjujuTestCase): + def setUp(self): + super(ConsumeTest, self).setUp() + + def test_consume( + self, + mock_consume, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_get_model.return_value = juju.model.Model() + + self.loop.run_until_complete( + self.libjuju.consume("offer_url", "model_name") + ) + mock_consume.assert_called_once() + mock_disconnect_model.assert_called_once() + mock_disconnect_controller.assert_called_once() + + def test_parsing_error_exception( + self, + mock_consume, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_get_model.return_value = juju.model.Model() + mock_consume.side_effect = juju.offerendpoints.ParseError("") + + with self.assertRaises(juju.offerendpoints.ParseError): + self.loop.run_until_complete( + self.libjuju.consume("offer_url", "model_name") + ) + mock_consume.assert_called_once() + mock_disconnect_model.assert_called_once() + mock_disconnect_controller.assert_called_once() + + def test_juju_error_exception( + self, + mock_consume, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_get_model.return_value = juju.model.Model() + mock_consume.side_effect = juju.errors.JujuError("") + + with self.assertRaises(juju.errors.JujuError): + self.loop.run_until_complete( + self.libjuju.consume("offer_url", "model_name") + ) + mock_consume.assert_called_once() + mock_disconnect_model.assert_called_once() + mock_disconnect_controller.assert_called_once() + + def test_juju_api_error_exception( + self, + mock_consume, + mock_disconnect_controller, + mock_disconnect_model, + mock_get_model, + mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_get_model.return_value = juju.model.Model() + mock_consume.side_effect = juju.errors.JujuAPIError({ + "error": "", + "response": "", + "request-id": "", + + }) + + with self.assertRaises(juju.errors.JujuAPIError): + self.loop.run_until_complete( + self.libjuju.consume("offer_url", "model_name") + ) + mock_consume.assert_called_once() + mock_disconnect_model.assert_called_once() + mock_disconnect_controller.assert_called_once()