commit 238085929eaccd0b0d6666e4ca1a50cda5fe1ec3
Author: Christos Stavrakakis <cstavr@grnet.gr>
Date:   Sat Feb 15 18:37:23 2014 +0200

    cyclades: Major refactor to plankton backend
    
    * Rename ImageBackend to PlanktonBackend
    * Rewrite PlanktonBackend methods in order that they refer to Pithos
      objects by their UUID
    * Convert PlanktonBackend to context manager that is responsible for
      returning the PithosBackend connection back to the pool, and remove
      stale 'image_backend' context manager
    * Make PlanktonBackend raise cloud (API) faults

diff --git a/snf-cyclades-app/synnefo/api/tests/images.py b/snf-cyclades-app/synnefo/api/tests/images.py
index ff844d7..6067180 100644
--- a/snf-cyclades-app/synnefo/api/tests/images.py
+++ b/snf-cyclades-app/synnefo/api/tests/images.py
@@ -42,9 +42,13 @@ from synnefo.lib import join_urls
 from mock import patch
 from functools import wraps
 
+compute_path = get_service_path(cyclades_services, 'compute',
+                                version='v2.0')
+IMAGES_URL = join_urls(compute_path, "images/")
+
 
 def assert_backend_closed(func):
-    """Decorator for ensuring that ImageBackend is returned to pool."""
+    """Decorator for ensuring that PlanktonBackend is returned to pool."""
     @wraps(func)
     def wrapper(self, backend):
         result = func(self, backend)
@@ -54,34 +58,12 @@ def assert_backend_closed(func):
     return wrapper
 
 
-class ComputeAPITest(BaseAPITest):
-    def setUp(self, *args, **kwargs):
-        super(ComputeAPITest, self).setUp(*args, **kwargs)
-        self.compute_path = get_service_path(cyclades_services, 'compute',
-                                             version='v2.0')
-    def myget(self, path, *args, **kwargs):
-        path = join_urls(self.compute_path, path)
-        return self.get(path, *args, **kwargs)
-
-    def myput(self, path, *args, **kwargs):
-        path = join_urls(self.compute_path, path)
-        return self.put(path, *args, **kwargs)
-
-    def mypost(self, path, *args, **kwargs):
-        path = join_urls(self.compute_path, path)
-        return self.post(path, *args, **kwargs)
-
-    def mydelete(self, path, *args, **kwargs):
-        path = join_urls(self.compute_path, path)
-        return self.delete(path, *args, **kwargs)
-
-
-@patch('synnefo.plankton.backend.ImageBackend')
-class ImageAPITest(ComputeAPITest):
+@patch('synnefo.plankton.backend.PlanktonBackend')
+class ImageAPITest(BaseAPITest):
     @assert_backend_closed
     def test_create_image(self, mimage):
         """Test that create image is not implemented"""
-        response = self.mypost('images/', 'user', json.dumps(''), 'json')
+        response = self.post(IMAGES_URL, 'user', json.dumps(''), 'json')
         self.assertEqual(response.status_code, 501)
 
     @assert_backend_closed
@@ -90,8 +72,8 @@ class ImageAPITest(ComputeAPITest):
         images = [{'id': 1, 'name': 'image-1'},
                   {'id': 2, 'name': 'image-2'},
                   {'id': 3, 'name': 'image-3'}]
-        mimage().list_images.return_value = images
-        response = self.myget('images', 'user')
+        mimage().__enter__().list_images.return_value = images
+        response = self.get(IMAGES_URL, 'user')
         self.assertSuccess(response)
         api_images = json.loads(response.content)['images']
         self.assertEqual(images, api_images)
@@ -150,8 +132,8 @@ class ImageAPITest(ComputeAPITest):
                    'created': '2012-11-26T11:52:54+00:00',
                    'updated': '2012-12-26T11:52:54+00:00',
                    'metadata': {}}]
-        mimage().list_images.return_value = images
-        response = self.myget('images/detail', 'user')
+        mimage().__enter__().list_images.return_value = images
+        response = self.get(join_urls(IMAGES_URL, "detail"), 'user')
         self.assertSuccess(response)
         api_images = json.loads(response.content)['images']
         self.assertEqual(len(result_images), len(api_images))
@@ -184,9 +166,10 @@ class ImageAPITest(ComputeAPITest):
                    'updated_at': new_time.isoformat(),
                    'deleted_at': new_time.isoformat(),
                    'properties': ''}]
-        mimage().list_images.return_value = images
+        mimage().__enter__().list_images.return_value = images
         response =\
