-
Notifications
You must be signed in to change notification settings - Fork 312
feat : Add support for depends_on functionality #728
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
Open
hari134
wants to merge
4
commits into
testcontainers:main
Choose a base branch
from
hari134:depends-on
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e19fbf
feat : Add support for depends_on functionality
hari134 08485c9
Fix: pass started dependencies across recursive functions
hari134 f18287e
Tests : add and update tests for depends_on functionality
hari134 03d9dcd
Merge branch 'main' into depends-on
hari134 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import pytest | ||
from docker.errors import APIError, ImageNotFound | ||
from testcontainers.core.container import DockerContainer | ||
|
||
|
||
def test_single_dependency_starts() -> None: | ||
container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
dependency_container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
container.depends_on(dependency_container) | ||
|
||
container.start() | ||
|
||
assert dependency_container.wait_until_running(), "Dependency did not reach running state" | ||
assert container.wait_until_running(), "Container did not reach running state" | ||
|
||
container.stop() | ||
dependency_container.stop() | ||
|
||
|
||
def test_multiple_dependencies_start() -> None: | ||
container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
dependency1 = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
dependency2 = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
container.depends_on([dependency1, dependency2]) | ||
|
||
dependency1.start() | ||
dependency2.start() | ||
assert dependency1.wait_until_running(), "Dependency 1 did not reach running state" | ||
assert dependency2.wait_until_running(), "Dependency 2 did not reach running state" | ||
|
||
container.start() | ||
assert container.wait_until_running(), "Container did not reach running state" | ||
|
||
container.stop() | ||
dependency1.stop() | ||
dependency2.stop() | ||
|
||
|
||
def test_dependency_failure() -> None: | ||
container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
failing_dependency = DockerContainer("nonexistent-image") | ||
container.depends_on(failing_dependency) | ||
|
||
with pytest.raises((APIError, ImageNotFound)): | ||
container.start() | ||
|
||
assert container._container is None | ||
|
||
|
||
def test_all_dependencies_fail() -> None: | ||
container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
failing_dependency1 = DockerContainer("nonexistent-image1") | ||
failing_dependency2 = DockerContainer("nonexistent-image2") | ||
container.depends_on([failing_dependency1, failing_dependency2]) | ||
|
||
with pytest.raises((APIError, ImageNotFound)): | ||
container.start() | ||
|
||
assert container._container is None | ||
assert failing_dependency1._container is None | ||
assert failing_dependency2._container is None | ||
|
||
|
||
def test_dependency_cleanup_on_partial_failure() -> None: | ||
container = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
dependency1 = DockerContainer("alpine:latest").with_command("tail -f /dev/null") | ||
failing_dependency = DockerContainer("nonexistent-image3") | ||
|
||
container.depends_on([dependency1, failing_dependency]) | ||
|
||
with pytest.raises(Exception): | ||
container.start() | ||
|
||
assert dependency1._container is None, "dependency1 was not cleaned up properly" | ||
assert failing_dependency._container is None, "failing_dependency was not cleaned up properly" | ||
assert container._container is None, "container was not cleaned up properly" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this doesn't seem like it work as the recursive call would create its own started_dependencies?
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.
Thank you for pointing this out! The current code creates a new started_dependencies list on each recursive call, which prevents complete cleanup if a nested dependency fails. I’ll update the code to pass started_dependencies across recursive calls to ensure full cleanup.
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.
@alexanderankin I have made the needed changes such that the _start_dependencies function passes the started_dependencies across the recursive function calls to maintain a consistent state. I have also added a new check for circular dependencies which is called while adding dependencies in depends_on function. The tests have also been updated to be more comprehensive. I will be happy to make changes in the future if any of the core parts change just tag me in that case.