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

fix(remote): parse comma-separated architectures #4543

Merged
merged 3 commits into from
Jan 26, 2024
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
29 changes: 21 additions & 8 deletions snapcraft/commands/remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022-2023 Canonical Ltd.
# Copyright 2022-2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -91,18 +91,17 @@ def fill_parser(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--status", action="store_true", help="display remote build status"
)
parser_target = parser.add_mutually_exclusive_group()
parser_target.add_argument(
parser.add_argument(
"--build-on",
type=lambda arg: [arch.strip() for arch in arg.split(",")],
metavar="arch",
nargs="+",
help=HIDDEN,
)
parser_target.add_argument(
parser.add_argument(
"--build-for",
type=lambda arg: [arch.strip() for arch in arg.split(",")],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this support something wacky like --build-for=amd64 --build-for=arm64,armhf?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. When there are duplicates, argparse/craft-cli uses the last --build-{on|for}, silently ignores the other --build-{on|for}s, and always provides a single string for this lambda.

Normally I wouldn't add a test for standard behavior, but since this is a delicate situation, I added an extra unit test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear, this wouldn't be a change in behavior right? The previous code would also only use the last --build-x?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this behavior is unchanged.

The only "loss" is that snapcraft remote-build --build-for amd64 arm64 doesn't work.

And I think that "loss" is OK because it is a regression in the new remote builder: snapcraft 7 only supported comma-separated architectures, not space-separated architectures.

metavar="arch",
nargs="+",
help="architecture to build for",
help="comma-separated list of architectures to build for",
)
parser.add_argument(
"--build-id", metavar="build-id", help="specific build id to retrieve"
Expand Down Expand Up @@ -141,7 +140,6 @@ def run(self, parsed_args: argparse.Namespace) -> None:

if parsed_args.build_on:
emit.message("Use --build-for instead of --build-on")
parsed_args.build_for = parsed_args.build_on

if not parsed_args.launchpad_accept_public_upload and not confirm_with_user(
_CONFIRMATION_PROMPT
Expand Down Expand Up @@ -381,11 +379,24 @@ def _determine_architectures(self) -> List[str]:
The build architectures can be set via the `--build-on` parameter or determined
from the build-on architectures listed in the project's snapcraft.yaml.

To retain backwards compatibility, `--build-for` can also be used to
set the architectures.

:returns: A list of architectures.

:raises SnapcraftError: If `--build-on` was provided and architectures are
defined in the project's snapcraft.yaml.
:raises SnapcraftError: If `--build-on` and `--build-for` are both provided.
"""
# argparse's `add_mutually_exclusive_group()` cannot be used because
# ArgumentParsingErrors executes the legacy remote-builder before this module
# can decide if the project is allowed to use the legacy remote-builder
if self._parsed_args.build_on and self._parsed_args.build_for:
raise SnapcraftError(
# use the same error as argparse produces for consistency
"Error: argument --build-for: not allowed with argument --build-on"
)

project_architectures = self._get_project_build_on_architectures()
if project_architectures and self._parsed_args.build_for:
raise SnapcraftError(
Expand All @@ -395,6 +406,8 @@ def _determine_architectures(self) -> List[str]:

if project_architectures:
archs = project_architectures
elif self._parsed_args.build_on:
archs = self._parsed_args.build_on
elif self._parsed_args.build_for:
archs = self._parsed_args.build_for
else:
Expand Down
94 changes: 87 additions & 7 deletions tests/unit/commands/test_remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright 2022-2023 Canonical Ltd.
# Copyright 2022-2024 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
Expand Down Expand Up @@ -152,6 +152,43 @@ def test_command_accept_upload(
mock_run_new_or_fallback_remote_build.assert_called_once()


@pytest.mark.parametrize(
"create_snapcraft_yaml", CURRENT_BASES | LEGACY_BASES, indirect=True
)
@pytest.mark.usefixtures(
"create_snapcraft_yaml", "mock_confirm", "use_new_remote_build"
)
def test_command_new_build_arguments_mutually_exclusive(capsys, mocker):
"""`--build-for` and `--build-on` are mutually exclusive in the new remote-build."""
mocker.patch.object(
sys,
"argv",
["snapcraft", "remote-build", "--build-on", "amd64", "--build-for", "arm64"],
)

cli.run()

_, err = capsys.readouterr()
assert "Error: argument --build-for: not allowed with argument --build-on" in err


@pytest.mark.parametrize(
"create_snapcraft_yaml", LEGACY_BASES | {"core22"}, indirect=True
)
@pytest.mark.usefixtures("create_snapcraft_yaml", "mock_confirm")
def test_command_legacy_build_arguments_not_mutually_exclusive(mocker, mock_run_legacy):
"""`--build-for` and `--build-on` are not mutually exclusive for legacy."""
mocker.patch.object(
sys,
"argv",
["snapcraft", "remote-build", "--build-on", "amd64", "--build-for", "arm64"],
)

cli.run()

mock_run_legacy.assert_called_once()


@pytest.mark.parametrize(
"create_snapcraft_yaml", CURRENT_BASES | LEGACY_BASES, indirect=True
)
Expand Down Expand Up @@ -710,13 +747,56 @@ def test_determine_architectures_host_arch(mocker, mock_remote_builder):
)


@pytest.mark.parametrize("build_flag", ["--build-for", "--build-on"])
@pytest.mark.parametrize(
("archs", "expected_archs"),
[
("amd64", ["amd64"]),
("amd64,arm64", ["amd64", "arm64"]),
("amd64,amd64,arm64 ", ["amd64", "amd64", "arm64"]),
],
)
@pytest.mark.parametrize(
"create_snapcraft_yaml", CURRENT_BASES | LEGACY_BASES, indirect=True
)
@pytest.mark.usefixtures(
"create_snapcraft_yaml", "mock_confirm", "use_new_remote_build"
)
def test_determine_architectures_provided_by_user_duplicate_arguments(
build_flag, archs, expected_archs, mocker, mock_remote_builder
):
"""Argparse should only consider the last argument provided for build flags."""
mocker.patch.object(
sys,
"argv",
# `--build-{for|on} armhf` should get silently ignored by argparse
["snapcraft", "remote-build", build_flag, "armhf", build_flag, archs],
)

cli.run()

mock_remote_builder.assert_called_with(
app_name="snapcraft",
build_id=None,
project_name="mytest",
architectures=expected_archs,
project_dir=Path(),
timeout=0,
)


@pytest.mark.parametrize("build_flag", ["--build-for", "--build-on"])
@pytest.mark.parametrize(
("args", "expected_archs"),
("archs", "expected_archs"),
[
(["--build-for", "amd64"], ["amd64"]),
(["--build-for", "amd64", "arm64"], ["amd64", "arm64"]),
("amd64", ["amd64"]),
("amd64,arm64", ["amd64", "arm64"]),
("amd64, arm64", ["amd64", "arm64"]),
("amd64,arm64 ", ["amd64", "arm64"]),
("amd64,arm64,armhf", ["amd64", "arm64", "armhf"]),
(" amd64 , arm64 , armhf ", ["amd64", "arm64", "armhf"]),
# launchpad will accept and ignore duplicates
(["--build-for", "amd64", "amd64"], ["amd64", "amd64"]),
(" amd64 , arm64 , arm64 ", ["amd64", "arm64", "arm64"]),
],
)
@pytest.mark.parametrize(
Expand All @@ -726,10 +806,10 @@ def test_determine_architectures_host_arch(mocker, mock_remote_builder):
"create_snapcraft_yaml", "mock_confirm", "use_new_remote_build"
)
def test_determine_architectures_provided_by_user(
args, expected_archs, mocker, mock_remote_builder
build_flag, archs, expected_archs, mocker, mock_remote_builder
):
"""Use architectures provided by the user."""
mocker.patch.object(sys, "argv", ["snapcraft", "remote-build"] + args)
mocker.patch.object(sys, "argv", ["snapcraft", "remote-build", build_flag, archs])

cli.run()

Expand Down
Loading