Skip to content

Commit bd60248

Browse files
committed
update unit tests
1 parent 38ae711 commit bd60248

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

modules/ip_info/ip_info.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,13 @@ def get_gateway_ip_if_interface(self):
337337
# only works if running on an interface
338338
return False
339339

340-
gw_ip = False
341340
gws = netifaces.gateways()
342341
try:
343-
gw_ip = gws["default"][netifaces.AF_INET][0]
344-
except KeyError:
342+
return gws["default"][netifaces.AF_INET][0]
343+
except (KeyError, IndexError):
345344
# no default gateway found
346345
return False
347346

348-
return gw_ip
349-
350347
def get_gateway_mac(self, gw_ip: str) -> Optional[str]:
351348
"""
352349
Given the gw_ip, this function tries to get the MAC

tests/test_ip_info.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import asyncio
66

7+
import netifaces
8+
79
from tests.module_factory import ModuleFactory
810
import maxminddb
911
import pytest
@@ -405,27 +407,28 @@ async def test_shutdown_gracefully(
405407

406408

407409
@pytest.mark.parametrize(
408-
"platform_system, subprocess_output, expected_ip",
410+
"is_running_non_stop, netifaces_ret, expected_return_value",
409411
[
410-
# Testcase 1: MacOS (Darwin) with valid output
411-
("Darwin", b"gateway: 192.168.1.1", "192.168.1.1"),
412-
# Testcase 2: Linux with valid output
413-
("Linux", b"default via 10.0.0.1 dev eth0", "10.0.0.1"),
414-
# Testcase 3: MacOS with invalid output
415-
("Darwin", b"No default gateway", False),
416-
# Testcase 4: Unsupported OS
417-
("Windows", b"", False),
412+
(
413+
True,
414+
{"default": {netifaces.AF_INET: ["192.168.1.1"]}},
415+
"192.168.1.1",
416+
),
417+
(True, {}, False),
418+
(True, {"default": {}}, False),
419+
(True, {"default": {netifaces.AF_INET: []}}, False),
420+
(False, "192.168.1.1", False),
418421
],
419422
)
420423
def test_get_gateway_ip(
421-
mocker, platform_system, subprocess_output, expected_ip
424+
mocker, is_running_non_stop, netifaces_ret, expected_return_value
422425
):
423426
ip_info = ModuleFactory().create_ip_info_obj()
424-
mocker.patch("platform.system", return_value=platform_system)
425-
mocker.patch("subprocess.check_output", return_value=subprocess_output)
426-
mocker.patch("sys.argv", ["-i", "eth0"])
427+
ip_info.is_running_non_stop = is_running_non_stop
428+
mocker.patch("netifaces.gateways", return_value=netifaces_ret)
429+
427430
result = ip_info.get_gateway_ip_if_interface()
428-
assert result == expected_ip
431+
assert result == expected_return_value
429432

430433

431434
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)