Skip to content

Commit

Permalink
test-driver.py: fix VM state directory deletion
Browse files Browse the repository at this point in the history
The previous version of the code would only kick in if the state
directory path pointed at a *file*, which never occurs. Making that
codepath actually work reveals an ordering bug, which this patch fixes
as well.

It also replaces the confusing, imperative case log message "delete VM
state directory" with "deleting VM state directory".

Finally, we hint the user about how to prevent this deletion. IE. by
passing the --keep-vm-state flag.

Bug report:
NixOS#91046 (comment)

Credit goes to Edef for the rebase on top of a recent nixpkgs commit
and for writing most of this commit message.

Co-authored-by: edef <edef@edef.eu>
  • Loading branch information
picnoir and edef1c committed Sep 7, 2020
1 parent 2ff0bdf commit ecb73fd
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions nixos/lib/test-driver/test-driver.py
Expand Up @@ -217,7 +217,7 @@ def __init__(self, args: Dict[str, Any]) -> None:
match = re.search("run-(.+)-vm$", cmd)
if match:
self.name = match.group(1)

self.logger = args["log"]
self.script = args.get("startCommand", self.create_startcommand(args))

tmp_dir = os.environ.get("TMPDIR", tempfile.gettempdir())
Expand All @@ -227,15 +227,17 @@ def create_dir(name: str) -> str:
os.makedirs(path, mode=0o700, exist_ok=True)
return path

self.state_dir = create_dir("vm-state-{}".format(self.name))
self.state_dir = os.path.join(tmp_dir, f"vm-state-{self.name}")
if not args["keepVmState"]:
self.cleanup_statedir()
os.makedirs(self.state_dir, mode=0o700, exist_ok=True)
self.shared_dir = create_dir("shared-xchg")

self.booted = False
self.connected = False
self.pid: Optional[int] = None
self.socket = None
self.monitor: Optional[socket.socket] = None
self.logger: Logger = args["log"]
self.allow_reboot = args.get("allowReboot", False)

@staticmethod
Expand Down Expand Up @@ -780,9 +782,10 @@ def process_serial_output() -> None:
self.log("QEMU running (pid {})".format(self.pid))

def cleanup_statedir(self) -> None:
self.log("delete the VM state directory")
if os.path.isfile(self.state_dir):
if os.path.isdir(self.state_dir):
shutil.rmtree(self.state_dir)
self.logger.log(f"deleting VM state directory {self.state_dir}")
self.logger.log("if you want to keep the VM state, pass --keep-vm-state")

def shutdown(self) -> None:
if not self.booted:
Expand Down Expand Up @@ -940,10 +943,10 @@ def subtest(name: str) -> Iterator[None]:
for nr, vde_socket, _, _ in vde_sockets:
os.environ["QEMU_VDE_SOCKET_{}".format(nr)] = vde_socket

machines = [create_machine({"startCommand": s}) for s in vm_scripts]
for machine in machines:
if not cli_args.keep_vm_state:
machine.cleanup_statedir()
machines = [
create_machine({"startCommand": s, "keepVmState": cli_args.keep_vm_state})
for s in vm_scripts
]
machine_eval = [
"{0} = machines[{1}]".format(m.name, idx) for idx, m in enumerate(machines)
]
Expand Down

0 comments on commit ecb73fd

Please sign in to comment.