diff --git a/test-requirements.txt b/test-requirements.txt index 7f7e405f..044f7c3f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,10 +1,8 @@ hacking>=0.10.0,<0.11 coverage>=3.6 -discover mock>=1.2 oslosphinx python-keystoneclient>=0.7.0 sphinx>=1.1.2,<1.2 testrepository>=0.0.18 -testtools>=0.9.34 diff --git a/tests/functional/test_swiftclient.py b/tests/functional/test_swiftclient.py index 5f9e271f..7a77c071 100644 --- a/tests/functional/test_swiftclient.py +++ b/tests/functional/test_swiftclient.py @@ -14,7 +14,7 @@ # limitations under the License. import os -import testtools +import unittest import time from io import BytesIO @@ -23,7 +23,7 @@ from six.moves import configparser import swiftclient -class TestFunctional(testtools.TestCase): +class TestFunctional(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestFunctional, self).__init__(*args, **kwargs) diff --git a/tests/unit/test_command_helpers.py b/tests/unit/test_command_helpers.py index d9d7efa6..24684ae2 100644 --- a/tests/unit/test_command_helpers.py +++ b/tests/unit/test_command_helpers.py @@ -15,13 +15,13 @@ import mock from six import StringIO -import testtools +import unittest from swiftclient import command_helpers as h from swiftclient.multithreading import OutputManager -class TestStatHelpers(testtools.TestCase): +class TestStatHelpers(unittest.TestCase): def setUp(self): super(TestStatHelpers, self).setUp() diff --git a/tests/unit/test_multithreading.py b/tests/unit/test_multithreading.py index 76758b69..8944d48e 100644 --- a/tests/unit/test_multithreading.py +++ b/tests/unit/test_multithreading.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import sys -import testtools +import unittest import threading import six @@ -25,7 +25,7 @@ from swiftclient import multithreading as mt from .utils import CaptureStream -class ThreadTestCase(testtools.TestCase): +class ThreadTestCase(unittest.TestCase): def setUp(self): super(ThreadTestCase, self).setUp() self.got_items = Queue() @@ -163,7 +163,7 @@ class TestConnectionThreadPoolExecutor(ThreadTestCase): ) -class TestOutputManager(testtools.TestCase): +class TestOutputManager(unittest.TestCase): def test_instantiation(self): output_manager = mt.OutputManager() diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py index 3fbe987a..997d992d 100644 --- a/tests/unit/test_service.py +++ b/tests/unit/test_service.py @@ -18,7 +18,7 @@ import mock import os import six import tempfile -import testtools +import unittest import time from concurrent.futures import Future @@ -49,7 +49,7 @@ else: import builtins -class TestSwiftPostObject(testtools.TestCase): +class TestSwiftPostObject(unittest.TestCase): def setUp(self): super(TestSwiftPostObject, self).setUp() @@ -69,7 +69,7 @@ class TestSwiftPostObject(testtools.TestCase): self.assertRaises(SwiftError, self.spo, 1) -class TestSwiftReader(testtools.TestCase): +class TestSwiftReader(unittest.TestCase): def setUp(self): super(TestSwiftReader, self).setUp() @@ -152,25 +152,7 @@ class TestSwiftReader(testtools.TestCase): '97ac82a5b825239e782d0339e2d7b910') -class _TestServiceBase(testtools.TestCase): - def _assertDictEqual(self, a, b, m=None): - # assertDictEqual is not available in py2.6 so use a shallow check - # instead - if not m: - m = '{0} != {1}'.format(a, b) - - if hasattr(self, 'assertDictEqual'): - self.assertDictEqual(a, b, m) - else: - 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) - self.assertEqual(b[k], v, m) - +class _TestServiceBase(unittest.TestCase): def _get_mock_connection(self, attempts=2): m = Mock(spec=Connection) type(m).attempts = PropertyMock(return_value=attempts) @@ -223,8 +205,8 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_object.assert_called_once_with( 'test_c', 'test_s', response_dict={} ) - self._assertDictEqual(expected_r, r) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, r) + self.assertEqual(expected_r, self._get_queue(mock_q)) def test_delete_segment_exception(self): mock_q = Queue() @@ -246,8 +228,8 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_object.assert_called_once_with( 'test_c', 'test_s', response_dict={} ) - self._assertDictEqual(expected_r, r) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, r) + self.assertEqual(expected_r, self._get_queue(mock_q)) self.assertGreaterEqual(r['error_timestamp'], before) self.assertLessEqual(r['error_timestamp'], after) self.assertIn('Traceback', r['traceback']) @@ -268,7 +250,7 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_object.assert_called_once_with( 'test_c', 'test_o', query_string=None, response_dict={} ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) def test_delete_object_exception(self): mock_q = Queue() @@ -294,7 +276,7 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_object.assert_called_once_with( 'test_c', 'test_o', query_string=None, response_dict={} ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) self.assertGreaterEqual(r['error_timestamp'], before) self.assertLessEqual(r['error_timestamp'], after) self.assertIn('Traceback', r['traceback']) @@ -321,7 +303,7 @@ class TestServiceDelete(_TestServiceBase): query_string='multipart-manifest=delete', response_dict={} ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) def test_delete_object_dlo_support(self): mock_q = Queue() @@ -352,7 +334,7 @@ class TestServiceDelete(_TestServiceBase): mock_conn, 'test_c', 'test_o', self.opts, mock_q ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) expected = [ mock.call('test_c', 'test_o', query_string=None, response_dict={}), mock.call('manifest_c', 'test_seg_1', response_dict={}), @@ -372,7 +354,7 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_container.assert_called_once_with( 'test_c', response_dict={} ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) def test_delete_empty_container_exception(self): mock_conn = self._get_mock_connection() @@ -394,13 +376,13 @@ class TestServiceDelete(_TestServiceBase): mock_conn.delete_container.assert_called_once_with( 'test_c', response_dict={} ) - self._assertDictEqual(expected_r, r) + self.assertEqual(expected_r, r) self.assertGreaterEqual(r['error_timestamp'], before) self.assertLessEqual(r['error_timestamp'], after) self.assertIn('Traceback', r['traceback']) -class TestSwiftError(testtools.TestCase): +class TestSwiftError(unittest.TestCase): def test_is_exception(self): se = SwiftError(5) @@ -430,7 +412,7 @@ class TestSwiftError(testtools.TestCase): self.assertEqual(str(se), '5 container:con object:obj segment:seg') -class TestServiceUtils(testtools.TestCase): +class TestServiceUtils(unittest.TestCase): def setUp(self): super(TestServiceUtils, self).setUp() @@ -525,7 +507,7 @@ class TestServiceUtils(testtools.TestCase): mock_headers) -class TestSwiftUploadObject(testtools.TestCase): +class TestSwiftUploadObject(unittest.TestCase): def setUp(self): self.suo = swiftclient.service.SwiftUploadObject @@ -614,7 +596,7 @@ class TestServiceList(_TestServiceBase): SwiftService._list_account_job( mock_conn, self.opts, mock_q ) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) long_opts = dict(self.opts, **{'long': True}) @@ -635,7 +617,7 @@ class TestServiceList(_TestServiceBase): SwiftService._list_account_job( mock_conn, long_opts, mock_q ) - self._assertDictEqual(expected_r_long, self._get_queue(mock_q)) + self.assertEqual(expected_r_long, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) def test_list_account_exception(self): @@ -657,7 +639,7 @@ class TestServiceList(_TestServiceBase): mock_conn.get_account.assert_called_once_with( marker='', prefix=None ) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) def test_list_container(self): @@ -680,7 +662,7 @@ class TestServiceList(_TestServiceBase): SwiftService._list_container_job( mock_conn, 'test_c', self.opts, mock_q ) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) long_opts = dict(self.opts, **{'long': True}) @@ -702,7 +684,7 @@ class TestServiceList(_TestServiceBase): SwiftService._list_container_job( mock_conn, 'test_c', long_opts, mock_q ) - self._assertDictEqual(expected_r_long, self._get_queue(mock_q)) + self.assertEqual(expected_r_long, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) def test_list_container_exception(self): @@ -726,7 +708,7 @@ class TestServiceList(_TestServiceBase): mock_conn.get_container.assert_called_once_with( 'test_c', marker='', delimiter='', prefix=None ) - self._assertDictEqual(expected_r, self._get_queue(mock_q)) + self.assertEqual(expected_r, self._get_queue(mock_q)) self.assertIsNone(self._get_queue(mock_q)) @mock.patch('swiftclient.service.get_conn') @@ -805,7 +787,7 @@ class TestServiceList(_TestServiceBase): self.assertEqual(observed_listing, expected_listing) -class TestService(testtools.TestCase): +class TestService(unittest.TestCase): def test_upload_with_bad_segment_size(self): for bad in ('ten', '1234X', '100.3'): @@ -913,7 +895,7 @@ class TestServiceUpload(_TestServiceBase): self.assertEqual(r['path'], f.name) del r['path'] - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.put_object.call_count, 1) mock_conn.put_object.assert_called_with('test_c', 'ใƒ†ใ‚นใƒˆ/dummy.dat', '', @@ -960,7 +942,7 @@ class TestServiceUpload(_TestServiceBase): options={'segment_container': None, 'checksum': True}) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.put_object.call_count, 1) mock_conn.put_object.assert_called_with('test_c_segments', @@ -1098,7 +1080,7 @@ class TestServiceUpload(_TestServiceBase): self.assertEqual(r['path'], f.name) del r['path'] - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.put_object.call_count, 1) mock_conn.put_object.assert_called_with('test_c', 'test_o', mock.ANY, @@ -1155,7 +1137,7 @@ class TestServiceUpload(_TestServiceBase): self.assertEqual(mtime, expected_mtime) del r['headers']['x-object-meta-mtime'] - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.put_object.call_count, 1) mock_conn.put_object.assert_called_with('test_c', 'test_o', mock.ANY, @@ -1559,7 +1541,7 @@ class TestServiceDownload(_TestServiceBase): 'test_c', 'test_o', resp_chunk_size=65536, headers={}, response_dict={} ) - self._assertDictEqual(expected_r, actual_r) + self.assertEqual(expected_r, actual_r) def test_download_object_job_with_mtime(self): mock_conn = self._get_mock_connection() @@ -1605,7 +1587,7 @@ class TestServiceDownload(_TestServiceBase): 'test_c', 'test_o', resp_chunk_size=65536, headers={}, response_dict={} ) - self._assertDictEqual(expected_r, actual_r) + self.assertEqual(expected_r, actual_r) def test_download_object_job_bad_mtime(self): mock_conn = self._get_mock_connection() @@ -1650,7 +1632,7 @@ class TestServiceDownload(_TestServiceBase): 'test_c', 'test_o', resp_chunk_size=65536, headers={}, response_dict={} ) - self._assertDictEqual(expected_r, actual_r) + self.assertEqual(expected_r, actual_r) def test_download_object_job_exception(self): mock_conn = self._get_mock_connection() @@ -1670,7 +1652,7 @@ class TestServiceDownload(_TestServiceBase): 'test_c', 'test_o', resp_chunk_size=65536, headers={}, response_dict={} ) - self._assertDictEqual(expected_r, actual_r) + self.assertEqual(expected_r, actual_r) def test_download(self): service = SwiftService() @@ -1814,7 +1796,7 @@ class TestServiceDownload(_TestServiceBase): 'header': {}, 'yes_all': False, 'skip_identical': True}) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.get_object.call_count, 1) mock_conn.get_object.assert_called_with( @@ -1876,7 +1858,7 @@ class TestServiceDownload(_TestServiceBase): self.assertEqual("Large object is identical", err.msg) self.assertEqual(304, err.http_status) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.get_object.call_count, 1) mock_conn.get_object.assert_called_with( @@ -1959,7 +1941,7 @@ class TestServiceDownload(_TestServiceBase): self.assertEqual("Large object is identical", err.msg) self.assertEqual(304, err.http_status) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.get_object.mock_calls, [ mock.call('test_c', 'test_o', @@ -2025,7 +2007,7 @@ class TestServiceDownload(_TestServiceBase): obj='test_o', options=options) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.get_container.mock_calls, [ mock.call('test_c_segments', @@ -2116,7 +2098,7 @@ class TestServiceDownload(_TestServiceBase): obj='test_o', options=options) - self._assertDictEqual(r, expected_r) + self.assertEqual(r, expected_r) self.assertEqual(mock_conn.get_object.mock_calls, [ mock.call('test_c', 'test_o', diff --git a/tests/unit/test_shell.py b/tests/unit/test_shell.py index 4bde190f..ee97824f 100644 --- a/tests/unit/test_shell.py +++ b/tests/unit/test_shell.py @@ -20,9 +20,8 @@ import logging import mock import os import tempfile -import testtools +import unittest import textwrap -from testtools import ExpectedException import six @@ -106,7 +105,7 @@ 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(testtools.TestCase): +class TestShell(unittest.TestCase): def setUp(self): super(TestShell, self).setUp() tmpfile = tempfile.NamedTemporaryFile(delete=False) @@ -1076,7 +1075,7 @@ class TestShell(testtools.TestCase): swiftclient.ClientException('bad auth') with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertEqual(output.err, 'bad auth\n') @@ -1088,7 +1087,7 @@ class TestShell(testtools.TestCase): swiftclient.ClientException('test', http_status=404) with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertEqual(output.err, 'Account not found\n') @@ -1107,7 +1106,7 @@ class TestShell(testtools.TestCase): swiftclient.ClientException('bad auth') with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertEqual(output.err, 'bad auth\n') @@ -1125,7 +1124,7 @@ class TestShell(testtools.TestCase): argv = ["", "post", "conta/iner"] with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertTrue(output.err != '') self.assertTrue(output.err.startswith('WARNING: / in')) @@ -1165,7 +1164,7 @@ class TestShell(testtools.TestCase): swiftclient.ClientException("bad auth") with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertEqual(output.err, 'bad auth\n') @@ -1174,7 +1173,7 @@ class TestShell(testtools.TestCase): argv = ["", "post", "container", "object", "bad_arg"] with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): swiftclient.shell.main(argv) self.assertTrue(output.err != '') @@ -1235,49 +1234,49 @@ class TestShell(testtools.TestCase): _check_expected(mock_swift, 12345) with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): # Test invalid states argv = ["", "upload", "-S", "1234X", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "Invalid segment size\n") output.clear() - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "K1234", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "Invalid segment size\n") output.clear() - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "K", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "Invalid segment size\n") def test_negative_upload_segment_size(self): with CaptureOutput() as output: - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "-40", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "-40K", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "-40M", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() - with ExpectedException(SystemExit): + with self.assertRaises(SystemExit): argv = ["", "upload", "-S", "-40G", "container", "object"] swiftclient.shell.main(argv) self.assertEqual(output.err, "segment-size should be positive\n") output.clear() -class TestSubcommandHelp(testtools.TestCase): +class TestSubcommandHelp(unittest.TestCase): def test_subcommand_help(self): for command in swiftclient.shell.commands: @@ -1298,7 +1297,7 @@ class TestSubcommandHelp(testtools.TestCase): @mock.patch.dict(os.environ, mocked_os_environ) -class TestDebugAndInfoOptions(testtools.TestCase): +class TestDebugAndInfoOptions(unittest.TestCase): @mock.patch('logging.basicConfig') @mock.patch('swiftclient.service.Connection') def test_option_after_posarg(self, connection, mock_logging): @@ -1329,7 +1328,7 @@ class TestDebugAndInfoOptions(testtools.TestCase): % (mock_logging.call_args_list, argv)) -class TestBase(testtools.TestCase): +class TestBase(unittest.TestCase): """ Provide some common methods to subclasses """ diff --git a/tests/unit/test_swiftclient.py b/tests/unit/test_swiftclient.py index 95a46a5b..c378dbdc 100644 --- a/tests/unit/test_swiftclient.py +++ b/tests/unit/test_swiftclient.py @@ -18,7 +18,7 @@ import mock import six import socket import string -import testtools +import unittest import warnings import tempfile from hashlib import md5 @@ -34,7 +34,7 @@ import swiftclient.utils import swiftclient -class TestClientException(testtools.TestCase): +class TestClientException(unittest.TestCase): def test_is_exception(self): self.assertTrue(issubclass(c.ClientException, Exception)) @@ -251,12 +251,12 @@ class TestGetAuth(MockHttpTest): self.assertEqual(url, 'storageURL') self.assertEqual(token, 'someauthtoken') - e = self.assertRaises(c.ClientException, c.get_auth, - 'http://www.test.com/invalid_cert', - 'asdf', 'asdf', auth_version='1.0') + with self.assertRaises(c.ClientException) as exc_context: + c.get_auth('http://www.test.com/invalid_cert', + 'asdf', 'asdf', auth_version='1.0') # TODO: this test is really on validating the mock and not the # the full plumbing into the requests's 'verify' option - self.assertIn('invalid_certificate', str(e)) + self.assertIn('invalid_certificate', str(exc_context.exception)) def test_auth_v1_timeout(self): # this test has some overlap with @@ -583,8 +583,9 @@ class TestHeadAccount(MockHttpTest): def test_server_error(self): body = 'c' * 65 c.http_connection = self.fake_http_connection(500, body=body) - e = self.assertRaises(c.ClientException, c.head_account, - 'http://www.tests.com', 'asdf') + with self.assertRaises(c.ClientException) as exc_context: + c.head_account('http://www.tests.com', 'asdf') + e = exc_context.exception self.assertEqual(e.http_response_content, body) self.assertEqual(e.http_status, 500) self.assertRequests([ @@ -617,17 +618,17 @@ class TestPostAccount(MockHttpTest): def test_server_error(self): body = 'c' * 65 c.http_connection = self.fake_http_connection(500, body=body) - e = self.assertRaises(c.ClientException, c.post_account, - 'http://www.tests.com', 'asdf', {}) - self.assertEqual(e.http_response_content, body) - self.assertEqual(e.http_status, 500) + with self.assertRaises(c.ClientException) as exc_mgr: + c.post_account('http://www.tests.com', 'asdf', {}) + self.assertEqual(exc_mgr.exception.http_response_content, body) + self.assertEqual(exc_mgr.exception.http_status, 500) self.assertRequests([ ('POST', 'http://www.tests.com', None, {'x-auth-token': 'asdf'}) ]) # TODO: this is a fairly brittle test of the __repr__ on the # ClientException which should probably be in a targeted test new_body = "[first 60 chars of response] " + body[0:60] - self.assertEqual(e.__str__()[-89:], new_body) + self.assertEqual(exc_mgr.exception.__str__()[-89:], new_body) class TestGetContainer(MockHttpTest): @@ -741,8 +742,9 @@ class TestHeadContainer(MockHttpTest): def test_server_error(self): body = 'c' * 60 c.http_connection = self.fake_http_connection(500, body=body) - e = self.assertRaises(c.ClientException, c.head_container, - 'http://www.test.com', 'asdf', 'container') + with self.assertRaises(c.ClientException) as exc_context: + c.head_container('http://www.test.com', 'asdf', 'container') + e = exc_context.exception self.assertRequests([ ('HEAD', '/container', '', {'x-auth-token': 'asdf'}), ]) @@ -765,9 +767,9 @@ class TestPutContainer(MockHttpTest): def test_server_error(self): body = 'c' * 60 c.http_connection = self.fake_http_connection(500, body=body) - e = self.assertRaises(c.ClientException, c.put_container, - 'http://www.test.com', 'token', 'container') - self.assertEqual(e.http_response_content, body) + with self.assertRaises(c.ClientException) as exc_context: + c.put_container('http://www.test.com', 'token', 'container') + self.assertEqual(exc_context.exception.http_response_content, body) self.assertRequests([ ('PUT', '/container', '', { 'x-auth-token': 'token', @@ -972,7 +974,9 @@ class TestPutObject(MockHttpTest): body = 'c' * 60 c.http_connection = self.fake_http_connection(500, body=body) args = ('http://www.test.com', 'asdf', 'asdf', 'asdf', 'asdf') - e = self.assertRaises(c.ClientException, c.put_object, *args) + with self.assertRaises(c.ClientException) as exc_context: + c.put_object(*args) + e = exc_context.exception self.assertEqual(e.http_response_content, body) self.assertEqual(e.http_status, 500) self.assertRequests([ @@ -1192,8 +1196,9 @@ class TestPostObject(MockHttpTest): body = 'c' * 60 c.http_connection = self.fake_http_connection(500, body=body) args = ('http://www.test.com', 'token', 'container', 'obj', {}) - e = self.assertRaises(c.ClientException, c.post_object, *args) - self.assertEqual(e.http_response_content, body) + with self.assertRaises(c.ClientException) as exc_context: + c.post_object(*args) + self.assertEqual(exc_context.exception.http_response_content, body) self.assertRequests([ ('POST', 'http://www.test.com/container/obj', '', { 'x-auth-token': 'token', @@ -1347,17 +1352,23 @@ class TestHTTPConnection(MockHttpTest): def test_bad_url_scheme(self): url = u'www.test.com' - exc = self.assertRaises(c.ClientException, c.http_connection, url) + with self.assertRaises(c.ClientException) as exc_context: + c.http_connection(url) + exc = exc_context.exception expected = u'Unsupported scheme "" in url "www.test.com"' self.assertEqual(expected, str(exc)) url = u'://www.test.com' - exc = self.assertRaises(c.ClientException, c.http_connection, url) + with self.assertRaises(c.ClientException) as exc_context: + c.http_connection(url) + exc = exc_context.exception expected = u'Unsupported scheme "" in url "://www.test.com"' self.assertEqual(expected, str(exc)) url = u'blah://www.test.com' - exc = self.assertRaises(c.ClientException, c.http_connection, url) + with self.assertRaises(c.ClientException) as exc_context: + c.http_connection(url) + exc = exc_context.exception expected = u'Unsupported scheme "blah" in url "blah://www.test.com"' self.assertEqual(expected, str(exc)) @@ -1524,8 +1535,9 @@ class TestConnection(MockHttpTest): } c.http_connection = self.fake_http_connection( *code_iter, headers=auth_resp_headers) - e = self.assertRaises(c.ClientException, conn.head_account) - self.assertIn('Account HEAD failed', str(e)) + with self.assertRaises(c.ClientException) as exc_context: + conn.head_account() + self.assertIn('Account HEAD failed', str(exc_context.exception)) self.assertEqual(conn.attempts, conn.retries + 1) # test default no-retry @@ -1533,8 +1545,9 @@ class TestConnection(MockHttpTest): 200, 498, headers=auth_resp_headers) conn = c.Connection('http://www.test.com/auth/v1.0', 'asdf', 'asdf') - e = self.assertRaises(c.ClientException, conn.head_account) - self.assertIn('Account HEAD failed', str(e)) + with self.assertRaises(c.ClientException) as exc_context: + conn.head_account() + self.assertIn('Account HEAD failed', str(exc_context.exception)) self.assertEqual(conn.attempts, 1) def test_resp_read_on_server_error(self): @@ -2132,9 +2145,9 @@ class TestLogging(MockHttpTest): def test_get_error(self): c.http_connection = self.fake_http_connection(404) - e = self.assertRaises(c.ClientException, c.get_object, - 'http://www.test.com', 'asdf', 'asdf', 'asdf') - self.assertEqual(e.http_status, 404) + with self.assertRaises(c.ClientException) as exc_context: + c.get_object('http://www.test.com', 'asdf', 'asdf', 'asdf') + self.assertEqual(exc_context.exception.http_status, 404) class TestCloseConnection(MockHttpTest): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index fe50f556..aae466c7 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import testtools +import unittest import mock import six import tempfile @@ -22,7 +22,7 @@ from hashlib import md5 from swiftclient import utils as u -class TestConfigTrueValue(testtools.TestCase): +class TestConfigTrueValue(unittest.TestCase): def test_TRUE_VALUES(self): for v in u.TRUE_VALUES: @@ -37,7 +37,7 @@ class TestConfigTrueValue(testtools.TestCase): self.assertIs(u.config_true_value(False), False) -class TestPrtBytes(testtools.TestCase): +class TestPrtBytes(unittest.TestCase): def test_zero_bytes(self): bytes_ = 0 @@ -119,7 +119,7 @@ class TestPrtBytes(testtools.TestCase): self.assertEqual('1024Y', u.prt_bytes(bytes_, True).lstrip()) -class TestTempURL(testtools.TestCase): +class TestTempURL(unittest.TestCase): def setUp(self): super(TestTempURL, self).setUp() @@ -164,7 +164,7 @@ class TestTempURL(testtools.TestCase): self.method) -class TestReadableToIterable(testtools.TestCase): +class TestReadableToIterable(unittest.TestCase): def test_iter(self): chunk_size = 4 @@ -216,7 +216,7 @@ class TestReadableToIterable(testtools.TestCase): self.assertEqual(actual_md5sum, data.get_md5sum()) -class TestLengthWrapper(testtools.TestCase): +class TestLengthWrapper(unittest.TestCase): def test_stringio(self): contents = six.StringIO(u'a' * 50 + u'b' * 50) @@ -292,7 +292,7 @@ class TestLengthWrapper(testtools.TestCase): self.assertEqual(md5(s).hexdigest(), data.get_md5sum()) -class TestGroupers(testtools.TestCase): +class TestGroupers(unittest.TestCase): def test_n_at_a_time(self): result = list(u.n_at_a_time(range(100), 9)) self.assertEqual([9] * 11 + [1], list(map(len, result))) diff --git a/tests/unit/utils.py b/tests/unit/utils.py index f8f5e908..1bfa8da8 100644 --- a/tests/unit/utils.py +++ b/tests/unit/utils.py @@ -18,7 +18,6 @@ from requests import RequestException from requests.structures import CaseInsensitiveDict from time import sleep import unittest -import testtools import mock import six from six.moves import reload_module @@ -189,7 +188,7 @@ def fake_http_connect(*code_iter, **kwargs): return connect -class MockHttpTest(testtools.TestCase): +class MockHttpTest(unittest.TestCase): def setUp(self): super(MockHttpTest, self).setUp()