Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / tests / images.py @ 23808592

History | View | Annotate | Download (15.2 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import json
35

    
36
from snf_django.lib.api import faults
37
from snf_django.utils.testing import BaseAPITest
38
from synnefo.lib.services import get_service_path
39
from synnefo.cyclades_settings import cyclades_services
40
from synnefo.lib import join_urls
41

    
42
from mock import patch
43
from functools import wraps
44

    
45
compute_path = get_service_path(cyclades_services, 'compute',
46
                                version='v2.0')
47
IMAGES_URL = join_urls(compute_path, "images/")
48

    
49

    
50
def assert_backend_closed(func):
51
    """Decorator for ensuring that PlanktonBackend is returned to pool."""
52
    @wraps(func)
53
    def wrapper(self, backend):
54
        result = func(self, backend)
55
        if backend.called is True:
56
            backend.return_value.close.asssert_called
57
        return result
58
    return wrapper
59

    
60

    
61
@patch('synnefo.plankton.backend.PlanktonBackend')
62
class ImageAPITest(BaseAPITest):
63
    @assert_backend_closed
64
    def test_create_image(self, mimage):
65
        """Test that create image is not implemented"""
66
        response = self.post(IMAGES_URL, 'user', json.dumps(''), 'json')
67
        self.assertEqual(response.status_code, 501)
68

    
69
    @assert_backend_closed
70
    def test_list_images(self, mimage):
71
        """Test that expected list of images is returned"""
72
        images = [{'id': 1, 'name': 'image-1'},
73
                  {'id': 2, 'name': 'image-2'},
74
                  {'id': 3, 'name': 'image-3'}]
75
        mimage().__enter__().list_images.return_value = images
76
        response = self.get(IMAGES_URL, 'user')
77
        self.assertSuccess(response)
78
        api_images = json.loads(response.content)['images']
79
        self.assertEqual(images, api_images)
80

    
81
    @assert_backend_closed
82
    def test_list_images_detail(self, mimage):
83
        images = [{'id': 1,
84
                   'name': 'image-1',
85
                   'status':'available',
86
                   'created_at': '2012-11-26 11:52:54',
87
                   'updated_at': '2012-12-26 11:52:54',
88
                   'owner': 'user1',
89
                   'deleted_at': '',
90
                   'properties': {'foo':'bar'}},
91
                  {'id': 2,
92
                   'name': 'image-2',
93
                   'status': 'deleted',
94
                   'created_at': '2012-11-26 11:52:54',
95
                   'updated_at': '2012-12-26 11:52:54',
96
                   'owner': 'user1',
97
                   'deleted_at': '2012-12-27 11:52:54',
98
                   'properties': ''},
99
                  {'id': 3,
100
                   'name': 'image-3',
101
                   'status': 'available',
102
                   'created_at': '2012-11-26 11:52:54',
103
                   'deleted_at': '',
104
                   'updated_at': '2012-12-26 11:52:54',
105
                   'owner': 'user1',
106
                   'properties': ''}]
107
        result_images = [
108
                  {'id': 1,
109
                   'name': 'image-1',
110
                   'status':'ACTIVE',
111
                   'progress': 100,
112
                   'created': '2012-11-26T11:52:54+00:00',
113
                   'updated': '2012-12-26T11:52:54+00:00',
114
                   'user_id': 'user1',
115
                   'tenant_id': 'user1',
116
                   'metadata': {'foo':'bar'}},
117
                  {'id': 2,
118
                   'name': 'image-2',
119
                   'status': 'DELETED',
120
                   'progress': 0,
121
                   'user_id': 'user1',
122
                   'tenant_id': 'user1',
123
                   'created': '2012-11-26T11:52:54+00:00',
124
                   'updated': '2012-12-26T11:52:54+00:00',
125
                   'metadata': {}},
126
                  {'id': 3,
127
                   'name': 'image-3',
128
                   'status': 'ACTIVE',
129
                   'progress': 100,
130
                   'user_id': 'user1',
131
                   'tenant_id': 'user1',
132
                   'created': '2012-11-26T11:52:54+00:00',
133
                   'updated': '2012-12-26T11:52:54+00:00',
134
                   'metadata': {}}]
135
        mimage().__enter__().list_images.return_value = images
136
        response = self.get(join_urls(IMAGES_URL, "detail"), 'user')
137
        self.assertSuccess(response)
138
        api_images = json.loads(response.content)['images']
139
        self.assertEqual(len(result_images), len(api_images))
140
        map(lambda image: image.pop("links"), api_images)
141
        self.assertEqual(result_images, api_images)
142

    
143
    @assert_backend_closed
144
    def test_list_images_detail_since(self, mimage):
145
        from datetime import datetime, timedelta
146
        from time import sleep
147
        old_time = datetime.now()
148
        new_time = old_time + timedelta(seconds=0.1)
149
        sleep(0.1)
150
        images = [
151
                  {'id': 1,
152
                   'name': 'image-1',
153
                   'status':'available',
154
                   'progress': 100,
155
                   'created_at': old_time.isoformat(),
156
                   'deleted_at': '',
157
                   'updated_at': old_time.isoformat(),
158
                   'owner': 'user1',
159
                   'properties': ''},
160
                  {'id': 2,
161
                   'name': 'image-2',
162
                   'status': 'deleted',
163
                   'progress': 0,
164
                   'owner': 'user2',
165
                   'created_at': new_time.isoformat(),
166
                   'updated_at': new_time.isoformat(),
167
                   'deleted_at': new_time.isoformat(),
168
                   'properties': ''}]
169
        mimage().__enter__().list_images.return_value = images
170
        response =\
171
            self.get(join_urls(IMAGES_URL, 'detail?changes-since=%sUTC' %
172
                               new_time))
173
        self.assertSuccess(response)
174
        api_images = json.loads(response.content)['images']
175
        self.assertEqual(1, len(api_images))
176

    
177
    @assert_backend_closed
178
    def test_get_image_details(self, mimage):
179
        image = {'id': 42,
180
                 'name': 'image-1',
181
                 'status': 'available',
182
                 'created_at': '2012-11-26 11:52:54',
183
                 'updated_at': '2012-12-26 11:52:54',
184
                 'deleted_at': '',
185
                 'owner': 'user1',
186
                 'properties': {'foo': 'bar'}}
187
        result_image = \
188
                  {'id': 42,
189
                   'name': 'image-1',
190
                   'status': 'ACTIVE',
191
                   'progress': 100,
192
                   'created': '2012-11-26T11:52:54+00:00',
193
                   'updated': '2012-12-26T11:52:54+00:00',
194
                   'user_id': 'user1',
195
                   'tenant_id': 'user1',
196
                   'metadata': {'foo': 'bar'}}
197
        mimage().__enter__().get_image.return_value = image
198
        response = self.get(join_urls(IMAGES_URL, "42"), 'user')
199
        self.assertSuccess(response)
200
        api_image = json.loads(response.content)['image']
201
        api_image.pop("links")
202
        self.assertEqual(api_image, result_image)
203

    
204
    def test_invalid_image(self, mimage):
205
        mimage().__enter__().get_image.side_effect = \
206
            faults.ItemNotFound('Image not found')
207
        response = self.get(join_urls(IMAGES_URL, "42"), 'user')
208
        self.assertItemNotFound(response)
209

    
210
    @assert_backend_closed
211
    def test_delete_image(self, mimage):
212
        response = self.delete(join_urls(IMAGES_URL, "42"), 'user')
213
        self.assertEqual(response.status_code, 204)
214
        mimage().__enter__().unregister.assert_called_once_with('42')
215

    
216
    @assert_backend_closed
217
    def test_catch_wrong_api_paths(self, *args):
218
        response = self.get(join_urls(IMAGES_URL, 'nonexistent/lala/foo'))
219
        self.assertEqual(response.status_code, 400)
220
        try:
221
            error = json.loads(response.content)
222
        except ValueError:
223
            self.assertTrue(False)
224

    
225
    @assert_backend_closed
226
    def test_method_not_allowed(self, *args):
227
        # /images/ allows only POST, GET
228
        response = self.put(IMAGES_URL, '', '')
229
        self.assertMethodNotAllowed(response)
230
        response = self.delete(IMAGES_URL, '')
231
        self.assertMethodNotAllowed(response)
232

    
233
        # /images/<imgid>/ allows only GET, DELETE
234
        response = self.post(join_urls(IMAGES_URL, "42"), 'user')
235
        self.assertMethodNotAllowed(response)
236
        response = self.put(join_urls(IMAGES_URL, "42"), 'user')
237
        self.assertMethodNotAllowed(response)
238

    
239
        # /images/<imgid>/metadata/ allows only POST, GET
240
        response = self.put(join_urls(IMAGES_URL, "42", "metadata"), 'user')
241
        self.assertMethodNotAllowed(response)
242
        response = self.delete(join_urls(IMAGES_URL, "42", "metadata"), 'user')
243
        self.assertMethodNotAllowed(response)
244

    
245
        # /images/<imgid>/metadata/<key> allows only PUT, GET, DELETE
246
        response = self.post(join_urls(IMAGES_URL, "42", "metadata", "foo"),
247
                             'user')
248
        self.assertMethodNotAllowed(response)
249

    
250

    
251
@patch('synnefo.plankton.backend.PlanktonBackend')
252
class ImageMetadataAPITest(BaseAPITest):
253
    def setUp(self):
254
        self.image = {'id': 42,
255
                 'name': 'image-1',
256
                 'status': 'available',
257
                 'created_at': '2012-11-26 11:52:54',
258
                 'updated_at': '2012-12-26 11:52:54',
259
                 'deleted_at': '',
260
                 'properties': {'foo': 'bar', 'foo2': 'bar2'}}
261
        self.result_image = \
262
                  {'id': 42,
263
                   'name': 'image-1',
264
                   'status': 'ACTIVE',
265
                   'progress': 100,
266
                   'created': '2012-11-26T11:52:54+00:00',
267
                   'updated': '2012-12-26T11:52:54+00:00',
268
                   'metadata': {'foo': 'bar'}}
269
        super(ImageMetadataAPITest, self).setUp()
270

    
271
    @assert_backend_closed
272
    def test_list_metadata(self, backend):
273
        backend().__enter__().get_image.return_value = self.image
274
        response = self.get(join_urls(IMAGES_URL, '42/metadata'), 'user')
275
        self.assertSuccess(response)
276
        meta = json.loads(response.content)['metadata']
277
        self.assertEqual(meta, self.image['properties'])
278

    
279
    @assert_backend_closed
280
    def test_get_metadata(self, backend):
281
        backend().__enter__().get_image.return_value = self.image
282
        response = self.get(join_urls(IMAGES_URL, '42/metadata/foo'), 'user')
283
        self.assertSuccess(response)
284
        meta = json.loads(response.content)['meta']
285
        self.assertEqual(meta['foo'], 'bar')
286

    
287
    @assert_backend_closed
288
    def test_get_invalid_metadata(self, backend):
289
        backend().__enter__().get_image.return_value = self.image
290
        response = self.get(join_urls(IMAGES_URL, '42/metadata/not_found'),
291
                            'user')
292
        self.assertItemNotFound(response)
293

    
294
    def test_delete_metadata_item(self, backend):
295
        backend().__enter__().get_image.return_value = self.image
296
        response = self.delete(join_urls(IMAGES_URL, '42/metadata/foo'),
297
                               'user')
298
        self.assertEqual(response.status_code, 204)
299
        backend().__enter__().update_metadata\
300
               .assert_called_once_with('42', {'properties': {'foo2': 'bar2'}})
301

    
302
    @assert_backend_closed
303
    def test_create_metadata_item(self, backend):
304
        backend().__enter__().get_image.return_value = self.image
305
        request = {'meta': {'foo3': 'bar3'}}
306
        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
307
                            json.dumps(request), 'json')
