Skip to content

Commit

Permalink
nixos/test-driver: target python 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
NickCao committed Nov 22, 2024
1 parent 5b5f018 commit 172a35f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
1 change: 1 addition & 0 deletions nixos/lib/test-driver/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ find = {}
test_driver = ["py.typed"]

[tool.ruff]
target-version = "py312"
line-length = 88

lint.select = ["E", "F", "I", "U", "N"]
Expand Down
12 changes: 6 additions & 6 deletions nixos/lib/test-driver/test_driver/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import signal
import tempfile
import threading
from collections.abc import Iterator
from collections.abc import Callable, Iterator
from contextlib import AbstractContextManager, contextmanager
from pathlib import Path
from typing import Any, Callable, Optional, Union
from typing import Any

from colorama import Fore, Style

Expand Down Expand Up @@ -208,7 +208,7 @@ def create_machine(
self,
start_command: str | dict,
*,
name: Optional[str] = None,
name: str | None = None,
keep_vm_state: bool = False,
) -> Machine:
# Legacy args handling
Expand Down Expand Up @@ -274,11 +274,11 @@ def check_polling_conditions(self) -> None:

def polling_condition(
self,
fun_: Optional[Callable] = None,
fun_: Callable | None = None,
*,
seconds_interval: float = 2.0,
description: Optional[str] = None,
) -> Union[Callable[[Callable], AbstractContextManager], AbstractContextManager]:
description: str | None = None,
) -> Callable[[Callable], AbstractContextManager] | AbstractContextManager:
driver = self

class Poll:
Expand Down
42 changes: 21 additions & 21 deletions nixos/lib/test-driver/test_driver/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import tempfile
import threading
import time
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from contextlib import _GeneratorContextManager, nullcontext
from pathlib import Path
from queue import Queue
from typing import Any, Callable, Optional
from typing import Any

from test_driver.logger import AbstractLogger

Expand Down Expand Up @@ -249,12 +249,12 @@ class Machine:
start_command: StartCommand
keep_vm_state: bool

process: Optional[subprocess.Popen]
pid: Optional[int]
monitor: Optional[socket.socket]
qmp_client: Optional[QMPSession]
shell: Optional[socket.socket]
serial_thread: Optional[threading.Thread]
process: subprocess.Popen | None
pid: int | None
monitor: socket.socket | None
qmp_client: QMPSession | None
shell: socket.socket | None
serial_thread: threading.Thread | None

booted: bool
connected: bool
Expand All @@ -274,7 +274,7 @@ def __init__(
logger: AbstractLogger,
name: str = "machine",
keep_vm_state: bool = False,
callbacks: Optional[list[Callable]] = None,
callbacks: list[Callable] | None = None,
) -> None:
self.out_dir = out_dir
self.tmp_dir = tmp_dir
Expand Down Expand Up @@ -344,7 +344,7 @@ def send_monitor_command(self, command: str) -> str:
return self.wait_for_monitor_prompt()

def wait_for_unit(
self, unit: str, user: Optional[str] = None, timeout: int = 900
self, unit: str, user: str | None = None, timeout: int = 900
) -> None:
"""
Wait for a systemd unit to get into "active" state.
Expand Down Expand Up @@ -374,7 +374,7 @@ def check_active(_: Any) -> bool:
):
retry(check_active, timeout)

def get_unit_info(self, unit: str, user: Optional[str] = None) -> dict[str, str]:
def get_unit_info(self, unit: str, user: str | None = None) -> dict[str, str]:
status, lines = self.systemctl(f'--no-pager show "{unit}"', user)
if status != 0:
raise Exception(
Expand All @@ -400,7 +400,7 @@ def get_unit_property(
self,
unit: str,
property: str,
user: Optional[str] = None,
user: str | None = None,
) -> str:
status, lines = self.systemctl(
f'--no-pager show "{unit}" --property="{property}"',
Expand All @@ -425,7 +425,7 @@ def get_unit_property(
assert match[1] == property, invalid_output_message
return match[2]

def systemctl(self, q: str, user: Optional[str] = None) -> tuple[int, str]:
def systemctl(self, q: str, user: str | None = None) -> tuple[int, str]:
"""
Runs `systemctl` commands with optional support for
`systemctl --user`
Expand Down Expand Up @@ -481,7 +481,7 @@ def execute(
command: str,
check_return: bool = True,
check_output: bool = True,
timeout: Optional[int] = 900,
timeout: int | None = 900,
) -> tuple[int, str]:
"""
Execute a shell command, returning a list `(status, stdout)`.
Expand Down Expand Up @@ -549,7 +549,7 @@ def execute(

return (rc, output.decode(errors="replace"))

def shell_interact(self, address: Optional[str] = None) -> None:
def shell_interact(self, address: str | None = None) -> None:
"""
Allows you to directly interact with the guest shell. This should
only be used during test development, not in production tests.
Expand Down Expand Up @@ -596,7 +596,7 @@ def console_interact(self) -> None:
break
self.send_console(char.decode())

