81 lines
2.5 KiB
Python
81 lines
2.5 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from linux_backuper.core.detector import detect_distro, parse_os_release
|
|
from linux_backuper.models import OsInfo
|
|
|
|
class TestOsDetector(unittest.TestCase):
|
|
def test_parse_almalinux_9(self):
|
|
content = """NAME="AlmaLinux"
|
|
VERSION="9.4 (Seafoam)"
|
|
ID="almalinux"
|
|
ID_LIKE="rhel centos fedora"
|
|
VERSION_ID="9.4"
|
|
PRETTY_NAME="AlmaLinux 9.4 (Seafoam)"
|
|
"""
|
|
info = parse_os_release(content)
|
|
self.assertEqual(info["id"], "almalinux")
|
|
self.assertEqual(info["name"], "AlmaLinux")
|
|
self.assertEqual(info["version_id"], "9.4")
|
|
|
|
def test_parse_debian_12(self):
|
|
content = """PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
|
|
NAME="Debian GNU/Linux"
|
|
VERSION_ID="12"
|
|
VERSION="12 (bookworm)"
|
|
VERSION_CODENAME=bookworm
|
|
ID=debian
|
|
"""
|
|
info = parse_os_release(content)
|
|
self.assertEqual(info["id"], "debian")
|
|
self.assertEqual(info["name"], "Debian GNU/Linux")
|
|
self.assertEqual(info["version_id"], "12")
|
|
|
|
def test_parse_ubuntu_2204(self):
|
|
content = """NAME="Ubuntu"
|
|
VERSION="22.04.4 LTS (Jammy Jellyfish)"
|
|
ID=ubuntu
|
|
ID_LIKE=debian
|
|
PRETTY_NAME="Ubuntu 22.04.4 LTS"
|
|
VERSION_ID="22.04"
|
|
"""
|
|
info = parse_os_release(content)
|
|
self.assertEqual(info["id"], "ubuntu")
|
|
self.assertEqual(info["name"], "Ubuntu")
|
|
self.assertEqual(info["version_id"], "22.04")
|
|
|
|
def test_parse_arch(self):
|
|
content = """NAME="Arch Linux"
|
|
PRETTY_NAME="Arch Linux"
|
|
ID=arch
|
|
BUILD_ID=rolling
|
|
"""
|
|
info = parse_os_release(content)
|
|
self.assertEqual(info["id"], "arch")
|
|
self.assertEqual(info["name"], "Arch Linux")
|
|
self.assertEqual(info["version_id"], "rolling")
|
|
|
|
def test_detect_distro_with_custom_file(self):
|
|
with tempfile.NamedTemporaryFile("w+", delete=False) as f:
|
|
f.write("""NAME="Fedora Linux"
|
|
VERSION="40 (Cloud Edition)"
|
|
ID=fedora
|
|
VERSION_ID="40"
|
|
""")
|
|
temp_path = f.name
|
|
|
|
try:
|
|
os_info = detect_distro(os_release_path=temp_path)
|
|
self.assertIsInstance(os_info, OsInfo)
|
|
self.assertEqual(os_info.distro_id, "fedora")
|
|
self.assertEqual(os_info.distro_name, "Fedora Linux")
|
|
self.assertEqual(os_info.version_id, "40")
|
|
self.assertTrue(len(os_info.arch) > 0)
|
|
self.assertTrue(len(os_info.kernel) > 0)
|
|
finally:
|
|
if os.path.exists(temp_path):
|
|
os.remove(temp_path)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|