From 68b007225cc1fd432dba07fe42a5d1362925bc32 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Fri, 11 Sep 2020 15:05:00 +0200 Subject: [PATCH] Add consume to libjuju.py This function allows to consume an offered endpoint in another model Change-Id: I320978aac654fbb8199fdfbf4e928b0e2ba5458b Signed-off-by: David Garcia --- n2vc/libjuju.py | 22 ++++++++ n2vc/tests/unit/test_libjuju.py | 93 +++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) 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() -- 2.25.1