~ubuntu-branches/ubuntu/raring/glance/raring-proposed

« back to all changes in this revision

Viewing changes to glance/tests/unit/test_swift_store.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Adam Gandelman, Chuck Short
  • Date: 2012-11-23 10:09:59 UTC
  • mfrom: (1.1.47)
  • Revision ID: package-import@ubuntu.com-20121123100959-a3mt0ziwvee17rkf
Tags: 2013.1~g1-0ubuntu1
[ Adam Gandelman ]
* debian/patches/*: Refreshed for opening of Grizzly.

[ Chuck Short ]
* debian/glance-client.install: Dropped
* New upstream version
* debian/rules: FTBFS if there are missing binaries
* debian/glance-registry.install: Add glance-replicator
* debian/patches/disable-swift-tests.patch: Dropped.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import swiftclient
28
28
 
29
29
from glance.common import exception
30
 
from glance.common import utils
31
30
from glance.openstack.common import cfg
 
31
from glance.openstack.common import uuidutils
32
32
from glance.store import BackendException
33
33
from glance.store.location import get_location_from_uri
34
34
import glance.store.swift
35
35
from glance.tests.unit import base
36
 
from glance.tests import utils
37
36
 
38
37
CONF = cfg.CONF
39
38
 
40
 
FAKE_UUID = utils.generate_uuid
 
39
FAKE_UUID = uuidutils.generate_uuid
41
40
 
42
41
Store = glance.store.swift.Store
43
42
FIVE_KB = (5 * 1024)
60
59
def stub_out_swiftclient(stubs, swift_store_auth_version):
61
60
    fixture_containers = ['glance']
62
61
    fixture_container_headers = {}
63
 
    fixture_headers = {'glance/%s' % FAKE_UUID:
64
 
                {'content-length': FIVE_KB,
65
 
                 'etag': 'c2e5db72bd7fd153f53ede5da5a06de3'}}
 
62
    fixture_headers = {
 
63
        'glance/%s' % FAKE_UUID: {
 
64
            'content-length': FIVE_KB,
 
65
            'etag': 'c2e5db72bd7fd153f53ede5da5a06de3'
 
66
        }
 
67
    }
66
68
    fixture_objects = {'glance/%s' % FAKE_UUID:
67
69
                       StringIO.StringIO("*" * FIVE_KB)}
68
70
 
70
72
        if container not in fixture_containers:
71
73
            msg = "No container %s found" % container
72
74
            raise swiftclient.ClientException(msg,
73
 
                        http_status=httplib.NOT_FOUND)
 
75
                                              http_status=httplib.NOT_FOUND)
74
76
        return fixture_container_headers
75
77
 
76
78
    def fake_put_container(url, token, container, **kwargs):
107
109
            read_len = fixture_object.len
108
110
            if read_len > MAX_SWIFT_OBJECT_SIZE:
109
111
                msg = ('Image size:%d exceeds Swift max:%d' %
110
 
                        (read_len, MAX_SWIFT_OBJECT_SIZE))
 
112
                       (read_len, MAX_SWIFT_OBJECT_SIZE))
111
113
                raise swiftclient.ClientException(
112
114
                        msg, http_status=httplib.REQUEST_ENTITY_TOO_LARGE)
113
115
            fixture_objects[fixture_key] = fixture_object
119
121
            msg = ("Object PUT failed - Object with key %s already exists"
120
122
                   % fixture_key)
121
123
            raise swiftclient.ClientException(msg,
122
 
                        http_status=httplib.CONFLICT)
 
124
                                              http_status=httplib.CONFLICT)
123
125
 
124
126
    def fake_get_object(url, token, container, name, **kwargs):
125
127
        # GET returns the tuple (list of headers, file object)
127
129
        if not fixture_key in fixture_headers:
128
130
            msg = "Object GET failed"
129
131
            raise swiftclient.ClientException(msg,
130
 
                        http_status=httplib.NOT_FOUND)
 
132
                                              http_status=httplib.NOT_FOUND)
131
133
 
132
134
        fixture = fixture_headers[fixture_key]
133
135
        if 'manifest' in fixture:
152
154
        except KeyError:
153
155
            msg = "Object HEAD failed - Object does not exist"
154
156
            raise swiftclient.ClientException(msg,
155
 
                        http_status=httplib.NOT_FOUND)
 
157
                                              http_status=httplib.NOT_FOUND)
156
158
 
157
159
    def fake_delete_object(url, token, container, name, **kwargs):
158
160
        # DELETE returns nothing
160
162
        if fixture_key not in fixture_headers.keys():
161
163
            msg = "Object DELETE failed - Object does not exist"
162
164
            raise swiftclient.ClientException(msg,
163
 
                        http_status=httplib.NOT_FOUND)
 
165
                                              http_status=httplib.NOT_FOUND)
164
166
        else:
165
167
            del fixture_headers[fixture_key]
166
168
            del fixture_objects[fixture_key]
198
200
    stubs.Set(swiftclient.client,
199
201
              'http_connection', fake_http_connection)
200
202
 
201
 
# change if you want to run the
202
 
# swift tests in ubuntu
203
 
swift_ubuntu_test = None
204
203
 
205
204
class SwiftTests(object):
206
205
 
208
207
    def swift_store_user(self):
209
208
        return urllib.quote(CONF.swift_store_user)
210
209
 
211
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
212
210
    def test_get_size(self):
213
211
        """
214
212
        Test that we can get the size of an object in the swift store
219
217
        image_size = self.store.get_size(loc)
220
218
        self.assertEqual(image_size, 5120)
221
219
 
222
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
223
220
    def test_get(self):
224
221
        """Test a "normal" retrieval of an image in chunks"""
225
222
        uri = "swift://%s:key@auth_address/glance/%s" % (
235
232
            data += chunk
236
233
        self.assertEqual(expected_data, data)
237
234
 
238
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
239
235
    def test_get_with_http_auth(self):
240
236
        """
241
237
        Test a retrieval from Swift with an HTTP authurl. This is
243
239
        http:// in the swift_store_auth_address config value
244
240
        """
245
241
        loc = get_location_from_uri("swift+http://%s:key@auth_address/"
246
 
                                    "glance/%s" % (
247
 
                self.swift_store_user, FAKE_UUID))
 
242
                                    "glance/%s" %
 
243
                                    (self.swift_store_user, FAKE_UUID))
248
244
        (image_swift, image_size) = self.store.get(loc)
249
245
        self.assertEqual(image_size, 5120)
250
246
 
255
251
            data += chunk
256
252
        self.assertEqual(expected_data, data)
257
253
 
258
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
259
254
    def test_get_non_existing(self):
260
255
        """
261
256
        Test that trying to retrieve a swift that doesn't exist
267
262
                          self.store.get,
268
263
                          loc)
269
264
 
270
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
271
265
    def test_add(self):
272
266
        """Test that we can add an image via the swift backend"""
273
267
        expected_swift_size = FIVE_KB
274
268
        expected_swift_contents = "*" * expected_swift_size
275
269
        expected_checksum = hashlib.md5(expected_swift_contents).hexdigest()
276
 
        expected_image_id = utils.generate_uuid()
 
270
        expected_image_id = uuidutils.generate_uuid()
277
271
        loc = 'swift+https://%s:key@localhost:8080/glance/%s'
278
272
        expected_location = loc % (self.swift_store_user,
279
273
                                   expected_image_id)
300
294
        self.assertEquals(expected_swift_contents, new_image_contents)
301
295
        self.assertEquals(expected_swift_size, new_image_swift_size)
302
296
 
303
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
304
297
    def test_add_auth_url_variations(self):
305
298
        """
306
299
        Test that we can add an image via the swift backend with
327
320
        }
328
321
 
329
322
        for variation, expected_location in variations.items():
330
 
            image_id = utils.generate_uuid()
 
323
            image_id = uuidutils.generate_uuid()
331
324
            expected_location = expected_location % (
332
325
                self.swift_store_user, image_id)
333
326
            expected_swift_size = FIVE_KB
334
327
            expected_swift_contents = "*" * expected_swift_size
335
328
            expected_checksum = \
336
 
                    hashlib.md5(expected_swift_contents).hexdigest()
 
329
                hashlib.md5(expected_swift_contents).hexdigest()
337
330
 
338
331
            image_swift = StringIO.StringIO(expected_swift_contents)
339
332
 
358
351
            self.assertEquals(expected_swift_contents, new_image_contents)
359
352
            self.assertEquals(expected_swift_size, new_image_swift_size)
360
353
 
361
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
362
354
    def test_add_no_container_no_create(self):
363
355
        """
364
356
        Tests that adding an image with a non-existing container
378
370
        # simply used self.assertRaises here
379
371
        exception_caught = False
380
372
        try:
381
 
            self.store.add(utils.generate_uuid(), image_swift, 0)
 
373
            self.store.add(uuidutils.generate_uuid(), image_swift, 0)
382
374
        except BackendException, e:
383
375
            exception_caught = True
384
376
            self.assertTrue("container noexist does not exist "
386
378
        self.assertTrue(exception_caught)
387
379
        self.assertEquals(SWIFT_PUT_OBJECT_CALLS, 0)
388
380
 
389
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
390
381
    def test_add_no_container_and_create(self):
391
382
        """
392
383
        Tests that adding an image with a non-existing container
395
386
        expected_swift_size = FIVE_KB
396
387
        expected_swift_contents = "*" * expected_swift_size
397
388
        expected_checksum = hashlib.md5(expected_swift_contents).hexdigest()
398
 
        expected_image_id = utils.generate_uuid()
 
389
        expected_image_id = uuidutils.generate_uuid()
399
390
        loc = 'swift+https://%s:key@localhost:8080/noexist/%s'
400
391
        expected_location = loc % (self.swift_store_user,
401
392
                                   expected_image_id)
424
415
        self.assertEquals(expected_swift_contents, new_image_contents)
425
416
        self.assertEquals(expected_swift_size, new_image_swift_size)
426
417
 
427
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
428
418
    def test_add_large_object(self):
429
419
        """
430
420
        Tests that adding a very large image. We simulate the large
435
425
        expected_swift_size = FIVE_KB
436
426
        expected_swift_contents = "*" * expected_swift_size
437
427
        expected_checksum = hashlib.md5(expected_swift_contents).hexdigest()
438
 
        expected_image_id = utils.generate_uuid()
 
428
        expected_image_id = uuidutils.generate_uuid()
439
429
        loc = 'swift+https://%s:key@localhost:8080/glance/%s'
440
430
        expected_location = loc % (self.swift_store_user,
441
431
                                   expected_image_id)
473
463
        self.assertEquals(expected_swift_contents, new_image_contents)
474
464
        self.assertEquals(expected_swift_size, new_image_swift_size)
475
465
 
476
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
477
466
    def test_add_large_object_zero_size(self):
478
467
        """
479
468
        Tests that adding an image to Swift which has both an unknown size and
489
478
        expected_swift_size = FIVE_KB
490
479
        expected_swift_contents = "*" * expected_swift_size
491
480
        expected_checksum = hashlib.md5(expected_swift_contents).hexdigest()
492
 
        expected_image_id = utils.generate_uuid()
 
481
        expected_image_id = uuidutils.generate_uuid()
493
482
        loc = 'swift+https://%s:key@localhost:8080/glance/%s'
494
483
        expected_location = loc % (self.swift_store_user,
495
484
                                   expected_image_id)
534
523
        self.assertEquals(expected_swift_contents, new_image_contents)
535
524
        self.assertEquals(expected_swift_size, new_image_swift_size)
536
525
 
537
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
538
526
    def test_add_already_existing(self):
539
527
        """
540
528
        Tests that adding an image with an existing identifier
557
545
            return False
558
546
        return False
559
547
 
560
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
561
548
    def test_no_user(self):
562
549
        """
563
550
        Tests that options without user disables the add method
564
551
        """
565
552
        self.assertTrue(self._option_required('swift_store_user'))
566
553
 
567
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
568
554
    def test_no_key(self):
569
555
        """
570
556
        Tests that options without key disables the add method
571
557
        """
572
558
        self.assertTrue(self._option_required('swift_store_key'))
573
559
 
574
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
575
560
    def test_no_auth_address(self):
576
561
        """
577
562
        Tests that options without auth address disables the add method
578
563
        """
579
564
        self.assertTrue(self._option_required('swift_store_auth_address'))
580
565
 
581
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
582
566
    def test_delete(self):
583
567
        """
584
568
        Test we can delete an existing image in the swift store
590
574
 
591
575
        self.assertRaises(exception.NotFound, self.store.get, loc)
592
576
 
593
 
    @utils.skip_if(swift_ubuntu_test is None, "Test requires swift")
594
577
    def test_delete_non_existing(self):
595
578
        """
596
579
        Test that trying to delete a swift that doesn't exist
658
641
        self.config(**conf)
659
642
        super(TestStoreAuthV1, self).setUp()
660
643
        self.stubs = stubout.StubOutForTesting()
661
 
        stub_out_swiftclient(self.stubs,
662
 
                                     conf['swift_store_auth_version'])
 
644
        stub_out_swiftclient(self.stubs, conf['swift_store_auth_version'])
663
645
        self.store = Store()
664
646
 
665
647
    def tearDown(self):