308
        self.assertEqual(response.status_code, 201)
309
        backend().__enter__().update_metadata.assert_called_once_with('42',
310
                {'properties':
311
                    {'foo': 'bar', 'foo2': 'bar2', 'foo3': 'bar3'}})
312

    
313
    @assert_backend_closed
314
    def test_create_metadata_malformed_1(self, backend):
315
        backend().__enter__().get_image.return_value = self.image
316
        request = {'met': {'foo3': 'bar3'}}
317
        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
318
                            json.dumps(request), 'json')
319
        self.assertBadRequest(response)
320

    
321
    @assert_backend_closed
322
    def test_create_metadata_malformed_2(self, backend):
323
        backend().__enter__().get_image.return_value = self.image
324
        request = {'metadata': [('foo3', 'bar3')]}
325
        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
326
                            json.dumps(request), 'json')
327
        self.assertBadRequest(response)
328

    
329
    @assert_backend_closed
330
    def test_create_metadata_malformed_3(self, backend):
331
        backend().__enter__().get_image.return_value = self.image
332
        request = {'met': {'foo3': 'bar3', 'foo4': 'bar4'}}
333
        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo3'), 'user',
334
                            json.dumps(request), 'json')
335
        self.assertBadRequest(response)
336

    
337
    @assert_backend_closed
