Skip to content

Commit

Permalink
skip test if cannot get hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 29, 2024
1 parent 2f0594a commit 059166b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/findssh/__init__.py
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion src/findssh/tests/test_coro.py
@@ -1,5 +1,6 @@
import ipaddress
import asyncio
import pytest

import findssh
import findssh.coro
Expand All @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion 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

Expand All @@ -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)
Expand Down

0 comments on commit 059166b

Please sign in to comment.