# Path: conjureup/controllers/juju/clouds/gui.py
# class CloudsController(BaseCloudController):
#     cancel_monitor = asyncio.Event()
# 
#     def __init__(self):
#         self.view = None
# 
#     def finish(self, cloud):
#         """ Load the selected cloud provider
#         """
#         self.cancel_monitor.set()
# 
#         if cloud in CUSTOM_PROVIDERS:
#             app.provider = load_schema(cloud)
#         else:
#             app.provider = load_schema(juju.get_cloud_types_by_name()[cloud])
# 
#         if app.provider.cloud_type == cloud_types.LOCALHOST:
#             app.provider._set_lxd_dir_env()
# 
#         try:
#             app.provider.load(cloud)
#         except errors.SchemaCloudError:
#             app.provider.cloud = utils.gen_cloud()
# 
#         if app.provider.model is None:
#             app.provider.model = utils.gen_model()
# 
#         track_event("Cloud selection", app.provider.cloud, "")
# 
#         return controllers.use('credentials').render()
# 
#     def render(self, going_back=False):
#         "Pick or create a cloud to bootstrap a new controller on"
#         all_clouds = juju.get_clouds()
#         compatible_clouds = juju.get_compatible_clouds()
#         cloud_types = juju.get_cloud_types_by_name()
#         # filter to only public clouds
#         public_clouds = sorted(
#             name for name, info in all_clouds.items()
#             if info['defined'] == 'public')
#         # filter to custom clouds
#         # exclude localhost because we treat that as "configuring a new cloud"
#         custom_clouds = sorted(
#             name for name, info in all_clouds.items()
#             if info['defined'] != 'public' and
#             cloud_types[name] != 'localhost')
# 
#         prev_screen = self.prev_screen
#         if app.alias_given or (app.spell_given and not app.addons):
#             # we were given an alias (so spell and addons are locked)
#             # or we were given a spell and there are no addons to change
#             # so we disable the back button
#             prev_screen = None
#         self.view = CloudView(app,
#                               public_clouds,
#                               custom_clouds,
#                               compatible_clouds,
#                               cb=self.finish,
#                               back=prev_screen)
# 
#         if 'localhost' in compatible_clouds:
#             app.log.debug(
#                 "Starting watcher for verifying LXD server is available.")
#             self.cancel_monitor.clear()
#             app.loop.create_task(
#                 self._monitor_localhost(LocalhostProvider())
#             )
#         self.view.show()
# 
#     async def _monitor_localhost(self, provider):
#         """ Checks that localhost/lxd is available and listening,
#         updates widget accordingly
#         """
# 
#         while not self.cancel_monitor.is_set():
#             try:
#                 provider._set_lxd_dir_env()
#                 client_compatible = await provider.is_client_compatible()
#                 server_compatible = await provider.is_server_compatible()
#                 has_network_bridge = await provider.get_networks()
#                 has_storage_pools = await provider.get_storage_pools()
#                 if not (client_compatible and server_compatible):
#                     raise errors.LXDCompatibilityError()
#                 elif not has_network_bridge:
#                     raise errors.LXDNetworkError()
#                 elif not has_storage_pools:
#                     raise errors.LXDStorageError()
# 
#                 events.LXDAvailable.set()
#                 self.cancel_monitor.set()
#                 self.view._update_localhost_widget(True)
#                 return
#             except errors.LXDError as e:
#                 self.view._update_localhost_widget(False, e.message)
#             await run_with_interrupt(asyncio.sleep(2),
#                                      self.cancel_monitor)
# 
#     def prev_screen(self):
#         self.cancel_monitor.set()
#         controllers.use('addons').render(going_back=True)


import unittest
from unittest.mock import MagicMock, call, patch
from conjureup.controllers.juju.clouds.gui import CloudsController
        self.view_patcher = patch(
            'conjureup.controllers.juju.clouds.gui.CloudView')
        self.view_patcher.start()
        self.app_patcher = patch(
            'conjureup.controllers.juju.clouds.gui.app')
        mock_app = self.app_patcher.start()
        mock_app.ui = MagicMock(name="app.ui")
        self.list_clouds_patcher = patch(
            'conjureup.juju.get_compatible_clouds')
        self.mock_list_clouds = self.list_clouds_patcher.start()
        self.mock_list_clouds.return_value = ['test1', 'test2']
        self.get_clouds_patcher = patch(
            'conjureup.juju.get_clouds')
        self.mock_get_clouds_patcher = self.get_clouds_patcher.start()

    def tearDown(self):
        self.finish_patcher.stop()
        self.view_patcher.stop()
        self.app_patcher.stop()
        self.list_clouds_patcher.stop()
        self.get_clouds_patcher.stop()

    def test_render(self):
        "call render"
        self.controller.render()


class CloudsGUIFinishTestCase(unittest.TestCase):

    def setUp(self):

