[lxc-devel] [pylxd/master] Avoid changing default endpoint fixes #346

jvrsantacruz on Github lxc-bot at linuxcontainers.org
Fri Dec 14 19:20:45 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 777 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181214/e0f87f9b/attachment.bin>
-------------- next part --------------
From c22a75cfc1eb44040f38d8f835ee5ea70f6bfaed Mon Sep 17 00:00:00 2001
From: Javier Santacruz <javier.santacruz at avature.net>
Date: Fri, 14 Dec 2018 20:14:41 +0100
Subject: [PATCH] Avoid changing default endpoint fixes #346

When an alternative lxd server is installed via snap,
pylxd will change the default to it, effectively
switching from the current server to a different one
without the user knowing.

This was making the default value of the Client unreliable
as it might change anytime without warning.

Signed-off-by: Javier Santacruz <javier.santacruz at avature.net>
---
 pylxd/client.py            | 10 ++--------
 pylxd/tests/test_client.py | 15 +--------------
 2 files changed, 3 insertions(+), 22 deletions(-)

diff --git a/pylxd/client.py b/pylxd/client.py
index c1edf401..cbe1e8a3 100644
--- a/pylxd/client.py
+++ b/pylxd/client.py
@@ -280,14 +280,8 @@ def __init__(
                 self.api = _APINode(
                     endpoint, cert=cert, verify=verify, timeout=timeout)
         else:
-            if 'LXD_DIR' in os.environ:
-                path = os.path.join(
-                    os.environ.get('LXD_DIR'), 'unix.socket')
-            else:
-                if os.path.exists('/var/snap/lxd/common/lxd/unix.socket'):
-                    path = '/var/snap/lxd/common/lxd/unix.socket'
-                else:
-                    path = '/var/lib/lxd/unix.socket'
+            path = os.path.join(
+                os.getenv('LXD_DIR', '/var/lib/lxd'), 'unix.socket')
             self.api = _APINode('http+unix://{}'.format(
                 parse.quote(path, safe='')), timeout=timeout)
         self.api = self.api[version]
diff --git a/pylxd/tests/test_client.py b/pylxd/tests/test_client.py
index 46dd6f9a..b77d6ca9 100644
--- a/pylxd/tests/test_client.py
+++ b/pylxd/tests/test_client.py
@@ -48,27 +48,14 @@ def tearDown(self):
         self.get_patcher.stop()
         self.post_patcher.stop()
 
-    @mock.patch('os.path.exists')
-    def test_create(self, _path_exists):
+    def test_create(self):
         """Client creation sets default API endpoint."""
-        _path_exists.return_value = False
         expected = 'http+unix://%2Fvar%2Flib%2Flxd%2Funix.socket/1.0'
 
         an_client = client.Client()
 
         self.assertEqual(expected, an_client.api._api_endpoint)
 
-    @mock.patch('os.path.exists')
-    @mock.patch('os.environ')
-    def test_create_with_snap_lxd(self, _environ, _path_exists):
-        # """Client creation sets default API endpoint."""
-        _path_exists.return_value = True
-        expected = ('http+unix://%2Fvar%2Fsnap%2Flxd%2F'
-                    'common%2Flxd%2Funix.socket/1.0')
-
-        an_client = client.Client()
-        self.assertEqual(expected, an_client.api._api_endpoint)
-
     def test_create_LXD_DIR(self):
         """When LXD_DIR is set, use it in the client."""
         os.environ['LXD_DIR'] = '/lxd'


More information about the lxc-devel mailing list