[lxc-devel] [pylxd/branch-2.0] Fix bug #232 for 2.0 series branch

ajkavanagh on Github lxc-bot at linuxcontainers.org
Tue Feb 6 15:37:37 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 651 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180206/6f2cb74e/attachment.bin>
-------------- next part --------------
From 0fd6e29e676dcbf8853230d9148b3e9e7758c1ec Mon Sep 17 00:00:00 2001
From: Virgil Dupras <hsoft at hardcoded.net>
Date: Wed, 31 May 2017 21:15:54 -0400
Subject: [PATCH 1/6] Fix broken CI testing

The whole test suite was broken lately for three reasons:

1. Version in `setp.cfg` was out of sync with git tagging, making `pbr`
   scream.
2. `mock_services` is outdated and unmaintained and doesn't work with
   newer versions of `requests-mock`.
3. The newly added `description` property wasn't properly added in
   mocked LXD.

I've fixed those two problems by updating the `setup.cfg` version,
pinning `requests-mock` to the last version to work and adding a
`description` field to the mocks.

On my machine, all tox tests pass now.
---
 pylxd/tests/mock_lxd.py | 1 +
 test-requirements.txt   | 6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 57b771e..5c65d6d 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -185,6 +185,7 @@ def profile_GET(request, context):
                 },
                 'created_at': "1983-06-16T00:00:00-00:00",
                 'last_used_at': "1983-06-16T00:00:00-00:00",
