Add mktime function to test.functional.utils

email.utils.parsedate and time.mktime is really useful
for standard timestamp translation because we don't have
to make a handmade format translation logic.

However, it tends to make a long formula because everytime
we have to call both parsedate and mktime to translate from
RFC2822 format. In addition, unfortunately S3 uses also
different timestamp formats from RFC2822 in the response xml.

To get an easy way for the translation work, this patch
add the mktime function to translate from any format supported
by S3 to a float in epoch time.

TODO:
Currently mktime in this patch doesn't support the format of
list objects last-modified timestamp. We should do it on the
later patch.

Change-Id: I51888bc625773f7673fe01e7bdc6636cf3709901
This commit is contained in:
Kota Tsuyuzaki 2015-04-01 19:08:51 -07:00
parent 1aaa73822d
commit 2a04e87b56
2 changed files with 30 additions and 11 deletions

View File

@ -16,11 +16,10 @@
import unittest
from hashlib import md5
from itertools import izip
from email.utils import parsedate
from time import mktime, strptime
from swift3.etree import fromstring, tostring, Element, SubElement
from swift3.test.functional import Swift3FunctionalTestCase
from swift3.test.functional.utils import mktime
MIN_SEGMENT_SIZE = 5242880
@ -151,9 +150,7 @@ class TestSwift3MultiUpload(Swift3FunctionalTestCase):
self.assertEquals(headers['content-type'], 'text/html; charset=UTF-8')
self.assertTrue('content-length' in headers)
self.assertEquals(headers['content-length'], '0')
# TODO: make a function like as mktime in swift3.test.function.utils
expected_parts_list = [(headers['etag'],
mktime(parsedate(headers['date'])))]
expected_parts_list = [(headers['etag'], mktime(headers['date']))]
# Upload Part Copy
key, upload_id = uploads[1]
@ -167,8 +164,7 @@ class TestSwift3MultiUpload(Swift3FunctionalTestCase):
self.conn.make_request('PUT', src_bucket, src_obj, body=src_content)
_, headers, _ = self.conn.make_request('HEAD', src_bucket, src_obj)
self.assertCommonResponseHeaders(headers)
last_modified_date_from_header = mktime(
parsedate(headers['last-modified']))
last_modified_date_from_header = mktime(headers['last-modified'])
status, headers, body, resp_etag = \
self._upload_part_copy(src_bucket, src_obj, bucket,
@ -184,8 +180,7 @@ class TestSwift3MultiUpload(Swift3FunctionalTestCase):
last_modified = elem.find('LastModified').text
self.assertTrue(last_modified is not None)
last_modified_from_xml = mktime(
strptime(last_modified, '%Y-%m-%dT%H:%M:%S'))
last_modified_from_xml = mktime(last_modified)
self.assertEquals(last_modified_date_from_header,
last_modified_from_xml)
@ -228,8 +223,7 @@ class TestSwift3MultiUpload(Swift3FunctionalTestCase):
# the last-modified header drops mili-seconds info
# by the constraint of the format.
# For now, we can do either the format check or round check
# last_modified_from_xml = mktime(
# strptime(last_modified, '%Y-%m-%dT%H:%M:%S'))
# last_modified_from_xml = mktime(last_modified)
# self.assertEquals(expected_date,
# last_modified_from_xml)
self.assertEquals(expected_etag, p.find('ETag').text)

View File

@ -16,6 +16,9 @@
from hashlib import md5
from swift3.etree import fromstring
import time
from email.utils import parsedate
def get_error_code(body):
elem = fromstring(body, 'Error')
@ -24,3 +27,25 @@ def get_error_code(body):
def calculate_md5(body):
return md5(body).digest().encode('base64').strip()
def mktime(timestamp_str):
"""
mktime creates a float instance in epoch time really like as time.mktime
the difference from time.mktime is allowing to 2 formats string for the
argumtent for the S3 testing usage.
TODO: support
:param timestamp_str: a string of timestamp formatted as
(a) RFC2822 (e.g. date header)
(b) %Y-%m-%dT%H:%M:%S (e.g. copy result)
:return : a float instance in epoch time
"""
try:
epoch_time = time.mktime(parsedate(timestamp_str))
except TypeError:
epoch_time = time.mktime(
time.strptime(timestamp_str, '%Y-%m-%dT%H:%M:%S'))
return epoch_time