diff --git a/juju/client/connector.py b/juju/client/connector.py index ca490910..6c494732 100644 --- a/juju/client/connector.py +++ b/juju/client/connector.py @@ -5,12 +5,14 @@ import logging import macaroonbakery.httpbakery as httpbakery + +from juju.client import client from juju.client.connection import Connection from juju.client.gocookies import GoCookieJar, go_to_py_cookie -from juju.client.jujudata import FileJujuData, API_ENDPOINTS_KEY +from juju.client.jujudata import API_ENDPOINTS_KEY, FileJujuData from juju.client.proxy.factory import proxy_from_config -from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError -from juju.client import client +from juju.errors import (JujuConnectionError, JujuError, + PylibjujuProgrammingError) from juju.version import SUPPORTED_MAJOR_VERSION, TARGET_JUJU_VERSION log = logging.getLogger('connector') @@ -113,8 +115,6 @@ async def connect_controller(self, controller_name=None, specified_facades=None) """ if not controller_name: controller_name = self.jujudata.current_controller() - if not controller_name: - raise JujuConnectionError('No current controller. Is Juju bootstrapped?') controller = self.jujudata.controllers()[controller_name] endpoints = controller[API_ENDPOINTS_KEY] diff --git a/juju/client/jujudata.py b/juju/client/jujudata.py index ef46f15b..ee0c3bbc 100644 --- a/juju/client/jujudata.py +++ b/juju/client/jujudata.py @@ -6,11 +6,13 @@ import os import pathlib -from juju.client import client as jujuclient import yaml + from juju import tag +from juju.client import client as jujuclient from juju.client.gocookies import GoCookieJar -from juju.errors import JujuError, PylibjujuProgrammingError +from juju.errors import (JujuControllerNotFoundError, JujuError, + PylibjujuProgrammingError) from juju.utils import juju_config_dir API_ENDPOINTS_KEY = 'api-endpoints' @@ -80,7 +82,7 @@ def current_controller(self): try: return self._load_yaml('controllers.yaml', 'current-controller') except FileNotFoundError: - return None + raise JujuControllerNotFoundError('No controllers.yaml file found. Did you forget to bootstrap Juju?') def current_model(self, controller_name=None, model_only=False): '''Return the current model, qualified by its controller name. diff --git a/juju/errors.py b/juju/errors.py index 2232420c..b108442e 100644 --- a/juju/errors.py +++ b/juju/errors.py @@ -123,6 +123,10 @@ class JujuModelConfigError(JujuConfigError): pass +class JujuControllerNotFoundError(JujuError): + pass + + class AbstractMethodError(Exception): pass diff --git a/tests/unit/test_jujudata.py b/tests/unit/test_jujudata.py index 2684b96d..5d7e7312 100644 --- a/tests/unit/test_jujudata.py +++ b/tests/unit/test_jujudata.py @@ -4,8 +4,10 @@ import unittest import mock +import pytest from juju.client.jujudata import FileJujuData +from juju.errors import JujuControllerNotFoundError class TestJujuData(unittest.IsolatedAsyncioTestCase): @@ -13,4 +15,5 @@ class TestJujuData(unittest.IsolatedAsyncioTestCase): async def test_verify_controller_uninitialized(self, mock_io_open): mock_io_open.side_effect = FileNotFoundError() jujudata = FileJujuData() - assert jujudata.current_controller() is None + with pytest.raises(JujuControllerNotFoundError): + jujudata.current_controller()