diff --git a/tests/test_interfaces.py b/tests/test_interfaces.py new file mode 100644 index 0000000..feb6e25 --- /dev/null +++ b/tests/test_interfaces.py @@ -0,0 +1,160 @@ +import os +import sys +import importlib +from datetime import datetime as real_datetime + +import requests + + +PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +if PROJECT_ROOT not in sys.path: + sys.path.insert(0, PROJECT_ROOT) + + +def import_interfaces(): + module_name = "scripts.interfaces" + if module_name in sys.modules: + del sys.modules[module_name] + return importlib.import_module(module_name) + + +def test_create_hostname(): + interfaces = import_interfaces() + assert interfaces.create_hostname("1.2.3.4") == "SDSAT-1234" + assert interfaces.create_hostname(None) is None + + +def test_get_public_ip_success(monkeypatch): + interfaces = import_interfaces() + + class FakeResp: + def json(self): + return {"ip": "9.9.9.9"} + + monkeypatch.setattr(interfaces.requests, "get", lambda *args, **kwargs: FakeResp()) + assert interfaces.get_public_ip() == "9.9.9.9" + + +def test_get_public_ip_failure(monkeypatch): + interfaces = import_interfaces() + + def boom(*args, **kwargs): + raise requests.RequestException("nope") + + monkeypatch.setattr(interfaces.requests, "get", boom) + assert interfaces.get_public_ip() is None + + +def test_get_mac_addresses(monkeypatch): + interfaces = import_interfaces() + + # Simule 2 interfaces, dont une sans AF_LINK + monkeypatch.setattr(interfaces.netifaces, "interfaces", lambda: ["eth0", "lo"]) + + def fake_ifaddresses(name): + if name == "eth0": + return {interfaces.netifaces.AF_LINK: [{"addr": "AA:BB:CC:DD:EE:FF"}]} + return {} # lo => KeyError sur AF_LINK + + monkeypatch.setattr(interfaces.netifaces, "ifaddresses", fake_ifaddresses) + + assert interfaces.get_mac_addresses() == ["AA:BB:CC:DD:EE:FF"] + + +def test_get_interface_info_found(monkeypatch): + interfaces = import_interfaces() + + monkeypatch.setattr(interfaces.netifaces, "interfaces", lambda: ["eth0"]) + + def fake_ifaddresses(name): + return { + interfaces.netifaces.AF_LINK: [{"addr": "AA:BB"}], + interfaces.netifaces.AF_INET: [{"addr": "192.168.1.10"}], + } + + monkeypatch.setattr(interfaces.netifaces, "ifaddresses", fake_ifaddresses) + + ip, name = interfaces.get_interface_info("AA:BB") + assert ip == "192.168.1.10" + assert name == "eth0" + + +def test_get_interface_info_not_found(monkeypatch): + interfaces = import_interfaces() + + monkeypatch.setattr(interfaces.netifaces, "interfaces", lambda: ["eth0"]) + + def fake_ifaddresses(name): + return { + interfaces.netifaces.AF_LINK: [{"addr": "AA:BB"}], + interfaces.netifaces.AF_INET: [{"addr": "192.168.1.10"}], + } + + monkeypatch.setattr(interfaces.netifaces, "ifaddresses", fake_ifaddresses) + + ip, name = interfaces.get_interface_info("ZZ:ZZ") + assert ip is None + assert name is None + + +def test_get_network_interface_info(monkeypatch): + interfaces = import_interfaces() + + monkeypatch.setattr(interfaces, "get_mac_addresses", lambda: ["M1", "M2"]) + + def fake_get_interface_info(mac): + if mac == "M1": + return "10.0.0.1", "eth0" + return None, None + + monkeypatch.setattr(interfaces, "get_interface_info", fake_get_interface_info) + + assert interfaces.get_network_interface_info() == [("M1", "10.0.0.1", "eth0")] + + +def test_write_log_writes_to_file(monkeypatch, tmp_path): + interfaces = import_interfaces() + + # Fixe la date pour un test stable + class FakeDateTime: + @staticmethod + def now(): + return real_datetime(2025, 1, 2, 3, 4, 5) + + @staticmethod + def strftime(fmt): # pas utilisé ici + return "" + + # On patch datetime.datetime.now via le module datetime importé + monkeypatch.setattr(interfaces.datetime, "datetime", FakeDateTime) + + interfaces.write_log("hello", log_dir=str(tmp_path)) + + log_file = tmp_path / "interfaces_check_log.txt" + assert log_file.exists() + + content = log_file.read_text(encoding="utf-8") + assert "2025-01-02 03:04:05" in content + assert "hello" in content + + +def test_main_runs_without_real_side_effects(monkeypatch, tmp_path): + interfaces = import_interfaces() + + # On neutralise les dépendances externes + monkeypatch.setattr(interfaces, "get_network_interface_info", lambda: []) + monkeypatch.setattr(interfaces, "get_public_ip", lambda: "1.2.3.4") + + # Capture les logs écrits + lines = [] + + def fake_write_log(action, log_dir=None): + lines.append(action) + + monkeypatch.setattr(interfaces, "write_log", fake_write_log) + + interfaces.main(log_dir=str(tmp_path)) + + assert "Aucune carte reseau trouvee." in lines + assert "Adresse IP publique : 1.2.3.4" in lines + assert "Hostname : SDSAT-1234" in lines