Skip to content

Commit 4f38c4e

Browse files
authored
Merge pull request #1009 from cderici/controller-name-not-found
#1009 #### Description `controllers.yaml` is read for the `controller_name` after a connection is established, and this creates a dependency on the juju-cli to be installed in the system, which is not required for pylibjuju. Fixes #996 #### QA Steps Testing this requires a bit of stateful actions. I see two easy ways to test: 1. Use the integration test I added. Add `import pdb;pdb.set_trace()` right before the `new_cont.connect(` line, and run the integration test with `--pdb` as follows: ``` tox -e integration -- -- tests/integration/test_connection.py::test_connection_happy_path --pdb ``` Before continuing, go find the `controllers.yaml` and rename it to something else (sort of easier than removing juju-cli etc): ``` mv controllers.yaml controllers.yaml.tmp ``` Now continue in the pdb (`c`) to see if the new connection will be established without a problem. 2. Do what the integration test is doing manually in the repl: ```python $ python -m asyncio >>> from juju import controller >>> c=controller.Controller();await c.connect() ``` Stop at this point to rename the `controllers.yaml` file. ```python >>> new_c=controller.Controller() >>> await new_c.connect(endpoint=c.endpoint, username=c.username, password=c.password, cacert=c.cacert) ``` This should succeed. Don't forget to rename your controllers.yaml back: ``` mv controllers.yaml.tmp controllers.yaml ``` #### Notes & Discussion JUJU-5317
2 parents beed55f + cb70f35 commit 4f38c4e

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

juju/client/connector.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from juju.client.gocookies import GoCookieJar, go_to_py_cookie
1010
from juju.client.jujudata import FileJujuData, API_ENDPOINTS_KEY
1111
from juju.client.proxy.factory import proxy_from_config
12-
from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError
12+
from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError, ControllerNameNotFound
1313
from juju.client import client
1414

1515
log = logging.getLogger('connector')
@@ -97,7 +97,11 @@ async def connect(self, **kwargs):
9797
if not self.controller_name:
9898
if 'endpoint' not in kwargs:
9999
raise PylibjujuProgrammingError("Please report this error to the maintainers.")
100-
self.controller_name = self.jujudata.controller_name_by_endpoint(kwargs['endpoint'])
100+
try:
101+
self.controller_name = self.jujudata.controller_name_by_endpoint(kwargs['endpoint'])
102+
except ControllerNameNotFound:
103+
# It's ok because we might not have the juju cli (controllers.yaml)
104+
pass
101105

102106
# Check if we support the target controller
103107
if not self._connection.info['server-version'].startswith(SUPPORTED_JUJU_API_PREFIX):

juju/client/jujudata.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import yaml
1111
from juju import tag
1212
from juju.client.gocookies import GoCookieJar
13-
from juju.errors import JujuError, PylibjujuProgrammingError
13+
from juju.errors import JujuError, PylibjujuProgrammingError, ControllerNameNotFound
1414
from juju.utils import juju_config_dir
1515

1616
API_ENDPOINTS_KEY = 'api-endpoints'
@@ -133,7 +133,11 @@ def controller_name_by_endpoint(self, endpoint):
133133
134134
:param str endpoint: The endpoint of the controller we're looking for
135135
"""
136-
for controller_name, controller in self.controllers().items():
136+
try:
137+
contrs = self.controllers()
138+
except FileNotFoundError:
139+
raise ControllerNameNotFound()
140+
for controller_name, controller in contrs.items():
137141
if isinstance(endpoint, str):
138142
if endpoint in controller[API_ENDPOINTS_KEY]:
139143
return controller_name

juju/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ class JujuModelConfigError(JujuConfigError):
118118

119119
class AbstractMethodError(Exception):
120120
pass
121+
122+
123+
class ControllerNameNotFound(Exception):
124+
pass

tests/integration/test_connection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
logger = logging.getLogger(__name__)
2525

2626

27+
@base.bootstrapped
28+
@pytest.mark.asyncio
29+
async def test_connection_happy_path(event_loop):
30+
async with base.CleanController() as contr:
31+
conn = contr.connection()
32+
new_cont = Controller()
33+
await new_cont.connect(endpoint=conn.endpoint,
34+
username=conn.username,
35+
password=conn.password,
36+
cacert=conn.cacert,
37+
)
38+
await new_cont.disconnect()
39+
40+
2741
@base.bootstrapped
2842
@pytest.mark.asyncio
2943
async def test_monitor(event_loop):

0 commit comments

Comments
 (0)