Commit 975270f2 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Apply uniform coding styl in unittests

parent 402b6f48
......@@ -130,7 +130,7 @@ class PithosClient(PithosRestAPI):
raise ClientError(msg, 1)
f = StringIO(data)
else:
data = f.read(size or ())
data = f.read(size) if size else f.read()
r = self.object_put(
obj,
data=data,
......
......@@ -56,17 +56,20 @@ example0 = dict(
uuid='an07h2r-us3r-uu1d-f0r-as7ak0s')
class Astakos(TestCase):
class FR(object):
json = example
headers = {}
content = json
status = None
status_code = 200
def release(self):
pass
khttp = 'kamaki.clients.connection.kamakicon.KamakiHTTPConnection'
class FR(object):
json = example
headers = {}
content = json
status = None
status_code = 200
def release(self):
pass
class Astakos(TestCase):
cached = False
......@@ -74,12 +77,13 @@ class Astakos(TestCase):
self.url = 'https://astakos.example.com'
self.token = 'ast@k0sT0k3n=='
self.client = AstakosClient(self.url, self.token)
from kamaki.clients.connection.kamakicon import KamakiHTTPConnection
self.C = KamakiHTTPConnection
def _authenticate(self):
with patch.object(self.C, 'perform_request', return_value=self.FR()):
r = self.client.authenticate()
def tearDown(self):
FR.json = example
@patch('%s.perform_request' % khttp, return_value=FR())
def _authenticate(self, PR):
r = self.client.authenticate()
self.cached = True
return r
......@@ -109,8 +113,8 @@ class Astakos(TestCase):
def test_list(self):
if not self.cached:
self._authenticate
with patch.object(self.FR, 'json', return_value=example0):
self._authenticate()
FR.json = example0
self._authenticate()
r = self.client.list()
self.assertTrue(len(r) == 1)
self.assertEqual(r[0]['auth_token'], self.token)
This diff is collapsed.
......@@ -126,6 +126,19 @@ example_images_detailed = [
"size": 2589802496}]
class FR(object):
json = example_images
headers = {}
content = json
status = None
status_code = 200
def release(self):
pass
khttp = 'kamaki.clients.connection.kamakicon.KamakiHTTPConnection'
class Image(TestCase):
def assert_dicts_are_deeply_equal(self, d1, d2):
......@@ -136,16 +149,6 @@ class Image(TestCase):
else:
self.assertEqual(unicode(v), unicode(d2[k]))
class FR(object):
json = example_images
headers = {}
content = json
status = None
status_code = 200
def release(self):
pass
def setUp(self):
self.url = 'http://image.example.com'
self.token = 'an1m@g370k3n=='
......@@ -155,177 +158,168 @@ class Image(TestCase):
self.C = KamakiHTTPConnection
def tearDown(self):
self.FR.json = example_images
self.FR.status_code = 200
FR.json = example_images
FR.status_code = 200
def test_list_public(self):
with patch.object(
self.C,
'perform_request',
return_value=self.FR()) as perform_req:
r = self.client.list_public()
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
params = perform_req.call_args[0][3]
self.assertEqual(params['sort_dir'], 'asc')
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(r[i], example_images[i])
@patch('%s.perform_request' % khttp, return_value=FR())
def test_list_public(self, PR):
r = self.client.list_public()
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
params = PR.call_args[0][3]
self.assertEqual(params['sort_dir'], 'asc')
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(r[i], example_images[i])
r = self.client.list_public(order='-')
params = perform_req.call_args[0][3]
self.assertEqual(params['sort_dir'], 'desc')
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
r = self.client.list_public(order='-')
params = PR.call_args[0][3]
self.assertEqual(params['sort_dir'], 'desc')
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
self.FR.json = example_images_detailed
r = self.client.list_public(detail=True)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/detail')
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(
r[i],
example_images_detailed[i])
FR.json = example_images_detailed
r = self.client.list_public(detail=True)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/detail')
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(
r[i],
example_images_detailed[i])
size_max = 1000000000
r = self.client.list_public(filters=dict(size_max=size_max))
params = perform_req.call_args[0][3]
self.assertEqual(params['size_max'], size_max)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
size_max = 1000000000
r = self.client.list_public(filters=dict(size_max=size_max))
params = PR.call_args[0][3]
self.assertEqual(params['size_max'], size_max)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
def test_get_meta(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_get_meta(self, PR):
img0 = example_images[0]
self.FR.json = img0
FR.json = img0
img0_headers = {}
for k, v in example_images_detailed[0].items():
img0_headers['x-image-meta-%s' % k] = v
self.FR.headers = img0_headers
with patch.object(self.C, 'perform_request', return_value=self.FR()):
r = self.client.get_meta(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
expected_path = '/images/%s' % img0['id']
self.assertEqual(self.client.http_client.path, expected_path)
FR.headers = img0_headers
r = self.client.get_meta(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
expected_path = '/images/%s' % img0['id']
self.assertEqual(self.client.http_client.path, expected_path)
self.assertEqual(r['id'], img0['id'])
self.assert_dicts_are_deeply_equal(r, example_images_detailed[0])
self.assertEqual(r['id'], img0['id'])
self.assert_dicts_are_deeply_equal(r, example_images_detailed[0])
def test_register(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_register(self, PR):
img0 = example_images_detailed[0]
img0_location = img0['location']
img0_name = 'A new img0 name'
with patch.object(
self.C,
'perform_request',
return_value=self.FR()) as perform_req:
self.client.register(img0_name, img0_location)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
(method, data, headers, params) = perform_req.call_args[0]
self.assertEqual(method, 'post')
self.assertTrue(0 == len(params))
self.client.register(img0_name, img0_location)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(self.client.http_client.path, '/images/')
(method, data, headers, params) = PR.call_args[0]
self.assertEqual(method, 'post')
self.assertTrue(0 == len(params))
val = 'Some random value'
param_dict = dict(
id=val,
store=val,
disk_format=val,
container_format=val,
size=val,
checksum=val,
is_public=val,
owner=val)
for key in param_dict.keys():
param = {key: val}
self.client.register(img0_name, img0_location, params=param)
(method, data, a_headers, a_params) = perform_req.call_args[0]
key = 'x-image-meta-%s' % key.replace('_', '-')
self.assertEqual(a_headers[key], val)
self.client.register(img0_name, img0_location, params=param_dict)
(method, data, a_headers, a_params) = perform_req.call_args[0]
self.assertEqual(len(param_dict), len(a_headers))
for key, val in param_dict.items():
key = 'x-image-meta-%s' % key.replace('_', '-')
self.assertEqual(a_headers[key], val)
val = 'Some random value'
param_dict = dict(
id=val,
store=val,
disk_format=val,
container_format=val,
size=val,
checksum=val,
is_public=val,
owner=val)
for key in param_dict.keys():
param = {key: val}
self.client.register(img0_name, img0_location, params=param)
(method, data, a_headers, a_params) = PR.call_args[0]
key = 'x-image-meta-%s' % key.replace('_', '-')
self.assertEqual(a_headers[key], val)
self.client.register(img0_name, img0_location, params=param_dict)
(method, data, a_headers, a_params) = PR.call_args[0]
self.assertEqual(len(param_dict), len(a_headers))
for key, val in param_dict.items():
key = 'x-image-meta-%s' % key.replace('_', '-')
self.assertEqual(a_headers[key], val)
props = dict(key0='val0', key2='val2', key3='val3')
self.client.register(img0_name, img0_location, properties=props)
(method, data, a_headers, a_params) = perform_req.call_args[0]
for k, v in props.items():
self.assertEquals(a_headers['X-Image-Meta-Property-%s' % k], v)
props = dict(key0='val0', key2='val2', key3='val3')
self.client.register(img0_name, img0_location, properties=props)
(method, data, a_headers, a_params) = PR.call_args[0]
for k, v in props.items():
self.assertEquals(a_headers['X-Image-Meta-Property-%s' % k], v)
def test_set_members(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_set_members(self, PR):
img0 = example_images_detailed[0]
members = ['use3r-1d-0', 'us2r-1d-1', 'us3r-1d-2']
with patch.object(
self.C,
'perform_request',
return_value=self.FR()) as perform_req:
self.assertRaises(
ClientError,
self.client.set_members,
img0['id'], members)
self.FR.status_code = 204
self.client.set_members(img0['id'], members)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members' % img0['id'])
(method, data, a_headers, a_params) = perform_req.call_args[0]
from json import loads
memberships = loads(data)['memberships']
for membership in memberships:
self.assertTrue(membership['member_id'] in members)
self.assertRaises(
ClientError,
self.client.set_members,
img0['id'], members)
FR.status_code = 204
self.client.set_members(img0['id'], members)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members' % img0['id'])
(method, data, a_headers, a_params) = PR.call_args[0]
from json import loads
memberships = loads(data)['memberships']
for membership in memberships:
self.assertTrue(membership['member_id'] in members)
def test_list_members(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_list_members(self, PR):
img0 = example_images_detailed[0]
members = ['use3r-1d-0', 'us2r-1d-1', 'us3r-1d-2']
self.FR.json = dict(members=members)
with patch.object(self.C, 'perform_request', return_value=self.FR()):
r = self.client.list_members(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members' % img0['id'])
self.assertEqual(r, members)
FR.json = dict(members=members)
r = self.client.list_members(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members' % img0['id'])
self.assertEqual(r, members)
def test_add_member(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_add_member(self, PR):
img0 = example_images_detailed[0]
new_member = 'us3r-15-n3w'
with patch.object(self.C, 'perform_request', return_value=self.FR()):
self.assertRaises(
ClientError,
self.client.add_member,
img0['id'], new_member)
self.FR.status_code = 204
self.client.add_member(img0['id'], new_member)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members/%s' % (img0['id'], new_member))
self.assertRaises(
ClientError,
self.client.add_member,
img0['id'], new_member)
FR.status_code = 204
self.client.add_member(img0['id'], new_member)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members/%s' % (img0['id'], new_member))
def test_remove_member(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_remove_member(self, PR):
img0 = example_images_detailed[0]
old_member = 'us3r-15-0ld'
with patch.object(self.C, 'perform_request', return_value=self.FR()):
self.assertRaises(
ClientError,
self.client.remove_member,
img0['id'], old_member)
self.FR.status_code = 204
self.client.remove_member(img0['id'], old_member)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members/%s' % (img0['id'], old_member))
self.assertRaises(
ClientError,
self.client.remove_member,
img0['id'], old_member)
FR.status_code = 204
self.client.remove_member(img0['id'], old_member)
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/images/%s/members/%s' % (img0['id'], old_member))
def test_list_shared(self):
@patch('%s.perform_request' % khttp, return_value=FR())
def test_list_shared(self, PR):
img0 = example_images_detailed[0]
self.FR.json = dict(shared_images=example_images)
with patch.object(self.C, 'perform_request', return_value=self.FR()):
r = self.client.list_shared(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/shared-images/%s' % img0['id'])
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(r[i], example_images[i])
FR.json = dict(shared_images=example_images)
r = self.client.list_shared(img0['id'])
self.assertEqual(self.client.http_client.url, self.url)
self.assertEqual(
self.client.http_client.path,
'/shared-images/%s' % img0['id'])
for i in range(len(r)):
self.assert_dicts_are_deeply_equal(r[i], example_images[i])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment