-
-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Docker user mapping inspection
- Loading branch information
Showing
11 changed files
with
230 additions
and
72 deletions.
There are no files selected for viewing
This file contains 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 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 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,71 @@ | ||
import subprocess | ||
from unittest.mock import MagicMock, call | ||
|
||
import pytest | ||
|
||
from briefcase.exceptions import BriefcaseCommandError | ||
from briefcase.integrations.base import ToolCache | ||
from briefcase.integrations.docker import Docker | ||
from briefcase.integrations.subprocess import Subprocess | ||
|
||
|
||
@pytest.fixture | ||
def mock_tools(mock_tools) -> ToolCache: | ||
mock_tools.subprocess = MagicMock(spec_set=Subprocess) | ||
mock_tools.docker = Docker(mock_tools) | ||
return mock_tools | ||
|
||
|
||
def test_cache_image(mock_tools, user_mapping_run_calls): | ||
"""A Docker image can be cached.""" | ||
# mock image not being cached in Docker | ||
mock_tools.subprocess.check_output.return_value = "" | ||
|
||
# Cache an image | ||
mock_tools.docker.cache_image("ubuntu:jammy") | ||
|
||
# Confirms that image is not available | ||
mock_tools.subprocess.check_output.assert_called_once_with( | ||
["docker", "images", "-q", "ubuntu:jammy"] | ||
) | ||
|
||
# Image is pulled and cached | ||
mock_tools.subprocess.run.assert_has_calls( | ||
user_mapping_run_calls + [call(["docker", "pull", "ubuntu:jammy"], check=True)] | ||
) | ||
|
||
|
||
def test_cache_image_already_cached(mock_tools, user_mapping_run_calls): | ||
"""A Docker image is not pulled if it is already cached.""" | ||
# mock image already cached in Docker | ||
mock_tools.subprocess.check_output.return_value = "99284ca6cea0" | ||
|
||
# Cache an image | ||
mock_tools.docker.cache_image("ubuntu:jammy") | ||
|
||
# Confirms that image is not available | ||
mock_tools.subprocess.check_output.assert_called_once_with( | ||
["docker", "images", "-q", "ubuntu:jammy"] | ||
) | ||
|
||
# Image is not pulled and cached | ||
mock_tools.subprocess.run.assert_has_calls(user_mapping_run_calls) | ||
|
||
|
||
def test_cache_bad_image(mock_tools): | ||
"""If an image is invalid, an exception raised.""" | ||
# mock image not being cached in Docker | ||
mock_tools.subprocess.check_output.return_value = "" | ||
|
||
# Mock a Docker failure due to a bad image | ||
mock_tools.subprocess.run.side_effect = subprocess.CalledProcessError( | ||
returncode=125, | ||
cmd="docker...", | ||
) | ||
|
||
# Try to cache an image that doesn't exist: | ||
with pytest.raises( | ||
BriefcaseCommandError, | ||
match=r"Unable to obtain the Docker image for ubuntu:does-not-exist.", | ||
): | ||
mock_tools.docker.cache_image("ubuntu:does-not-exist") |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.