summaryrefslogtreecommitdiff
path: root/tests/unit/test_service.py
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2016-02-01 17:43:03 +0000
committerGerrit Code Review <review@openstack.org>2016-02-01 17:43:03 +0000
commitfee68f732450a1ce8add565dacabbf74d5c82c09 (patch)
tree529517bb54735336947531e9d298f5877ad28864 /tests/unit/test_service.py
parente5f058e27ed45af7e06c1d531833279d5f8ef6fe (diff)
parent337570a03a57b2bceb615c4fe99ccaa18e0220c9 (diff)
downloadpython-swiftclient-fee68f732450a1ce8add565dacabbf74d5c82c09.tar.gz
Merge "Don't trust X-Object-Meta-Mtime"
Diffstat (limited to 'tests/unit/test_service.py')
-rw-r--r--tests/unit/test_service.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/unit/test_service.py b/tests/unit/test_service.py
index 8700819..64039ad 100644
--- a/tests/unit/test_service.py
+++ b/tests/unit/test_service.py
@@ -1535,6 +1535,97 @@ class TestServiceDownload(_TestServiceBase):
)
self._assertDictEqual(expected_r, actual_r)
+ def test_download_object_job_with_mtime(self):
+ mock_conn = self._get_mock_connection()
+ objcontent = six.BytesIO(b'objcontent')
+ mock_conn.get_object.side_effect = [
+ ({'content-type': 'text/plain',
+ 'etag': '2cbbfe139a744d6abbe695e17f3c1991',
+ 'x-object-meta-mtime': '1454113727.682512'},
+ objcontent)
+ ]
+ expected_r = self._get_expected({
+ 'success': True,
+ 'start_time': 1,
+ 'finish_time': 2,
+ 'headers_receipt': 3,
+ 'auth_end_time': 4,
+ 'read_length': len(b'objcontent'),
+ })
+
+ with mock.patch.object(builtins, 'open') as mock_open, \
+ mock.patch('swiftclient.service.utime') as mock_utime:
+ written_content = Mock()
+ mock_open.return_value = written_content
+ s = SwiftService()
+ _opts = self.opts.copy()
+ _opts['no_download'] = False
+ actual_r = s._download_object_job(
+ mock_conn, 'test_c', 'test_o', _opts)
+ actual_r = dict( # Need to override the times we got from the call
+ actual_r,
+ **{
+ 'start_time': 1,
+ 'finish_time': 2,
+ 'headers_receipt': 3
+ }
+ )
+ mock_open.assert_called_once_with('test_o', 'wb')
+ mock_utime.assert_called_once_with(
+ 'test_o', (1454113727.682512, 1454113727.682512))
+ written_content.write.assert_called_once_with(b'objcontent')
+
+ mock_conn.get_object.assert_called_once_with(
+ 'test_c', 'test_o', resp_chunk_size=65536, headers={},
+ response_dict={}
+ )
+ self._assertDictEqual(expected_r, actual_r)
+
+ def test_download_object_job_bad_mtime(self):
+ mock_conn = self._get_mock_connection()
+ objcontent = six.BytesIO(b'objcontent')
+ mock_conn.get_object.side_effect = [
+ ({'content-type': 'text/plain',
+ 'etag': '2cbbfe139a744d6abbe695e17f3c1991',
+ 'x-object-meta-mtime': 'foo'},
+ objcontent)
+ ]
+ expected_r = self._get_expected({
+ 'success': True,
+ 'start_time': 1,
+ 'finish_time': 2,
+ 'headers_receipt': 3,
+ 'auth_end_time': 4,
+ 'read_length': len(b'objcontent'),
+ })
+
+ with mock.patch.object(builtins, 'open') as mock_open, \
+ mock.patch('swiftclient.service.utime') as mock_utime:
+ written_content = Mock()
+ mock_open.return_value = written_content
+ s = SwiftService()
+ _opts = self.opts.copy()
+ _opts['no_download'] = False
+ actual_r = s._download_object_job(
+ mock_conn, 'test_c', 'test_o', _opts)
+ actual_r = dict( # Need to override the times we got from the call
+ actual_r,
+ **{
+ 'start_time': 1,
+ 'finish_time': 2,
+ 'headers_receipt': 3
+ }
+ )
+ mock_open.assert_called_once_with('test_o', 'wb')
+ self.assertEqual(0, len(mock_utime.mock_calls))
+ written_content.write.assert_called_once_with(b'objcontent')
+
+ mock_conn.get_object.assert_called_once_with(
+ 'test_c', 'test_o', resp_chunk_size=65536, headers={},
+ response_dict={}
+ )
+ self._assertDictEqual(expected_r, actual_r)
+
def test_download_object_job_exception(self):
mock_conn = self._get_mock_connection()
mock_conn.get_object = Mock(side_effect=self.exc)