From 24397918cdd41b940176f7f64b50a1fa908b7828 Mon Sep 17 00:00:00 2001
From: R1kaB3rN <100738684+R1kaB3rN@users.noreply.github.com>
Date: Fri, 24 Jan 2025 22:21:00 -0800
Subject: [PATCH] tests: add delta update test (#354)

* tests: add e2e test for delta update

* workflows: add delta update test
---
 .github/workflows/e2e.yml | 15 ++++++--
 tests/test_delta.py       | 75 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 tests/test_delta.py

diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml
index c9d66e7d3..0a97adb8a 100644
--- a/.github/workflows/e2e.yml
+++ b/.github/workflows/e2e.yml
@@ -11,7 +11,7 @@ permissions:
 
 jobs:
   build:
-    runs-on: ubuntu-22.04
+    runs-on: ubuntu-latest
 
     steps:
       - uses: actions/checkout@v4
@@ -19,7 +19,10 @@ jobs:
           fetch-depth: 0
       - name: Install dependencies
         run: |
-          sudo apt-get install meson scdoc python3-hatchling python3-build python3-installer python3-filelock shellcheck bubblewrap cargo
+          sudo apt-get update
+          echo "deb http://archive.ubuntu.com/ubuntu/ noble noble-updates noble-backports main universe restricted multiverse" | sudo tee -a /etc/apt/sources.list
+          sudo apt-get install meson scdoc python3-hatchling python3-build python3-installer python3-filelock shellcheck bubblewrap cargo libzstd1 python3-xxhash python3-cbor2 apparmor apparmor-profiles apparmor-profiles-extra python3-xlib
+          sudo apparmor_parser /usr/share/apparmor/extra-profiles/bwrap-userns-restrict
           python3 -m pip install --upgrade pip
           pip install uv
       - name: Initialize submodules
@@ -29,12 +32,20 @@ jobs:
         run: |
           ./configure.sh --user-install
           make install
+      - name: Make system install
+        run: |
+          ./configure.sh --prefix=/usr
+          sudo make install
       - name: Run shellcheck
         run: |
           shellcheck tests/*.sh
       - name: Setup venv
         run: |
           uv venv --python 3.10
+      - name: Test delta update
+        run: |
+          python3 tests/test_delta.py
+          rm -rf "$HOME/.local/share/umu" "$HOME/Games/umu" "$HOME/.local/share/Steam/compatibilitytools.d" "$HOME/.cache/umu"
       - name: Test steamrt install
         run: |
           source .venv/bin/activate
diff --git a/tests/test_delta.py b/tests/test_delta.py
new file mode 100644
index 000000000..33f74c7e0
--- /dev/null
+++ b/tests/test_delta.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+
+import json
+import shutil
+import ssl
+import subprocess
+import sys
+import tarfile
+import tempfile
+from pathlib import Path
+from urllib.request import Request, urlopen
+
+import cbor2
+
+
+def main():  # noqa: D103
+    url = (
+        "https://api.github.com/repos/Open-Wine-Components/umu-mkpatch/releases/latest"
+    )
+    headers = {
+        "Accept": "application/vnd.github+json",
+        "X-GitHub-Api-Version": "2022-11-28",
+        "User-Agent": "",
+    }
+    codename = "UMU-Latest"
+
+    with urlopen(  # noqa: S310
+        Request(url, headers=headers),  # noqa: S310
+        context=ssl.create_default_context(),
+    ) as resp:
+        releases = json.loads(resp.read())
+        for release in releases["assets"]:
+            if not release["name"].endswith("cbor"):
+                continue
+            if release["name"].startswith(codename):
+                durl = release["browser_download_url"]
+                break
+
+    with urlopen(durl) as resp:  # noqa: S310
+        patch = cbor2.loads(resp.read())
+        version = patch.get("contents")[0].get("source")
+
+    url = f"https://github.com/Open-Wine-Components/umu-proton/releases/download/{version}/{version}.tar.gz"
+    with urlopen(url) as resp:  # noqa: S310
+        buffer = bytearray(64 * 1024)
+        view = memoryview(buffer)
+        with tempfile.NamedTemporaryFile(mode="ab+", buffering=0) as file:
+            while size := resp.readinto(buffer):
+                file.write(view[:size])
+            cache = Path.home().joinpath(".cache", "umu")
+            cache.mkdir(parents=True, exist_ok=True)
+            file.seek(0)
+            cache.joinpath(f"{version}.tar.gz").write_bytes(file.read())
+
+    with tarfile.open(cache.joinpath(f"{version}.tar.gz")) as tar:
+        tar.extraction_filter = tarfile.tar_filter
+        tar.extractall(path=cache)  # noqa: S202
+        compat = Path.home().joinpath(".local", "share", "umu", "compatibilitytools")
+        compat.mkdir(parents=True, exist_ok=True)
+        shutil.move(cache.joinpath(version), compat)
+        compat.joinpath(version).rename(compat.joinpath("UMU-Latest"))
+
+    exe = shutil.which("umu-run", path="/usr/local/bin")
+    if exe is None:
+        return 1
+
+    return subprocess.run(
+        (exe, "wineboot", "-u"),
+        env={"PROTONPATH": "UMU-Latest", "GAMEID": "umu-0", "UMU_LOG": "1"},
+        check=False,
+    ).returncode
+
+
+if __name__ == "__main__":
+    sys.exit(main())