test-driver.py: directly import pathlib.Path

This commit is contained in:
Patrick Hilhorst 2021-11-20 01:37:08 +01:00
parent 7201052de2
commit 1334a62539
No known key found for this signature in database
GPG key ID: 3BB083304DADC0FF

View file

@ -4,6 +4,7 @@ from queue import Queue, Empty
from typing import Tuple, Any, Callable, Dict, Iterator, Optional, List, Iterable from typing import Tuple, Any, Callable, Dict, Iterator, Optional, List, Iterable
from xml.sax.saxutils import XMLGenerator from xml.sax.saxutils import XMLGenerator
from colorama import Style from colorama import Style
from pathlib import Path
import queue import queue
import io import io
import threading import threading
@ -11,7 +12,6 @@ import argparse
import base64 import base64
import codecs import codecs
import os import os
import pathlib
import ptpython.repl import ptpython.repl
import pty import pty
import re import re
@ -239,8 +239,8 @@ class StartCommand:
def cmd( def cmd(
self, self,
monitor_socket_path: pathlib.Path, monitor_socket_path: Path,
shell_socket_path: pathlib.Path, shell_socket_path: Path,
allow_reboot: bool = False, # TODO: unused, legacy? allow_reboot: bool = False, # TODO: unused, legacy?
) -> str: ) -> str:
display_opts = "" display_opts = ""
@ -272,8 +272,8 @@ class StartCommand:
@staticmethod @staticmethod
def build_environment( def build_environment(
state_dir: pathlib.Path, state_dir: Path,
shared_dir: pathlib.Path, shared_dir: Path,
) -> dict: ) -> dict:
# We make a copy to not update the current environment # We make a copy to not update the current environment
env = dict(os.environ) env = dict(os.environ)
@ -288,10 +288,10 @@ class StartCommand:
def run( def run(
self, self,
state_dir: pathlib.Path, state_dir: Path,
shared_dir: pathlib.Path, shared_dir: Path,
monitor_socket_path: pathlib.Path, monitor_socket_path: Path,
shell_socket_path: pathlib.Path, shell_socket_path: Path,
) -> subprocess.Popen: ) -> subprocess.Popen:
return subprocess.Popen( return subprocess.Popen(
self.cmd(monitor_socket_path, shell_socket_path), self.cmd(monitor_socket_path, shell_socket_path),
@ -334,7 +334,7 @@ class LegacyStartCommand(StartCommand):
self, self,
netBackendArgs: Optional[str] = None, netBackendArgs: Optional[str] = None,
netFrontendArgs: Optional[str] = None, netFrontendArgs: Optional[str] = None,
hda: Optional[Tuple[pathlib.Path, str]] = None, hda: Optional[Tuple[Path, str]] = None,
cdrom: Optional[str] = None, cdrom: Optional[str] = None,
usb: Optional[str] = None, usb: Optional[str] = None,
bios: Optional[str] = None, bios: Optional[str] = None,
@ -394,11 +394,11 @@ class Machine:
the machine lifecycle with the help of a start script / command.""" the machine lifecycle with the help of a start script / command."""
name: str name: str
tmp_dir: pathlib.Path tmp_dir: Path
shared_dir: pathlib.Path shared_dir: Path
state_dir: pathlib.Path state_dir: Path
monitor_path: pathlib.Path monitor_path: Path
shell_path: pathlib.Path shell_path: Path
start_command: StartCommand start_command: StartCommand
keep_vm_state: bool keep_vm_state: bool
@ -421,7 +421,7 @@ class Machine:
def __init__( def __init__(
self, self,
tmp_dir: pathlib.Path, tmp_dir: Path,
start_command: StartCommand, start_command: StartCommand,
name: str = "machine", name: str = "machine",
keep_vm_state: bool = False, keep_vm_state: bool = False,
@ -463,7 +463,7 @@ class Machine:
hda = None hda = None
if args.get("hda"): if args.get("hda"):
hda_arg: str = args.get("hda", "") hda_arg: str = args.get("hda", "")
hda_arg_path: pathlib.Path = pathlib.Path(hda_arg) hda_arg_path: Path = Path(hda_arg)
hda = (hda_arg_path, args.get("hdaInterface", "")) hda = (hda_arg_path, args.get("hdaInterface", ""))
return LegacyStartCommand( return LegacyStartCommand(
netBackendArgs=args.get("netBackendArgs"), netBackendArgs=args.get("netBackendArgs"),
@ -814,12 +814,12 @@ class Machine:
"""Copy a file from the host into the guest via the `shared_dir` shared """Copy a file from the host into the guest via the `shared_dir` shared
among all the VMs (using a temporary directory). among all the VMs (using a temporary directory).
""" """
host_src = pathlib.Path(source) host_src = Path(source)
vm_target = pathlib.Path(target) vm_target = Path(target)
with tempfile.TemporaryDirectory(dir=self.shared_dir) as shared_td: with tempfile.TemporaryDirectory(dir=self.shared_dir) as shared_td:
shared_temp = pathlib.Path(shared_td) shared_temp = Path(shared_td)
host_intermediate = shared_temp / host_src.name host_intermediate = shared_temp / host_src.name
vm_shared_temp = pathlib.Path("/tmp/shared") / shared_temp.name vm_shared_temp = Path("/tmp/shared") / shared_temp.name
vm_intermediate = vm_shared_temp / host_src.name vm_intermediate = vm_shared_temp / host_src.name
self.succeed(make_command(["mkdir", "-p", vm_shared_temp])) self.succeed(make_command(["mkdir", "-p", vm_shared_temp]))
@ -836,11 +836,11 @@ class Machine:
all the VMs (using a temporary directory). all the VMs (using a temporary directory).
""" """
# Compute the source, target, and intermediate shared file names # Compute the source, target, and intermediate shared file names
out_dir = pathlib.Path(os.environ.get("out", os.getcwd())) out_dir = Path(os.environ.get("out", os.getcwd()))
vm_src = pathlib.Path(source) vm_src = Path(source)
with tempfile.TemporaryDirectory(dir=self.shared_dir) as shared_td: with tempfile.TemporaryDirectory(dir=self.shared_dir) as shared_td:
shared_temp = pathlib.Path(shared_td) shared_temp = Path(shared_td)
vm_shared_temp = pathlib.Path("/tmp/shared") / shared_temp.name vm_shared_temp = Path("/tmp/shared") / shared_temp.name
vm_intermediate = vm_shared_temp / vm_src.name vm_intermediate = vm_shared_temp / vm_src.name
intermediate = shared_temp / vm_src.name intermediate = shared_temp / vm_src.name
# Copy the file to the shared directory inside VM # Copy the file to the shared directory inside VM
@ -911,12 +911,12 @@ class Machine:
self.log("starting vm") self.log("starting vm")
def clear(path: pathlib.Path) -> pathlib.Path: def clear(path: Path) -> Path:
if path.exists(): if path.exists():
path.unlink() path.unlink()
return path return path
def create_socket(path: pathlib.Path) -> socket.socket: def create_socket(path: Path) -> socket.socket:
s = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM) s = socket.socket(family=socket.AF_UNIX, type=socket.SOCK_STREAM)
s.bind(str(path)) s.bind(str(path))
s.listen(1) s.listen(1)
@ -1061,7 +1061,7 @@ class VLan:
""" """
nr: int nr: int
socket_dir: pathlib.Path socket_dir: Path
process: subprocess.Popen process: subprocess.Popen
pid: int pid: int
@ -1070,7 +1070,7 @@ class VLan:
def __repr__(self) -> str: def __repr__(self) -> str:
return f"<Vlan Nr. {self.nr}>" return f"<Vlan Nr. {self.nr}>"
def __init__(self, nr: int, tmp_dir: pathlib.Path): def __init__(self, nr: int, tmp_dir: Path):
self.nr = nr self.nr = nr
self.socket_dir = tmp_dir / f"vde{self.nr}.ctl" self.socket_dir = tmp_dir / f"vde{self.nr}.ctl"
@ -1123,7 +1123,7 @@ class Driver:
): ):
self.tests = tests self.tests = tests
tmp_dir = pathlib.Path(os.environ.get("TMPDIR", tempfile.gettempdir())) tmp_dir = Path(os.environ.get("TMPDIR", tempfile.gettempdir()))
tmp_dir.mkdir(mode=0o700, exist_ok=True) tmp_dir.mkdir(mode=0o700, exist_ok=True)
with rootlog.nested("start all VLans"): with rootlog.nested("start all VLans"):
@ -1232,7 +1232,7 @@ class Driver:
"Using legacy create_machine(), please instantiate the" "Using legacy create_machine(), please instantiate the"
"Machine class directly, instead" "Machine class directly, instead"
) )
tmp_dir = pathlib.Path(os.environ.get("TMPDIR", tempfile.gettempdir())) tmp_dir = Path(os.environ.get("TMPDIR", tempfile.gettempdir()))
tmp_dir.mkdir(mode=0o700, exist_ok=True) tmp_dir.mkdir(mode=0o700, exist_ok=True)
if args.get("startCommand"): if args.get("startCommand"):
@ -1318,7 +1318,7 @@ if __name__ == "__main__":
action=EnvDefault, action=EnvDefault,
envvar="testScript", envvar="testScript",
help="the test script to run", help="the test script to run",
type=pathlib.Path, type=Path,
) )
args = arg_parser.parse_args() args = arg_parser.parse_args()