diff options
author | Mike Gilbert <floppym@gentoo.org> | 2024-03-02 21:28:42 -0500 |
---|---|---|
committer | Mike Gilbert <floppym@gentoo.org> | 2024-03-15 16:05:34 -0400 |
commit | 0cc3f9e269b26173c2f81d731c5fe208b758270b (patch) | |
tree | 70cd2dbbbe52d966c333abf10a66001087edac1a | |
parent | Replace linux_reflink extension module (diff) | |
download | portage-0cc3f9e2.tar.gz portage-0cc3f9e2.tar.bz2 portage-0cc3f9e2.zip |
Improve testCopyFileSparse
Actually create sparse blocks at the start and end.
Check file size before/after copying.
Ensure sparse output when _fastcopy succeeds.
Signed-off-by: Mike Gilbert <floppym@gentoo.org>
-rw-r--r-- | lib/portage/tests/util/file_copy/test_copyfile.py | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/portage/tests/util/file_copy/test_copyfile.py b/lib/portage/tests/util/file_copy/test_copyfile.py index e91a47bed..e114e6ae3 100644 --- a/lib/portage/tests/util/file_copy/test_copyfile.py +++ b/lib/portage/tests/util/file_copy/test_copyfile.py @@ -3,13 +3,14 @@ import shutil import tempfile +from unittest.mock import patch import pytest from portage import os from portage.tests import TestCase from portage.checksum import perform_md5 -from portage.util.file_copy import copyfile +from portage.util.file_copy import copyfile, _fastcopy class CopyFileTestCase(TestCase): @@ -42,25 +43,37 @@ class CopyFileSparseTestCase(TestCase): # files too big, in case the filesystem doesn't support # sparse files. with open(src_path, "wb") as f: + f.seek(2**16, os.SEEK_SET) f.write(content) - f.seek(2**17, 1) - f.write(content) - f.seek(2**18, 1) + f.seek(2**17, os.SEEK_SET) f.write(content) # Test that sparse blocks are handled correctly at - # the end of the file (involves seek and truncate). - f.seek(2**17, 1) + # the end of the file. + f.truncate(2**18) - copyfile(src_path, dest_path) + fastcopy_success = False + + def mock_fastcopy(src, dst): + nonlocal fastcopy_success + _fastcopy(src, dst) + fastcopy_success = True + + with patch("portage.util.file_copy._fastcopy", new=mock_fastcopy): + copyfile(src_path, dest_path) self.assertEqual(perform_md5(src_path), perform_md5(dest_path)) - # This last part of the test is expected to fail when sparse - # copy is not implemented, so mark it xfail: - pytest.xfail(reason="sparse copy is not implemented") + src_stat = os.stat(src_path) + dest_stat = os.stat(dest_path) + + self.assertEqual(src_stat.st_size, dest_stat.st_size) # If sparse blocks were preserved, then both files should # consume the same number of blocks. - self.assertEqual(os.stat(src_path).st_blocks, os.stat(dest_path).st_blocks) + # This is expected to fail when sparse copy is not implemented. + if src_stat.st_blocks != dest_stat.st_blocks: + if fastcopy_success: + pytest.fail(reason="sparse copy failed with _fastcopy") + pytest.xfail(reason="sparse copy is not implemented") finally: shutil.rmtree(tempdir) |