Skip to content
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

providers: allow parallel installations when not running as a snap #4088

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions snapcraft/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
get_managed_environment_log_path,
get_managed_environment_project_path,
get_managed_environment_snap_channel,
is_snapcraft_running_from_snap,
)

SNAPCRAFT_BASE_TO_PROVIDER_BASE = {
Expand Down Expand Up @@ -183,12 +184,14 @@ def get_base_configuration(
)
snap_name = "snapcraft"
snap_channel = "stable"
else:
# Use SNAP_INSTANCE_NAME for snapcraft's snap name, as it may not be 'snapcraft'
# if the '--name' parameter was used to install snapcraft.
# If snapcraft is not running as a snap, then envvar will not exist so default
# to 'snapcraft'.
elif is_snapcraft_running_from_snap():
# Use SNAP_INSTANCE_NAME for snapcraft's snap name, as it may not be
# 'snapcraft' if the '--name' parameter was used to install snapcraft.
snap_name = os.getenv("SNAP_INSTANCE_NAME", "snapcraft")
else:
# If snapcraft is not running as a snap, then envvar may not exist so
# default to 'snapcraft'.
snap_name = "snapcraft"

return bases.BuilddBase(
alias=alias,
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def test_get_base_configuration(
):
"""Verify the base configuration is properly configured."""
mocker.patch("sys.platform", "linux")
mocker.patch(
"snapcraft.providers.is_snapcraft_running_from_snap", return_value=True
)
mocker.patch(
"snapcraft.providers.get_managed_environment_snap_channel",
return_value="test-channel",
Expand Down Expand Up @@ -268,6 +271,9 @@ def test_get_base_configuration_snap_instance_name_default(
):
"""If `SNAP_INSTANCE_NAME` does not exist, use the default name 'snapcraft'."""
mocker.patch("sys.platform", "linux")
mocker.patch(
"snapcraft.providers.is_snapcraft_running_from_snap", return_value=True
)
mocker.patch(
"snapcraft.providers.get_managed_environment_snap_channel",
return_value=None,
Expand All @@ -292,6 +298,40 @@ def test_get_base_configuration_snap_instance_name_default(
)


def test_get_base_configuration_snap_instance_name_not_running_as_snap(
tmp_path,
mocker,
monkeypatch,
):
"""If snapcraft is not running as a snap, then use the default name 'snapcraft'."""
mocker.patch(
"snapcraft.providers.is_snapcraft_running_from_snap", return_value=False
)
mocker.patch("sys.platform", "linux")
mocker.patch(
"snapcraft.providers.get_managed_environment_snap_channel",
return_value=None,
)
mocker.patch("snapcraft.providers.get_command_environment")
mocker.patch("snapcraft.providers.get_instance_name")
mock_buildd_base = mocker.patch("snapcraft.providers.bases.BuilddBase")
monkeypatch.setenv("SNAP_INSTANCE_NAME", "other-snap")

providers.get_base_configuration(
alias=bases.BuilddBaseAlias.JAMMY,
instance_name="test-instance-name",
)

mock_buildd_base.assert_called_with(
alias=ANY,
compatibility_tag=ANY,
environment=ANY,
hostname=ANY,
snaps=[bases.buildd.Snap(name="snapcraft", channel=None, classic=True)],
packages=ANY,
)


def test_get_command_environment(mocker, mock_default_command_environment):
"""Verify command environment is properly constructed."""
command_environment = providers.get_command_environment()
Expand Down