Skip to content

CLI: Add container prune command#40547

Draft
ptrivedi wants to merge 1 commit into
masterfrom
user/ptrivedi/container-prune
Draft

CLI: Add container prune command#40547
ptrivedi wants to merge 1 commit into
masterfrom
user/ptrivedi/container-prune

Conversation

@ptrivedi
Copy link
Copy Markdown
Contributor

Summary

Implement the wslc container prune command to remove all stopped containers. The backend IWSLCSession::PruneContainers API already exists; this adds the CLI frontend.

Changes

  • ContainerPruneCommand: New command class with --session arg
  • ContainerService::Prune(): Service layer using RAII PruneResult for exception-safe CoTaskMem cleanup
  • PruneContainers task: Prints pruned container IDs and total reclaimed space
  • PruneContainersResult: New model struct in ContainerModel.h
  • Localization: 3 new strings (description, long description, space reclaimed format)
  • Unit tests: CLI parsing test cases in CommandLineTestCases.h
  • E2E tests: Help output, no-stopped containers, stopped container pruned, running container preserved, multiple stopped containers
  • Updated container help output test to include prune subcommand

Implement the 'wslc container prune' command to remove all stopped
containers. The backend IWSLCSession::PruneContainers API already
exists; this adds the CLI frontend.

Changes:
- ContainerPruneCommand: new command class with --session arg
- ContainerService::Prune(): service layer using RAII PruneResult
- PruneContainers task: prints pruned container IDs and reclaimed space
- PruneContainersResult model struct
- 3 localization strings (desc, long desc, space reclaimed)
- CLI parsing unit tests in CommandLineTestCases.h
- E2E tests: help, no-stopped, stopped, running-preserved, multi-stopped
- Updated container help output test to include prune subcommand

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines +47 to +51
// Prune when no stopped containers exist should succeed with zero reclaimed space
const auto result = RunWslc(L"container prune");
result.Verify({.Stderr = L"", .ExitCode = 0});

VerifyStdoutContains(result, L"Total reclaimed space:");
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.

Since it's zero reclaimed, can we use the .Verify({.Stdout = "...: 0", ...}); instead?

const auto result = RunWslc(L"container prune");
result.Verify({.Stderr = L"", .ExitCode = 0});

VerifyStdoutContains(result, L"Total reclaimed space:");
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.

Is there a chance we can hardcode the values here based on manual test, or they are unpredictable?

Comment on lines +70 to +77
auto listResult = RunWslc(L"container list --all");
listResult.Verify({.Stderr = L"", .ExitCode = 0});
for (const auto& line : listResult.GetStdoutLines())
{
VERIFY_IS_FALSE(
line.find(L"prune-test-container") != std::wstring::npos,
L"Container 'prune-test-container' should have been pruned");
}
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.

We have a method for this VerifyContainerIsNotListed 😊

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.

Same for the other methods

private:
const TestImage& DebianImage = DebianTestImage();

static void VerifyStdoutContains(const WSLCExecutionResult& result, const std::wstring& substring)
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.

Unless my comment suggestion above does not help eliminate the need for this method, I would suggest moving this method to the WSLCExecutor where another method called StdoutContainsLine method exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants