From 059166b829381a560f8ad2d0fa0e04dec7523ae6 Mon Sep 17 00:00:00 2001 From: scivision Date: Sun, 28 Jan 2024 22:02:50 -0500 Subject: [PATCH] skip test if cannot get hostname --- src/findssh/__init__.py | 5 ++++- src/findssh/tests/test_coro.py | 9 ++++++++- src/findssh/tests/test_threadpool.py | 10 +++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/findssh/__init__.py b/src/findssh/__init__.py index a8d4c7f..ca13955 100644 --- a/src/findssh/__init__.py +++ b/src/findssh/__init__.py @@ -13,7 +13,10 @@ def get_lan_ip() -> ipaddress.IPv4Address | ipaddress.IPv6Address: ref: http://stackoverflow.com/a/23822431 """ - return ipaddress.ip_address(socket.gethostbyname(socket.gethostname())) + if not (name := socket.gethostname()): + raise OSError("cannot get hostname") + + return ipaddress.ip_address(socket.gethostbyname(name)) def address2net( diff --git a/src/findssh/tests/test_coro.py b/src/findssh/tests/test_coro.py index 24afbe1..72069d5 100755 --- a/src/findssh/tests/test_coro.py +++ b/src/findssh/tests/test_coro.py @@ -1,5 +1,6 @@ import ipaddress import asyncio +import pytest import findssh import findssh.coro @@ -9,7 +10,13 @@ def test_coroutine(): - net = findssh.address2net(findssh.get_lan_ip()) + try: + ip = findssh.get_lan_ip() + except OSError as excp: + if excp == "cannot get hostname": + pytest.skip("cannot get hostname") + + net = findssh.address2net(ip) hosts = asyncio.run(findssh.coro.get_hosts(net, PORT, TIMEOUT)) for host, svc in hosts: assert isinstance(host, ipaddress.IPv4Address) diff --git a/src/findssh/tests/test_threadpool.py b/src/findssh/tests/test_threadpool.py index 1e5f35f..e751696 100644 --- a/src/findssh/tests/test_threadpool.py +++ b/src/findssh/tests/test_threadpool.py @@ -1,7 +1,9 @@ """ normally we use coroutines, but for demo purposes we have threadpool too. """ + import ipaddress +import pytest import findssh.threadpool @@ -10,7 +12,13 @@ def test_threadpool(): - net = findssh.address2net(findssh.get_lan_ip()) + try: + ip = findssh.get_lan_ip() + except OSError as excp: + if excp == "cannot get hostname": + pytest.skip("cannot get hostname") + + net = findssh.address2net(ip) hosts = findssh.threadpool.get_hosts(net, PORT, TIMEOUT) for host, svc in hosts: assert isinstance(host, ipaddress.IPv4Address)