Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wait-for-it
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Joël Schwab
wait-for-it
Commits
c5e0b41e
Commit
c5e0b41e
authored
5 years ago
by
Marcel Huber
Browse files
Options
Downloads
Patches
Plain Diff
improved socket related tests
parent
9dac1929
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/wait-for-it.py
+38
-23
38 additions, 23 deletions
test/wait-for-it.py
with
38 additions
and
23 deletions
test/wait-for-it.py
+
38
−
23
View file @
c5e0b41e
...
...
@@ -23,6 +23,8 @@ except:
MISSING_ARGS_TEXT
=
"
Error: you need to provide a host and port to test.
"
HELP_TEXT
=
"
Usage:
"
# Start of help text
DIVIDE_LINE
=
'
-
'
*
71
# Output line of dashes
_SOCKET_TIMEOUT
=
5.0
socket
.
setdefaulttimeout
(
_SOCKET_TIMEOUT
)
class
TestWaitForIt
(
unittest
.
TestCase
):
...
...
@@ -51,10 +53,13 @@ class TestWaitForIt(unittest.TestCase):
returncode
=
proc
.
poll
()
return
(
returncode
,
_stdout
,
_stderr
)
def
open_local_port
(
self
,
host
=
"
localhost
"
,
port
=
8929
,
timeout
=
5
):
s
=
socket
.
socket
()
def
open_local_port
(
self
,
host
=
"
localhost
"
,
port
=
8929
,
timeout
=
None
):
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
if
timeout
:
s
.
settimeout
(
timeout
)
s
.
bind
((
host
,
port
))
s
.
listen
(
timeout
)
s
.
listen
(
1
)
return
s
def
check_args
(
self
,
args
,
stdout_regex
,
stderr_regex
,
exitcode
):
...
...
@@ -103,23 +108,27 @@ class TestWaitForIt(unittest.TestCase):
def
test_host_port
(
self
):
"""
Check that --host and --port args work correctly.
"""
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
--host=localhost --port=8929 --timeout=1
"
,
""
,
"
wait-for-it.sh: waiting 1 seconds for localhost:8929
"
,
0
)
soc
.
close
()
try
:
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
--host=localhost --port=8929 --timeout=1
"
,
""
,
"
wait-for-it.sh: waiting 1 seconds for localhost:8929
"
,
0
)
finally
:
soc
.
close
()
def
test_combined_host_port
(
self
):
"""
Tests that wait-for-it.sh returns correctly after establishing a
connectionm using combined host and ports.
"""
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 --timeout=1
"
,
""
,
"
wait-for-it.sh: waiting 1 seconds for localhost:8929
"
,
0
)
soc
.
close
()
try
:
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 --timeout=1
"
,
""
,
"
wait-for-it.sh: waiting 1 seconds for localhost:8929
"
,
0
)
finally
:
soc
.
close
()
def
test_port_failure_with_timeout
(
self
):
"""
Note exit status of 124 is exected, passed from the timeout
"""
Note exit status of 124 is ex
p
ected, passed from the timeout
command.
"""
self
.
check_args
(
"
localhost:8929 --timeout=1
"
,
""
,
...
...
@@ -129,21 +138,27 @@ class TestWaitForIt(unittest.TestCase):
def
test_command_execution
(
self
):
"""
Checks that a command executes correctly after a port test
passes.
"""
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 -- echo
\"
CMD OUTPUT
\"
"
,
"
CMD OUTPUT
"
,
"
.*wait-for-it.sh: localhost:8929 is available after 0 seconds
"
,
0
)
soc
.
close
()
try
:
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 -- echo
\"
CMD OUTPUT
\"
"
,
"
CMD OUTPUT
"
,
"
.*wait-for-it.sh: localhost:8929 is available after 0 seconds
"
,
0
)
finally
:
soc
.
close
()
def
test_failed_command_execution
(
self
):
"""
Check command failure.
The command in question outputs STDERR and an exit code of 2
"""
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 -- ls not_real_file
"
,
""
,
"
.*No such file or directory
\n
"
,
2
)
soc
.
close
()
expected_exitcode
,
_
,
_
=
self
.
execute
(
'
ls not_real_file
'
)
try
:
soc
=
self
.
open_local_port
(
port
=
8929
)
self
.
check_args
(
"
localhost:8929 -- ls not_real_file
"
,
""
,
"
.*No such file or directory
\n
"
,
expected_exitcode
)
finally
:
soc
.
close
()
def
test_command_after_connection_failure
(
self
):
"""
Test that a command still runs even if a connection times out and
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment