more test coverage
This commit is contained in:
parent
b25dd1e314
commit
1544dc0295
15 changed files with 3150 additions and 424 deletions
|
|
@ -91,3 +91,176 @@ def test_specific_paths_for_hints_differs_between_backends():
|
|||
paths = set(r.specific_paths_for_hints({"nginx"}))
|
||||
assert "/etc/sysconfig/nginx" in paths
|
||||
assert "/etc/sysconfig/nginx.conf" in paths
|
||||
|
||||
|
||||
def test_read_os_release_file_not_found(tmp_path: Path):
|
||||
result = platform._read_os_release(str(tmp_path / "nonexistent"))
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_read_os_release_handles_invalid_line(tmp_path: Path):
|
||||
p = tmp_path / "os-release"
|
||||
p.write_text(
|
||||
"ID=ubuntu\n" "NO_EQUALS_SIGN\n" 'VERSION="22.04"\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
result = platform._read_os_release(str(p))
|
||||
assert result["ID"] == "ubuntu"
|
||||
assert result["VERSION"] == "22.04"
|
||||
assert "NO_EQUALS_SIGN" not in result
|
||||
|
||||
|
||||
def test_detect_platform_debian(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "debian", "VERSION_ID": "11"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "debian"
|
||||
assert result.pkg_backend == "dpkg"
|
||||
|
||||
|
||||
def test_detect_platform_ubuntu(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "ubuntu", "VERSION_ID": "22.04"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "debian"
|
||||
assert result.pkg_backend == "dpkg"
|
||||
|
||||
|
||||
def test_detect_platform_fedora(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "fedora", "VERSION_ID": "38"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "redhat"
|
||||
assert result.pkg_backend == "rpm"
|
||||
|
||||
|
||||
def test_detect_platform_rocky(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "rocky", "VERSION_ID": "9"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "redhat"
|
||||
assert result.pkg_backend == "rpm"
|
||||
|
||||
|
||||
def test_detect_platform_unknown_fallback_to_dpkg(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "unknown"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
monkeypatch.setattr(platform.shutil, "which", lambda x: x == "dpkg")
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "debian"
|
||||
assert result.pkg_backend == "dpkg"
|
||||
|
||||
|
||||
def test_detect_platform_unknown_fallback_to_rpm(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "unknown"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
monkeypatch.setattr(platform.shutil, "which", lambda x: x == "rpm")
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "redhat"
|
||||
assert result.pkg_backend == "rpm"
|
||||
|
||||
|
||||
def test_detect_platform_completely_unknown(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "unknown"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
monkeypatch.setattr(platform.shutil, "which", lambda x: False)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "unknown"
|
||||
assert result.pkg_backend == "unknown"
|
||||
|
||||
|
||||
def test_detect_platform_debian_like(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "linuxmint", "ID_LIKE": "debian"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "debian"
|
||||
assert result.pkg_backend == "dpkg"
|
||||
|
||||
|
||||
def test_detect_platform_rhel_like(monkeypatch):
|
||||
def fake_read_os_release(path: str = "/etc/os-release") -> dict:
|
||||
return {"ID": "centos", "ID_LIKE": "rhel fedora"}
|
||||
|
||||
monkeypatch.setattr(platform, "_read_os_release", fake_read_os_release)
|
||||
result = platform.detect_platform()
|
||||
assert result.os_family == "redhat"
|
||||
assert result.pkg_backend == "rpm"
|
||||
|
||||
|
||||
def test_get_backend_returns_dpkg(monkeypatch):
|
||||
info = platform.PlatformInfo(os_family="debian", pkg_backend="dpkg", os_release={})
|
||||
backend = platform.get_backend(info)
|
||||
assert isinstance(backend, platform.DpkgBackend)
|
||||
assert backend.name == "dpkg"
|
||||
|
||||
|
||||
def test_get_backend_returns_rpm(monkeypatch):
|
||||
info = platform.PlatformInfo(os_family="redhat", pkg_backend="rpm", os_release={})
|
||||
backend = platform.get_backend(info)
|
||||
assert isinstance(backend, platform.RpmBackend)
|
||||
assert backend.name == "rpm"
|
||||
|
||||
|
||||
def test_get_backend_unknown_with_rpm(monkeypatch):
|
||||
info = platform.PlatformInfo(
|
||||
os_family="unknown", pkg_backend="unknown", os_release={}
|
||||
)
|
||||
monkeypatch.setattr(platform.shutil, "which", lambda x: x == "rpm")
|
||||
backend = platform.get_backend(info)
|
||||
assert isinstance(backend, platform.RpmBackend)
|
||||
|
||||
|
||||
def test_get_backend_unknown_with_dpkg(monkeypatch):
|
||||
info = platform.PlatformInfo(
|
||||
os_family="unknown", pkg_backend="unknown", os_release={}
|
||||
)
|
||||
monkeypatch.setattr(platform.shutil, "which", lambda x: x == "dpkg")
|
||||
backend = platform.get_backend(info)
|
||||
assert isinstance(backend, platform.DpkgBackend)
|
||||
|
||||
|
||||
def test_dpkg_backend_specific_paths():
|
||||
backend = platform.DpkgBackend()
|
||||
paths = backend.specific_paths_for_hints({"nginx"})
|
||||
assert "/etc/default/nginx" in paths
|
||||
assert "/etc/init.d/nginx" in paths
|
||||
assert "/etc/sysctl.d/nginx.conf" in paths
|
||||
|
||||
|
||||
def test_rpm_backend_specific_paths():
|
||||
backend = platform.RpmBackend()
|
||||
paths = backend.specific_paths_for_hints({"nginx"})
|
||||
assert "/etc/sysconfig/nginx" in paths
|
||||
assert "/etc/sysconfig/nginx.conf" in paths
|
||||
assert "/etc/sysctl.d/nginx.conf" in paths
|
||||
|
||||
|
||||
def test_is_pkg_config_path_dpkg():
|
||||
backend = platform.DpkgBackend()
|
||||
assert backend.is_pkg_config_path("/etc/apt/sources.list") is True
|
||||
assert backend.is_pkg_config_path("/etc/apt/trusted.gpg") is True
|
||||
assert backend.is_pkg_config_path("/etc/ssh/sshd_config") is False
|
||||
|
||||
|
||||
def test_is_pkg_config_path_rpm():
|
||||
backend = platform.RpmBackend()
|
||||
assert backend.is_pkg_config_path("/etc/dnf/dnf.conf") is True
|
||||
assert backend.is_pkg_config_path("/etc/yum.conf") is True
|
||||
assert backend.is_pkg_config_path("/etc/yum.repos.d/custom.repo") is True
|
||||
assert backend.is_pkg_config_path("/etc/ssh/sshd_config") is False
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue