Skip to content

Commit

Permalink
fix: use local keys for package-repos in core24
Browse files Browse the repository at this point in the history
Fixes #4743
  • Loading branch information
tigarmo committed Apr 24, 2024
1 parent cf61c67 commit dbd750a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
14 changes: 13 additions & 1 deletion snapcraft/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

from .legacy_cli import _LIB_NAMES, _ORIGINAL_LIB_NAME_LOG_LEVEL
from .parts import plugins
from .parts.yaml_utils import extract_parse_info
from .parts.yaml_utils import extract_parse_info, get_snap_project

APP_METADATA = AppMetadata(
name="snapcraft",
Expand Down Expand Up @@ -230,6 +230,18 @@ def _create_dispatcher(self) -> craft_cli.Dispatcher:
default_command=craft_app_commands.lifecycle.PackCommand,
)

@override
def _get_local_keys_path(self) -> pathlib.Path | None:
if self._snapcraft_yaml_path is None:
return None

snap_project = get_snap_project()
keys_dir = snap_project.assets_dir / "keys"
if keys_dir.is_dir():
return keys_dir

return None


def create_app() -> Snapcraft:
"""Create a Snapcraft application with the proper commands."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ package-repositories:
suites: [focal]
key-id: 78E1918602959B9C59103100F1831DDAFC42E99D
url: http://ppa.launchpad.net/snappy-dev/snapcraft-daily/ubuntu
# Set an invalid keyserver here to force the use of the local key
key-server: keyserver.invalid.com
33 changes: 33 additions & 0 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import json
import os
import sys
from pathlib import Path
from textwrap import dedent

import craft_cli
Expand Down Expand Up @@ -467,3 +468,35 @@ def test_run_envvar_invalid(snapcraft_yaml, base, monkeypatch):
"'SNAPCRAFT_REMOTE_BUILD_STRATEGY'. Valid values are 'disable-fallback' and "
"'force-fallback'"
)


@pytest.mark.parametrize(
("project_location", "assets_dir", "expected_keys_path"),
[
# These locations come from yaml_utils._SNAP_PROJECT_FILES
# snapcraft.yaml locations where the keys are expected to be in snap/keys
("snapcraft.yaml", "snap", "snap/keys"),
("snap/snapcraft.yaml", "snap", "snap/keys"),
(".snapcraft.yaml", "snap", "snap/keys"),
# snapcraft.yaml location where the keys are expected to be somewhere else
("build-aux/snap/snapcraft.yaml", "build-aux/snap", "build-aux/snap/keys"),
],
)
def test_local_keys_path(new_dir, project_location, assets_dir, expected_keys_path):
"""Check that _get_local_keys_path() is correct given the location of snapcraft.yaml."""
snap_dir = new_dir / Path(project_location).parent
snap_dir.mkdir(exist_ok=True, parents=True)

# The project itself doesn't really matter, but must exist
project_yaml = snap_dir / "snapcraft.yaml"
project_yaml.write_text(PARSE_INFO_PROJECT)

app = application.create_app()
keys_dir = Path(assets_dir) / "keys"

# If the keys dir doesn't exist the method should return None
assert not keys_dir.is_dir()
assert app._get_local_keys_path() is None

keys_dir.mkdir(exist_ok=True, parents=True)
assert app._get_local_keys_path() == Path(expected_keys_path)

0 comments on commit dbd750a

Please sign in to comment.