From d3bd30da4191e576f4e7b78498979ffc8892eeb5 Mon Sep 17 00:00:00 2001 From: Chmouel Boudjnah Date: Tue, 6 Aug 2013 14:15:48 +0200 Subject: [PATCH] Fix sync test when localhost on port 80 is binded - When localhost:80 was binding the tests was trying to connect into it. - To test you can simply run sudo python -m SimpleHTTPServer 80 which should show : 1.0.0.127.in-addr.arpa - - [06/Aug/2013 14:10:42] code 501, message Unsupported method ('DELETE') 1.0.0.127.in-addr.arpa - - [06/Aug/2013 14:10:42] "DELETE /a/c/o HTTP/1.1" 501 - (the test was passing since 501 would raise ClientException). mock delete_object in the fourth test to fix that - Refactor the code to use mock.patch as well. Closes-Bug: 1208802 Change-Id: I5ddd4ac3a97879f51cf5883fcfc0fe0f0adaeff6 --- test/unit/container/test_sync.py | 150 ++++++++++++++++--------------- 1 file changed, 77 insertions(+), 73 deletions(-) diff --git a/test/unit/container/test_sync.py b/test/unit/container/test_sync.py index 163f4e8172..eaabdb327c 100644 --- a/test/unit/container/test_sync.py +++ b/test/unit/container/test_sync.py @@ -13,9 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest - import re +import unittest +from contextlib import nested + +import mock + from test.unit import FakeLogger from swift.container import sync from swift.common import utils @@ -407,26 +410,23 @@ class TestContainerSync(unittest.TestCase): cring = FakeRing() oring = FakeRing() cs = sync.ContainerSync({}, container_ring=cring, object_ring=oring) - orig_ContainerBroker = sync.ContainerBroker - orig_hash_path = sync.hash_path - orig_delete_object = sync.delete_object - try: - def fake_hash_path(account, container, obj, raw_digest=False): - # Ensures that no rows match for full syncing, ordinal is 0 and - # all hashes are 0 - return '\x00' * 16 - - sync.hash_path = fake_hash_path - fcb = FakeContainerBroker( - 'path', - info={'account': 'a', 'container': 'c', - 'x_container_sync_point1': 2, - 'x_container_sync_point2': -1}, - metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), - 'x-container-sync-key': ('key', 1)}, - items_since=[{'ROWID': 1, 'name': 'o'}]) - sync.ContainerBroker = lambda p: fcb + def fake_hash_path(account, container, obj, raw_digest=False): + # Ensures that no rows match for full syncing, ordinal is 0 and + # all hashes are 0 + return '\x00' * 16 + fcb = FakeContainerBroker( + 'path', + info={'account': 'a', 'container': 'c', + 'x_container_sync_point1': 2, + 'x_container_sync_point2': -1}, + metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), + 'x-container-sync-key': ('key', 1)}, + items_since=[{'ROWID': 1, 'name': 'o'}]) + with nested( + mock.patch('swift.container.sync.ContainerBroker', + lambda p: fcb), + mock.patch('swift.container.sync.hash_path', fake_hash_path)): cs._myips = ['10.0.0.0'] # Match cs._myport = 1000 # Match cs.allowed_sync_hosts = ['127.0.0.1'] @@ -437,21 +437,23 @@ class TestContainerSync(unittest.TestCase): self.assertEquals(fcb.sync_point1, None) self.assertEquals(fcb.sync_point2, -1) - def fake_hash_path(account, container, obj, raw_digest=False): - # Ensures that all rows match for full syncing, ordinal is 0 - # and all hashes are 1 - return '\x01' * 16 - - sync.hash_path = fake_hash_path - fcb = FakeContainerBroker( - 'path', - info={'account': 'a', 'container': 'c', - 'x_container_sync_point1': 1, - 'x_container_sync_point2': 1}, - metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), - 'x-container-sync-key': ('key', 1)}, - items_since=[{'ROWID': 1, 'name': 'o'}]) - sync.ContainerBroker = lambda p: fcb + def fake_hash_path(account, container, obj, raw_digest=False): + # Ensures that all rows match for full syncing, ordinal is 0 + # and all hashes are 1 + return '\x01' * 16 + fcb = FakeContainerBroker('path', info={'account': 'a', + 'container': 'c', + 'x_container_sync_point1': 1, + 'x_container_sync_point2': 1}, + metadata={'x-container-sync-to': + ('http://127.0.0.1/a/c', 1), + 'x-container-sync-key': + ('key', 1)}, + items_since=[{'ROWID': 1, 'name': 'o'}]) + with nested( + mock.patch('swift.container.sync.ContainerBroker', + lambda p: fcb), + mock.patch('swift.container.sync.hash_path', fake_hash_path)): cs._myips = ['10.0.0.0'] # Match cs._myport = 1000 # Match cs.allowed_sync_hosts = ['127.0.0.1'] @@ -462,15 +464,15 @@ class TestContainerSync(unittest.TestCase): self.assertEquals(fcb.sync_point1, -1) self.assertEquals(fcb.sync_point2, -1) - fcb = FakeContainerBroker( - 'path', - info={'account': 'a', 'container': 'c', - 'x_container_sync_point1': 2, - 'x_container_sync_point2': -1}, - metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), - 'x-container-sync-key': ('key', 1)}, - items_since=[{'ROWID': 1, 'name': 'o'}]) - sync.ContainerBroker = lambda p: fcb + fcb = FakeContainerBroker( + 'path', + info={'account': 'a', 'container': 'c', + 'x_container_sync_point1': 2, + 'x_container_sync_point2': -1}, + metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), + 'x-container-sync-key': ('key', 1)}, + items_since=[{'ROWID': 1, 'name': 'o'}]) + with mock.patch('swift.container.sync.ContainerBroker', lambda p: fcb): cs._myips = ['10.0.0.0'] # Match cs._myport = 1000 # Match cs.allowed_sync_hosts = ['127.0.0.1'] @@ -482,16 +484,22 @@ class TestContainerSync(unittest.TestCase): self.assertEquals(fcb.sync_point1, None) self.assertEquals(fcb.sync_point2, -1) - fcb = FakeContainerBroker( - 'path', - info={'account': 'a', 'container': 'c', - 'x_container_sync_point1': 2, - 'x_container_sync_point2': -1}, - metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), - 'x-container-sync-key': ('key', 1)}, - items_since=[{'ROWID': 1, 'name': 'o', 'created_at': '1.2', - 'deleted': True}]) - sync.ContainerBroker = lambda p: fcb + def fake_delete_object(*args, **kwargs): + raise ClientException + fcb = FakeContainerBroker( + 'path', + info={'account': 'a', 'container': 'c', + 'x_container_sync_point1': 2, + 'x_container_sync_point2': -1}, + metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), + 'x-container-sync-key': ('key', 1)}, + items_since=[{'ROWID': 1, 'name': 'o', 'created_at': '1.2', + 'deleted': True}]) + with nested( + mock.patch('swift.container.sync.ContainerBroker', + lambda p: fcb), + mock.patch('swift.container.sync.delete_object', + fake_delete_object)): cs._myips = ['10.0.0.0'] # Match cs._myport = 1000 # Match cs.allowed_sync_hosts = ['127.0.0.1'] @@ -502,20 +510,20 @@ class TestContainerSync(unittest.TestCase): self.assertEquals(fcb.sync_point1, None) self.assertEquals(fcb.sync_point2, -1) - def fake_delete_object(*args, **kwargs): - pass - - sync.delete_object = fake_delete_object - fcb = FakeContainerBroker( - 'path', - info={'account': 'a', 'container': 'c', - 'x_container_sync_point1': 2, - 'x_container_sync_point2': -1}, - metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), - 'x-container-sync-key': ('key', 1)}, - items_since=[{'ROWID': 1, 'name': 'o', 'created_at': '1.2', - 'deleted': True}]) - sync.ContainerBroker = lambda p: fcb + fcb = FakeContainerBroker( + 'path', + info={'account': 'a', 'container': 'c', + 'x_container_sync_point1': 2, + 'x_container_sync_point2': -1}, + metadata={'x-container-sync-to': ('http://127.0.0.1/a/c', 1), + 'x-container-sync-key': ('key', 1)}, + items_since=[{'ROWID': 1, 'name': 'o', 'created_at': '1.2', + 'deleted': True}]) + with nested( + mock.patch('swift.container.sync.ContainerBroker', + lambda p: fcb), + mock.patch('swift.container.sync.delete_object', + lambda *x, **y: None)): cs._myips = ['10.0.0.0'] # Match cs._myport = 1000 # Match cs.allowed_sync_hosts = ['127.0.0.1'] @@ -525,10 +533,6 @@ class TestContainerSync(unittest.TestCase): self.assertEquals(cs.container_skips, 0) self.assertEquals(fcb.sync_point1, None) self.assertEquals(fcb.sync_point2, 1) - finally: - sync.ContainerBroker = orig_ContainerBroker - sync.hash_path = orig_hash_path - sync.delete_object = orig_delete_object def test_container_second_loop(self): cring = FakeRing()