Merge "Ensure that files in tests are closed."
This commit is contained in:
commit
3a339e9e4d
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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')
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
@ -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')
|
||||
|
Loading…
x
Reference in New Issue
Block a user