Skip to content

Commit 22172e6

Browse files
committed
Move and refactor broken hardlink tests
1 parent c6df67d commit 22172e6

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

test/unit/test_dss_client.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,6 @@ def _assert_manifest_not_updated(self):
186186
for row in ManifestDownloadContext._parse_manifest(self.manifest_file):
187187
self.assertNotIn('file_path', row)
188188

189-
def _assert_link_fails(self, num_logs, download_func, *args, **kwargs):
190-
"""Test that download_func(*args, **kwargs) fails to link num_logs times"""
191-
os_error = OSError()
192-
os_error.errno = errno.EMLINK
193-
for error in [os_error, ValueError(), RuntimeError()]:
194-
with self.subTest(error=error):
195-
with patch('os.link', side_effect=os_error):
196-
from hca.dss.util import log as log_
197-
with self.assertLogs(logger=log_, level=logging.WARNING) as logs:
198-
download_func(*args, **kwargs)
199-
for record in logs.records:
200-
self.assertEqual(record.funcName, 'hardlink')
201-
self.assertEqual(record.msg, 'Failed to link source `%s` to destination `%s`; reverting to copying')
202-
self.assertEqual(len(logs.records), num_logs)
203-
204189

205190
class TestManifestDownloadFilestore(DSSClientTestCase):
206191

@@ -358,12 +343,6 @@ def _assert_bundle_json(self, prefix=''):
358343
actual_hash = hashlib.sha256(f.read()).hexdigest()
359344
self.assertEqual(actual_hash, expected_hash)
360345

361-
@unittest.skipIf(os.name is 'nt', 'os.link is not used on Windows')
362-
def test_link_fails(self):
363-
"""If hardlinking fails, we should revert to copy"""
364-
num_logs = 9 # 3 fake bundles with 3 data files each
365-
self._assert_link_fails(num_logs, self._mock_download_manifest, self.manifest_file, 'aws', layout='bundle')
366-
367346
def test_download_dir_empty(self):
368347
self._test_download_dir('')
369348

@@ -498,11 +477,6 @@ def test_download_dir(self):
498477
def test_download_dir_dot_dir(self):
499478
self._test_download_dir(os.path.join('.', 'a_nested_dir'))
500479

501-
@unittest.skipIf(os.name is 'nt', 'os.link is not used on Windows')
502-
def test_link_fails(self):
503-
num_logs = 5 # 4 files in bundle, + bundle manifest
504-
self._assert_link_fails(num_logs, self._mock_download, 'any_bundle_uuid', 'aws')
505-
506480
@staticmethod
507481
def _fake_get_collection(collections):
508482
"""Used for mocking :meth:`hca.dss.DSSClient.get_collection`"""

test/unit/util/__init__.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
import errno
34
import os
45
import sys
56
import unittest
7+
from unittest.mock import patch
8+
9+
from hca.dss.util import hardlink
10+
11+
from test.unit import TmpDirTestCase
612

713
pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')) # noqa
814
sys.path.insert(0, pkg_root) # noqa
@@ -17,5 +23,34 @@ def test_dict_merge(self):
1723
dict3 = {'a': {'b': {'c': 1, 'd': 1}}, 'c': 2, 'd': 2}
1824
self.assertEqual(hca.util._merge_dict(dict1, dict2), dict3)
1925

26+
27+
class TestLinking(TmpDirTestCase):
28+
"""TmpDirTestCase will ensure any links / files we create are cleaned up"""
29+
src = 'link_source'
30+
dst = 'link_destination'
31+
32+
def test_link_limit(self):
33+
"""Ensure that we copy in the case that the link limit is reached"""
34+
os_error = OSError()
35+
os_error.errno = errno.EMLINK
36+
self._link_with_error(self.src, self.dst, os_error)
37+
source_stat = os.stat(self.src)
38+
dest_stat = os.stat(self.dst)
39+
self.assertTrue(source_stat.st_dev != dest_stat.st_dev or source_stat.st_ino != dest_stat.st_ino)
40+
41+
def test_link_fails(self):
42+
"""Ensure that linking fails in other cases"""
43+
for error in (ValueError(), RuntimeError()):
44+
with self.subTest(error=error):
45+
with self.assertRaises(type(error)):
46+
self._link_with_error(self.src, self.dst, error)
47+
48+
def _link_with_error(self, src, dst, error):
49+
with open(src, 'w'):
50+
pass
51+
with patch('os.link', side_effect=error):
52+
hardlink(src, dst)
53+
54+
2055
if __name__ == "__main__":
2156
unittest.main()

0 commit comments

Comments
 (0)