Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
17
Merge Requests
17
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
nexedi
slapos.core
Commits
ea344fd3
Commit
ea344fd3
authored
Nov 01, 2024
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
testing/utils: add findFreeTCPPortRange
parent
cee572c4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
0 deletions
+20
-0
slapos/testing/utils.py
slapos/testing/utils.py
+20
-0
No files found.
slapos/testing/utils.py
View file @
ea344fd3
...
...
@@ -32,6 +32,7 @@ import unittest
import
os
import
logging
import
multiprocessing
import
random
import
shutil
import
subprocess
import
tempfile
...
...
@@ -64,6 +65,25 @@ def findFreeTCPPort(ip=''):
return
s
.
getsockname
()[
1
]
def
findFreeTCPPortRange
(
ip
=
''
,
count
=
1
):
# type: (str, int) -> int
"""Find a range of consecutive `count` free TCP ports to listen to.
"""
for
_
in
range
(
10
):
# retry 10 times
port
=
random
.
randrange
(
20000
,
30000
)
for
offset
in
range
(
count
):
with
closing
(
socket
.
socket
(
socket
.
AF_INET6
if
':'
in
ip
else
socket
.
AF_INET
,
socket
.
SOCK_STREAM
))
as
s
:
try
:
s
.
bind
((
ip
,
port
+
offset
))
except
OSError
:
port
=
None
break
if
port
is
None
:
raise
RuntimeError
(
"Can't find port"
)
return
port
def
getPortFromPath
(
path
):
# type: (str) -> int
"""A stable port using a hash from path.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment