Re-RAII-ify the NixOS integration test driver's VLAN class.

We have no usecase for manually/selectively starting or stopping VLANs
in integration tests.
By starting and stopping the VLANs with the constructor and destructor
of VLAN objects, we remove the obligation and complexity to maintain
network lifetime separately.
This commit is contained in:
Jacek Galowicz 2021-09-27 15:00:58 +02:00 committed by David Arnold
parent 32face8dea
commit 5c666cdf62

View File

@ -1027,16 +1027,16 @@ class Machine:
class VLan:
"""A handle to the vlan with this number, that also knows how to manage
it's lifecycle.
"""This class handles a VLAN that the run-vm scripts identify via its
number handles. The network's lifetime equals the object's lifetime.
"""
nr: int
socket_dir: pathlib.Path
process: Optional[subprocess.Popen]
pid: Optional[int]
fd: Optional[io.TextIOBase]
process: subprocess.Popen
pid: int
fd: io.TextIOBase
def __repr__(self) -> str:
return f"<Vlan Nr. {self.nr}>"
@ -1048,8 +1048,6 @@ class VLan:
# TODO: don't side-effect environment here
os.environ[f"QEMU_VDE_SOCKET_{self.nr}"] = str(self.socket_dir)
def start(self) -> None:
rootlog.info("start vlan")
pty_master, pty_slave = pty.openpty()
@ -1073,12 +1071,8 @@ class VLan:
rootlog.info(f"running vlan (pid {self.pid})")
def release(self) -> None:
if self.pid is None:
return
def __del__(self) -> None:
rootlog.info(f"kill vlan (pid {self.pid})")
assert self.fd
assert self.process
self.fd.close()
self.process.terminate()
@ -1103,10 +1097,8 @@ class Driver:
tmp_dir = pathlib.Path(os.environ.get("TMPDIR", tempfile.gettempdir()))
tmp_dir.mkdir(mode=0o700, exist_ok=True)
self.vlans = [VLan(nr, tmp_dir) for nr in vlans]
with rootlog.nested("start all VLans"):
for vlan in self.vlans:
vlan.start()
self.vlans = [VLan(nr, tmp_dir) for nr in vlans]
def cmd(scripts: List[str]) -> Iterator[NixStartScript]:
for s in scripts:
@ -1127,8 +1119,6 @@ class Driver:
with rootlog.nested("clean up"):
for machine in self.machines:
machine.release()
for vlan in self.vlans:
vlan.release()
def subtest(self, name: str) -> Iterator[None]:
"""Group logs under a given test name"""