141 lines
5.7 KiB
Python
141 lines
5.7 KiB
Python
"""Unit tests for DiffEngine."""
|
|
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from linux_backuper.core.archiver import SnapshotArchiver
|
|
from linux_backuper.core.diff import DiffEngine
|
|
from linux_backuper.models import SnapshotManifest
|
|
|
|
class TestDiffEngine(unittest.TestCase):
|
|
def setUp(self):
|
|
self.test_dir = tempfile.mkdtemp(prefix="test_diff_")
|
|
self.backup_dir = os.path.join(self.test_dir, "backups")
|
|
os.makedirs(self.backup_dir, exist_ok=True)
|
|
|
|
self.mock_root = os.path.join(self.test_dir, "root")
|
|
self.mock_etc = os.path.join(self.mock_root, "etc")
|
|
os.makedirs(self.mock_etc, exist_ok=True)
|
|
|
|
# File A
|
|
with open(os.path.join(self.mock_etc, "hosts"), "w") as f:
|
|
f.write("127.0.0.1 localhost\n")
|
|
# File B
|
|
with open(os.path.join(self.mock_etc, "issue"), "w") as f:
|
|
f.write("Test Linux\n")
|
|
|
|
# Snapshot 1
|
|
archiver = SnapshotArchiver(target_dir=self.backup_dir)
|
|
entries1, sha1, bytes1, _ = archiver.collect_file_list([self.mock_etc])
|
|
|
|
# Write fake meta/packages.txt into archive
|
|
temp_meta = os.path.join(self.test_dir, "meta1")
|
|
os.makedirs(temp_meta, exist_ok=True)
|
|
pkg_file1 = os.path.join(temp_meta, "packages.txt")
|
|
with open(pkg_file1, "w") as f:
|
|
f.write("bash 5.1.0\ncoreutils 8.32\ncurl 7.74.0\n")
|
|
|
|
entries1.append((pkg_file1, "meta/packages.txt"))
|
|
sha1["meta/packages.txt"] = "dummy"
|
|
|
|
m1 = SnapshotManifest(
|
|
schema_version="1.0",
|
|
snapshot_id="snap1",
|
|
created_at="2026-10-01T12:00:00Z",
|
|
hostname="host1",
|
|
profile="quick",
|
|
compression="gzip",
|
|
os={"distro_id": "debian", "distro_name": "Debian", "version_id": "12", "kernel": "6.1", "arch": "x86_64"},
|
|
package_manager="dpkg",
|
|
package_count=3,
|
|
included_paths=[self.mock_etc],
|
|
stats={"file_count": len(entries1), "uncompressed_bytes": bytes1, "compressed_bytes": 0},
|
|
files_sha256=sha1
|
|
)
|
|
self.archive1 = archiver.create_snapshot_archive("snap1", m1, entries1)
|
|
|
|
# Modify file A, delete file B, add file C for Snapshot 2
|
|
with open(os.path.join(self.mock_etc, "hosts"), "w") as f:
|
|
f.write("127.0.0.1 localhost\n192.168.1.1 router\n")
|
|
os.remove(os.path.join(self.mock_etc, "issue"))
|
|
with open(os.path.join(self.mock_etc, "resolv.conf"), "w") as f:
|
|
f.write("nameserver 1.1.1.1\n")
|
|
|
|
entries2, sha2, bytes2, _ = archiver.collect_file_list([self.mock_etc])
|
|
|
|
temp_meta2 = os.path.join(self.test_dir, "meta2")
|
|
os.makedirs(temp_meta2, exist_ok=True)
|
|
pkg_file2 = os.path.join(temp_meta2, "packages.txt")
|
|
with open(pkg_file2, "w") as f:
|
|
f.write("bash 5.2.0\ncoreutils 8.32\nwget 1.21\n") # bash upgraded, curl removed, wget added
|
|
|
|
entries2.append((pkg_file2, "meta/packages.txt"))
|
|
sha2["meta/packages.txt"] = "dummy2"
|
|
|
|
m2 = SnapshotManifest(
|
|
schema_version="1.0",
|
|
snapshot_id="snap2",
|
|
created_at="2026-10-02T12:00:00Z",
|
|
hostname="host1",
|
|
profile="quick",
|
|
compression="gzip",
|
|
os={"distro_id": "debian", "distro_name": "Debian", "version_id": "12", "kernel": "6.1", "arch": "x86_64"},
|
|
package_manager="dpkg",
|
|
package_count=3,
|
|
included_paths=[self.mock_etc],
|
|
stats={"file_count": len(entries2), "uncompressed_bytes": bytes2, "compressed_bytes": 0},
|
|
files_sha256=sha2
|
|
)
|
|
self.archive2 = archiver.create_snapshot_archive("snap2", m2, entries2)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.test_dir, ignore_errors=True)
|
|
|
|
def test_diff_snapshots(self):
|
|
engine = DiffEngine(target_dir=self.backup_dir)
|
|
res = engine.diff_snapshots("snap1", "snap2")
|
|
|
|
self.assertEqual(res["type"], "snapshot_vs_snapshot")
|
|
self.assertEqual(res["snapshot_1"], "snap1")
|
|
self.assertEqual(res["snapshot_2"], "snap2")
|
|
|
|
files = res["files"]
|
|
# hosts is modified
|
|
self.assertTrue(any("hosts" in f for f in files["modified"]))
|
|
# resolv.conf is added
|
|
self.assertTrue(any("resolv.conf" in f for f in files["added"]))
|
|
# issue is removed
|
|
self.assertTrue(any("issue" in f for f in files["removed"]))
|
|
|
|
# Check packages
|
|
pkgs = res["packages"]
|
|
self.assertIn("wget", pkgs["added"])
|
|
self.assertIn("curl", pkgs["removed"])
|
|
self.assertIn("bash", pkgs["changed"])
|
|
self.assertEqual(pkgs["changed"]["bash"]["old"], "5.1.0")
|
|
self.assertEqual(pkgs["changed"]["bash"]["new"], "5.2.0")
|
|
|
|
def test_diff_with_live(self):
|
|
engine = DiffEngine(target_dir=self.backup_dir)
|
|
# Compare live mock_root against snap1
|
|
# Currently mock_root has modified hosts, deleted issue, added resolv.conf
|
|
res = engine.diff_with_live("snap1", target_root=self.mock_root)
|
|
|
|
self.assertEqual(res["type"], "live_vs_snapshot")
|
|
files = res["files"]
|
|
self.assertTrue(any("hosts" in f for f in files["modified"]))
|
|
self.assertTrue(any("issue" in f for f in files["missing_on_live"]))
|
|
self.assertTrue(any("resolv.conf" in f for f in files["added_on_live"]))
|
|
|
|
def test_generate_unified_file_diff(self):
|
|
engine = DiffEngine(target_dir=self.backup_dir)
|
|
diff_text = engine.generate_unified_file_diff("snap1", "etc/hosts", target_root=self.mock_root)
|
|
self.assertIsNotNone(diff_text)
|
|
self.assertIn("+192.168.1.1 router", diff_text)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|