-
Notifications
You must be signed in to change notification settings - Fork 544
Feat pull images before teardown containers on up command #1298
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
lisongmin
wants to merge
3
commits into
containers:main
Choose a base branch
from
lisongmin:feat-pull-before-teardown
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Pull images before teardown containers on compose up, which reduce the downtime of services. |
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,165 @@ | ||
| from argparse import Namespace | ||
| from unittest import IsolatedAsyncioTestCase | ||
| from unittest.mock import AsyncMock | ||
| from unittest.mock import Mock | ||
| from unittest.mock import call | ||
| from unittest.mock import patch | ||
|
|
||
| from parameterized import parameterized | ||
|
|
||
| from podman_compose import PullImage | ||
|
|
||
|
|
||
| class TestPullImage(IsolatedAsyncioTestCase): | ||
| def test_unsupported_policy_fallback_to_missing(self) -> None: | ||
| pull_image = PullImage("localhost/test:1", policy="unsupported") | ||
| assert pull_image.policy == "missing" | ||
|
|
||
| def test_update_policy(self) -> None: | ||
| pull_image = PullImage("localhost/test:1", policy="never") | ||
| assert pull_image.policy == "never" | ||
|
|
||
| # not supported policy | ||
| pull_image.update_policy("unsupported") | ||
| assert pull_image.policy == "never" | ||
|
|
||
| pull_image.update_policy("missing") | ||
| assert pull_image.policy == "missing" | ||
|
|
||
| pull_image.update_policy("newer") | ||
| assert pull_image.policy == "newer" | ||
|
|
||
| pull_image.update_policy("always") | ||
| assert pull_image.policy == "always" | ||
|
|
||
| # Ensure policy is not downgraded | ||
| pull_image.update_policy("build") | ||
| assert pull_image.policy == "always" | ||
|
|
||
| def test_pull_args(self) -> None: | ||
| pull_image = PullImage("localhost/test:1", policy="always", quiet=True) | ||
| assert pull_image.pull_args == ["--policy", "always", "--quiet", "localhost/test:1"] | ||
|
|
||
| pull_image.quiet = False | ||
| assert pull_image.pull_args == ["--policy", "always", "localhost/test:1"] | ||
|
|
||
| @patch("podman_compose.Podman") | ||
| async def test_pull_success(self, podman_mock: Mock) -> None: | ||
| pull_image = PullImage("localhost/test:1", policy="always", quiet=True) | ||
|
|
||
| run_mock = AsyncMock() | ||
| run_mock.return_value = 0 | ||
| podman_mock.run = run_mock | ||
|
|
||
| result = await pull_image.pull(podman_mock) | ||
| assert result == 0 | ||
| run_mock.assert_called_once_with( | ||
| [], "pull", ["--policy", "always", "--quiet", "localhost/test:1"] | ||
| ) | ||
|
|
||
| @patch("podman_compose.Podman") | ||
| async def test_pull_failed(self, podman_mock: Mock) -> None: | ||
| pull_image = PullImage( | ||
| "localhost/test:1", | ||
| policy="always", | ||
| quiet=True, | ||
| ignore_pull_error=True, | ||
| ) | ||
|
|
||
| run_mock = AsyncMock() | ||
| run_mock.return_value = 1 | ||
| podman_mock.run = run_mock | ||
|
|
||
| # with ignore_pull_error=True, should return 0 even if pull fails | ||
| result = await pull_image.pull(podman_mock) | ||
| assert result == 0 | ||
|
|
||
| # with ignore_pull_error=False, should return the actual error code | ||
| pull_image.ignore_pull_error = False | ||
| result = await pull_image.pull(podman_mock) | ||
| assert result == 1 | ||
|
|
||
| @patch("podman_compose.Podman") | ||
| async def test_pull_with_never_policy(self, podman_mock: Mock) -> None: | ||
| pull_image = PullImage( | ||
| "localhost/test:1", | ||
| policy="never", | ||
| quiet=True, | ||
| ignore_pull_error=True, | ||
| ) | ||
|
|
||
| run_mock = AsyncMock() | ||
| run_mock.return_value = 1 | ||
| podman_mock.run = run_mock | ||
|
|
||
| result = await pull_image.pull(podman_mock) | ||
| assert result == 0 | ||
| assert run_mock.call_count == 0 | ||
|
|
||
| @parameterized.expand([ | ||
| ( | ||
| "Local image should not pull", | ||
| Namespace(), | ||
| [{"image": "localhost/a:latest"}], | ||
| 0, | ||
| [], | ||
| ), | ||
| ( | ||
| "Remote image should pull", | ||
| Namespace(), | ||
| [{"image": "ghcr.io/a:latest"}], | ||
| 1, | ||
| [ | ||
| call([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]), | ||
| ], | ||
| ), | ||
| ( | ||
| "The same image in service should call once", | ||
| Namespace(), | ||
| [ | ||
| {"image": "ghcr.io/a:latest"}, | ||
| {"image": "ghcr.io/a:latest"}, | ||
| {"image": "ghcr.io/b:latest"}, | ||
| ], | ||
| 2, | ||
| [ | ||
| call([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]), | ||
| call([], "pull", ["--policy", "missing", "ghcr.io/b:latest"]), | ||
| ], | ||
| ), | ||
| ]) | ||
| @patch("podman_compose.Podman") | ||
| async def test_pull_images( | ||
| self, | ||
| desc: str, | ||
| args: Namespace, | ||
| services: list[dict], | ||
| call_count: int, | ||
| calls: list, | ||
| podman_mock: Mock, | ||
| ) -> None: | ||
| run_mock = AsyncMock() | ||
| run_mock.return_value = 0 | ||
| podman_mock.run = run_mock | ||
|
|
||
| assert await PullImage.pull_images(podman_mock, args, services) == 0 | ||
| assert run_mock.call_count == call_count | ||
| if calls: | ||
| run_mock.assert_has_calls(calls, any_order=True) | ||
|
|
||
| @patch("podman_compose.Podman") | ||
| async def test_pull_images_with_build_section( | ||
| self, | ||
| podman_mock: Mock, | ||
| ) -> None: | ||
| run_mock = AsyncMock() | ||
| run_mock.return_value = 1 | ||
| podman_mock.run = run_mock | ||
|
|
||
| args: Namespace = Namespace() | ||
| services: list[dict] = [ | ||
| {"image": "ghcr.io/a:latest", "build": {"context": "."}}, | ||
| ] | ||
| assert await PullImage.pull_images(podman_mock, args, services) == 0 | ||
| assert run_mock.call_count == 1 | ||
| run_mock.assert_called_with([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]) |
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.
Rather large commit just to fix pylint warnings
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 got the following warning from pylint:
And since build image and pull image are both prepare images before running, I split it out to the
prepare_images.Any idea to fix the warning with less changes?