Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openeuler
avocado
提交
a69bca46
A
avocado
项目概览
openeuler
/
avocado
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
avocado
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
a69bca46
编写于
12月 14, 2018
作者:
C
Caio Carrara
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'ldoktor/is_port_free2'
Signed-off-by:
N
Caio Carrara
<
ccarrara@redhat.com
>
上级
13cd8cf9
6c63153f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
100 addition
and
14 deletion
+100
-14
avocado/utils/network.py
avocado/utils/network.py
+34
-14
python-avocado.spec
python-avocado.spec
+2
-0
requirements-selftests.txt
requirements-selftests.txt
+3
-0
selftests/unit/test_utils_network.py
selftests/unit/test_utils_network.py
+61
-0
未找到文件。
avocado/utils/network.py
浏览文件 @
a69bca46
...
...
@@ -23,29 +23,49 @@ from six.moves import xrange as range
from
.data_structures
import
Borg
#: Families taken into account in this class
FAMILIES
=
(
socket
.
AF_INET
,
socket
.
AF_INET6
)
#: Protocols taken into account in this class
PROTOCOLS
=
(
socket
.
SOCK_STREAM
,
socket
.
SOCK_DGRAM
)
def
is_port_free
(
port
,
address
):
"""
Return True if the given port is available for use.
Currently we only check for TCP/UDP connections on IPv4/6
:param port: Port number
:param address: Socket address to bind or connect
"""
if
address
==
"localhost"
or
not
address
:
localhost
=
True
protocols
=
PROTOCOLS
else
:
localhost
=
False
# sock.connect always connects for UDP
protocols
=
(
socket
.
SOCK_STREAM
,
)
sock
=
None
try
:
s
=
socket
.
socket
()
if
address
==
"localhost"
:
s
.
bind
((
address
,
port
))
free
=
True
else
:
s
.
connect
((
address
,
port
))
free
=
False
except
socket
.
error
:
if
address
==
"localhost"
:
free
=
False
else
:
free
=
True
s
.
close
()
return
free
for
family
in
FAMILIES
:
for
protocol
in
protocols
:
try
:
sock
=
socket
.
socket
(
family
,
protocol
)
if
localhost
:
sock
.
bind
((
""
,
port
))
else
:
sock
.
connect
((
address
,
port
))
return
False
except
socket
.
error
as
exc
:
if
exc
.
errno
in
(
93
,
94
):
# Unsupported combinations
continue
if
localhost
:
return
False
sock
.
close
()
return
True
finally
:
if
sock
is
not
None
:
sock
.
close
()
def
find_free_port
(
start_port
=
1024
,
end_port
=
65535
,
address
=
"localhost"
,
sequent
=
True
):
...
...
python-avocado.spec
浏览文件 @
a69bca46
...
...
@@ -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
...
...
requirements-selftests.txt
浏览文件 @
a69bca46
...
...
@@ -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
selftests/unit/test_utils_network.py
浏览文件 @
a69bca46
import
netifaces
import
socket
import
unittest
try
:
...
...
@@ -33,5 +35,64 @@ 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
)
self
.
assertTrue
(
network
.
is_port_free
(
port
,
"localhost"
))
local_addrs
=
get_all_local_addrs
()
ipv4_addrs
=
[
"localhost"
,
""
]
+
list
(
local_addrs
[
0
])
ipv6_addrs
=
[
"localhost"
,
""
]
+
list
(
local_addrs
[
1
])
good
=
[]
bad
=
[]
skip
=
[]
sock
=
None
for
family
in
network
.
FAMILIES
:
if
family
==
socket
.
AF_INET
:
addrs
=
ipv4_addrs
else
:
addrs
=
ipv6_addrs
for
addr
in
addrs
:
for
protocol
in
network
.
PROTOCOLS
:
try
:
sock
=
socket
.
socket
(
family
,
protocol
)
sock
.
bind
((
addr
,
port
))
if
network
.
is_port_free
(
port
,
"localhost"
):
bad
.
append
(
"%s, %s, %s: reports free"
%
(
family
,
protocol
,
addr
))
else
:
good
.
append
(
"%s, %s, %s"
%
(
family
,
protocol
,
addr
))
except
Exception
as
exc
:
if
getattr
(
exc
,
'errno'
,
None
)
in
(
-
2
,
2
,
22
,
94
):
skip
.
append
(
"%s, %s, %s: Not supported: %s"
%
(
family
,
protocol
,
addr
,
exc
))
else
:
bad
.
append
(
"%s, %s, %s: Failed to bind: %s"
%
(
family
,
protocol
,
addr
,
exc
))
finally
:
if
sock
is
not
None
:
sock
.
close
()
self
.
assertFalse
(
bad
,
"Following combinations failed:
\n
%s
\n\n
"
"Following combinations passed:
\n
%s
\n\n
"
"Following combinations were skipped:
\n
%s"
%
(
"
\n
"
.
join
(
bad
),
"
\n
"
.
join
(
good
),
"
\n
"
.
join
(
skip
)))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录