Miscellaneous (mostly test) cleanup

* Always use testtools.TestCase, since we're relying on testtools
 * Always use mock (as opposed to unittest.mock) since we're relying on
   mock
 * Add note about when a missing logging handler was added
 * Stop %-formatting the giant usage string that doesn't actually need
   any formatting
 * Prefer assertIs, assertIn, assertIsInstance over assertTrue
 * Use else-self.fail instead of sentinel values
 * Check resp.get('error') is None before checking resp['success'] is
   True, so test failures actually tell you something useful
 * Tighten some isinstance assertions
 * Import MockHttpTest from correct location
 * Only populate clean_os_environ once
 * Use setUp for setup, not __init__
 * Replace assertIn(key, dict) and assertEqual(foo, dict[key]) with
   assertEqual(foo, dict.get(key)) when key is a literal and foo is not
   None
 * Use mock.patch.object instead of manually patching for tests
 * Use six.binary_type instead of type(''.encode('utf-8'))
 * Stop shadowing builtin bytes
 * Reclaim some margin
 * Stop checking the return-type of encode_utf8; we already know it's
   bytes

Change-Id: I2138ea553378ce88810b7353147c8645a8f8c90e
This commit is contained in:
Tim Burke 2015-09-09 17:41:21 -07:00
parent 43b2c6bfe5
commit 9fed7ed5e1
10 changed files with 139 additions and 197 deletions

View File

