Fixes up Bucket to throw proper NotFound and NotEmpty exceptions
in constructor and delete() method, and fixes up objectstore_unittest to properly use assertRaises() to check for proper exceptions and remove the assert_ calls.
This commit is contained in:
@@ -44,6 +44,9 @@ class Duplicate(Error):
|
||||
class NotAuthorized(Error):
|
||||
pass
|
||||
|
||||
class NotEmpty(Error):
|
||||
pass
|
||||
|
||||
def wrap_exception(f):
|
||||
def _wrap(*args, **kw):
|
||||
try:
|
||||
|
@@ -107,7 +107,7 @@ class Bucket(object):
|
||||
try:
|
||||
return context.user.is_admin() or self.owner_id == context.project.id
|
||||
except Exception, e:
|
||||
pass
|
||||
return False
|
||||
|
||||
def list_keys(self, prefix='', marker=None, max_keys=1000, terse=False):
|
||||
object_names = []
|
||||
@@ -161,7 +161,7 @@ class Bucket(object):
|
||||
|
||||
def delete(self):
|
||||
if len(os.listdir(self.path)) > 0:
|
||||
raise exception.NotAuthorized()
|
||||
raise exception.NotEmpty()
|
||||
os.rmdir(self.path)
|
||||
os.remove(self.path+'.json')
|
||||
|
||||
|
@@ -23,6 +23,7 @@ import os
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
from nova.exception import NotEmpty, NotFound, NotAuthorized
|
||||
from nova import flags
|
||||
from nova import objectstore
|
||||
from nova import test
|
||||
@@ -96,49 +97,37 @@ class ObjectStoreTestCase(test.BaseTestCase):
|
||||
# another user is not authorized
|
||||
self.context.user = self.um.get_user('user2')
|
||||
self.context.project = self.um.get_project('proj2')
|
||||
self.assert_(bucket.is_authorized(self.context) == False)
|
||||
self.assertFalse(bucket.is_authorized(self.context))
|
||||
|
||||
# admin is authorized to use bucket
|
||||
self.context.user = self.um.get_user('admin_user')
|
||||
self.context.project = None
|
||||
self.assert_(bucket.is_authorized(self.context))
|
||||
self.assertTrue(bucket.is_authorized(self.context))
|
||||
|
||||
# new buckets are empty
|
||||
self.assert_(bucket.list_keys()['Contents'] == [])
|
||||
self.assertTrue(bucket.list_keys()['Contents'] == [])
|
||||
|
||||
# storing keys works
|
||||
bucket['foo'] = "bar"
|
||||
|
||||
self.assert_(len(bucket.list_keys()['Contents']) == 1)
|
||||
self.assertEquals(len(bucket.list_keys()['Contents']), 1)
|
||||
|
||||
self.assert_(bucket['foo'].read() == 'bar')
|
||||
self.assertEquals(bucket['foo'].read(), 'bar')
|
||||
|
||||
# md5 of key works
|
||||
self.assert_(bucket['foo'].md5 == hashlib.md5('bar').hexdigest())
|
||||
self.assertEquals(bucket['foo'].md5, hashlib.md5('bar').hexdigest())
|
||||
|
||||
# deleting non-empty bucket throws exception
|
||||
exception = False
|
||||
try:
|
||||
bucket.delete()
|
||||
except:
|
||||
exception = True
|
||||
|
||||
self.assert_(exception)
|
||||
# deleting non-empty bucket should throw a NotEmpty exception
|
||||
self.assertRaises(NotEmpty, bucket.delete)
|
||||
|
||||
# deleting key
|
||||
del bucket['foo']
|
||||
|
||||
# deleting empty button
|
||||
# deleting empty bucket
|
||||
bucket.delete()
|
||||
|
||||
# accessing deleted bucket throws exception
|
||||
exception = False
|
||||
try:
|
||||
objectstore.bucket.Bucket('new_bucket')
|
||||
except:
|
||||
exception = True
|
||||
|
||||
self.assert_(exception)
|
||||
self.assertRaises(NotFound, objectstore.bucket.Bucket, 'new_bucket')
|
||||
|
||||
def test_images(self):
|
||||
self.context.user = self.um.get_user('user1')
|
||||
@@ -167,7 +156,7 @@ class ObjectStoreTestCase(test.BaseTestCase):
|
||||
# verify image permissions
|
||||
self.context.user = self.um.get_user('user2')
|
||||
self.context.project = self.um.get_project('proj2')
|
||||
self.assert_(my_img.is_authorized(self.context) == False)
|
||||
self.assertFalse(my_img.is_authorized(self.context))
|
||||
|
||||
# class ApiObjectStoreTestCase(test.BaseTestCase):
|
||||
# def setUp(self):
|
||||
|
Reference in New Issue
Block a user