-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add prune instances #1058
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
base: main
Are you sure you want to change the base?
Changes from all commits
45bd767
4cade65
43a8250
91c8ffb
c3999ec
fef7796
5b38cc6
dbc0068
1449184
1c06d19
5b7dad5
3deae89
51fc6d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 on lines
+53
to
+59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I think
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, would the |
||
| 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 |
| 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 |
| 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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. |
||
| ) | ||
|
Comment on lines
+24
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 You already have the second test written, but I recommend adding parametrizing it with |
||
| 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, | ||
| ) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a third unit test for when |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
|
|
||
| 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) | ||
Uh oh!
There was an error while loading. Please reload this page.