Skip to content

Commit

Permalink
providers: allow parallel installations when not running as a snap (#…
Browse files Browse the repository at this point in the history
…4088)

Signed-off-by: Callahan Kovacs <[email protected]>
  • Loading branch information
mr-cal authored Apr 4, 2023
1 parent b852f9a commit cb6f385
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
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

0 comments on commit cb6f385

Please sign in to comment.