-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add WSLAContainer flag to auto delete container after exit #14196
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
Changes from all commits
6d89e84
d7a1db4
682a154
1780f47
150af37
91a1b79
75de800
9242aec
f9e282a
0fba6af
00ae9fe
b971462
ae95a45
91a6b90
b286979
fe4dfc2
7643c18
4439bfc
12cc451
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 |
|---|---|---|
|
|
@@ -4191,4 +4191,83 @@ class WSLATests | |
|
|
||
| VERIFY_ARE_EQUAL(result.Output[1], std::format("{}\n", expectedOrder)); | ||
| } | ||
|
|
||
| TEST_METHOD(ContainerAutoRemove) | ||
| { | ||
| WSL2_TEST_ONLY(); | ||
| SKIP_TEST_ARM64(); | ||
|
|
||
| // Test that a container with the Rm flag is automatically deleted on Stop(). | ||
| { | ||
| WSLAContainerLauncher launcher("debian:latest", "test-auto-remove", {"/bin/cat"}, {}, {}, WSLAProcessFlagsStdin); | ||
| launcher.SetContainerFlags(WSLAContainerFlagsRm); | ||
|
|
||
| auto container = launcher.Launch(*m_defaultSession); | ||
|
|
||
| VERIFY_ARE_EQUAL(container.State(), WslaContainerStateRunning); | ||
| VERIFY_SUCCEEDED(container.Get().Stop(WSLASignalSIGKILL, 0)); | ||
|
|
||
| VERIFY_ARE_EQUAL(container.Get().Delete(), RPC_E_DISCONNECTED); | ||
|
|
||
| wil::com_ptr<IWSLAContainer> notFound; | ||
| VERIFY_ARE_EQUAL(m_defaultSession->OpenContainer("test-auto-remove", ¬Found), HRESULT_FROM_WIN32(ERROR_NOT_FOUND)); | ||
| } | ||
|
|
||
| // Test that a container with the Rm flag is automatically deleted when the init process is killed. | ||
| { | ||
| WSLAContainerLauncher launcher("debian:latest", "test-auto-remove", {"/bin/cat"}, {}, {}, WSLAProcessFlagsStdin); | ||
| launcher.SetContainerFlags(WSLAContainerFlagsRm); | ||
|
|
||
| // Prevent container from being deleted when handle is closed so we can verify auto-remove behavior. | ||
| auto container = launcher.Launch(*m_defaultSession); | ||
| auto process = container.GetInitProcess(); | ||
|
|
||
| VERIFY_ARE_EQUAL(container.State(), WslaContainerStateRunning); | ||
| VERIFY_SUCCEEDED(process.Get().Signal(WSLASignalSIGKILL)); | ||
| process.Wait(); | ||
|
|
||
| VERIFY_ARE_EQUAL(container.Get().Delete(), RPC_E_DISCONNECTED); | ||
|
|
||
| wil::com_ptr<IWSLAContainer> notFound; | ||
| VERIFY_ARE_EQUAL(m_defaultSession->OpenContainer("test-auto-remove", ¬Found), HRESULT_FROM_WIN32(ERROR_NOT_FOUND)); | ||
| } | ||
|
|
||
| // Test that the container autoremove flag is applied when the container exits on its own. | ||
| { | ||
| WSLAContainerLauncher launcher("debian:latest", "test-hostname", {"/bin/sh", "-c", "echo foo"}); | ||
| launcher.SetContainerFlags(WSLAContainerFlagsRm); | ||
|
|
||
| auto container = launcher.Launch(*m_defaultSession); | ||
| auto process = container.GetInitProcess(); | ||
| process.Wait(); | ||
|
|
||
| VERIFY_ARE_EQUAL(container.Get().Delete(), RPC_E_DISCONNECTED); | ||
|
|
||
| wil::com_ptr<IWSLAContainer> notFound; | ||
| VERIFY_ARE_EQUAL(m_defaultSession->OpenContainer("test-auto-remove", ¬Found), HRESULT_FROM_WIN32(ERROR_NOT_FOUND)); | ||
| } | ||
|
|
||
| // Test that the Rm flag is persisted across wsla sessions. | ||
| { | ||
| { | ||
| WSLAContainerLauncher launcher("debian:latest", "test-auto-remove", {"/bin/cat"}, {}, {}, WSLAProcessFlagsStdin); | ||
| launcher.SetContainerFlags(WSLAContainerFlagsRm); | ||
|
|
||
| auto container = launcher.Create(*m_defaultSession); | ||
| container.SetDeleteOnClose(false); | ||
|
|
||
| ResetTestSession(); | ||
| } | ||
|
|
||
| auto container = OpenContainer(m_defaultSession.get(), "test-auto-remove"); | ||
kvega005 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| VERIFY_SUCCEEDED(container.Get().Start(WSLAContainerStartFlagsNone)); | ||
| VERIFY_SUCCEEDED(container.Get().Stop(WSLASignalSIGKILL, 0)); | ||
|
|
||
| // verifyContainerDeleted("test-auto-remove"); | ||
| VERIFY_ARE_EQUAL(container.Get().Delete(), RPC_E_DISCONNECTED); | ||
|
|
||
| wil::com_ptr<IWSLAContainer> notFound; | ||
| VERIFY_ARE_EQUAL(m_defaultSession->OpenContainer("test-auto-remove", ¬Found), HRESULT_FROM_WIN32(ERROR_NOT_FOUND)); | ||
| } | ||
| } | ||
|
Collaborator
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. nit: I think it would also be a good idea to add test coverage for the "stop detection" code path. To do that we could create a container with the AutoRm flag that just does "echo foo", and validate that after the init process has exited, the container gets deleted (we'll probably have to check with a timeout since that won't be synchronous though, but we can use |
||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.