-            self.myget('images/detail?changes-since=%sUTC' % new_time)
+            self.get(join_urls(IMAGES_URL, 'detail?changes-since=%sUTC' %
+                               new_time))
         self.assertSuccess(response)
         api_images = json.loads(response.content)['images']
         self.assertEqual(1, len(api_images))
@@ -211,29 +194,28 @@ class ImageAPITest(ComputeAPITest):
                    'user_id': 'user1',
                    'tenant_id': 'user1',
                    'metadata': {'foo': 'bar'}}
-        mimage.return_value.get_image.return_value = image
-        response = self.myget('images/42', 'user')
+        mimage().__enter__().get_image.return_value = image
+        response = self.get(join_urls(IMAGES_URL, "42"), 'user')
         self.assertSuccess(response)
         api_image = json.loads(response.content)['image']
         api_image.pop("links")
         self.assertEqual(api_image, result_image)
 
-    @assert_backend_closed
     def test_invalid_image(self, mimage):
-        mimage.return_value.get_image.side_effect = faults.ItemNotFound('Image not found')
-        response = self.myget('images/42', 'user')
+        mimage().__enter__().get_image.side_effect = \
+            faults.ItemNotFound('Image not found')
+        response = self.get(join_urls(IMAGES_URL, "42"), 'user')
         self.assertItemNotFound(response)
 
     @assert_backend_closed
     def test_delete_image(self, mimage):
-        response = self.mydelete("images/42", "user")
+        response = self.delete(join_urls(IMAGES_URL, "42"), 'user')
         self.assertEqual(response.status_code, 204)
-        mimage.return_value.unregister.assert_called_once_with('42')
-        mimage.return_value._delete.assert_not_called('42')
+        mimage().__enter__().unregister.assert_called_once_with('42')
 
     @assert_backend_closed
     def test_catch_wrong_api_paths(self, *args):
-        response = self.myget('nonexistent')
+        response = self.get(join_urls(IMAGES_URL, 'nonexistent/lala/foo'))
         self.assertEqual(response.status_code, 400)
         try:
             error = json.loads(response.content)
@@ -243,36 +225,31 @@ class ImageAPITest(ComputeAPITest):
     @assert_backend_closed
     def test_method_not_allowed(self, *args):
         # /images/ allows only POST, GET
-        response = self.myput('images', '', '')
+        response = self.put(IMAGES_URL, '', '')
         self.assertMethodNotAllowed(response)
-        response = self.mydelete('images')
+        response = self.delete(IMAGES_URL, '')
         self.assertMethodNotAllowed(response)
 
         # /images/<imgid>/ allows only GET, DELETE
-        response = self.mypost("images/42")
-        self.assertMethodNotAllowed(response)
-        response = self.myput('images/42', '', '')
-        self.assertMethodNotAllowed(response)
-
-        # /images/<imgid>/metadata/ allows only POST, GET
-        response = self.myput('images/42/metadata', '', '')
+        response = self.post(join_urls(IMAGES_URL, "42"), 'user')
         self.assertMethodNotAllowed(response)
-        response = self.mydelete('images/42/metadata')
+        response = self.put(join_urls(IMAGES_URL, "42"), 'user')
         self.assertMethodNotAllowed(response)
 
         # /images/<imgid>/metadata/ allows only POST, GET
-        response = self.myput('images/42/metadata', '', '')
+        response = self.put(join_urls(IMAGES_URL, "42", "metadata"), 'user')
         self.assertMethodNotAllowed(response)
-        response = self.mydelete('images/42/metadata')
+        response = self.delete(join_urls(IMAGES_URL, "42", "metadata"), 'user')
         self.assertMethodNotAllowed(response)
 
         # /images/<imgid>/metadata/<key> allows only PUT, GET, DELETE
-        response = self.mypost('images/42/metadata/foo')
+        response = self.post(join_urls(IMAGES_URL, "42", "metadata", "foo"),
+                             'user')
         self.assertMethodNotAllowed(response)
 
 
