119 lines
5.7 KiB
Python
119 lines
5.7 KiB
Python
"""Run real packaged update/restart/rollback against a disposable bundle copy."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
import uuid
|
|
|
|
import win32gui
|
|
import win32process
|
|
|
|
from dx_productivity.update.installer import InstallPlan, prepare_updater
|
|
from packaged_menu import tools_running, wait_for
|
|
from toolbar_ui import ToolbarUI
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("release", type=Path)
|
|
parser.add_argument("archive", type=Path)
|
|
parser.add_argument("--collapsed", action="store_true")
|
|
parser.add_argument("--evidence", type=Path)
|
|
parser.add_argument("--test-root", type=Path, help="Disposable copy parent on a disk with room for backup and staging")
|
|
parser.add_argument("--exit-method", choices=("menu", "window-close"), default="menu")
|
|
options = parser.parse_args()
|
|
test_root = options.test_root or Path(tempfile.gettempdir())
|
|
test_root.mkdir(parents=True, exist_ok=True)
|
|
base = test_root.resolve() / f"dx-packaged-update-{uuid.uuid4().hex[:8]}"
|
|
current = base / "中文 便携工具"
|
|
shutil.copytree(options.release, current)
|
|
marker = current / "before-update.txt"
|
|
marker.write_text("old-copy", encoding="utf-8")
|
|
executable = current / "DxDesignerTools.exe"
|
|
environment = os.environ.copy()
|
|
environment["PATH"] = r"C:\Windows\System32;C:\Windows"
|
|
environment.pop("PYTHONPATH", None)
|
|
environment.pop("QT_QPA_PLATFORM", None)
|
|
profile = base / "profile"
|
|
scratch = base / "scratch"
|
|
profile.mkdir()
|
|
scratch.mkdir()
|
|
environment["LOCALAPPDATA"] = str(profile)
|
|
environment["TEMP"] = environment["TMP"] = str(scratch)
|
|
updater = prepare_updater(current, base / "cache")
|
|
old_ui = subprocess.Popen([str(executable), "ui"], env=environment, cwd=current)
|
|
old_toolbar = ToolbarUI(old_ui.pid)
|
|
if not options.collapsed:
|
|
old_toolbar.expand()
|
|
|
|
def exit_toolbar(toolbar):
|
|
if options.exit_method == "window-close":
|
|
# The toolbar's closeEvent uses request_exit, including shutdown;
|
|
# the shared panel's closeEvent only collapses and is not used.
|
|
handle = toolbar.toolbar.handle
|
|
assert win32process.GetWindowThreadProcessId(handle)[1] == toolbar.pid
|
|
win32gui.PostMessage(handle, 0x10, 0, 0)
|
|
toolbar.toolbar.wait_not("exists", timeout=15)
|
|
else:
|
|
toolbar.exit()
|
|
digest = hashlib.sha256(options.archive.read_bytes()).hexdigest()
|
|
plan = InstallPlan(current_root=current, staged_zip=options.archive.resolve(), backup_root=base / "backups",
|
|
restart_command=(str(executable), "ui", "--smoke-test-ms", "250"),
|
|
launch_command=(str(executable), "ui"), sha256=digest)
|
|
plan_file = base / "install-plan.json"
|
|
plan_file.write_text(json.dumps(plan.to_mapping(), ensure_ascii=False, indent=2), encoding="utf-8")
|
|
process = subprocess.Popen([str(updater), "update", "--plan", str(plan_file)], env=environment, cwd=updater.parent)
|
|
time.sleep(2)
|
|
assert process.poll() is None and marker.exists(), "Updater failed to wait for the running UI"
|
|
exit_toolbar(old_toolbar)
|
|
assert old_ui.wait(15) == 0
|
|
return_code = process.wait(60)
|
|
install_log = base / "install.log"
|
|
assert return_code == 0, (return_code, str(base), install_log.read_text(encoding="utf-8") if install_log.exists() else "No install log")
|
|
assert not marker.exists()
|
|
assert any(p.is_file() for p in (base / "backups").glob("*/before-update.txt"))
|
|
|
|
def restarted():
|
|
for item in tools_running():
|
|
if Path(item.executable).resolve() == executable.resolve():
|
|
return item
|
|
tool = wait_for(restarted)
|
|
updated_toolbar = ToolbarUI(tool.pid)
|
|
time.sleep(2)
|
|
assert updated_toolbar.toolbar.exists(), "Updated UI closed after the smoke probe"
|
|
exit_toolbar(updated_toolbar)
|
|
wait_for(lambda: not restarted(), 15)
|
|
print("PASS PACKAGED UPDATE AND PERSISTENT RESTART", flush=True)
|
|
|
|
rollback_marker = current / "before-rollback.txt"
|
|
rollback_marker.write_text("restore-me", encoding="utf-8")
|
|
rollback = InstallPlan(current_root=current, staged_zip=options.archive.resolve(), backup_root=base / "rollback-backups",
|
|
restart_command=(r"C:\Windows\System32\cmd.exe", "/d", "/c", "exit", "3"), sha256=digest)
|
|
rollback_file = base / "rollback-plan.json"
|
|
rollback_file.write_text(json.dumps(rollback.to_mapping(), ensure_ascii=False, indent=2), encoding="utf-8")
|
|
failure = subprocess.run([str(updater), "update", "--plan", str(rollback_file)], env=environment, cwd=updater.parent, timeout=60)
|
|
assert failure.returncode == 1
|
|
assert rollback_marker.read_text(encoding="utf-8") == "restore-me"
|
|
assert (current / "_runtime/python312.dll").is_file()
|
|
assert "rolled-back" in (base / "install.log").read_text(encoding="utf-8")
|
|
result = {"release": str(options.release.resolve()), "archive_sha256": digest, "test_root": str(base),
|
|
"waited_for_open_ui": True, "installed": True, "persistent_restart": True,
|
|
"smoke_failure_rolled_back": True, "source_release_modified": False, "python_on_path": False,
|
|
"old_ui_collapsed": options.collapsed, "exit_method": options.exit_method,
|
|
"profile_isolated": True}
|
|
report = options.evidence or Path.cwd() / "build/test-evidence/packaged-update.json"
|
|
report.parent.mkdir(parents=True, exist_ok=True)
|
|
report.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print("PASS PACKAGED UPDATE ROLLBACK", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|