about summary refs log tree commit diff
path: root/nixos/lib/test-driver
diff options
context:
space:
mode:
authorJacek Galowicz <jacek.galowicz@cyberus-technology.de>2021-09-27 15:00:58 +0200
committerDavid Arnold <david.arnold@iohk.io>2021-10-05 14:38:48 -0500
commit5c666cdf62615442cab413121384588c6ecebef5 (patch)
tree2ecb8e74398f2225a0ab39e69e9dce2839980f0c /nixos/lib/test-driver
parent32face8dea96371239cb4d48b34086940e43c3c6 (diff)
downloadnixlib-5c666cdf62615442cab413121384588c6ecebef5.tar
nixlib-5c666cdf62615442cab413121384588c6ecebef5.tar.gz
nixlib-5c666cdf62615442cab413121384588c6ecebef5.tar.bz2
nixlib-5c666cdf62615442cab413121384588c6ecebef5.tar.lz
nixlib-5c666cdf62615442cab413121384588c6ecebef5.tar.xz
nixlib-5c666cdf62615442cab413121384588c6ecebef5.tar.zst
nixlib-5c666cdf62615442cab413121384588c6ecebef5.zip
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.
Diffstat (limited to 'nixos/lib/test-driver')
-rwxr-xr-xnixos/lib/test-driver/test-driver.py24
1 files changed, 7 insertions, 17 deletions
diff --git a/nixos/lib/test-driver/test-driver.py b/nixos/lib/test-driver/test-driver.py
index 7cea1d9ce691..56c510c4e692 100755
--- a/nixos/lib/test-driver/test-driver.py
+++ b/nixos/lib/test-driver/test-driver.py
@@ -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"""