test/loop: check for data integrity

Add a simple check that data written through the loop device is
actually ending up in the file. NB: this this will _fail_ if the
fd is cleared via `clear_fd` without the use of `flush_buf`. It
seems that the kernel (as of 5.13.8) will indeed not clear the
buffer cache of the loop device if the backing file is detached
via `LOOP_CLR_FD`. On the other hand, if the autoclear flag is,
i.e. the backing file cleared when the last file descriptor of
the loop device is closed, the buffer cached will be cleared as
part of the `release` operation of the block device.
This commit is contained in:
Christian Kellner 2021-08-13 10:03:44 +00:00 committed by Achilleas Koutsou
parent 43fb869860
commit 4126a3af7c

View file

@ -25,6 +25,8 @@ def tempdir_fixture():
@pytest.mark.skipif(not TestBase.can_bind_mount(), reason="root only")
def test_basic(tempdir):
test_data = b"osbuild"
path = os.path.join(tempdir, "test.img")
ctl = loop.LoopControl()
@ -68,6 +70,15 @@ def test_basic(tempdir):
info = lo.get_status()
assert not info.autoclear
with open(os.path.join("/dev", lo.devname), "wb") as f:
f.write(test_data)
# the `flush_buf` seems to be necessary when calling
# `LoopInfo.clear_fd`, otherwise the data integrity
# check later will fail
lo.flush_buf()
lo.clear_fd()
finally:
if lo:
with contextlib.suppress(OSError):
@ -78,6 +89,11 @@ def test_basic(tempdir):
ctl.close()
# check for data integrity, i.e. that what we wrote via the
# loop device was actually written to the underlying file
with open(path, "rb") as f:
assert f.read(len(test_data)) == test_data
# closing must be a no-op on a closed LoopControl
ctl.close()