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
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()