Skip to content
Open
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
2 changes: 2 additions & 0 deletions craft_application/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
from .init import InitCommand
from .lifecycle import get_lifecycle_command_group, LifecycleCommand, TestCommand
from .other import get_other_command_group
from .prune_instances import PruneInstancesCommand
from .remote import RemoteBuild # Not part of the default commands.

__all__ = [
"AppCommand",
"ExtensibleCommand",
"InitCommand",
"PruneInstancesCommand",
"RemoteBuild",
"lifecycle",
"LifecycleCommand",
Expand Down
2 changes: 2 additions & 0 deletions craft_application/commands/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from craft_cli import CommandGroup, emit

from . import InitCommand, base
from .prune_instances import PruneInstancesCommand

if TYPE_CHECKING: # pragma: no cover
import argparse
Expand All @@ -29,6 +30,7 @@ def get_other_command_group() -> CommandGroup:
"""Return the lifecycle related command group."""
commands: list[type[base.AppCommand]] = [
InitCommand,
PruneInstancesCommand,
VersionCommand,
]

Expand Down
59 changes: 59 additions & 0 deletions craft_application/commands/prune_instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2026 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Provider-related commands."""

from __future__ import annotations

import textwrap
from typing import TYPE_CHECKING

from . import base

if TYPE_CHECKING: # pragma: no cover
import argparse


class PruneInstancesCommand(base.AppCommand):
"""Prune instances for the active provider."""

name = "prune-instances"
help_msg = "Prune instances for the selected provider or all providers"
overview = textwrap.dedent(
"""
Prune instances using the standard provider-selection logic.
Use --provider to prune instances for a specific provider, or
--all-providers to prune instances from all providers.
"""
)

def _fill_parser(self, parser: argparse.ArgumentParser) -> None:
provider_group = parser.add_mutually_exclusive_group()
provider_group.add_argument(
"--all-providers",
action="store_true",
help="Prune instances from all providers",
)
provider_group.add_argument(
"--provider",
help="Prune instances for a specific provider; omit to use the standard provider-selection logic",
choices=["lxd", "multipass"],
)

def run(self, parsed_args: argparse.Namespace) -> None:
"""Run the prune-instances command."""
self._services.provider.prune_instances(
all_providers=parsed_args.all_providers,
provider_name=parsed_args.provider,
prune_templates=parsed_args.prune_templates,
)
Comment thread
mr-cal marked this conversation as resolved.
Comment on lines +53 to +59

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed, I think include_templates accidentally got removed when making the other args mutually exclusive.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hmm, would the _fill_parser() method add the include_templates or prune_templates flag?

51 changes: 51 additions & 0 deletions craft_application/services/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,54 @@ def prepare_instance(instance: craft_providers.Executor) -> None:
finally:
if active_fetch_service:
self._services.get("fetch").teardown_instance()

def prune_instances(
self,
*,
all_providers: bool = False,
provider_name: str | None = None,
prune_templates: bool = False,
) -> None:
"""Prune instances and optionally templates for the provider(s).

:param all_providers: Whether to prune instances for all providers or just the
current provider.
:param provider_name: Optional name of the provider to prune (if all_providers
is False).
:param prune_templates: Whether to also prune templates (if supported by the
provider).
"""
providers: list[craft_providers.Provider] = []

if provider_name:
providers.append(self._get_provider_by_name(name=provider_name))
elif all_providers:
self.add_provider_if_available(
providers, "lxd", "LXD provider not available, skipping LXD pruning."
)
self.add_provider_if_available(
providers,
"multipass",
"Multipass provider not available, skipping multipass pruning.",
)
else:
providers.append(self.get_provider(name=provider_name))

for provider in providers:
emit.progress(f"Pruning instances for provider {provider.name!r}...")
provider.prune(prune_templates=prune_templates)

def add_provider_if_available(
self,
providers: list[craft_providers.Provider],
provider_key: str,
unavailable_message: str,
) -> None:
"""Add provider to the providers list if it is available, otherwise logs the unavailable_message."""
try:
provider = self._get_provider_by_name(provider_key)
provider.ensure_provider_is_available()
except (craft_providers.ProviderError, RuntimeError):
emit.debug(unavailable_message)
return
providers.append(provider)
23 changes: 23 additions & 0 deletions tests/spread/testcraft/prune-instances/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
summary: Testcraft should prune its instances

systems:
- ubuntu-24.04-64

execute: |
# Create a LXD instance.
testcraft pull --use-lxd

# Verify the instance exists. The name should start with 'testcraft-prune-instances-test-'.
lxc list --format csv -c n | grep "^testcraft-prune-instances-test-"

# Prune instances.
testcraft prune-instances --all-providers

# Verify the instance is gone.
if lxc list --format csv -c n | grep "^testcraft-prune-instances-test-"; then
echo "Instance was not pruned!"
exit 1
fi

restore: |
testcraft prune-instances --all-providers
11 changes: 11 additions & 0 deletions tests/spread/testcraft/prune-instances/testcraft.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: prune-instances-test
summary: test summary
version: "0.1"

base: ubuntu@24.04
platforms:
amd64:

parts:
my-part:
plugin: nil
4 changes: 2 additions & 2 deletions tests/unit/commands/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import argparse

import pytest
from craft_application.commands import InitCommand
from craft_application.commands import InitCommand, PruneInstancesCommand
from craft_application.commands.other import VersionCommand, get_other_command_group

OTHER_COMMANDS = {InitCommand, VersionCommand}
OTHER_COMMANDS = {InitCommand, VersionCommand, PruneInstancesCommand}


@pytest.mark.parametrize("commands", [OTHER_COMMANDS])
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/commands/test_prune_instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This file is part of craft-application.
#
# Copyright 2026 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests for prune-instances command."""

import argparse

from craft_application.commands import PruneInstancesCommand


def test_prune_instances_run(app_metadata, mock_services):
parsed_args = argparse.Namespace(
all_providers=True,
provider="lxd",
prune_templates=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed.

)
Comment on lines +24 to +28

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agreed - these argparse values don't reflect what a user would be able to provide at the command line.

IIRC, manually defining parsed_args = argparse.Namespace(...) in the test bypasses code like the mutually exclusive args. To test that code, you have to mock argv and assert the error with capsys (example).

I think you can do this in 2 tests. One capsys-based test that ensures the args are mutually exclusive and another test that verifies the namespace args are successfully passed to provider.prune_instances(...).

You already have the second test written, but I recommend adding parametrizing it with @pytest.mark.parameterize() such that the test ensures what you set in parsed_args = argparse.Namespace(...) actually get passed to provider.prune_instances(...).

command = PruneInstancesCommand({"app": app_metadata, "services": mock_services})
command.run(parsed_args)

mock_services.provider.prune_instances.assert_called_once_with(
all_providers=True,
provider_name="lxd",
prune_templates=True,
)
63 changes: 63 additions & 0 deletions tests/unit/services/test_provider.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add a third unit test for when provider_name = None? It should call get_provider(), which you can mock to return lxd.

Original file line number Diff line number Diff line change
Expand Up @@ -1174,3 +1174,66 @@ def test_configure_instance_with_pro_skipped(mocker, provider_service):
mock_instance.install_pro_client.assert_not_called()
mock_instance.attach_pro_subscription.assert_not_called()
mock_instance.enable_pro_service.assert_not_called()


def test_prune_instances_single_provider(monkeypatch, provider_service):
"""Prune for a single provider."""
mock_lxd = mock.MagicMock()
mock_lxd.name = "lxd"

monkeypatch.setattr(provider_service, "get_provider", lambda name: mock_lxd)

provider_service.prune_instances(provider_name="lxd", prune_templates=True)

mock_lxd.prune.assert_called_once_with(prune_templates=True)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you can ignore this, as this test needs to be updated once https://github.com/canonical/craft-application/pull/1058/changes#r3120374251 is accomdated.

def test_prune_instances_none_provider(monkeypatch, provider_service):
"""Prune for a single provider."""
mock_lxd = mock.MagicMock()
mock_lxd.name = "lxd"

monkeypatch.setattr(provider_service, "get_provider", lambda name: mock_lxd)

provider_service.prune_instances(provider_name=None, prune_templates=True)

mock_lxd.prune.assert_called_once_with(prune_templates=True)


def test_prune_instances_all_providers(monkeypatch, provider_service):
"""Prune for all supported providers."""
mock_lxd = mock.MagicMock()
mock_lxd.name = "lxd"
mock_multipass = mock.MagicMock()
mock_multipass.name = "multipass"

def mock_get_by_name(name):
if name == "lxd":
return mock_lxd
if name == "multipass":
return mock_multipass
raise RuntimeError(f"Unknown provider: {name}")

monkeypatch.setattr(provider_service, "_get_provider_by_name", mock_get_by_name)

provider_service.prune_instances(all_providers=True, prune_templates=False)

mock_lxd.prune.assert_called_once_with(prune_templates=False)
mock_multipass.prune.assert_called_once_with(prune_templates=False)
Comment on lines +1203 to +1222

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we need to care about this - we're able to assume commands.prune_instances is only providing lowercase provider names to this function.



def test_prune_instances_all_providers_skips_unsupported(monkeypatch, provider_service):
"""Prune for all supported providers, skipping those not supported on the host."""
mock_lxd = mock.MagicMock()
mock_lxd.name = "lxd"

def mock_get_by_name(name):
if name == "lxd":
return mock_lxd
raise RuntimeError("Unsupported on this platform")

monkeypatch.setattr(provider_service, "_get_provider_by_name", mock_get_by_name)

provider_service.prune_instances(all_providers=True)

mock_lxd.prune.assert_called_once_with(prune_templates=False)
Loading