-@patch('synnefo.plankton.backend.ImageBackend')
-class ImageMetadataAPITest(ComputeAPITest):
+@patch('synnefo.plankton.backend.PlanktonBackend')
+class ImageMetadataAPITest(BaseAPITest):
     def setUp(self):
         self.image = {'id': 42,
                  'name': 'image-1',
@@ -293,92 +270,94 @@ class ImageMetadataAPITest(ComputeAPITest):
 
     @assert_backend_closed
     def test_list_metadata(self, backend):
-        backend.return_value.get_image.return_value = self.image
-        response = self.myget('images/42/metadata', 'user')
+        backend().__enter__().get_image.return_value = self.image
+        response = self.get(join_urls(IMAGES_URL, '42/metadata'), 'user')
         self.assertSuccess(response)
         meta = json.loads(response.content)['metadata']
         self.assertEqual(meta, self.image['properties'])
 
     @assert_backend_closed
     def test_get_metadata(self, backend):
-        backend.return_value.get_image.return_value = self.image
-        response = self.myget('images/42/metadata/foo', 'user')
+        backend().__enter__().get_image.return_value = self.image
+        response = self.get(join_urls(IMAGES_URL, '42/metadata/foo'), 'user')
         self.assertSuccess(response)
         meta = json.loads(response.content)['meta']
         self.assertEqual(meta['foo'], 'bar')
 
     @assert_backend_closed
     def test_get_invalid_metadata(self, backend):
-        backend.return_value.get_image.return_value = self.image
-        response = self.myget('images/42/metadata/not_found', 'user')
+        backend().__enter__().get_image.return_value = self.image
+        response = self.get(join_urls(IMAGES_URL, '42/metadata/not_found'),
+                            'user')
         self.assertItemNotFound(response)
 
     def test_delete_metadata_item(self, backend):
-        backend.return_value.get_image.return_value = self.image
-        response = self.mydelete('images/42/metadata/foo', 'user')
+        backend().__enter__().get_image.return_value = self.image
+        response = self.delete(join_urls(IMAGES_URL, '42/metadata/foo'),
+                               'user')
         self.assertEqual(response.status_code, 204)
-        backend.return_value.update_metadata.assert_called_once_with('42', {'properties': {'foo2':
-                                                    'bar2'}})
+        backend().__enter__().update_metadata\
+               .assert_called_once_with('42', {'properties': {'foo2': 'bar2'}})
 
     @assert_backend_closed
     def test_create_metadata_item(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'meta': {'foo3': 'bar3'}}
-        response = self.myput('images/42/metadata/foo3', 'user',
-                              json.dumps(request), 'json')
+        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
+                            json.dumps(request), 'json')
         self.assertEqual(response.status_code, 201)
-        backend.return_value.update_metadata.assert_called_once_with('42',
+        backend().__enter__().update_metadata.assert_called_once_with('42',
                 {'properties':
                     {'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}})
 
     @assert_backend_closed
     def test_create_metadata_malformed_1(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'met': {'foo3': 'bar3'}}
-        response = self.myput('images/42/metadata/foo3', 'user',
-                              json.dumps(request), 'json')
+        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
+                            json.dumps(request), 'json')
         self.assertBadRequest(response)
 
     @assert_backend_closed
     def test_create_metadata_malformed_2(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'metadata': [('foo3', 'bar3')]}
-        response = self.myput('images/42/metadata/foo3', 'user',
-                              json.dumps(request), 'json')
+        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
+                            json.dumps(request), 'json')
         self.assertBadRequest(response)
 
     @assert_backend_closed
     def test_create_metadata_malformed_3(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'met': {'foo3': 'bar3', 'foo4': 'bar4'}}
-        response = self.myput('images/42/metadata/foo3', 'user',
-                                json.dumps(request), 'json')
+        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
+                            json.dumps(request), 'json')
         self.assertBadRequest(response)
 
     @assert_backend_closed
     def test_create_metadata_malformed_4(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'met': {'foo3': 'bar3'}}
-        response = self.myput('images/42/metadata/foo4', 'user',
-                              json.dumps(request), 'json')
+        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo4'), 'user',
+                            json.dumps(request), 'json')
         self.assertBadRequest(response)
 
     @assert_backend_closed
     def test_update_metadata_item(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'metadata': {'foo': 'bar_new', 'foo4': 'bar4'}}
-        response = self.mypost('images/42/metadata', 'user',
-                               json.dumps(request), 'json')
+        response = self.post(join_urls(IMAGES_URL, '42/metadata'), 'user',
+                             json.dumps(request), 'json')
         self.assertEqual(response.status_code, 201)
-        backend.return_value.update_metadata.assert_called_once_with('42',
+        backend().__enter__().update_metadata.assert_called_once_with('42',
                 {'properties':
                     {'foo': 'bar_new', 'foo2': 'bar2', 'foo4': 'bar4'}
                 })
 
     @assert_backend_closed
     def test_update_metadata_malformed(self, backend):
-        backend.return_value.get_image.return_value = self.image
+        backend().__enter__().get_image.return_value = self.image
         request = {'meta': {'foo': 'bar_new', 'foo4': 'bar4'}}
-        response = self.mypost('images/42/metadata', 'user',
-                               json.dumps(request), 'json')
+        response = self.post(join_urls(IMAGES_URL, '42/metadata'), 'user',
+                             json.dumps(request), 'json')
         self.assertBadRequest(response)
