nixos/lib/test-driver: make wait_for_unit ask for ActiveState only

This is a hotfix for https://github.com/NixOS/nixpkgs/issues/266690,
where `systemctl show $unit` sometimes randomly leaves ActiveState out
This commit is contained in:
Someone Serge 2023-11-16 17:47:58 +00:00
parent 498c76e2b2
commit 392fbc90a8
No known key found for this signature in database
GPG Key ID: 7B0E3B1390D61DA4

View File

@ -447,8 +447,7 @@ class Machine:
"""
def check_active(_: Any) -> bool:
info = self.get_unit_info(unit, user)
state = info["ActiveState"]
state = self.get_unit_property(unit, "ActiveState", user)
if state == "failed":
raise Exception(f'unit "{unit}" reached state "{state}"')
@ -491,6 +490,35 @@ class Machine:
if line_pattern.match(line)
)
def get_unit_property(
self,
unit: str,
property: str,
user: Optional[str] = None,
) -> str:
status, lines = self.systemctl(
f'--no-pager show "{unit}" --property="{property}"',
user,
)
if status != 0:
raise Exception(
f'retrieving systemctl property "{property}" for unit "{unit}"'
+ ("" if user is None else f' under user "{user}"')
+ f" failed with exit code {status}"
)
invalid_output_message = (
f'systemctl show --property "{property}" "{unit}"'
f"produced invalid output: {lines}"
)
line_pattern = re.compile(r"^([^=]+)=(.*)$")
match = line_pattern.match(lines)
assert match is not None, invalid_output_message
assert match[1] == property, invalid_output_message
return match[2]
def systemctl(self, q: str, user: Optional[str] = None) -> Tuple[int, str]:
"""
Runs `systemctl` commands with optional support for