git: 5e598a761c37 - stable/13 - pytest: delete interfaces from inside the jail.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 09 Feb 2023 16:11:26 UTC
The branch stable/13 has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=5e598a761c37dbd491579a0805968ae50ac72465 commit 5e598a761c37dbd491579a0805968ae50ac72465 Author: Alexander V. Chernikov <melifaro@FreeBSD.org> AuthorDate: 2023-01-25 16:32:52 +0000 Commit: Alexander V. Chernikov <melifaro@FreeBSD.org> CommitDate: 2023-02-09 15:46:25 +0000 pytest: delete interfaces from inside the jail. This change follows the approach used in 80fc25025ffc, to minimise the impact of the delayed interface migration. MFC after: 2 weeks (cherry picked from commit 20ea7f26e41375828a390fba613b86acfe366add) --- tests/atf_python/sys/net/vnet.py | 46 ++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/tests/atf_python/sys/net/vnet.py b/tests/atf_python/sys/net/vnet.py index 1a8efe0d9edc..1f61269ffe6c 100644 --- a/tests/atf_python/sys/net/vnet.py +++ b/tests/atf_python/sys/net/vnet.py @@ -1,6 +1,7 @@ #!/usr/local/bin/python3 import copy import ipaddress +import re import os import socket import sys @@ -150,6 +151,7 @@ class VnetInterface(object): class IfaceFactory(object): INTERFACES_FNAME = "created_ifaces.lst" + AUTODELETE_TYPES = ("epair", "lo", "tap", "tun") def __init__(self): self.file_name = self.INTERFACES_FNAME @@ -158,19 +160,46 @@ class IfaceFactory(object): with open(self.file_name, "a") as f: f.write(iface_name + "\n") + def _list_ifaces(self) -> List[str]: + ret: List[str] = [] + try: + with open(self.file_name, "r") as f: + for line in f: + ret.append(line.strip()) + except OSError: + pass + return ret + def create_iface(self, alias_name: str, iface_name: str) -> List[VnetInterface]: ifaces = VnetInterface.create_iface(alias_name, iface_name) for iface in ifaces: - self._register_iface(iface.name) + if not self.is_autodeleted(iface.name): + self._register_iface(iface.name) return ifaces + @staticmethod + def is_autodeleted(iface_name: str) -> bool: + iface_type = re.split(r"\d+", iface_name)[0] + return iface_type in IfaceFactory.AUTODELETE_TYPES + + def cleanup_vnet_interfaces(self, vnet_name: str) -> List[str]: + """Destroys""" + ifaces_lst = ToolsHelper.get_output( + "/usr/sbin/jexec {} ifconfig -l".format(vnet_name) + ) + for iface_name in ifaces_lst.split(): + if not self.is_autodeleted(iface_name): + if iface_name not in self._list_ifaces(): + print("Skipping interface {}:{}".format(vnet_name, iface_name)) + continue + run_cmd( + "/usr/sbin/jexec {} ifconfig {} destroy".format(vnet_name, iface_name) + ) + def cleanup(self): try: - with open(self.file_name, "r") as f: - for line in f: - run_cmd("/sbin/ifconfig {} destroy".format(line.strip())) os.unlink(self.INTERFACES_FNAME) - except Exception: + except OSError: pass @@ -260,7 +289,7 @@ class VnetFactory(object): try: jid_str = run_cmd(cmd) jid = int(jid_str) - except ValueError as e: + except ValueError: print("Jail creation failed, output: {}".format(jid_str)) raise self._register_vnet(vnet_name) @@ -276,13 +305,12 @@ class VnetFactory(object): return VnetInstance(vnet_alias, vnet_name, jid, ifaces) def cleanup(self): + iface_factory = IfaceFactory() try: with open(self.file_name) as f: for line in f: vnet_name = line.strip() - ToolsHelper.print_output( - "/usr/sbin/jexec {} ifconfig -l".format(vnet_name) - ) + iface_factory.cleanup_vnet_interfaces(vnet_name) run_cmd("/usr/sbin/jail -r {}".format(vnet_name)) os.unlink(self.JAILS_FNAME) except OSError: