From 28a582f3f4dadaa4172ab94b174572165a1c10fc Mon Sep 17 00:00:00 2001 From: Pete Zaitcev Date: Mon, 15 Oct 2018 20:57:17 -0500 Subject: [PATCH] py3: adapt account/backend.py and utils.py Gradatim Ferociter Change-Id: I6f3fac450c14f8dbc549ab2b0645a31aa0cece89 --- swift/account/backend.py | 11 +++++---- test/unit/account/test_backend.py | 37 ++++++++++++++++--------------- tox.ini | 2 ++ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/swift/account/backend.py b/swift/account/backend.py index df2954fb70..dddcbce6ef 100644 --- a/swift/account/backend.py +++ b/swift/account/backend.py @@ -20,6 +20,8 @@ from uuid import uuid4 import sqlite3 +import six + from swift.common.utils import Timestamp from swift.common.db import DatabaseBroker, utf8encode, zero_like @@ -244,7 +246,7 @@ class AccountBroker(DatabaseBroker): """ Create a container with the given attributes. - :param name: name of the container to create + :param name: name of the container to create (a native string) :param put_timestamp: put_timestamp of the container to create :param delete_timestamp: delete_timestamp of the container to create :param object_count: number of objects in the container @@ -384,8 +386,9 @@ class AccountBroker(DatabaseBroker): put_timestamp, 0) """ delim_force_gte = False - (marker, end_marker, prefix, delimiter) = utf8encode( - marker, end_marker, prefix, delimiter) + if six.PY2: + (marker, end_marker, prefix, delimiter) = utf8encode( + marker, end_marker, prefix, delimiter) if reverse: # Reverse the markers if we are reversing the listing. marker, end_marker = end_marker, marker @@ -415,7 +418,7 @@ class AccountBroker(DatabaseBroker): query_args.append(marker) # Always set back to False delim_force_gte = False - elif marker and marker >= prefix: + elif marker and (not prefix or marker >= prefix): query += ' name > ? AND' query_args.append(marker) elif prefix: diff --git a/test/unit/account/test_backend.py b/test/unit/account/test_backend.py index 6bb89ae529..38ef87bac8 100644 --- a/test/unit/account/test_backend.py +++ b/test/unit/account/test_backend.py @@ -32,6 +32,8 @@ import random import mock import base64 +import six + from swift.account.backend import AccountBroker from swift.common.utils import Timestamp from test.unit import patch_policies, with_tempdir, make_timestamp_iter @@ -720,26 +722,23 @@ class TestAccountBroker(unittest.TestCase): broker.put_container('b', Timestamp(2).internal, Timestamp(0).internal, 0, 0, POLICIES.default.idx) - hasha = hashlib.md5( - '%s-%s' % ('a', "%s-%s-%s-%s" % ( - Timestamp(1).internal, Timestamp(0).internal, 0, 0)) - ).digest() - hashb = hashlib.md5( - '%s-%s' % ('b', "%s-%s-%s-%s" % ( - Timestamp(2).internal, Timestamp(0).internal, 0, 0)) - ).digest() - hashc = \ - ''.join(('%02x' % (ord(a) ^ ord(b)) for a, b in zip(hasha, hashb))) + text = '%s-%s' % ('a', "%s-%s-%s-%s" % ( + Timestamp(1).internal, Timestamp(0).internal, 0, 0)) + hasha = hashlib.md5(text.encode('ascii')).digest() + text = '%s-%s' % ('b', "%s-%s-%s-%s" % ( + Timestamp(2).internal, Timestamp(0).internal, 0, 0)) + hashb = hashlib.md5(text.encode('ascii')).digest() + hashc = ''.join(('%02x' % (ord(a) ^ ord(b) if six.PY2 else a ^ b) + for a, b in zip(hasha, hashb))) self.assertEqual(broker.get_info()['hash'], hashc) broker.put_container('b', Timestamp(3).internal, Timestamp(0).internal, 0, 0, POLICIES.default.idx) - hashb = hashlib.md5( - '%s-%s' % ('b', "%s-%s-%s-%s" % ( - Timestamp(3).internal, Timestamp(0).internal, 0, 0)) - ).digest() - hashc = \ - ''.join(('%02x' % (ord(a) ^ ord(b)) for a, b in zip(hasha, hashb))) + text = '%s-%s' % ('b', "%s-%s-%s-%s" % ( + Timestamp(3).internal, Timestamp(0).internal, 0, 0)) + hashb = hashlib.md5(text.encode('ascii')).digest() + hashc = ''.join(('%02x' % (ord(a) ^ ord(b) if six.PY2 else a ^ b) + for a, b in zip(hasha, hashb))) self.assertEqual(broker.get_info()['hash'], hashc) def test_merge_items(self): @@ -767,7 +766,9 @@ class TestAccountBroker(unittest.TestCase): sorted([rec['name'] for rec in items])) def test_merge_items_overwrite_unicode(self): - snowman = u'\N{SNOWMAN}'.encode('utf-8') + snowman = u'\N{SNOWMAN}' + if six.PY2: + snowman = snowman.encode('utf-8') broker1 = AccountBroker(':memory:', account='a') broker1.initialize(Timestamp('1').internal, 0) id1 = broker1.get_info()['id'] @@ -1729,7 +1730,7 @@ class TestAccountBrokerBeforePerPolicyContainerTrack( 0, 0, int(policy)) total_container_count = self.broker.get_info()['container_count'] - self.assertEqual(total_container_count, num_containers / 2) + self.assertEqual(total_container_count, num_containers // 2) # trigger migration policy_info = self.broker.get_policy_stats(do_migrations=True) diff --git a/tox.ini b/tox.ini index 5f285c5b01..8f70c95d59 100644 --- a/tox.ini +++ b/tox.ini @@ -29,6 +29,8 @@ setenv = VIRTUAL_ENV={envdir} [testenv:py35] commands = nosetests {posargs:\ + test/unit/account/test_backend.py \ + test/unit/account/test_utils.py \ test/unit/cli/test_dispersion_report.py \ test/unit/cli/test_form_signature.py \ test/unit/cli/test_info.py \