Skip to content

Commit 1aee1df

Browse files
committed
Merge pull request #511 from rmohr/fix-open-files
Properly close files and sockets in unit tests
2 parents 134e4b7 + a86bfff commit 1aee1df

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

tests/test.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,11 +2115,10 @@ def test_load_config(self):
21152115
folder = tempfile.mkdtemp()
21162116
self.addCleanup(shutil.rmtree, folder)
21172117
dockercfg_path = os.path.join(folder, '.dockercfg')
2118-
f = open(dockercfg_path, 'w')
2119-
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
2120-
f.write('auth = {0}\n'.format(auth_))
2121-
f.write('email = [email protected]')
2122-
f.close()
2118+
with open(dockercfg_path, 'w') as f:
2119+
auth_ = base64.b64encode(b'sakuya:izayoi').decode('ascii')
2120+
f.write('auth = {0}\n'.format(auth_))
2121+
f.write('email = [email protected]')
21232122
cfg = docker.auth.load_config(dockercfg_path)
21242123
self.assertTrue(docker.auth.INDEX_URL in cfg)
21252124
self.assertNotEqual(cfg[docker.auth.INDEX_URL], None)
@@ -2175,18 +2174,18 @@ def test_tar_with_excludes(self):
21752174
(['test/foo', 'a.txt'], ['bar', 'bar/a.txt', 'bar/b.py',
21762175
'bar/other.png', 'test']),
21772176
):
2178-
archive = docker.utils.tar(base, exclude=exclude)
2179-
tar = tarfile.open(fileobj=archive)
2180-
self.assertEqual(sorted(tar.getnames()), names)
2177+
with docker.utils.tar(base, exclude=exclude) as archive:
2178+
tar = tarfile.open(fileobj=archive)
2179+
self.assertEqual(sorted(tar.getnames()), names)
21812180

21822181
def test_tar_with_empty_directory(self):
21832182
base = tempfile.mkdtemp()
21842183
self.addCleanup(shutil.rmtree, base)
21852184
for d in ['foo', 'bar']:
21862185
os.makedirs(os.path.join(base, d))
2187-
archive = docker.utils.tar(base)
2188-
tar = tarfile.open(fileobj=archive)
2189-
self.assertEqual(sorted(tar.getnames()), ['bar', 'foo'])
2186+
with docker.utils.tar(base) as archive:
2187+
tar = tarfile.open(fileobj=archive)
2188+
self.assertEqual(sorted(tar.getnames()), ['bar', 'foo'])
21902189

21912190
def test_tar_with_file_symlinks(self):
21922191
base = tempfile.mkdtemp()
@@ -2195,19 +2194,19 @@ def test_tar_with_file_symlinks(self):
21952194
f.write("content")
21962195
os.makedirs(os.path.join(base, 'bar'))
21972196
os.symlink('../foo', os.path.join(base, 'bar/foo'))
2198-
archive = docker.utils.tar(base)
2199-
tar = tarfile.open(fileobj=archive)
2200-
self.assertEqual(sorted(tar.getnames()), ['bar', 'bar/foo', 'foo'])
2197+
with docker.utils.tar(base) as archive:
2198+
tar = tarfile.open(fileobj=archive)
2199+
self.assertEqual(sorted(tar.getnames()), ['bar', 'bar/foo', 'foo'])
22012200

22022201
def test_tar_with_directory_symlinks(self):
22032202
base = tempfile.mkdtemp()
22042203
self.addCleanup(shutil.rmtree, base)
22052204
for d in ['foo', 'bar']:
22062205
os.makedirs(os.path.join(base, d))
22072206
os.symlink('../foo', os.path.join(base, 'bar/foo'))
2208-
archive = docker.utils.tar(base)
2209-
tar = tarfile.open(fileobj=archive)
2210-
self.assertEqual(sorted(tar.getnames()), ['bar', 'bar/foo', 'foo'])
2207+
with docker.utils.tar(base) as archive:
2208+
tar = tarfile.open(fileobj=archive)
2209+
self.assertEqual(sorted(tar.getnames()), ['bar', 'bar/foo', 'foo'])
22112210

22122211

22132212
class StreamTest(Cleanup, unittest.TestCase):
@@ -2293,21 +2292,21 @@ def test_early_stream_response(self):
22932292
b'\r\n'
22942293
) + b'\r\n'.join(lines)
22952294

2296-
client = docker.Client(base_url="http+unix://" + self.socket_file)
2297-
for i in range(5):
2298-
try:
2299-
stream = client.build(
2300-
path=self.build_context,
2301-
stream=True
2302-
)
2303-
break
2304-
except requests.ConnectionError as e:
2305-
if i == 4:
2306-
raise e
2307-
2308-
self.assertEqual(list(stream), [
2309-
str(i).encode() for i in range(50)])
2310-
2295+
with docker.Client(base_url="http+unix://" + self.socket_file) \
2296+
as client:
2297+
for i in range(5):
2298+
try:
2299+
stream = client.build(
2300+
path=self.build_context,
2301+
stream=True
2302+
)
2303+
break
2304+
except requests.ConnectionError as e:
2305+
if i == 4:
2306+
raise e
2307+
2308+
self.assertEqual(list(stream), [
2309+
str(i).encode() for i in range(50)])
23112310

23122311
if __name__ == '__main__':
23132312
unittest.main()

0 commit comments

Comments
 (0)