2013-10-09 12:03:50 -07:00
|
|
|
# Copyright (c) 2010-2013 OpenStack, LLC.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
|
|
# implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
import unittest
|
2014-06-25 13:28:42 -07:00
|
|
|
import mock
|
2014-03-24 18:20:22 +01:00
|
|
|
import six
|
2014-02-24 22:41:45 -08:00
|
|
|
import tempfile
|
2014-10-29 10:11:29 +00:00
|
|
|
from hashlib import md5
|
2014-02-24 22:41:45 -08:00
|
|
|
|
2013-10-09 12:03:50 -07:00
|
|
|
from swiftclient import utils as u
|
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestConfigTrueValue(unittest.TestCase):
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_TRUE_VALUES(self):
|
|
|
|
for v in u.TRUE_VALUES:
|
|
|
|
self.assertEqual(v, v.lower())
|
|
|
|
|
2015-09-09 17:41:21 -07:00
|
|
|
@mock.patch.object(u, 'TRUE_VALUES', 'hello world'.split())
|
2013-10-09 12:03:50 -07:00
|
|
|
def test_config_true_value(self):
|
2015-09-09 17:41:21 -07:00
|
|
|
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)
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestPrtBytes(unittest.TestCase):
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_zero_bytes(self):
|
|
|
|
bytes_ = 0
|
|
|
|
raw = '0'
|
|
|
|
human = '0'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_one_byte(self):
|
|
|
|
bytes_ = 1
|
|
|
|
raw = '1'
|
|
|
|
human = '1'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_less_than_one_k(self):
|
|
|
|
bytes_ = (2 ** 10) - 1
|
|
|
|
raw = '1023'
|
|
|
|
human = '1023'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_one_k(self):
|
|
|
|
bytes_ = 2 ** 10
|
|
|
|
raw = '1024'
|
|
|
|
human = '1.0K'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_a_decimal_k(self):
|
|
|
|
bytes_ = (3 * 2 ** 10) + 512
|
|
|
|
raw = '3584'
|
|
|
|
human = '3.5K'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_a_bit_less_than_one_meg(self):
|
|
|
|
bytes_ = (2 ** 20) - (2 ** 10)
|
|
|
|
raw = '1047552'
|
|
|
|
human = '1023K'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_just_a_hair_less_than_one_meg(self):
|
|
|
|
bytes_ = (2 ** 20) - (2 ** 10) + 1
|
|
|
|
raw = '1047553'
|
|
|
|
human = '1.0M'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_one_meg(self):
|
|
|
|
bytes_ = 2 ** 20
|
|
|
|
raw = '1048576'
|
|
|
|
human = '1.0M'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(raw, u.prt_bytes(bytes_, False).lstrip())
|
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_ten_meg(self):
|
|
|
|
bytes_ = 10 * 2 ** 20
|
|
|
|
human = '10M'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_bit_less_than_ten_meg(self):
|
|
|
|
bytes_ = (10 * 2 ** 20) - (100 * 2 ** 10)
|
|
|
|
human = '9.9M'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_just_a_hair_less_than_ten_meg(self):
|
|
|
|
bytes_ = (10 * 2 ** 20) - 1
|
|
|
|
human = '10.0M'
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual(human, u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_a_yotta(self):
|
|
|
|
bytes_ = 42 * 2 ** 80
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual('42Y', u.prt_bytes(bytes_, True).lstrip())
|
2013-10-09 12:03:50 -07:00
|
|
|
|
|
|
|
def test_overflow(self):
|
|
|
|
bytes_ = 2 ** 90
|
2014-01-17 11:30:46 +01:00
|
|
|
self.assertEqual('1024Y', u.prt_bytes(bytes_, True).lstrip())
|
2014-02-24 22:41:45 -08:00
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestTempURL(unittest.TestCase):
|
2014-06-25 13:28:42 -07:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestTempURL, self).setUp()
|
|
|
|
self.url = '/v1/AUTH_account/c/o'
|
|
|
|
self.seconds = 3600
|
|
|
|
self.key = 'correcthorsebatterystaple'
|
|
|
|
self.method = 'GET'
|
|
|
|
|
2015-09-04 14:57:30 -07:00
|
|
|
@mock.patch('hmac.HMAC.hexdigest', return_value='temp_url_signature')
|
|
|
|
@mock.patch('time.time', return_value=1400000000)
|
2014-06-25 13:28:42 -07:00
|
|
|
def test_generate_temp_url(self, time_mock, hmac_mock):
|
|
|
|
expected_url = (
|
|
|
|
'/v1/AUTH_account/c/o?'
|
|
|
|
'temp_url_sig=temp_url_signature&'
|
|
|
|
'temp_url_expires=1400003600')
|
|
|
|
url = u.generate_temp_url(self.url, self.seconds, self.key,
|
|
|
|
self.method)
|
|
|
|
self.assertEqual(url, expected_url)
|
|
|
|
|
2015-09-04 14:57:30 -07:00
|
|
|
@mock.patch('hmac.HMAC.hexdigest', return_value="temp_url_signature")
|
|
|
|
def test_generate_absolute_expiry_temp_url(self, hmac_mock):
|
|
|
|
expected_url = ('/v1/AUTH_account/c/o?'
|
|
|
|
'temp_url_sig=temp_url_signature&'
|
|
|
|
'temp_url_expires=2146636800')
|
|
|
|
url = u.generate_temp_url(self.url, 2146636800, self.key, self.method,
|
|
|
|
absolute=True)
|
|
|
|
self.assertEqual(url, expected_url)
|
|
|
|
|
2014-06-25 13:28:42 -07:00
|
|
|
def test_generate_temp_url_bad_seconds(self):
|
|
|
|
self.assertRaises(TypeError,
|
|
|
|
u.generate_temp_url,
|
|
|
|
self.url,
|
|
|
|
'not_an_int',
|
|
|
|
self.key,
|
|
|
|
self.method)
|
|
|
|
|
|
|
|
self.assertRaises(ValueError,
|
|
|
|
u.generate_temp_url,
|
|
|
|
self.url,
|
|
|
|
-1,
|
|
|
|
self.key,
|
|
|
|
self.method)
|
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestReadableToIterable(unittest.TestCase):
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
def test_iter(self):
|
|
|
|
chunk_size = 4
|
|
|
|
write_data = tuple(x.encode() for x in ('a', 'b', 'c', 'd'))
|
|
|
|
actual_md5sum = md5()
|
|
|
|
|
|
|
|
with tempfile.TemporaryFile() as f:
|
|
|
|
for x in write_data:
|
|
|
|
f.write(x * chunk_size)
|
|
|
|
actual_md5sum.update(x * chunk_size)
|
|
|
|
f.seek(0)
|
|
|
|
data = u.ReadableToIterable(f, chunk_size, True)
|
|
|
|
|
|
|
|
for i, data_chunk in enumerate(data):
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(chunk_size, len(data_chunk))
|
|
|
|
self.assertEqual(data_chunk, write_data[i] * chunk_size)
|
2014-10-29 10:11:29 +00:00
|
|
|
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(actual_md5sum.hexdigest(), data.get_md5sum())
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
def test_md5_creation(self):
|
|
|
|
# Check creation with a real and noop md5 class
|
|
|
|
data = u.ReadableToIterable(None, None, md5=True)
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(md5().hexdigest(), data.get_md5sum())
|
2015-09-09 17:41:21 -07:00
|
|
|
self.assertIs(type(data.md5sum), type(md5()))
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
data = u.ReadableToIterable(None, None, md5=False)
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual('', data.get_md5sum())
|
2015-09-09 17:41:21 -07:00
|
|
|
self.assertIs(type(data.md5sum), u.NoopMD5)
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
def test_unicode(self):
|
|
|
|
# Check no errors are raised if unicode data is feed in.
|
|
|
|
unicode_data = u'abc'
|
|
|
|
actual_md5sum = md5(unicode_data.encode()).hexdigest()
|
|
|
|
chunk_size = 2
|
|
|
|
|
|
|
|
with tempfile.TemporaryFile(mode='w+') as f:
|
|
|
|
f.write(unicode_data)
|
|
|
|
f.seek(0)
|
|
|
|
data = u.ReadableToIterable(f, chunk_size, True)
|
|
|
|
|
|
|
|
x = next(data)
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(2, len(x))
|
|
|
|
self.assertEqual(unicode_data[:2], x)
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
x = next(data)
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(1, len(x))
|
|
|
|
self.assertEqual(unicode_data[2:], x)
|
2014-10-29 10:11:29 +00:00
|
|
|
|
2015-08-03 12:20:44 +09:00
|
|
|
self.assertEqual(actual_md5sum, data.get_md5sum())
|
2014-10-29 10:11:29 +00:00
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestLengthWrapper(unittest.TestCase):
|
2014-02-24 22:41:45 -08:00
|
|
|
|
|
|
|
def test_stringio(self):
|
2015-09-23 10:42:43 -07:00
|
|
|
contents = six.StringIO(u'a' * 50 + u'b' * 50)
|
|
|
|
contents.seek(22)
|
2014-10-29 10:11:29 +00:00
|
|
|
data = u.LengthWrapper(contents, 42, True)
|
2015-09-23 10:42:43 -07:00
|
|
|
s = u'a' * 28 + u'b' * 14
|
2014-10-29 10:11:29 +00:00
|
|
|
read_data = u''.join(iter(data.read, ''))
|
|
|
|
|
|
|
|
self.assertEqual(42, len(data))
|
|
|
|
self.assertEqual(42, len(read_data))
|
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s.encode()).hexdigest(), data.get_md5sum())
|
|
|
|
|
2015-09-23 10:42:43 -07:00
|
|
|
data.reset()
|
|
|
|
self.assertEqual(md5().hexdigest(), data.get_md5sum())
|
|
|
|
|
|
|
|
read_data = u''.join(iter(data.read, ''))
|
|
|
|
self.assertEqual(42, len(read_data))
|
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s.encode()).hexdigest(), data.get_md5sum())
|
|
|
|
|
2014-10-29 10:11:29 +00:00
|
|
|
def test_bytesio(self):
|
2015-09-23 10:42:43 -07:00
|
|
|
contents = six.BytesIO(b'a' * 50 + b'b' * 50)
|
|
|
|
contents.seek(22)
|
2014-10-29 10:11:29 +00:00
|
|
|
data = u.LengthWrapper(contents, 42, True)
|
2015-09-23 10:42:43 -07:00
|
|
|
s = b'a' * 28 + b'b' * 14
|
2014-10-29 10:11:29 +00:00
|
|
|
read_data = b''.join(iter(data.read, ''))
|
|
|
|
|
2014-02-24 22:41:45 -08:00
|
|
|
self.assertEqual(42, len(data))
|
|
|
|
self.assertEqual(42, len(read_data))
|
2014-10-29 10:11:29 +00:00
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s).hexdigest(), data.get_md5sum())
|
2014-02-24 22:41:45 -08:00
|
|
|
|
|
|
|
def test_tempfile(self):
|
2014-10-29 10:11:29 +00:00
|
|
|
with tempfile.NamedTemporaryFile(mode='wb') as f:
|
|
|
|
f.write(b'a' * 100)
|
2014-02-24 22:41:45 -08:00
|
|
|
f.flush()
|
2014-10-29 10:11:29 +00:00
|
|
|
contents = open(f.name, 'rb')
|
|
|
|
data = u.LengthWrapper(contents, 42, True)
|
|
|
|
s = b'a' * 42
|
|
|
|
read_data = b''.join(iter(data.read, ''))
|
|
|
|
|
2014-02-24 22:41:45 -08:00
|
|
|
self.assertEqual(42, len(data))
|
|
|
|
self.assertEqual(42, len(read_data))
|
2014-10-29 10:11:29 +00:00
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s).hexdigest(), data.get_md5sum())
|
2014-02-24 22:41:45 -08:00
|
|
|
|
|
|
|
def test_segmented_file(self):
|
2014-10-29 10:11:29 +00:00
|
|
|
with tempfile.NamedTemporaryFile(mode='wb') as f:
|
2014-02-24 22:41:45 -08:00
|
|
|
segment_length = 1024
|
|
|
|
segments = ('a', 'b', 'c', 'd')
|
|
|
|
for c in segments:
|
2014-10-29 10:11:29 +00:00
|
|
|
f.write((c * segment_length).encode())
|
2014-02-24 22:41:45 -08:00
|
|
|
f.flush()
|
|
|
|
for i, c in enumerate(segments):
|
2014-10-29 10:11:29 +00:00
|
|
|
contents = open(f.name, 'rb')
|
2014-02-24 22:41:45 -08:00
|
|
|
contents.seek(i * segment_length)
|
2014-10-29 10:11:29 +00:00
|
|
|
data = u.LengthWrapper(contents, segment_length, True)
|
|
|
|
read_data = b''.join(iter(data.read, ''))
|
|
|
|
s = (c * segment_length).encode()
|
|
|
|
|
2014-02-24 22:41:45 -08:00
|
|
|
self.assertEqual(segment_length, len(data))
|
|
|
|
self.assertEqual(segment_length, len(read_data))
|
2014-10-29 10:11:29 +00:00
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s).hexdigest(), data.get_md5sum())
|
2015-09-23 10:42:43 -07:00
|
|
|
|
|
|
|
data.reset()
|
|
|
|
self.assertEqual(md5().hexdigest(), data.get_md5sum())
|
|
|
|
read_data = b''.join(iter(data.read, ''))
|
|
|
|
self.assertEqual(segment_length, len(data))
|
|
|
|
self.assertEqual(segment_length, len(read_data))
|
|
|
|
self.assertEqual(s, read_data)
|
|
|
|
self.assertEqual(md5(s).hexdigest(), data.get_md5sum())
|
2015-06-11 14:33:39 -07:00
|
|
|
|
|
|
|
|
2015-12-04 11:28:05 -08:00
|
|
|
class TestGroupers(unittest.TestCase):
|
2015-06-11 14:33:39 -07:00
|
|
|
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)))
|
|
|
|
|
|
|
|
result = list(u.n_at_a_time(range(100), 10))
|
|
|
|
self.assertEqual([10] * 10, list(map(len, result)))
|
|
|
|
|
|
|
|
result = list(u.n_at_a_time(range(100), 11))
|
|
|
|
self.assertEqual([11] * 9 + [1], list(map(len, result)))
|
|
|
|
|
|
|
|
result = list(u.n_at_a_time(range(100), 12))
|
|
|
|
self.assertEqual([12] * 8 + [4], list(map(len, result)))
|
|
|
|
|
|
|
|
def test_n_groups(self):
|
|
|
|
result = list(u.n_groups(range(100), 9))
|
|
|
|
self.assertEqual([12] * 8 + [4], list(map(len, result)))
|
|
|
|
|
|
|
|
result = list(u.n_groups(range(100), 10))
|
|
|
|
self.assertEqual([10] * 10, list(map(len, result)))
|
|
|
|
|
|
|
|
result = list(u.n_groups(range(100), 11))
|
|
|
|
self.assertEqual([10] * 10, list(map(len, result)))
|
|
|
|
|
|
|
|
result = list(u.n_groups(range(100), 12))
|
|
|
|
self.assertEqual([9] * 11 + [1], list(map(len, result)))
|