From: David Garcia Date: Tue, 25 Aug 2020 13:22:30 +0000 (+0200) Subject: Add list_offers function to libjuju.py X-Git-Tag: release-v9.0-start~25 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F38%2F9638%2F5;p=osm%2FN2VC.git Add list_offers function to libjuju.py This function return the list of offers available in a model Change-Id: Iebc9f55c79f27caab5b2f5a2874e1d2d93dc9bc4 Signed-off-by: David Garcia --- diff --git a/n2vc/libjuju.py b/n2vc/libjuju.py index 2f18837..d8b0858 100644 --- a/n2vc/libjuju.py +++ b/n2vc/libjuju.py @@ -22,7 +22,7 @@ from juju.errors import JujuAPIError from juju.model import Model from juju.machine import Machine from juju.application import Application -from juju.client._definitions import FullStatus +from juju.client._definitions import FullStatus, QueryApplicationOffersResults from n2vc.juju_watcher import JujuModelWatcher from n2vc.provisioner import AsyncSSHProvisioner from n2vc.n2vc_conn import N2VCConnector @@ -966,3 +966,17 @@ class Libjuju: return models finally: await self.disconnect_controller(controller) + + async def list_offers(self, model_name: str) -> QueryApplicationOffersResults: + """List models with certain names + + :param: model_name: Model name + + :return: Returns list of offers + """ + + controller = await self.get_controller() + try: + return await controller.list_offers(model_name) + finally: + await self.disconnect_controller(controller) diff --git a/n2vc/tests/unit/test_libjuju.py b/n2vc/tests/unit/test_libjuju.py index f517afc..27f0454 100644 --- a/n2vc/tests/unit/test_libjuju.py +++ b/n2vc/tests/unit/test_libjuju.py @@ -907,3 +907,38 @@ class ModelsExistTest(LibjujuTestCase): ) self.assertTrue(exist) self.assertEqual(non_existing_models, []) + + +@asynctest.mock.patch("n2vc.libjuju.Libjuju.get_controller") +@asynctest.mock.patch("n2vc.libjuju.Libjuju.disconnect_controller") +@asynctest.mock.patch("juju.controller.Controller.list_offers") +class ListOffers(LibjujuTestCase): + def setUp(self): + super(ListOffers, self).setUp() + + def test_disconnect_controller( + self, mock_list_offers, mock_disconnect_controller, mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_list_offers.side_effect = Exception() + with self.assertRaises(Exception): + self.loop.run_until_complete(self.libjuju.list_offers("model")) + mock_disconnect_controller.assert_called_once() + + def test_empty_list( + self, mock_list_offers, mock_disconnect_controller, mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_list_offers.return_value = [] + offers = self.loop.run_until_complete(self.libjuju.list_offers("model")) + self.assertEqual(offers, []) + mock_disconnect_controller.assert_called_once() + + def test_non_empty_list( + self, mock_list_offers, mock_disconnect_controller, mock_get_controller, + ): + mock_get_controller.return_value = juju.controller.Controller() + mock_list_offers.return_value = ["offer"] + offers = self.loop.run_until_complete(self.libjuju.list_offers("model")) + self.assertEqual(offers, ["offer"]) + mock_disconnect_controller.assert_called_once()