@ -46,6 +46,7 @@ USER_METADATA_TYPE = tuple('x-%s-meta-' % type_ for type_ in
try:
from logging import NullHandler
except ImportError:
# Added in Python 2.7
class NullHandler(logging.Handler):
def handle(self, record):
pass
@ -110,11 +111,7 @@ def quote(value, safe='/'):
"""
if six.PY3:
return _quote(value, safe=safe)
value = encode_utf8(value)
if isinstance(value, bytes):
return _quote(value, safe)
else:
return value
return _quote(encode_utf8(value), safe)
def encode_utf8(value):

View File

@ -51,7 +51,7 @@ def immediate_exit(signum, frame):
stderr.write(" Aborted\n")
os_exit(2)
st_delete_options = '''[-all] [--leave-segments]
st_delete_options = '''[--all] [--leave-segments]
[--object-threads <threads>]
[--container-threads <threads>]
<container> [object]
@ -1151,7 +1151,7 @@ def main(arguments=None):
version = client_version
parser = OptionParser(version='python-swiftclient %s' % version,
usage='''
usage: %%prog [--version] [--help] [--os-help] [--snet] [--verbose]
usage: %prog [--version] [--help] [--os-help] [--snet] [--verbose]
[--debug] [--info] [--quiet] [--auth <auth_url>]
[--auth-version <auth_version>] [--user <username>]
[--key <api_key>] [--retries <num_retries>]
@ -1191,29 +1191,29 @@ Positional arguments:
auth Display auth related environment variables.
Examples:
%%prog download --help
%prog download --help
%%prog -A https://auth.api.rackspacecloud.com/v1.0 -U user -K api_key stat -v
%prog -A https://auth.api.rackspacecloud.com/v1.0 -U user -K api_key stat -v
%%prog --os-auth-url https://api.example.com/v2.0 --os-tenant-name tenant \\
%prog --os-auth-url https://api.example.com/v2.0 --os-tenant-name tenant \\
--os-username user --os-password password list
%%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\
%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\
--os-project-name project1 --os-project-domain-name domain1 \\
--os-username user --os-user-domain-name domain1 \\
--os-password password list
%%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\
%prog --os-auth-url https://api.example.com/v3 --auth-version 3\\
--os-project-id 0123456789abcdef0123456789abcdef \\
--os-user-id abcdef0123456789abcdef0123456789 \\
--os-password password list
%%prog --os-auth-token 6ee5eb33efad4e45ab46806eac010566 \\
%prog --os-auth-token 6ee5eb33efad4e45ab46806eac010566 \\
--os-storage-url https://10.1.5.2:8080/v1/AUTH_ced809b6a4baea7aeab61a \\
list
%%prog list --lh
'''.strip('\n') % globals())
%prog list --lh
'''.strip('\n'))
parser.add_option('--os-help', action='store_true', dest='os_help',
help='Show OpenStack authentication options.')
parser.add_option('--os_help', action='store_true', help=SUPPRESS_HELP)

View File

@ -35,34 +35,30 @@ def config_true_value(value):
(isinstance(value, six.string_types) and value.lower() in TRUE_VALUES)
def prt_bytes(bytes, human_flag):
def prt_bytes(num_bytes, human_flag):
"""
convert a number > 1024 to printable format, either in 4 char -h format as
with ls -lh or return as 12 char right justified string
"""
if human_flag:
suffix = ''
mods = list('KMGTPEZY')
temp = float(bytes)
if temp > 0:
while temp > 1023:
try:
suffix = mods.pop(0)
except IndexError:
break
temp /= 1024.0
if suffix != '':
if temp >= 10:
bytes = '%3d%s' % (temp, suffix)
else:
bytes = '%.1f%s' % (temp, suffix)
if suffix == '': # must be < 1024
bytes = '%4s' % bytes
else:
bytes = '%12s' % bytes
if not human_flag:
return '%12s' % num_bytes
return bytes
num = float(num_bytes)
suffixes = [None] + list('KMGTPEZY')
for suffix in suffixes[:-1]:
if num <= 1023:
break
num /= 1024.0
else:
suffix = suffixes[-1]
if not suffix: # num_bytes must be < 1024
return '%4s' % num_bytes
elif num >= 10:
return '%3d%s' % (num, suffix)
else:
return '%.1f%s' % (num, suffix)
def generate_temp_url(path, seconds, key, method, absolute=False):

View File

@ -13,11 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
try:
from unittest import mock
except ImportError:
import mock
import mock
from six import StringIO
import testtools

View File

@ -82,14 +82,13 @@ class TestConnectionThreadPoolExecutor(ThreadTestCase):
)
# Now a job that fails
went_boom = False
try:
f = pool.submit(self._func, "go boom")
f.result()
except Exception as e:
went_boom = True
self.assertEqual('I went boom!', str(e))
self.assertTrue(went_boom)
else:
self.fail('I never went boom!')
# Has the connection been returned to the pool?
f = pool.submit(self._func, "succeed")
@ -106,24 +105,22 @@ class TestConnectionThreadPoolExecutor(ThreadTestCase):
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn_fail, 1)
with ctpe as pool:
# Now a connection that fails
connection_failed = False
try:
f = pool.submit(self._func, "succeed")
f.result()
except Exception as e:
connection_failed = True
self.assertEqual('This is a failed connection', str(e))
self.assertTrue(connection_failed)
else:
self.fail('The connection did not fail')
# Make sure we don't lock up on failed connections
connection_failed = False
try:
f = pool.submit(self._func, "go boom")
f.result()
except Exception as e:
connection_failed = True
self.assertEqual('This is a failed connection', str(e))
self.assertTrue(connection_failed)
else:
self.fail('The connection did not fail')
def test_lazy_connections(self):
ctpe = mt.ConnectionThreadPoolExecutor(self._create_conn, 10)

View File

@ -83,7 +83,7 @@ class TestSwiftReader(testtools.TestCase):
self.assertEqual(sr._expected_etag, None)
self.assertNotEqual(sr._actual_md5, None)
self.assertTrue(isinstance(sr._actual_md5, self.md5_type))
self.assertIs(type(sr._actual_md5), self.md5_type)
def test_create_with_large_object_headers(self):
# md5 should not be initialized if large object headers are present
@ -110,7 +110,7 @@ class TestSwiftReader(testtools.TestCase):
self.assertEqual(sr._expected_etag, None)
self.assertNotEqual(sr._actual_md5, None)
self.assertTrue(isinstance(sr._actual_md5, self.md5_type))
self.assertIs(type(sr._actual_md5), self.md5_type)
# Check Contentlength raises error if it isnt an integer
self.assertRaises(SwiftError, self.sr, 'path', 'body',
@ -160,10 +160,10 @@ class _TestServiceBase(testtools.TestCase):
if hasattr(self, 'assertDictEqual'):
self.assertDictEqual(a, b, m)
else:
self.assertTrue(isinstance(a, dict),
'First argument is not a dictionary')
self.assertTrue(isinstance(b, dict),
'Second argument is not a dictionary')
self.assertIsInstance(a, dict,
'First argument is not a dictionary')
self.assertIsInstance(b, dict,
'Second argument is not a dictionary')
self.assertEqual(len(a), len(b), m)
for k, v in a.items():
self.assertIn(k, b, m)
@ -248,7 +248,7 @@ class TestServiceDelete(_TestServiceBase):
self._assertDictEqual(expected_r, self._get_queue(mock_q))
self.assertGreaterEqual(r['error_timestamp'], before)
self.assertLessEqual(r['error_timestamp'], after)
self.assertTrue('Traceback' in r['traceback'])
self.assertIn('Traceback', r['traceback'])
def test_delete_object(self):
mock_q = Queue()
@ -295,7 +295,7 @@ class TestServiceDelete(_TestServiceBase):
self._assertDictEqual(expected_r, r)
self.assertGreaterEqual(r['error_timestamp'], before)
self.assertLessEqual(r['error_timestamp'], after)
self.assertTrue('Traceback' in r['traceback'])
self.assertIn('Traceback', r['traceback'])
def test_delete_object_slo_support(self):
# If SLO headers are present the delete call should include an
@ -395,14 +395,14 @@ class TestServiceDelete(_TestServiceBase):
self._assertDictEqual(expected_r, r)
self.assertGreaterEqual(r['error_timestamp'], before)
self.assertLessEqual(r['error_timestamp'], after)
self.assertTrue('Traceback' in r['traceback'])
self.assertIn('Traceback', r['traceback'])
class TestSwiftError(testtools.TestCase):
def test_is_exception(self):
se = SwiftError(5)
self.assertTrue(isinstance(se, Exception))
self.assertIsInstance(se, Exception)
def test_empty_swifterror_creation(self):
se = SwiftError(5)
@ -444,7 +444,7 @@ class TestServiceUtils(testtools.TestCase):
swiftclient.service.process_options(opt_c)
self.assertTrue('os_options' in opt_c)
self.assertIn('os_options', opt_c)
del opt_c['os_options']
self.assertEqual(opt_c['auth_version'], '2.0')
opt_c['auth_version'] = '1.0'
@ -838,7 +838,8 @@ class TestService(testtools.TestCase):
'c', [SwiftUploadObject(obj['path'])])
responses = [x for x in resp_iter]
for resp in responses:
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(2, len(responses))
create_container_resp, upload_obj_resp = responses
self.assertEqual(create_container_resp['action'],
@ -934,7 +935,7 @@ class TestServiceUpload(_TestServiceBase):
options={'segment_container': None,
'checksum': False})
self.assertNotIn('error', r)
self.assertIsNone(r.get('error'))
self.assertEqual(mock_conn.put_object.call_count, 1)
mock_conn.put_object.assert_called_with('test_c_segments',
'test_s_1',
@ -973,8 +974,7 @@ class TestServiceUpload(_TestServiceBase):
options={'segment_container': None,
'checksum': True})
self.assertIn('error', r)
self.assertIn('md5 mismatch', str(r['error']))
self.assertIn('md5 mismatch', str(r.get('error')))
self.assertEqual(mock_conn.put_object.call_count, 1)
mock_conn.put_object.assert_called_with('test_c_segments',
@ -1128,9 +1128,8 @@ class TestServiceUpload(_TestServiceBase):
'segment_size': 0,
'checksum': True})
self.assertEqual(r['success'], False)
self.assertIn('error', r)
self.assertIn('md5 mismatch', str(r['error']))
self.assertIs(r['success'], False)
self.assertIn('md5 mismatch', str(r.get('error')))
self.assertEqual(mock_conn.put_object.call_count, 1)
expected_headers = {'x-object-meta-mtime': mock.ANY}
@ -1165,9 +1164,9 @@ class TestServiceUpload(_TestServiceBase):
'header': '',
'segment_size': 0})
self.assertTrue(r['success'])
self.assertIn('status', r)
self.assertEqual(r['status'], 'skipped-identical')
self.assertIsNone(r.get('error'))
self.assertIs(True, r['success'])
self.assertEqual(r.get('status'), 'skipped-identical')
self.assertEqual(mock_conn.put_object.call_count, 0)
self.assertEqual(mock_conn.head_object.call_count, 1)
mock_conn.head_object.assert_called_with('test_c', 'test_o')
@ -1208,7 +1207,7 @@ class TestServiceUpload(_TestServiceBase):
'segment_size': 10})
self.assertIsNone(r.get('error'))
self.assertTrue(r['success'])
self.assertIs(True, r['success'])
self.assertEqual('skipped-identical', r.get('status'))
self.assertEqual(0, mock_conn.put_object.call_count)
self.assertEqual([mock.call('test_c', 'test_o')],
@ -1255,7 +1254,7 @@ class TestServiceUpload(_TestServiceBase):
'segment_size': 10})
self.assertIsNone(r.get('error'))
self.assertTrue(r['success'])
self.assertIs(True, r['success'])
self.assertEqual('skipped-identical', r.get('status'))
self.assertEqual(0, mock_conn.put_object.call_count)
self.assertEqual(1, mock_conn.head_object.call_count)
@ -1498,7 +1497,8 @@ class TestServiceDownload(_TestServiceBase):
'test',
self.opts)
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(resp['action'], 'download_object')
self.assertEqual(resp['object'], 'test')
self.assertEqual(resp['path'], 'test')
@ -1517,7 +1517,8 @@ class TestServiceDownload(_TestServiceBase):
'example/test',
options)
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(resp['action'], 'download_object')
self.assertEqual(resp['object'], 'example/test')
self.assertEqual(resp['path'], 'temp_dir/example/test')
@ -1537,7 +1538,8 @@ class TestServiceDownload(_TestServiceBase):
'example/test',
options)
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(resp['action'], 'download_object')
self.assertEqual(resp['object'], 'example/test')
self.assertEqual(resp['path'], 'test')
@ -1557,7 +1559,8 @@ class TestServiceDownload(_TestServiceBase):
'example/test',
options)
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(resp['action'], 'download_object')
self.assertEqual(resp['object'], 'example/test')
self.assertEqual(resp['path'], 'test')
@ -1578,7 +1581,8 @@ class TestServiceDownload(_TestServiceBase):
'example/test',
options)
self.assertTrue(resp['success'])
self.assertIsNone(resp.get('error'))
self.assertIs(True, resp['success'])
self.assertEqual(resp['action'], 'download_object')
self.assertEqual(resp['object'], 'example/test')
self.assertEqual(resp['path'], 'new/dir/test')

View File

@ -19,7 +19,7 @@ import hashlib
import mock
import os
import tempfile
import unittest
import testtools
import textwrap
from testtools import ExpectedException
@ -32,10 +32,9 @@ import swiftclient.shell
import swiftclient.utils
from os.path import basename, dirname
from tests.unit.test_swiftclient import MockHttpTest
from tests.unit.utils import (
from .utils import (
CaptureOutput, fake_get_auth_keystone, _make_fake_import_keystone_client,
FakeKeystone, StubResponse)
FakeKeystone, StubResponse, MockHttpTest)
from swiftclient.utils import EMPTY_ETAG
@ -55,12 +54,6 @@ for key in os.environ:
if any(key.startswith(m) for m in environ_prefixes):
clean_os_environ[key] = ''
clean_os_environ = {}
environ_prefixes = ('ST_', 'OS_')
for key in os.environ:
if any(key.startswith(m) for m in environ_prefixes):
clean_os_environ[key] = ''
def _make_args(cmd, opts, os_opts, separator='-', flags=None, cmd_args=None):
"""
@ -112,9 +105,9 @@ def _make_cmd(cmd, opts, os_opts, use_env=False, flags=None, cmd_args=None):
@mock.patch.dict(os.environ, mocked_os_environ)
class TestShell(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestShell, self).__init__(*args, **kwargs)
class TestShell(testtools.TestCase):
def setUp(self):
super(TestShell, self).setUp()
tmpfile = tempfile.NamedTemporaryFile(delete=False)
self.tmpfile = tmpfile.name
@ -123,6 +116,7 @@ class TestShell(unittest.TestCase):
os.remove(self.tmpfile)
except OSError:
pass
super(TestShell, self).tearDown()
@mock.patch('swiftclient.service.Connection')
def test_stat_account(self, connection):
@ -1024,12 +1018,12 @@ class TestShell(unittest.TestCase):
output.clear()
class TestSubcommandHelp(unittest.TestCase):
class TestSubcommandHelp(testtools.TestCase):
def test_subcommand_help(self):
for command in swiftclient.shell.commands:
help_var = 'st_%s_help' % command
self.assertTrue(help_var in vars(swiftclient.shell))
self.assertTrue(hasattr(swiftclient.shell, help_var))
with CaptureOutput() as out:
argv = ['', command, '--help']
self.assertRaises(SystemExit, swiftclient.shell.main, argv)
@ -1044,7 +1038,7 @@ class TestSubcommandHelp(unittest.TestCase):
self.assertEqual(out.strip('\n'), expected)
class TestBase(unittest.TestCase):
class TestBase(testtools.TestCase):
"""
Provide some common methods to subclasses
"""
@ -1106,7 +1100,7 @@ class TestParsing(TestBase):
'service_type', 'project_id', 'auth_token',
'project_domain_name']
for key in expected_os_opts_keys:
self.assertTrue(key in actual_os_opts_dict)
self.assertIn(key, actual_os_opts_dict)
cli_key = key
if key == 'object_storage_url':
# exceptions to the pattern...
@ -1119,7 +1113,7 @@ class TestParsing(TestBase):
self.assertEqual(expect, actual, 'Expected %s for %s, got %s'
% (expect, key, actual))
for key in actual_os_opts_dict:
self.assertTrue(key in expected_os_opts_keys)
self.assertIn(key, expected_os_opts_keys)
# check that equivalent keys have equal values
equivalents = [('os_username', 'user'),
@ -1446,9 +1440,7 @@ class TestKeystoneOptions(MockHttpTest):
for key in self.all_os_opts.keys():
expected = os_opts.get(key, self.defaults.get(key))
key = key.replace('-', '_')
self.assertTrue(key in actual_args,
'Expected key %s not found in args %s'
% (key, actual_args))
self.assertIn(key, actual_args)
self.assertEqual(expected, actual_args[key],
'Expected %s for key %s, found %s'
% (expected, key, actual_args[key]))
@ -1464,16 +1456,12 @@ class TestKeystoneOptions(MockHttpTest):
key = key.replace('-', '_')
if key == 'region_name':
key = 'filter_value'
self.assertTrue(key in actual_args,
'Expected key %s not found in args %s'
% (key, actual_args))
self.assertIn(key, actual_args)
self.assertEqual(expected, actual_args[key],
'Expected %s for key %s, found %s'
% (expected, key, actual_args[key]))
key, v = 'attr', 'region'
self.assertTrue(key in actual_args,
'Expected key %s not found in args %s'
% (key, actual_args))
self.assertIn(key, actual_args)
self.assertEqual(v, actual_args[key],
'Expected %s for key %s, found %s'
% (v, key, actual_args[key]))

View File

@ -14,18 +14,14 @@
# limitations under the License.
import logging
try:
from unittest import mock
except ImportError:
import mock
import mock
import six
import socket
import testtools
import warnings
import tempfile
from hashlib import md5
from six import binary_type
from six.moves.urllib.parse import urlparse
from .utils import (MockHttpTest, fake_get_auth_keystone, StubResponse,
@ -44,7 +40,7 @@ class TestClientException(testtools.TestCase):
def test_format(self):
exc = c.ClientException('something failed')
self.assertTrue('something failed' in str(exc))
self.assertIn('something failed', str(exc))
test_kwargs = (
'scheme',
'host',
@ -60,7 +56,7 @@ class TestClientException(testtools.TestCase):
'http_%s' % value: value,
}
exc = c.ClientException('test', **kwargs)
self.assertTrue(value in str(exc))
self.assertIn(value, str(exc))
class MockHttpResponse(object):
@ -133,10 +129,10 @@ class TestHttpHelpers(MockHttpTest):
def test_http_connection(self):
url = 'http://www.test.com'
_junk, conn = c.http_connection(url)
self.assertTrue(isinstance(conn, c.HTTPConnection))
self.assertIs(type(conn), c.HTTPConnection)
url = 'https://www.test.com'
_junk, conn = c.http_connection(url)
self.assertTrue(isinstance(conn, c.HTTPConnection))
self.assertIs(type(conn), c.HTTPConnection)
url = 'ftp://www.test.com'
self.assertRaises(c.ClientException, c.http_connection, url)
@ -146,18 +142,16 @@ class TestHttpHelpers(MockHttpTest):
u'x-account-meta-\u0394': '123',
u'x-object-meta-\u0394': '123'}
encoded_str_type = type(''.encode())
r = swiftclient.encode_meta_headers(headers)
self.assertEqual(len(headers), len(r))
# ensure non meta headers are not encoded
self.assertTrue('abc' in r)
self.assertTrue(isinstance(r['abc'], encoded_str_type))
self.assertIs(type(r.get('abc')), binary_type)
del r['abc']
for k, v in r.items():
self.assertTrue(isinstance(k, encoded_str_type))
self.assertTrue(isinstance(v, encoded_str_type))
self.assertIs(type(k), binary_type)
self.assertIs(type(v), binary_type)
def test_set_user_agent_default(self):
_junk, conn = c.http_connection('http://www.example.com')
@ -826,7 +820,7 @@ class TestPutObject(MockHttpTest):
c.http_connection = self.fake_http_connection(200)
args = ('http://www.test.com', 'TOKEN', 'container', 'obj', 'body', 4)
value = c.put_object(*args)
self.assertTrue(isinstance(value, six.string_types))
self.assertIsInstance(value, six.string_types)
self.assertEqual(value, EMPTY_ETAG)
self.assertRequests([
('PUT', '/container/obj', 'body', {
@ -852,7 +846,7 @@ class TestPutObject(MockHttpTest):
conn[1].getresponse = resp.fake_response
conn[1]._request = resp._fake_request
value = c.put_object(*args, headers=headers, http_conn=conn)
self.assertTrue(isinstance(value, six.string_types))
self.assertIsInstance(value, six.string_types)
# Test for RFC-2616 encoded symbols
self.assertIn(("a-b", b".x:yz mn:fg:lp"),
resp.buffer)
@ -921,8 +915,7 @@ class TestPutObject(MockHttpTest):
contents=mock_file, **kwarg)
req_data = resp.requests_params['data']
self.assertTrue(isinstance(req_data,
swiftclient.utils.LengthWrapper))
self.assertIs(type(req_data), swiftclient.utils.LengthWrapper)
self.assertEqual(raw_data_len, len(req_data.read()))
def test_chunk_upload(self):
@ -1691,7 +1684,6 @@ class TestConnection(MockHttpTest):
# check timeout is passed to keystone client
self.assertEqual(1, len(fake_ks.calls))
self.assertTrue('timeout' in fake_ks.calls[0])
self.assertEqual(33.0, fake_ks.calls[0].get('timeout'))
# check timeout passed to HEAD for account
self.assertEqual(timeouts, [33.0])
@ -1761,9 +1753,7 @@ class TestConnection(MockHttpTest):
parsed = urlparse(url)
return parsed, LocalConnection()
orig_conn = c.http_connection
try:
c.http_connection = local_http_connection
with mock.patch.object(c, 'http_connection', local_http_connection):
conn = c.Connection('http://www.example.com', 'asdf', 'asdf',
retries=1, starting_backoff=.0001)
@ -1795,8 +1785,6 @@ class TestConnection(MockHttpTest):
self.assertEqual(contents.seeks, [])
self.assertEqual(str(exc), "put_object('c', 'o', ...) failure "
"and no ability to reset contents for reupload.")
finally:
c.http_connection = orig_conn
class TestResponseDict(MockHttpTest):
@ -1841,10 +1829,8 @@ class TestResponseDict(MockHttpTest):
*call[1:],
response_dict=resp_dict)
self.assertTrue('test' in resp_dict)
self.assertEqual('should be untouched', resp_dict['test'])
self.assertTrue('response_dicts' in resp_dict)
self.assertEqual([{}], resp_dict['response_dicts'])
self.assertEqual('should be untouched', resp_dict.get('test'))
self.assertEqual([{}], resp_dict.get('response_dicts'))
def test_response_dict(self):
# test response_dict is populated and
@ -1858,15 +1844,13 @@ class TestResponseDict(MockHttpTest):
conn = c.Connection('http://127.0.0.1:8080', 'user', 'key')
getattr(conn, call[0])(*call[1:], response_dict=resp_dict)
for key in ('test', 'status', 'headers', 'reason',
'response_dicts'):
self.assertTrue(key in resp_dict)
self.assertEqual('should be untouched', resp_dict.pop('test'))
self.assertEqual('Fake', resp_dict['reason'])
self.assertEqual(200, resp_dict['status'])
self.assertTrue('x-works' in resp_dict['headers'])
self.assertEqual('yes', resp_dict['headers']['x-works'])
children = resp_dict.pop('response_dicts')
self.assertEqual('should be untouched',
resp_dict.pop('test', None))
self.assertEqual('Fake', resp_dict.get('reason'))
self.assertEqual(200, resp_dict.get('status'))
self.assertIn('headers', resp_dict)
self.assertEqual('yes', resp_dict['headers'].get('x-works'))
children = resp_dict.pop('response_dicts', [])
self.assertEqual(1, len(children))
self.assertEqual(resp_dict, children[0])
@ -1883,15 +1867,13 @@ class TestResponseDict(MockHttpTest):
conn = c.Connection('http://127.0.0.1:8080', 'user', 'key')
getattr(conn, call[0])(*call[1:], response_dict=resp_dict)
for key in ('test', 'status', 'headers', 'reason',
'response_dicts'):
self.assertTrue(key in resp_dict)
self.assertEqual('should be untouched', resp_dict.pop('test'))
self.assertEqual('Fake', resp_dict['reason'])
self.assertEqual(200, resp_dict['status'])
self.assertTrue('x-works' in resp_dict['headers'])
self.assertEqual('yes', resp_dict['headers']['x-works'])
children = resp_dict.pop('response_dicts')
self.assertEqual('should be untouched',
resp_dict.pop('test', None))
self.assertEqual('Fake', resp_dict.get('reason'))
self.assertEqual(200, resp_dict.get('status'))
self.assertIn('headers', resp_dict)
self.assertEqual('yes', resp_dict['headers'].get('x-works'))
children = resp_dict.pop('response_dicts', [])
self.assertEqual(2, len(children))
self.assertEqual({'existing': 'response dict'}, children[0])
self.assertEqual(resp_dict, children[1])
@ -1916,7 +1898,7 @@ class TestLogging(MockHttpTest):
c.http_connection = self.fake_http_connection(200)
args = ('http://www.test.com', 'asdf', 'asdf', 'asdf', 'asdf')
value = c.put_object(*args)
self.assertTrue(isinstance(value, six.string_types))
self.assertIsInstance(value, six.string_types)
def test_head_error(self):
c.http_connection = self.fake_http_connection(500)
@ -1966,14 +1948,14 @@ class TestServiceToken(MockHttpTest):
conn = c.Connection('http://www.test.com', 'asdf', 'asdf',
os_options=self.os_options)
self.assertTrue(isinstance(conn, c.Connection))
self.assertIs(type(conn), c.Connection)
conn.get_auth = self.get_auth
conn.get_service_auth = self.get_service_auth
self.assertEqual(conn.attempts, 0)
self.assertEqual(conn.service_token, None)
self.assertTrue(isinstance(conn, c.Connection))
self.assertIs(type(conn), c.Connection)
return conn
def get_auth(self):
@ -2057,8 +2039,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('GET', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/?format=json',
actual['full_path'])
@ -2073,8 +2054,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('HEAD', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com', actual['full_path'])
@ -2089,8 +2069,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('POST', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com', actual['full_path'])
self.assertEqual(conn.attempts, 1)
@ -2104,8 +2083,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('DELETE', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1',
actual['full_path'])
@ -2121,8 +2099,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('GET', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1?format=json',
actual['full_path'])
@ -2137,8 +2114,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('HEAD', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1',
actual['full_path'])
@ -2153,8 +2129,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('POST', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1',
actual['full_path'])
@ -2169,8 +2144,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('PUT', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1',
actual['full_path'])
@ -2185,8 +2159,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('GET', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1/obj1',
actual['full_path'])
@ -2201,8 +2174,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('HEAD', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1/obj1',
actual['full_path'])
@ -2217,8 +2189,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('PUT', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1/obj1',
actual['full_path'])
@ -2233,8 +2204,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('POST', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1/obj1',
actual['full_path'])
@ -2249,8 +2219,7 @@ class TestServiceToken(MockHttpTest):
for actual in self.iter_request_log():
self.assertEqual('DELETE', actual['method'])
actual_hdrs = actual['headers']
self.assertTrue('X-Service-Token' in actual_hdrs)
self.assertEqual('stoken', actual_hdrs['X-Service-Token'])
self.assertEqual('stoken', actual_hdrs.get('X-Service-Token'))
self.assertEqual('token', actual_hdrs['X-Auth-Token'])
self.assertEqual('http://storage_url.com/container1/obj1?a_string',
actual['full_path'])

View File

@ -28,17 +28,13 @@ class TestConfigTrueValue(testtools.TestCase):
for v in u.TRUE_VALUES:
self.assertEqual(v, v.lower())
@mock.patch.object(u, 'TRUE_VALUES', 'hello world'.split())
def test_config_true_value(self):
orig_trues = u.TRUE_VALUES
try:
u.TRUE_VALUES = 'hello world'.split()
for val in 'hello world HELLO WORLD'.split():
self.assertTrue(u.config_true_value(val) is True)
self.assertTrue(u.config_true_value(True) is True)
self.assertTrue(u.config_true_value('foo') is False)
self.assertTrue(u.config_true_value(False) is False)
finally:
u.TRUE_VALUES = orig_trues
for val in 'hello world HELLO WORLD'.split():
self.assertIs(u.config_true_value(val), True)
self.assertIs(u.config_true_value(True), True)
self.assertIs(u.config_true_value('foo'), False)
self.assertIs(u.config_true_value(False), False)
class TestPrtBytes(testtools.TestCase):
@ -192,11 +188,11 @@ class TestReadableToIterable(testtools.TestCase):
# Check creation with a real and noop md5 class
data = u.ReadableToIterable(None, None, md5=True)
self.assertEqual(md5().hexdigest(), data.get_md5sum())
self.assertTrue(isinstance(data.md5sum, type(md5())))
self.assertIs(type(data.md5sum), type(md5()))
data = u.ReadableToIterable(None, None, md5=False)
self.assertEqual('', data.get_md5sum())
self.assertTrue(isinstance(data.md5sum, type(u.NoopMD5())))
self.assertIs(type(data.md5sum), u.NoopMD5)
def test_unicode(self):
# Check no errors are raised if unicode data is feed in.

View File

@ -263,9 +263,8 @@ class MockHttpTest(testtools.TestCase):
conn.resp.status = status
if auth_token:
headers = args[1]
self.assertTrue('X-Auth-Token' in headers)
actual_token = headers.get('X-Auth-Token')
self.assertEqual(auth_token, actual_token)
self.assertEqual(auth_token,
headers.get('X-Auth-Token'))
if query_string:
self.assertTrue(url.endswith('?' + query_string))
if url.endswith('invalid_cert') and not insecure: