From c1f8f266d0c465760535d0305d65138199f67daf Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 20 Jul 2013 13:44:11 -0700 Subject: [PATCH] Ensure that files in tests are closed. This is needed on Pythons which do not have reference counting GCs (e.g. PyPy). Change-Id: I5a613e832e9a7a149b3e9317c053c3048f34afcb --- test/unit/common/ring/test_ring.py | 6 +++-- test/unit/common/test_wsgi.py | 40 ++++++++++++++++------------- test/unit/container/test_updater.py | 15 ++++++----- test/unit/obj/test_base.py | 11 +++++--- test/unit/obj/test_replicator.py | 13 ++++++---- test/unit/obj/test_updater.py | 17 +++++++----- 6 files changed, 60 insertions(+), 42 deletions(-) diff --git a/test/unit/common/ring/test_ring.py b/test/unit/common/ring/test_ring.py index 64d8f327a0..793c3cb1e2 100644 --- a/test/unit/common/ring/test_ring.py +++ b/test/unit/common/ring/test_ring.py @@ -18,6 +18,7 @@ import cPickle as pickle import os import sys import unittest +from contextlib import closing from gzip import GzipFile from shutil import rmtree from time import sleep, time @@ -56,14 +57,15 @@ class TestRingData(unittest.TestCase): [{'id': 0, 'zone': 0}, {'id': 1, 'zone': 1}], 30) ring_fname = os.path.join(self.testdir, 'foo.ring.gz') for p in xrange(pickle.HIGHEST_PROTOCOL): - pickle.dump(rd, GzipFile(ring_fname, 'wb'), protocol=p) + with closing(GzipFile(ring_fname, 'wb')) as f: + pickle.dump(rd, f, protocol=p) ring_data = ring.RingData.load(ring_fname) self.assert_ring_data_equal(rd, ring_data) def test_roundtrip_serialization(self): ring_fname = os.path.join(self.testdir, 'foo.ring.gz') rd = ring.RingData( - [array.array('H', [0, 1, 0, 1]), array.array('H',[0, 1, 0, 1])], + [array.array('H', [0, 1, 0, 1]), array.array('H', [0, 1, 0, 1])], [{'id': 0, 'zone': 0}, {'id': 1, 'zone': 1}], 30) rd.save(ring_fname) rd2 = ring.RingData.load(ring_fname) diff --git a/test/unit/common/test_wsgi.py b/test/unit/common/test_wsgi.py index 38d97aafcf..8f80570a94 100644 --- a/test/unit/common/test_wsgi.py +++ b/test/unit/common/test_wsgi.py @@ -27,6 +27,7 @@ from textwrap import dedent from gzip import GzipFile from StringIO import StringIO from collections import defaultdict +from contextlib import closing from urllib import quote from eventlet import listen @@ -41,24 +42,27 @@ from mock import patch def _fake_rings(tmpdir): - pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], - [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', - 'port': 6012}, - {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', - 'port': 6022}], 30), - GzipFile(os.path.join(tmpdir, 'account.ring.gz'), 'wb')) - pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], - [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', - 'port': 6011}, - {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', - 'port': 6021}], 30), - GzipFile(os.path.join(tmpdir, 'container.ring.gz'), 'wb')) - pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], - [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', - 'port': 6010}, - {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', - 'port': 6020}], 30), - GzipFile(os.path.join(tmpdir, 'object.ring.gz'), 'wb')) + with closing(GzipFile(os.path.join(tmpdir, 'account.ring.gz'), 'wb')) as f: + pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], + [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', + 'port': 6012}, + {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', + 'port': 6022}], 30), + f) + with closing(GzipFile(os.path.join(tmpdir, 'container.ring.gz'), 'wb')) as f: + pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], + [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', + 'port': 6011}, + {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', + 'port': 6021}], 30), + f) + with closing(GzipFile(os.path.join(tmpdir, 'object.ring.gz'), 'wb')) as f: + pickle.dump(ring.RingData([[0, 1, 0, 1], [1, 0, 1, 0]], + [{'id': 0, 'zone': 0, 'device': 'sda1', 'ip': '127.0.0.1', + 'port': 6010}, + {'id': 1, 'zone': 1, 'device': 'sdb1', 'ip': '127.0.0.1', + 'port': 6020}], 30), + f) class TestWSGI(unittest.TestCase): diff --git a/test/unit/container/test_updater.py b/test/unit/container/test_updater.py index 7241eb6620..16da6227f6 100644 --- a/test/unit/container/test_updater.py +++ b/test/unit/container/test_updater.py @@ -16,6 +16,7 @@ import cPickle as pickle import os import unittest +from contextlib import closing from gzip import GzipFile from shutil import rmtree from tempfile import mkdtemp @@ -38,12 +39,14 @@ class TestContainerUpdater(unittest.TestCase): self.testdir = os.path.join(mkdtemp(), 'tmp_test_container_updater') rmtree(self.testdir, ignore_errors=1) os.mkdir(self.testdir) - pickle.dump(RingData([[0, 1, 0, 1], [1, 0, 1, 0]], - [{'id': 0, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', - 'zone': 0}, - {'id': 1, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', - 'zone': 2}], 30), - GzipFile(os.path.join(self.testdir, 'account.ring.gz'), 'wb')) + ring_file = os.path.join(self.testdir, 'account.ring.gz') + with closing(GzipFile(ring_file, 'wb')) as f: + pickle.dump(RingData([[0, 1, 0, 1], [1, 0, 1, 0]], + [{'id': 0, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', + 'zone': 0}, + {'id': 1, 'ip': '127.0.0.1', 'port': 12345, 'device': 'sda1', + 'zone': 2}], 30), + f) self.devices_dir = os.path.join(self.testdir, 'devices') os.mkdir(self.devices_dir) self.sda1 = os.path.join(self.devices_dir, 'sda1') diff --git a/test/unit/obj/test_base.py b/test/unit/obj/test_base.py index ba2cf705eb..3fa5a49906 100644 --- a/test/unit/obj/test_base.py +++ b/test/unit/obj/test_base.py @@ -17,11 +17,13 @@ from __future__ import with_statement import unittest import os +from contextlib import closing from gzip import GzipFile from shutil import rmtree import cPickle as pickle import time import tempfile + from test.unit import FakeLogger, mock as unit_mock from swift.common import utils from swift.common.utils import hash_path, mkdirs, normalize_timestamp @@ -48,10 +50,11 @@ def _create_test_ring(path): 'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'port': 6000}] intended_part_shift = 30 intended_reload_time = 15 - pickle.dump( - ring.RingData(intended_replica2part2dev_id, intended_devs, - intended_part_shift), - GzipFile(testgz, 'wb')) + with closing(GzipFile(testgz, 'wb')) as f: + pickle.dump( + ring.RingData(intended_replica2part2dev_id, intended_devs, + intended_part_shift), + f) return ring.Ring(path, ring_name='object', reload_time=intended_reload_time) diff --git a/test/unit/obj/test_replicator.py b/test/unit/obj/test_replicator.py index d7a19b5e43..c4d951b7fe 100644 --- a/test/unit/obj/test_replicator.py +++ b/test/unit/obj/test_replicator.py @@ -23,9 +23,11 @@ from shutil import rmtree import cPickle as pickle import time import tempfile -from contextlib import contextmanager +from contextlib import contextmanager, closing + from eventlet.green import subprocess from eventlet import Timeout, tpool + from test.unit import FakeLogger from swift.common import utils from swift.common.utils import hash_path, mkdirs, normalize_timestamp @@ -121,12 +123,13 @@ def _create_test_ring(path): 'ip': 'fe80::202:b3ff:fe1e:8329', 'port': 6000}, {'id': 6, 'device': 'sda', 'zone': 7, 'ip': '2001:0db8:85a3:0000:0000:8a2e:0370:7334', 'port': 6000}, - ] + ] intended_part_shift = 30 intended_reload_time = 15 - pickle.dump(ring.RingData(intended_replica2part2dev_id, - intended_devs, intended_part_shift), - GzipFile(testgz, 'wb')) + with closing(GzipFile(testgz, 'wb')) as f: + pickle.dump(ring.RingData(intended_replica2part2dev_id, + intended_devs, intended_part_shift), + f) return ring.Ring(path, ring_name='object', reload_time=intended_reload_time) diff --git a/test/unit/obj/test_updater.py b/test/unit/obj/test_updater.py index 5e7d934bc0..17ed7bd105 100644 --- a/test/unit/obj/test_updater.py +++ b/test/unit/obj/test_updater.py @@ -16,6 +16,7 @@ import cPickle as pickle import os import unittest +from contextlib import closing from gzip import GzipFile from shutil import rmtree from time import time @@ -41,13 +42,15 @@ class TestObjectUpdater(unittest.TestCase): 'object_updater') rmtree(self.testdir, ignore_errors=1) os.mkdir(self.testdir) - pickle.dump( - RingData([[0, 1, 0, 1], [1, 0, 1, 0]], - [{'id': 0, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', - 'zone': 0}, - {'id': 1, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', - 'zone': 2}], 30), - GzipFile(os.path.join(self.testdir, 'container.ring.gz'), 'wb')) + ring_file = os.path.join(self.testdir, 'container.ring.gz') + with closing(GzipFile(ring_file, 'wb')) as f: + pickle.dump( + RingData([[0, 1, 0, 1], [1, 0, 1, 0]], + [{'id': 0, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', + 'zone': 0}, + {'id': 1, 'ip': '127.0.0.1', 'port': 1, 'device': 'sda1', + 'zone': 2}], 30), + f) self.devices_dir = os.path.join(self.testdir, 'devices') os.mkdir(self.devices_dir) self.sda1 = os.path.join(self.devices_dir, 'sda1')