-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Race condition in DockerContainerTestCase (#6587) #6594
Fix: Race condition in DockerContainerTestCase (#6587) #6594
Conversation
7e66b4f
to
35c9c8a
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #6594 +/- ##
===========================================
- Coverage 85.42% 85.41% -0.02%
===========================================
Files 155 155
Lines 20750 20760 +10
===========================================
+ Hits 17726 17732 +6
- Misses 3024 3028 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned up some repetitive bits by restructuring the loop
Subject: [PATCH] REVIEW
---
Index: test/docker_container_test_case.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/test/docker_container_test_case.py b/test/docker_container_test_case.py
--- a/test/docker_container_test_case.py (revision 35c9c8a9594d993cd3cc088fdb97ec9478efa9e2)
+++ b/test/docker_container_test_case.py (date 1727224805142)
@@ -87,34 +87,31 @@
try:
time_it = datetime.now()
container_info = cls._docker.api.inspect_container(container.id)
- network_settings = container_info['NetworkSettings']
if is_sibling: # no coverage
- container_ip = network_settings['IPAddress']
+ container_ip = container_info['NetworkSettings']['IPAddress']
assert isinstance(container_ip, str)
endpoint = (container_ip, container_port)
log.info('Launched sibling container %s from image %s, listening on %s:%i',
container.name, image, container_ip, container_port)
else:
- retries = 0
- ports = network_settings['Ports']
- while len(ports[f'{container_port}/tcp']) < 1:
- if (datetime.now() - time_it).seconds > 3:
- # Wait for the published ports of the container that's
- # supposedly running, otherwise giveup (let it fail).
- log.error('Unreachable TCP port %s for container %s',
- container_port, container.name)
+ retries, seconds = 0, 0.0
+ while True:
+ ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp']
+ if len(ports) > 0:
break
- time.sleep(.33)
- container_info = cls._docker.api.inspect_container(container.id)
- ports = container_info['NetworkSettings']['Ports']
- retries += 1
- milisecs = (datetime.now() - time_it).microseconds / 1000
- port = one(ports[f'{container_port}/tcp'])
+ elif seconds > 3:
+ raise RuntimeError('Unreachable TCP port', container_port, container.name)
+ else:
+ time.sleep(.33)
+ container_info = cls._docker.api.inspect_container(container.id)
+ retries += 1
+ seconds = (datetime.now() - time_it).total_seconds()
+ port = one(ports)
host_ip = port['HostIp']
host_port = int(port['HostPort'])
- log.info('Launched container %s from image %s after %dms (retries %d), '
+ log.info('Launched container %s from image %s after %.3fs and %d retries, '
'with container port %s mapped to %s:%i on the host',
- container.name, image, milisecs, retries, container_port, host_ip, host_port)
+ container.name, image, seconds, retries, container_port, host_ip, host_port)
endpoint = (host_ip, host_port)
except BaseException: # no coverage
container.kill()
test/docker_container_test_case.py
Outdated
container_info = cls._docker.api.inspect_container(container.id) | ||
ports = container_info['NetworkSettings']['Ports'] | ||
retries += 1 | ||
milisecs = (datetime.now() - time_it).microseconds / 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"milliseconds" has 2 L's
milisecs = (datetime.now() - time_it).microseconds / 1000 | |
millisecs = (datetime.now() - time_it).microseconds / 1000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is doing what you want:
>>> from datetime import timedelta
>>> timedelta(seconds=3).microseconds
0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ty
test/docker_container_test_case.py
Outdated
# Wait for the published ports of the container that's | ||
# supposedly running, otherwise giveup (let it fail). | ||
log.error('Unreachable TCP port %s for container %s', | ||
container_port, container.name) | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why postpone the failure? Raising an exception here makes both the source code and the resulting stack trace easier to understand.
test/docker_container_test_case.py
Outdated
container_port, container.name) | ||
break | ||
time.sleep(.33) | ||
container_info = cls._docker.api.inspect_container(container.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is inspect_container
an expensive (i.e. time-consuming) operation? If not, then it seems redundant to track both milliseconds and retries in the loop.
35c9c8a
to
5009f13
Compare
Requested peer review from @dsotirho-ucsc since @nadove-ucsc is now code owner / sys-admin. |
test/docker_container_test_case.py
Outdated
@@ -81,22 +85,32 @@ def _create_container(cls, image: str, container_port: int, **kwargs) -> Netloc: | |||
ports=ports, | |||
**kwargs) | |||
try: | |||
time_it = datetime.now() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time_it = datetime.now() | |
start = datetime.now() |
This should be moved above the call to cls._docker.containers.run()
so that even if sleep() is not called in the while loop, a time greater than 0.00
will be reported.
test/docker_container_test_case.py
Outdated
seconds = 0.0 | ||
while True: | ||
ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seconds = 0.0 | |
while True: | |
ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp'] | |
while True: | |
seconds = (datetime.now() - time_it).total_seconds() | |
ports = container_info['NetworkSettings']['Ports'][f'{container_port}/tcp'] |
Moving the time calculation to the start of the while loop will let seconds
reflect the total time taken even if sleep() is not called.
test/docker_container_test_case.py
Outdated
else: | ||
time.sleep(.33) | ||
container_info = cls._docker.api.inspect_container(container.id) | ||
seconds = (datetime.now() - time_it).total_seconds() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seconds = (datetime.now() - time_it).total_seconds() |
5009f13
to
ac51467
Compare
ac51467
to
841f1e6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved
Security design review
|
841f1e6
to
dde1540
Compare
Connected issues: #6587
Checklist
Author
develop
issues/<GitHub handle of author>/<issue#>-<slug>
1 when the issue title describes a problem, the corresponding PR
title is
Fix:
followed by the issue titleAuthor (partiality)
p
tag to titles of partial commitspartial
or completely resolves all connected issuespartial
labelAuthor (chains)
base
or this PR is not chained to another PRchained
or is not chained to another PRAuthor (reindex, API changes)
r
tag to commit title or the changes introduced by this PR will not require reindexing of any deploymentreindex:dev
or the changes introduced by it will not require reindexing ofdev
reindex:anvildev
or the changes introduced by it will not require reindexing ofanvildev
reindex:anvilprod
or the changes introduced by it will not require reindexing ofanvilprod
reindex:prod
or the changes introduced by it will not require reindexing ofprod
reindex:partial
and its description documents the specific reindexing procedure fordev
,anvildev
,anvilprod
andprod
or requires a full reindex or carries none of the labelsreindex:dev
,reindex:anvildev
,reindex:anvilprod
andreindex:prod
API
or this PR does not modify a REST APIa
(A
) tag to commit title for backwards (in)compatible changes or this PR does not modify a REST APIapp.py
or this PR does not modify a REST APIAuthor (upgrading deployments)
make docker_images.json
and committed the resulting changes or this PR does not modifyazul_docker_images
, or any other variables referenced in the definition of that variableu
tag to commit title or this PR does not require upgrading deploymentsupgrade
or does not require upgrading deploymentsdeploy:shared
or does not modifydocker_images.json
, and does not require deploying theshared
component for any other reasondeploy:gitlab
or does not require deploying thegitlab
componentdeploy:runner
or does not require deploying therunner
imageAuthor (hotfixes)
F
tag to main commit title or this PR does not include permanent fix for a temporary hotfixanvilprod
andprod
) have temporary hotfixes for any of the issues connected to this PRAuthor (before every review)
develop
, squashed old fixupsmake requirements_update
or this PR does not modifyrequirements*.txt
,common.mk
,Makefile
andDockerfile
R
tag to commit title or this PR does not modifyrequirements*.txt
reqs
or does not modifyrequirements*.txt
make integration_test
passes in personal deployment or this PR does not modify functionality that could affect the IT outcomePeer reviewer (after approval)
System administrator (after approval)
demo
orno demo
no demo
no sandbox
N reviews
label is accurateOperator (before pushing merge the commit)
reindex:…
labels andr
commit title tagno demo
develop
_select dev.shared && CI_COMMIT_REF_NAME=develop make -C terraform/shared apply_keep_unused
or this PR is not labeleddeploy:shared
_select dev.gitlab && CI_COMMIT_REF_NAME=develop make -C terraform/gitlab apply
or this PR is not labeleddeploy:gitlab
_select anvildev.shared && CI_COMMIT_REF_NAME=develop make -C terraform/shared apply_keep_unused
or this PR is not labeleddeploy:shared
_select anvildev.gitlab && CI_COMMIT_REF_NAME=develop make -C terraform/gitlab apply
or this PR is not labeleddeploy:gitlab
deploy:gitlab
deploy:gitlab
System administrator
dev.gitlab
are complete or this PR is not labeleddeploy:gitlab
anvildev.gitlab
are complete or this PR is not labeleddeploy:gitlab
Operator (before pushing merge the commit)
_select dev.gitlab && make -C terraform/gitlab/runner
or this PR is not labeleddeploy:runner
_select anvildev.gitlab && make -C terraform/gitlab/runner
or this PR is not labeleddeploy:runner
sandbox
label or PR is labeledno sandbox
dev
or PR is labeledno sandbox
anvildev
or PR is labeledno sandbox
sandbox
deployment or PR is labeledno sandbox
anvilbox
deployment or PR is labeledno sandbox
sandbox
deployment or PR is labeledno sandbox
anvilbox
deployment or PR is labeledno sandbox
sandbox
or this PR does not remove catalogs or otherwise causes unreferenced indices indev
anvilbox
or this PR does not remove catalogs or otherwise causes unreferenced indices inanvildev
sandbox
or this PR is not labeledreindex:dev
anvilbox
or this PR is not labeledreindex:anvildev
sandbox
or this PR is not labeledreindex:dev
anvilbox
or this PR is not labeledreindex:anvildev
p
if the PR is also labeledpartial
Operator (chain shortening)
develop
or this PR is not labeledbase
chained
label from the blocked PR or this PR is not labeledbase
base
base
label from this PR or this PR is not labeledbase
Operator (after pushing the merge commit)
dev
anvildev
dev
dev
anvildev
anvildev
_select dev.shared && make -C terraform/shared apply
or this PR is not labeleddeploy:shared
_select anvildev.shared && make -C terraform/shared apply
or this PR is not labeleddeploy:shared
dev
anvildev
Operator (reindex)
dev
or this PR is neither labeledreindex:partial
norreindex:dev
anvildev
or this PR is neither labeledreindex:partial
norreindex:anvildev
dev
or this PR is neither labeledreindex:partial
norreindex:dev
anvildev
or this PR is neither labeledreindex:partial
norreindex:anvildev
dev
or this PR is neither labeledreindex:partial
norreindex:dev
anvildev
or this PR is neither labeledreindex:partial
norreindex:anvildev
dev
or this PR does not require reindexingdev
anvildev
or this PR does not require reindexinganvildev
dev
or this PR does not require reindexingdev
anvildev
or this PR does not require reindexinganvildev
dev
or this PR does not require reindexingdev
anvildev
or this PR does not require reindexinganvildev
Operator
deploy:shared
,deploy:gitlab
,deploy:runner
,API
,reindex:partial
,reindex:anvilprod
andreindex:prod
labels to the next promotion PRs or this PR carries none of these labelsdeploy:shared
,deploy:gitlab
,deploy:runner
,API
,reindex:partial
,reindex:anvilprod
andreindex:prod
labels, from the description of this PR to that of the next promotion PRs or this PR carries none of these labelsShorthand for review comments
L
line is too longW
line wrapping is wrongQ
bad quotesF
other formatting problem