From cbaf34c69fb045001adbbe194894aa7396f27675 Mon Sep 17 00:00:00 2001 From: Andrey Pavlov Date: Tue, 29 Sep 2015 13:21:49 +0300 Subject: [PATCH] Fixed problem with tags update Now tags can be added and removed simultaneously in one call Change-Id: I7709c408e78cf08f220f9e0eae9e8359eb5ed284 Closes-bug: #1500790 --- saharaclient/api/images.py | 16 +++++++++------- saharaclient/tests/unit/test_images.py | 23 +++++++++-------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/saharaclient/api/images.py b/saharaclient/api/images.py index 66a89ca3..7d7df030 100644 --- a/saharaclient/api/images.py +++ b/saharaclient/api/images.py @@ -49,12 +49,14 @@ class ImageManager(base.ResourceManager): to_add = list(new_tags - old_tags) to_remove = list(old_tags - new_tags) - if len(to_add) != 0: - return self._post('/images/%s/tag' % image_id, - {'tags': to_add}, 'image') + add_response, remove_response = None, None - if len(to_remove) != 0: - return self._post('/images/%s/untag' % image_id, - {'tags': to_remove}, 'image') + if to_add: + add_response = self._post('/images/%s/tag' % image_id, + {'tags': to_add}, 'image') - return self._get('/images/%s' % image_id, 'image') + if to_remove: + remove_response = self._post('/images/%s/untag' % image_id, + {'tags': to_remove}, 'image') + + return remove_response or add_response or self.get(image_id) diff --git a/saharaclient/tests/unit/test_images.py b/saharaclient/tests/unit/test_images.py index b158fedd..51732c5b 100644 --- a/saharaclient/tests/unit/test_images.py +++ b/saharaclient/tests/unit/test_images.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import mock - from saharaclient.api import images from saharaclient.tests.unit import base @@ -64,27 +62,24 @@ class ImageTest(base.BaseTestCase): self.assertEqual(self.body, json.loads(self.responses.last_request.body)) - @mock.patch('saharaclient.api.images.ImageManager.get') - def test_update_tags(self, mget): + def test_update_tags(self): + url = self.URL + '/images/id' tag_url = self.URL + '/images/id/tag' untag_url = self.URL + '/images/id/untag' - self.responses.post(tag_url, json={'image': self.body}, - status_code=202) - self.responses.post(untag_url, json={'image': self.body}, - status_code=202) + body = self.body.copy() + body['tags'] = ['fake', '0.1'] - image = mock.Mock() - mget.return_value = image + self.responses.post(tag_url, json={'image': body}, + status_code=202) + self.responses.post(untag_url, json={'image': body}, + status_code=202) + self.responses.get(url, json={'image': body}) - image.tags = [] resp = self.client.images.update_tags('id', ['username', 'tag']) - self.assertEqual(tag_url, self.responses.last_request.url) self.assertIsInstance(resp, images.Image) self.assertFields(self.body, resp) - image.tags = ['username', 'tag'] resp = self.client.images.update_tags('id', ['username']) - self.assertEqual(untag_url, self.responses.last_request.url) self.assertIsInstance(resp, images.Image) self.assertFields(self.body, resp)