test/lvm2: use LoopControl.loop_for_fd

Instead of having custom code that basically duplicates the
functionality of `LoopControl.loop_for_fd` use that instead.
Additionally, the version used in the test had a bug where
it did not re-create the Loop device in the main loop when
it was close due to an error, leading errors in subsequent
usages of the device that would often manifest in CI runs:
  fcntl.ioctl(self.fd, self.LOOP_SET_FD, fd)
  ValueError: file descriptor cannot be a negative integer (-1)
This commit is contained in:
Christian Kellner 2021-11-12 14:16:44 +00:00
parent 2e9ffaf517
commit 280aca8f07

View file

@ -2,7 +2,6 @@
# Test for the util.lvm2 module
#
import errno
import os
import json
import subprocess
@ -41,8 +40,6 @@ def tempdir_fixture():
def make_loop(ctl, fd: int, offset, sizelimit, sector_size=512):
lo = loop.Loop(ctl.get_unbound())
if not sizelimit:
stat = os.fstat(fd)
sizelimit = stat.st_size - offset
@ -50,27 +47,7 @@ def make_loop(ctl, fd: int, offset, sizelimit, sector_size=512):
else:
sizelimit *= sector_size
while True:
try:
lo.set_fd(fd)
except OSError as e:
lo.close()
if e.errno == errno.EBUSY:
continue
raise e
# `set_status` returns EBUSY when the pages from the previously
# bound file have not been fully cleared yet.
try:
lo.set_status(offset=offset,
sizelimit=sizelimit,
autoclear=True)
except BlockingIOError:
lo.clear_fd()
lo.close()
continue
break
return lo
return ctl.loop_for_fd(fd, offset, sizelimit=sizelimit, autoclear=True)
def pvcreate(path: PathLike):