def succeed(self, *commands: str, timeout: Optional[int] = None) -> str:
def succeed(self, *commands: str, timeout: int | None = None) -> str:
"""
Execute a shell command, raising an exception if the exit status is
not zero, otherwise returning the standard output. Similar to `execute`,
Expand All @@ -613,7 +613,7 @@ def succeed(self, *commands: str, timeout: Optional[int] = None) -> str:
output += out
return output

def fail(self, *commands: str, timeout: Optional[int] = None) -> str:
def fail(self, *commands: str, timeout: int | None = None) -> str:
"""
Like `succeed`, but raising an exception if the command returns a zero
status.
Expand Down Expand Up @@ -725,7 +725,7 @@ def tty_matches(last: bool) -> bool:
with self.nested(f"waiting for {regexp} to appear on tty {tty}"):
retry(tty_matches, timeout)

def send_chars(self, chars: str, delay: Optional[float] = 0.01) -> None:
def send_chars(self, chars: str, delay: float | None = 0.01) -> None:
"""
Simulate typing a sequence of characters on the virtual keyboard,
e.g., `send_chars("foobar\n")` will type the string `foobar`
Expand Down Expand Up @@ -799,10 +799,10 @@ def port_is_closed(_: Any) -> bool:
with self.nested(f"waiting for TCP port {port} on {addr} to be closed"):
retry(port_is_closed, timeout)

def start_job(self, jobname: str, user: Optional[str] = None) -> tuple[int, str]:
def start_job(self, jobname: str, user: str | None = None) -> tuple[int, str]:
return self.systemctl(f"start {jobname}", user)

def stop_job(self, jobname: str, user: Optional[str] = None) -> tuple[int, str]:
def stop_job(self, jobname: str, user: str | None = None) -> tuple[int, str]:
return self.systemctl(f"stop {jobname}", user)

def wait_for_job(self, jobname: str) -> None:
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def console_matches(_: Any) -> bool:
pass

def send_key(
self, key: str, delay: Optional[float] = 0.01, log: Optional[bool] = True
self, key: str, delay: float | None = 0.01, log: bool | None = True
) -> None:
"""
Simulate pressing keys on the virtual keyboard, e.g.,
Expand Down
8 changes: 4 additions & 4 deletions nixos/lib/test-driver/test_driver/polling_condition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
from collections.abc import Callable
from math import isfinite
from typing import Callable, Optional

from test_driver.logger import AbstractLogger

Expand All @@ -12,18 +12,18 @@ class PollingConditionError(Exception):
class PollingCondition:
condition: Callable[[], bool]
seconds_interval: float
description: Optional[str]
description: str | None
logger: AbstractLogger

last_called: float
entry_count: int

def __init__(
self,
condition: Callable[[], Optional[bool]],
condition: Callable[[], bool | None],
logger: AbstractLogger,
seconds_interval: float = 2.0,
description: Optional[str] = None,
description: str | None = None,
):
self.condition = condition # type: ignore
self.seconds_interval = seconds_interval
Expand Down

0 comments on commit 172a35f

Please sign in to comment.