The following pull request was submitted through Github. It can be accessed and reviewed at: https://github.com/lxc/pylxd/pull/101
This e-mail was sent by the LXC bot, direct replies will not reach the author unless they happen to be subscribed to this list. === Description (from pull-request) === The client init will raise an exception if the endpoint isn't accessible. This fixes one of the two issues I've discovered while trying to sort out what's going on with my local lxd install.
From 1aab599ab04da4e5c4ab68d7bb0bad118d2513ba Mon Sep 17 00:00:00 2001 From: Paul Hummer <p...@eventuallyanyway.com> Date: Tue, 24 May 2016 14:26:22 -0600 Subject: [PATCH 1/2] Add validation to the `pylxd.Client` creation. An exception is raised when the endpoint is invalid. --- pylxd/client.py | 10 ++++++++++ pylxd/exceptions.py | 2 ++ pylxd/tests/mock_lxd.py | 7 +++++++ pylxd/tests/test_client.py | 26 ++++++++++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 pylxd/exceptions.py diff --git a/pylxd/client.py b/pylxd/client.py index d2eb311..165e6ca 100644 --- a/pylxd/client.py +++ b/pylxd/client.py @@ -22,6 +22,7 @@ import requests import requests_unixsocket +from pylxd import exceptions from pylxd.container import Container from pylxd.image import Image from pylxd.operation import Operation @@ -202,6 +203,15 @@ def __init__(self, endpoint=None, version='1.0'): quote(path, safe=''))) self.api = self.api[version] + # Verify the connection is valid. + try: + response = self.api.get() + if response.status_code != 200: + raise exceptions.ClientConnectionFailed() + except (requests.exceptions.ConnectionError, + requests.exceptions.InvalidURL): + raise exceptions.ClientConnectionFailed() + self.containers = self.Containers(self) self.images = self.Images(self) self.operations = self.Operations(self) diff --git a/pylxd/exceptions.py b/pylxd/exceptions.py new file mode 100644 index 0000000..41b5400 --- /dev/null +++ b/pylxd/exceptions.py @@ -0,0 +1,2 @@ +class ClientConnectionFailed(Exception): + """An exception raised when the Client connection fails.""" diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py index e580ffd..9f144db 100644 --- a/pylxd/tests/mock_lxd.py +++ b/pylxd/tests/mock_lxd.py @@ -36,6 +36,13 @@ def profile_GET(request, context): RULES = [ + # General service endpoints + { + 'text': '', + 'method': 'GET', + 'url': r'^http://pylxd.test/1.0$', + }, + # Containers { 'text': json.dumps({'metadata': [ diff --git a/pylxd/tests/test_client.py b/pylxd/tests/test_client.py index ecd4698..512ff49 100644 --- a/pylxd/tests/test_client.py +++ b/pylxd/tests/test_client.py @@ -11,6 +11,16 @@ class TestClient(unittest.TestCase): """Tests for pylxd.client.Client.""" + def setUp(self): + self.patcher = mock.patch('pylxd.client._APINode.get') + self.get = self.patcher.start() + + response = mock.MagicMock(status_code=200) + self.get.return_value = response + + def tearDown(self): + self.patcher.stop() + def test_create(self): """Client creation sets default API endpoint.""" expected = 'http+unix://%2Fvar%2Flib%2Flxd%2Funix.socket/1.0' @@ -37,6 +47,22 @@ def test_create_endpoint(self): self.assertEqual(expected, an_client.api._api_endpoint) + def test_connection_404(self): + """If the endpoint 404s, an exception is raised.""" + response = mock.MagicMock(status_code=404) + self.get.return_value = response + + self.assertRaises(Exception, client.Client) + + def test_connection_failed(self): + """If the connection fails, an exception is raised.""" + def raise_exception(): + raise requests.exceptions.ConnectionError() + self.get.side_effect = raise_exception + self.get.return_value = None + + self.assertRaises(Exception, client.Client) + class TestAPINode(unittest.TestCase): """Tests for pylxd.client._APINode.""" From d1af9d4a245ae6cc60baca4080b88fd2ef8f911c Mon Sep 17 00:00:00 2001 From: Paul Hummer <p...@eventuallyanyway.com> Date: Tue, 24 May 2016 14:28:11 -0600 Subject: [PATCH 2/2] Add `pylxd.client.Client` to `pylxd.Client`. --- pylxd/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pylxd/__init__.py b/pylxd/__init__.py index 0ba37c6..6fac57e 100644 --- a/pylxd/__init__.py +++ b/pylxd/__init__.py @@ -17,3 +17,4 @@ __version__ = pbr.version.VersionInfo('pylxd').version_string() from pylxd.deprecated import api # NOQA +from pylxd.client import Client # NOQA
_______________________________________________ lxc-devel mailing list lxc-devel@lists.linuxcontainers.org http://lists.linuxcontainers.org/listinfo/lxc-devel