Add list_offers function to libjuju.py 38/9638/5
authorDavid Garcia <david.garcia@canonical.com>
Tue, 25 Aug 2020 13:22:30 +0000 (15:22 +0200)
committergarciadav <david.garcia@canonical.com>
Fri, 11 Sep 2020 12:34:10 +0000 (14:34 +0200)
This function return the list of offers available in a model

Change-Id: Iebc9f55c79f27caab5b2f5a2874e1d2d93dc9bc4
Signed-off-by: David Garcia <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/tests/unit/test_libjuju.py

index 2f18837..d8b0858 100644 (file)
@@ -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)
index f517afc..27f0454 100644 (file)
@@ -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()