# Path: conjureup/controllers/juju/clouds/gui.py
# class CloudsController(BaseCloudController):
#     cancel_monitor = asyncio.Event()
# 
#     def __init__(self):
#         self.view = None
# 
#     def finish(self, cloud):
#         """ Load the selected cloud provider
#         """
#         self.cancel_monitor.set()
# 
#         if cloud in CUSTOM_PROVIDERS:
#             app.provider = load_schema(cloud)
#         else:
#             app.provider = load_schema(juju.get_cloud_types_by_name()[cloud])
# 
#         if app.provider.cloud_type == cloud_types.LOCALHOST:
#             app.provider._set_lxd_dir_env()
# 
#         try:
#             app.provider.load(cloud)
#         except errors.SchemaCloudError:
#             app.provider.cloud = utils.gen_cloud()
# 
#         if app.provider.model is None:
#             app.provider.model = utils.gen_model()
# 
#         track_event("Cloud selection", app.provider.cloud, "")
# 
#         return controllers.use('credentials').render()
# 
#     def render(self, going_back=False):
#         "Pick or create a cloud to bootstrap a new controller on"
#         all_clouds = juju.get_clouds()
#         compatible_clouds = juju.get_compatible_clouds()
#         cloud_types = juju.get_cloud_types_by_name()
#         # filter to only public clouds
#         public_clouds = sorted(
#             name for name, info in all_clouds.items()
#             if info['defined'] == 'public')
#         # filter to custom clouds
#         # exclude localhost because we treat that as "configuring a new cloud"
#         custom_clouds = sorted(
#             name for name, info in all_clouds.items()
#             if info['defined'] != 'public' and
#             cloud_types[name] != 'localhost')
# 
#         prev_screen = self.prev_screen
#         if app.alias_given or (app.spell_given and not app.addons):
#             # we were given an alias (so spell and addons are locked)
#             # or we were given a spell and there are no addons to change
#             # so we disable the back button
#             prev_screen = None
#         self.view = CloudView(app,
#                               public_clouds,
#                               custom_clouds,
#                               compatible_clouds,
#                               cb=self.finish,
#                               back=prev_screen)
# 
#         if 'localhost' in compatible_clouds:
#             app.log.debug(
#                 "Starting watcher for verifying LXD server is available.")
#             self.cancel_monitor.clear()
#             app.loop.create_task(
#                 self._monitor_localhost(LocalhostProvider())
#             )
#         self.view.show()
# 
#     async def _monitor_localhost(self, provider):
#         """ Checks that localhost/lxd is available and listening,
#         updates widget accordingly
#         """
# 
#         while not self.cancel_monitor.is_set():
#             try:
#                 provider._set_lxd_dir_env()
#                 client_compatible = await provider.is_client_compatible()
#                 server_compatible = await provider.is_server_compatible()
#                 has_network_bridge = await provider.get_networks()
#                 has_storage_pools = await provider.get_storage_pools()
#                 if not (client_compatible and server_compatible):
#                     raise errors.LXDCompatibilityError()
#                 elif not has_network_bridge:
#                     raise errors.LXDNetworkError()
#                 elif not has_storage_pools:
#                     raise errors.LXDStorageError()
# 
#                 events.LXDAvailable.set()
#                 self.cancel_monitor.set()
#                 self.view._update_localhost_widget(True)
#                 return
#             except errors.LXDError as e:
#                 self.view._update_localhost_widget(False, e.message)
#             await run_with_interrupt(asyncio.sleep(2),
#                                      self.cancel_monitor)
# 
#     def prev_screen(self):
#         self.cancel_monitor.set()
#         controllers.use('addons').render(going_back=True)


# Path: test/controllers_juju/test_controllers_clouds_gui.py
import unittest
from unittest.mock import MagicMock, call, patch
from conjureup.controllers.juju.clouds.gui import CloudsController

        self.view_patcher = patch(
            'conjureup.controllers.juju.clouds.gui.CloudView')
        self.view_patcher.start()
        self.app_patcher = patch(
            'conjureup.controllers.juju.clouds.gui.app')
        mock_app = self.app_patcher.start()
        mock_app.ui = MagicMock(name="app.ui")
        self.list_clouds_patcher = patch(
            'conjureup.juju.get_compatible_clouds')
        self.mock_list_clouds = self.list_clouds_patcher.start()
        self.mock_list_clouds.return_value = ['test1', 'test2']
        self.get_clouds_patcher = patch(
            'conjureup.juju.get_clouds')
        self.mock_get_clouds_patcher = self.get_clouds_patcher.start()

    def tearDown(self):
        self.finish_patcher.stop()
        self.view_patcher.stop()
        self.app_patcher.stop()
        self.list_clouds_patcher.stop()
        self.get_clouds_patcher.stop()

    def test_render(self):
        "call render"
        self.controller.render()


class CloudsGUIFinishTestCase(unittest.TestCase):

    def setUp(self):

        self.controller = CloudsController()