Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

4.4 KB
Raw Blame
#!/usr/bin/env python3
"""Check legacy backup routing around a controlled update pull failure."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest

ODS = Path(__file__).resolve().parents[1]


class LegacyUpdateBackupTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory(prefix="ods-legacy-update-")
        self.addCleanup(self.temp.cleanup)
        self.root = Path(self.temp.name)
        self.install = self.root / "install with spaces"
        self.binary = self.root / "bin"
        self.binary.mkdir()
        for relative in ("data/open-webui", "config", "lib", "installers/lib"):
            (self.install / relative).mkdir(parents=True)
        for relative in ("ods-backup.sh", "lib/rsync.sh", "lib/backup-paths.sh", "lib/dashboard-private-state.sh",
                         "installers/lib/compose-images.sh", "docker-compose.base.yml", "manifest.json"):
            shutil.copy2(ODS / relative, self.install / relative)
        # Use the real safety checks, with their trusted source dependencies,
        # while keeping the synthetic installation and its data separate.
        (self.install / "scripts").mkdir()
        for helper in ("backup-native-preflight.py", "source-update-preflight.py"):
            (self.install / "scripts" / helper).symlink_to(ODS / "scripts" / helper)
        (self.install / ".compose-flags").write_text("-f docker-compose.base.yml\n")
        (self.install / ".version").write_text("2.6.0\n")
        (self.install / ".env").write_text(
            "ODS_VERSION=2.6.0\nODS_MODE=local\nGPU_BACKEND=cpu\nGPU_COUNT=1\nTIER=1\n"
            "SHIELD_API_KEY=fixture\nLLAMA_CPU_LIMIT=8.0\nLLAMA_CPU_RESERVATION=2.0\n")
        self.payload = b"User data that must survive the failed image pull.\n"
        (self.install / "data/open-webui/notes.txt").write_bytes(self.payload)
        (self.binary / "docker").write_text("""#!/bin/bash
set -euo pipefail
if [[ "$1" == info ]]; then printf '16\\n'; exit 0; fi
if [[ "$1" == compose && " $* " == *" --format json "* ]]; then
  printf '%s\\n' '{"services":{"fixture":{"image":"fixture.invalid/ods:test"}}}'
  exit 0
fi
if [[ "$1" == pull ]]; then echo 'fixture pull stopped after backup' >&2; exit 2; fi
echo "Unexpected fixture Docker command: $*" >&2
exit 91
""")
        (self.binary / "docker").chmod(0o755)
        self.env = dict(os.environ, INSTALL_DIR=str(self.install), ODS_HOME=str(self.install), HOME=str(self.root),
                        PATH=f"{self.binary}:{os.environ['PATH']}", NO_COLOR="1")
        self.env.pop("ODS_DIR", None)

    def update(self):
        result = subprocess.run(["bash", str(ODS / "ods-cli"), "update", "--force"],
                                env=self.env, capture_output=True, text=True, timeout=30)
        self.assertNotEqual(result.returncode, 0, "The fixture intentionally rejects image pulls")
        self.assertIn("fixture pull stopped after backup", result.stdout + result.stderr)
        return result.stdout + result.stderr

    def test_user_data_backup_cannot_substitute_for_update_snapshot(self):
        before_env = (self.install / ".env").read_bytes()
        before_flags = (self.install / ".compose-flags").read_bytes()
        result = subprocess.run(["bash", str(ODS / "ods-cli"), "update", "--force"],
                                env=self.env, capture_output=True, text=True, timeout=30)
        output = result.stdout + result.stderr
        self.assertNotEqual(result.returncode, 0)
        self.assertIn("ods-update.sh is missing or not executable", output)
        self.assertNotIn("fixture pull stopped after backup", output)
        self.assertFalse((self.install / ".backups").exists())
        self.assertEqual((self.install / ".env").read_bytes(), before_env)
        self.assertEqual((self.install / ".compose-flags").read_bytes(), before_flags)

    def test_preferred_snapshot_script_keeps_its_existing_label_contract(self):
        snapshot = self.install / "ods-update.sh"
        snapshot.write_text('#!/bin/bash\nprintf "%s\\n" "$@" > "$(dirname "$0")/snapshot-args"\n')
        snapshot.chmod(0o755)
        self.update()
        arguments = (self.install / "snapshot-args").read_text().splitlines()
        self.assertEqual(arguments[0], "backup")
        self.assertRegex(arguments[1], r"^pre-update-\d{8}-\d{6}$")
        self.assertFalse((self.install / ".backups").exists())


if __name__ == "__main__":
    unittest.main(verbosity=2)