Merge "Improve unit tests for glance.common package"
This commit is contained in:
commit
685382d3d8
|
@ -95,3 +95,6 @@ class TestPasteApp(test_utils.BaseTestCase):
|
||||||
expected_middleware = context.UnauthenticatedContextMiddleware
|
expected_middleware = context.UnauthenticatedContextMiddleware
|
||||||
self._do_test_load_paste_app(expected_middleware,
|
self._do_test_load_paste_app(expected_middleware,
|
||||||
paste_config_file=paste_config_file)
|
paste_config_file=paste_config_file)
|
||||||
|
|
||||||
|
def test_get_path_non_exist(self):
|
||||||
|
self.assertRaises(RuntimeError, config._get_deployment_config_file)
|
|
@ -19,6 +19,8 @@ import os
|
||||||
import StringIO
|
import StringIO
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
import webob
|
||||||
|
|
||||||
from glance.common import exception
|
from glance.common import exception
|
||||||
from glance.common import utils
|
from glance.common import utils
|
||||||
from glance.tests import utils as test_utils
|
from glance.tests import utils as test_utils
|
||||||
|
@ -62,6 +64,17 @@ class TestUtils(test_utils.BaseTestCase):
|
||||||
meat = ''.join(chunks)
|
meat = ''.join(chunks)
|
||||||
self.assertEqual(meat, 'aaabbbcccdddeeefffggghhh')
|
self.assertEqual(meat, 'aaabbbcccdddeeefffggghhh')
|
||||||
|
|
||||||
|
def test_cooperative_reader_of_iterator_stop_iteration_err(self):
|
||||||
|
"""Ensure cooperative reader supports iterator backends too"""
|
||||||
|
reader = utils.CooperativeReader([l * 3 for l in ''])
|
||||||
|
chunks = []
|
||||||
|
while True:
|
||||||
|
chunks.append(reader.read(3))
|
||||||
|
if chunks[-1] == '':
|
||||||
|
break
|
||||||
|
meat = ''.join(chunks)
|
||||||
|
self.assertEqual(meat, '')
|
||||||
|
|
||||||
def test_limiting_reader(self):
|
def test_limiting_reader(self):
|
||||||
"""Ensure limiting reader class accesses all bytes of file"""
|
"""Ensure limiting reader class accesses all bytes of file"""
|
||||||
BYTES = 1024
|
BYTES = 1024
|
||||||
|
@ -105,9 +118,75 @@ class TestUtils(test_utils.BaseTestCase):
|
||||||
|
|
||||||
self.assertRaises(exception.ImageSizeLimitExceeded, _consume_all_read)
|
self.assertRaises(exception.ImageSizeLimitExceeded, _consume_all_read)
|
||||||
|
|
||||||
|
def test_bool_from_string(self):
|
||||||
|
actual = utils.bool_from_string('true')
|
||||||
|
self.assertEqual(True, actual)
|
||||||
|
|
||||||
|
actual = utils.bool_from_string(1)
|
||||||
|
self.assertEqual(True, actual)
|
||||||
|
|
||||||
|
def test_get_meta_from_headers(self):
|
||||||
|
resp = webob.Response()
|
||||||
|
resp.headers = {"x-image-meta-*": 'test'}
|
||||||
|
result = utils.get_image_meta_from_headers(resp)
|
||||||
|
self.assertEqual({'*': 'test', 'properties': {}}, result)
|
||||||
|
|
||||||
|
def test_add_features_to_http_headers(self):
|
||||||
|
features_test1 = {'x-image-meta-size': 'test'}
|
||||||
|
url = ("http://glance.example.com/v1/"
|
||||||
|
"images/71c675ab-d94f-49cd-a114-e12490b328d9")
|
||||||
|
headers = {"x-image-meta-uri": url}
|
||||||
|
self.assertRaises(exception.UnsupportedHeaderFeature,
|
||||||
|
utils.add_features_to_http_headers,
|
||||||
|
features_test1, headers)
|
||||||
|
|
||||||
|
def test_image_meta(self):
|
||||||
|
image_meta = {'x-image-meta-size': 'test'}
|
||||||
|
image_meta_properties = {'properties': {'test': "test"}}
|
||||||
|
actual = utils.image_meta_to_http_headers(image_meta)
|
||||||
|
actual_test2 = utils.image_meta_to_http_headers(
|
||||||
|
image_meta_properties)
|
||||||
|
self.assertEqual({'x-image-meta-x-image-meta-size': u'test'}, actual)
|
||||||
|
self.assertEqual({'x-image-meta-property-test': u'test'},
|
||||||
|
actual_test2)
|
||||||
|
|
||||||
|
def test_create_pretty_table(self):
|
||||||
|
class MyPrettyTable(utils.PrettyTable):
|
||||||
|
def __init__(self):
|
||||||
|
self.columns = []
|
||||||
|
|
||||||
|
# Test add column
|
||||||
|
my_pretty_table = MyPrettyTable()
|
||||||
|
my_pretty_table.add_column(1, label='test')
|
||||||
|
# Test make header
|
||||||
|
test_res = my_pretty_table.make_header()
|
||||||
|
self.assertEqual('t\n-', test_res)
|
||||||
|
# Test make row
|
||||||
|
result = my_pretty_table.make_row('t')
|
||||||
|
self.assertEqual("t", result)
|
||||||
|
result = my_pretty_table._clip_and_justify(
|
||||||
|
data='test', width=4, just=1)
|
||||||
|
self.assertEqual("test", result)
|
||||||
|
|
||||||
|
def test_mutating(self):
|
||||||
|
class FakeContext():
|
||||||
|
def __init__(self):
|
||||||
|
self.read_only = False
|
||||||
|
|
||||||
|
class Fake():
|
||||||
|
def __init__(self):
|
||||||
|
self.context = FakeContext()
|
||||||
|
|
||||||
|
def fake_function(req, context):
|
||||||
|
return 'test passed'
|
||||||
|
|
||||||
|
req = webob.Request.blank('/some_request')
|
||||||
|
result = utils.mutating(fake_function)
|
||||||
|
self.assertEqual("test passed", result(req, Fake()))
|
||||||
|
|
||||||
def test_validate_key_cert_key(self):
|
def test_validate_key_cert_key(self):
|
||||||
var_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
var_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||||
'../', 'var'))
|
'../../', 'var'))
|
||||||
keyfile = os.path.join(var_dir, 'privatekey.key')
|
keyfile = os.path.join(var_dir, 'privatekey.key')
|
||||||
certfile = os.path.join(var_dir, 'certificate.crt')
|
certfile = os.path.join(var_dir, 'certificate.crt')
|
||||||
utils.validate_key_cert(keyfile, certfile)
|
utils.validate_key_cert(keyfile, certfile)
|
|
@ -18,6 +18,7 @@
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
import datetime
|
||||||
import eventlet.patcher
|
import eventlet.patcher
|
||||||
import webob
|
import webob
|
||||||
|
|
||||||
|
@ -98,6 +99,28 @@ class ResourceTest(test_utils.BaseTestCase):
|
||||||
|
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_get_action_args_invalid_index(self):
|
||||||
|
env = {'wsgiorg.routing_args': []}
|
||||||
|
expected = {}
|
||||||
|
actual = wsgi.Resource(None, None, None).get_action_args(env)
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_get_action_args_del_controller_error(self):
|
||||||
|
actions = {'format': None,
|
||||||
|
'action': 'update',
|
||||||
|
'id': 12}
|
||||||
|
env = {'wsgiorg.routing_args': [None, actions]}
|
||||||
|
expected = {'action': 'update', 'id': 12}
|
||||||
|
actual = wsgi.Resource(None, None, None).get_action_args(env)
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_get_action_args_del_format_error(self):
|
||||||
|
actions = {'action': 'update', 'id': 12}
|
||||||
|
env = {'wsgiorg.routing_args': [None, actions]}
|
||||||
|
expected = {'action': 'update', 'id': 12}
|
||||||
|
actual = wsgi.Resource(None, None, None).get_action_args(env)
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
def test_dispatch(self):
|
def test_dispatch(self):
|
||||||
class Controller(object):
|
class Controller(object):
|
||||||
def index(self, shirt, pants=None):
|
def index(self, shirt, pants=None):
|
||||||
|
@ -136,6 +159,18 @@ class JSONResponseSerializerTest(test_utils.BaseTestCase):
|
||||||
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_to_json_with_date_format_value(self):
|
||||||
|
fixture = {"date": datetime.datetime(1, 3, 8, 2)}
|
||||||
|
expected = '{"date": "0001-03-08T02:00:00"}'
|
||||||
|
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_to_json_with_more_deep_format(self):
|
||||||
|
fixture = {"is_public": True, "name": [{"name1": "test"}]}
|
||||||
|
expected = '{"is_public": true, "name": [{"name1": "test"}]}'
|
||||||
|
actual = wsgi.JSONResponseSerializer().to_json(fixture)
|
||||||
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
def test_default(self):
|
def test_default(self):
|
||||||
fixture = {"key": "value"}
|
fixture = {"key": "value"}
|
||||||
response = webob.Response()
|
response = webob.Response()
|
||||||
|
@ -200,6 +235,26 @@ class JSONRequestDeserializerTest(test_utils.BaseTestCase):
|
||||||
expected = {"body": {"key": "value"}}
|
expected = {"body": {"key": "value"}}
|
||||||
self.assertEqual(actual, expected)
|
self.assertEqual(actual, expected)
|
||||||
|
|
||||||
|
def test_has_body_has_transfer_encoding(self):
|
||||||
|
request = wsgi.Request.blank('/')
|
||||||
|
request.method = 'POST'
|
||||||
|
request.body = 'fake_body'
|
||||||
|
request.headers['transfer-encoding'] = 0
|
||||||
|
self.assertTrue('transfer-encoding' in request.headers)
|
||||||
|
self.assertTrue(wsgi.JSONRequestDeserializer().has_body(request))
|
||||||
|
|
||||||
|
def test_get_bind_addr_default_value(self):
|
||||||
|
expected = ('0.0.0.0', '123456')
|
||||||
|
actual = wsgi.get_bind_addr(default_port="123456")
|
||||||
|
self.assertEqual(expected, actual)
|
||||||
|
|
||||||
|
|
||||||
|
class ServerTest(test_utils.BaseTestCase):
|
||||||
|
def test_create_pool(self):
|
||||||
|
actual = wsgi.Server(threads=1).create_pool()
|
||||||
|
self.assertTrue(True, isinstance(actual,
|
||||||
|
eventlet.greenpool.GreenPool))
|
||||||
|
|
||||||
|
|
||||||
class TestHelpers(test_utils.BaseTestCase):
|
class TestHelpers(test_utils.BaseTestCase):
|
||||||
|
|
|
@ -18,8 +18,11 @@
|
||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
import testtools
|
||||||
|
|
||||||
from glance.common import config
|
from glance.common import config
|
||||||
from glance.common import exception
|
from glance.common import exception
|
||||||
|
from glance.common import client as test_client
|
||||||
from glance import context
|
from glance import context
|
||||||
from glance.db.sqlalchemy import api as db_api
|
from glance.db.sqlalchemy import api as db_api
|
||||||
from glance.db.sqlalchemy import models as db_models
|
from glance.db.sqlalchemy import models as db_models
|
||||||
|
@ -1061,3 +1064,27 @@ class TestRegistryV1Client(base.IsolatedUnitTest):
|
||||||
"""Tests deleting image members"""
|
"""Tests deleting image members"""
|
||||||
self.client.add_member(UUID2, 'pattieblack')
|
self.client.add_member(UUID2, 'pattieblack')
|
||||||
self.assertTrue(self.client.delete_member(UUID2, 'pattieblack'))
|
self.assertTrue(self.client.delete_member(UUID2, 'pattieblack'))
|
||||||
|
|
||||||
|
|
||||||
|
class TestBaseClient(testtools.TestCase):
|
||||||
|
"""
|
||||||
|
Test proper actions made for both valid and invalid requests
|
||||||
|
against a Registry service
|
||||||
|
"""
|
||||||
|
def test_connect_kwargs_default_values(self):
|
||||||
|
actual = test_client.BaseClient('127.0.0.1').get_connect_kwargs()
|
||||||
|
self.assertEqual({'timeout': None}, actual)
|
||||||
|
|
||||||
|
def test_connect_kwargs(self):
|
||||||
|
base_client = test_client.BaseClient(
|
||||||
|
host='127.0.0.1', port=80, timeout=1, use_ssl=True)
|
||||||
|
actual = base_client.get_connect_kwargs()
|
||||||
|
expected = {'insecure': False,
|
||||||
|
'key_file': None,
|
||||||
|
'ca_file': '/etc/ssl/certs/ca-certificates.crt',
|
||||||
|
'cert_file': None,
|
||||||
|
'timeout': 1}
|
||||||
|
self.assertEqual(expected['insecure'], actual['insecure'])
|
||||||
|
self.assertEqual(expected['key_file'], actual['key_file'])
|
||||||
|
self.assertEqual(expected['cert_file'], actual['cert_file'])
|
||||||
|
self.assertEqual(expected['timeout'], actual['timeout'])
|
||||||
|
|
Loading…
Reference in New Issue