提交 24616a50 编写于 作者: L Lukáš Doktor

utils.network: Use all local addresses in is_port_free

When binding to 0.0.0.0 all local addresses are used and these are
not covered by "localhost" binding. Let's use "" which makes sure we can
bind on all local addresses (including eg. 192.168.122.1).
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 d5611a80
......@@ -50,7 +50,7 @@ def is_port_free(port, address):
try:
s = socket.socket(family, protocol)
if address == "localhost":
s.bind((address, port))
s.bind(("", port))
else:
s.connect((address, port))
return False
......
......@@ -128,6 +128,7 @@ BuildRequires: libcdio
BuildRequires: libvirt-python
BuildRequires: perl-Test-Harness
BuildRequires: psmisc
BuildRequires: python2-netifaces
%if 0%{?rhel}
BuildRequires: PyYAML
%else
......@@ -136,6 +137,7 @@ BuildRequires: python2-yaml
%if %{with_python3}
BuildRequires: python3-libvirt
BuildRequires: python3-yaml
BuildRequires: python3-netifaces
%endif
%endif
......
......@@ -34,3 +34,6 @@ lxml==4.2.4
# in make develop. The proper solution would be migrate
# our make develop process to use pip
libvirt-python==4.6.0
# For avocado.utils.network selftests
netifaces
import netifaces
import socket
import unittest
......@@ -34,17 +35,29 @@ class PortTrackerTest(unittest.TestCase):
self.assertNotIn(22, tracker.retained_ports)
def get_all_local_addrs():
"""
Returns all ipv4/ipv6 addresses that are associated with this machine
"""
ipv4_addrs = []
ipv6_addrs = []
for interface in netifaces.interfaces():
ifaddresses = netifaces.ifaddresses(interface)
ipv4_addrs += [_['addr']
for _ in ifaddresses.get(netifaces.AF_INET, [])]
ipv6_addrs += [_['addr']
for _ in ifaddresses.get(netifaces.AF_INET6, [])]
return ipv4_addrs, ipv6_addrs
class FreePort(unittest.TestCase):
def test_is_port_free(self):
port = network.find_free_port(sequent=False)
if port is None:
# Use this temporarily as in Travis the current implementation
# fails to find free port.
self.skipTest("No free port")
self.assertTrue(network.is_port_free(port, "localhost"))
ipv4_addrs = ["localhost", "127.0.0.1"]
ipv6_addrs = ["localhost", "::1"]
local_addrs = get_all_local_addrs()
ipv4_addrs = ["localhost", ""] + list(local_addrs[0])
ipv6_addrs = ["localhost", ""] + list(local_addrs[1])
good = []
bad = []
skip = []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册