+                'description': "Some description",
                 'devices': {
                     'root': {
                         'path': "/",
diff --git a/test-requirements.txt b/test-requirements.txt
index 0c5e340..9d3df30 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -3,5 +3,7 @@ nose>=1.3.7
 mock>=1.3.0
 flake8>=2.5.0
 coverage>=4.1
-# See https://github.com/novafloss/mock-services/pull/15
--e git://github.com/rockstar/mock-services.git@aba3977d1a3f43afd77d99f241ee1111c20deeed#egg=mock-services
+mock-services>=0.3
+# mock-services is old and unmaintained. Doesn't work with newer versions of
+# requests-mock. Thus, we have to pin it down.
+requests-mock<1.2

From d8ea618946c2064ce67f3d699a292b1d60ea5ac4 Mon Sep 17 00:00:00 2001
From: Virgil Dupras <hsoft at hardcoded.net>
Date: Wed, 31 May 2017 21:32:38 -0400
Subject: [PATCH 2/6] Removing tox envs from travis cache

I suspect that this is what preventing CI checks in PR #233 from
passing.
---
 .travis.yml | 1 -
 1 file changed, 1 deletion(-)

diff --git a/.travis.yml b/.travis.yml
index 11974ad..fc7ed01 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -16,7 +16,6 @@ script:
   - test -d .tox/$TOXENV/log && cat .tox/$TOXENV/log/*.log || true
 cache:
   directories:
-    - .tox/$TOXENV
     - $HOME/.cache/pip
 after_success:
   - codecov

From 041a66a8cc49de46704570d0bec9df418cd0940d Mon Sep 17 00:00:00 2001
From: Virgil Dupras <hsoft at hardcoded.net>
Date: Sat, 3 Jun 2017 21:59:01 -0400
Subject: [PATCH 3/6] Support older versions of LXD

When using pylxd on an old LXD version, we would crash when saving
objects because some attributes supported by pylxd are not supported by
the LXD server.
---
 pylxd/model.py | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/pylxd/model.py b/pylxd/model.py
index 3ca5ebf..6f0bb24 100644
--- a/pylxd/model.py
+++ b/pylxd/model.py
@@ -19,6 +19,9 @@
 from pylxd.operation import Operation
 
 
+MISSING = object()
+
+
 class Attribute(object):
     """A metadata class for model attributes."""
 
@@ -149,9 +152,23 @@ def sync(self, rollback=False):
         # XXX: rockstar (25 Jun 2016) - This has the potential to step
         # on existing attributes.
         response = self.api.get()
-        for key, val in response.json()['metadata'].items():
+        payload = response.json()['metadata']
+        for key, val in payload.items():
             if key not in self.__dirty__ or rollback:
-                setattr(self, key, val)
+                try:
+                    setattr(self, key, val)
+                    self.__dirty__.remove(key)
+                except AttributeError:
+                    # We have received an attribute from the server that we
+                    # don't support in our model. Ignore this error, it
+                    # doesn't hurt us.
+                    pass
+
+        # Make sure that *all* supported attributes are set, even those that
+        # aren't supported by the server.
+        missing_attrs = set(self.__attributes__.keys()) - set(payload.keys())
+        for missing_attr in missing_attrs:
+            setattr(self, missing_attr, MISSING)
         if rollback:
             del self.__dirty__[:]
     fetch = deprecated("fetch is deprecated; please use sync")(sync)
@@ -188,8 +205,12 @@ def delete(self, wait=False):
     def marshall(self):
         """Marshall the object in preparation for updating to the server."""
         marshalled = {}
-        for key, val in self.__attributes__.items():
-            if ((not val.readonly and not val.optional) or
-                    (val.optional and hasattr(self, key))):
-                marshalled[key] = getattr(self, key)
+        for key, attr in self.__attributes__.items():
+            if ((not attr.readonly and not attr.optional) or
+                    (attr.optional and hasattr(self, key))):
+                val = getattr(self, key)
+                # Don't send back to the server an attribute it doesn't
+                # support.
+                if val is not MISSING:
+                    marshalled[key] = val
         return marshalled

From 66ae11d6e045cfa9a2254e46148749ee2ae1cd2b Mon Sep 17 00:00:00 2001
From: Virgil Dupras <hsoft at hardcoded.net>
Date: Thu, 1 Jun 2017 20:53:36 -0400
Subject: [PATCH 4/6] Make models resilient to new attributes in LXD

It's been a few times now (ref #188, #230) where we're in a tricky situation
because a new attribute in LXD makes pylxd crash. It's not fun because
it makes us *have* to update pylxd and LXD at the same time.

This commit makes models ignore `AttributeError` during `sync()`, which
should normally fix this problem once and for all.

Depends on #233
---
 pylxd/tests/mock_lxd.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 5c65d6d..4f9eccf 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -213,7 +213,8 @@ def profile_GET(request, context):
                 ],
                 'stateful': False,
                 'status': "Running",
-                'status_code': 103
+                'status_code': 103,
+                'unsupportedbypylxd': "This attribute is not supported by pylxd. We want to test whether the mere presence of it makes it crash."
             }},
         'method': 'GET',
         'url': r'^http://pylxd.test/1.0/containers/an-container$',

From 501bfc3281f6be7def428e9140650d2f1d237bf0 Mon Sep 17 00:00:00 2001
From: Alex Kavanagh <alex at ajkavanagh.co.uk>
Date: Tue, 6 Feb 2018 15:30:49 +0000
Subject: [PATCH 5/6] Fix up pep8 issues on cherry-pick 5904292b0

---
 pylxd/tests/mock_lxd.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 4f9eccf..40396d0 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -214,7 +214,9 @@ def profile_GET(request, context):
                 'stateful': False,
                 'status': "Running",
                 'status_code': 103,
-                'unsupportedbypylxd': "This attribute is not supported by pylxd. We want to test whether the mere presence of it makes it crash."
+                'unsupportedbypylxd': ("This attribute is not supported by "
+                                       "pylxd. We want to test whether the "
+                                       "mere presence of it makes it crash.")
             }},
         'method': 'GET',
         'url': r'^http://pylxd.test/1.0/containers/an-container$',

From 76aec47c7dfd16cbd82e1b85a39ec3de6f05e866 Mon Sep 17 00:00:00 2001
From: Alex Kavanagh <alex at ajkavanagh.co.uk>
Date: Tue, 6 Feb 2018 15:32:30 +0000
Subject: [PATCH 6/6] bump version to 2.0.6 prior to merge

---
 setup.cfg | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/setup.cfg b/setup.cfg
index 5fa3e8b..8ea40a9 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -1,7 +1,7 @@
 [metadata]
 name = pylxd
 summary = python library for lxd
-version = 2.0.5
+version = 2.0.6
 description-file =
     README.rst
 author = Paul Hummer


More information about the lxc-devel mailing list