338
    def test_create_metadata_malformed_4(self, backend):
339
        backend().__enter__().get_image.return_value = self.image
340
        request = {'met': {'foo3': 'bar3'}}
341
        response = self.put(join_urls(IMAGES_URL, '42/metadata/foo4'), 'user',
342
                            json.dumps(request), 'json')
343
        self.assertBadRequest(response)
344

    
345
    @assert_backend_closed
346
    def test_update_metadata_item(self, backend):
347
        backend().__enter__().get_image.return_value = self.image
348
        request = {'metadata': {'foo': 'bar_new', 'foo4': 'bar4'}}
349
        response = self.post(join_urls(IMAGES_URL, '42/metadata'), 'user',
350
                             json.dumps(request), 'json')
351
        self.assertEqual(response.status_code, 201)
352
        backend().__enter__().update_metadata.assert_called_once_with('42',
353
                {'properties':
354
                    {'foo': 'bar_new', 'foo2': 'bar2', 'foo4': 'bar4'}
355
                })
356

    
357
    @assert_backend_closed
358
    def test_update_metadata_malformed(self, backend):
359
        backend().__enter__().get_image.return_value = self.image
360
        request = {'meta': {'foo': 'bar_new', 'foo4': 'bar4'}}
361
        response = self.post(join_urls(IMAGES_URL, '42/metadata'), 'user',
362
                             json.dumps(request), 'json')
363
        self.assertBadRequest(response)