| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WSLCE2EContainerKillTests.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains end-to-end tests for WSLC. |
| 12 | --*/ |
| 13 | |
| 14 | #include "precomp.h" |
| 15 | #include "windows/Common.h" |
| 16 | #include "WSLCExecutor.h" |
| 17 | #include "WSLCE2EHelpers.h" |
| 18 | #include "TestImageRegistry.h" |
| 19 | |
| 20 | namespace WSLCE2ETests { |
| 21 | using namespace wsl::shared; |
| 22 | |
| 23 | class WSLCE2EContainerKillTests |
| 24 | { |
| 25 | WSLC_TEST_CLASS(WSLCE2EContainerKillTests) |
| 26 | |
| 27 | TEST_CLASS_SETUP(ClassSetup) |
| 28 | { |
| 29 | TestImageRegistry::Instance().EnsureLoaded(DebianImage); |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | TEST_CLASS_CLEANUP(ClassCleanup) |
| 34 | { |
| 35 | EnsureContainerDoesNotExist(WslcContainerName); |
| 36 | EnsureContainerDoesNotExist(WslcContainerName2); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | TEST_METHOD_SETUP(TestMethodSetup) |
| 41 | { |
| 42 | EnsureContainerDoesNotExist(WslcContainerName); |
| 43 | EnsureContainerDoesNotExist(WslcContainerName2); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_HelpCommand) |
| 48 | { |
| 49 | auto result = RunWslc(L"container kill --help"); |
| 50 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 51 | VERIFY_IS_FALSE(result.Stdout.value().empty()); |
| 52 | } |
| 53 | |
| 54 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_KillsRunningContainer) |
| 55 | { |
| 56 | // Run a container in the background |
| 57 | auto result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag())); |
| 58 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 59 | auto containerId = result.GetStdoutOneLine(); |
| 60 | VERIFY_IS_FALSE(containerId.empty()); |
| 61 | |
| 62 | // Verify container is running |
| 63 | VerifyContainerIsListed(containerId, L"running"); |
| 64 | |
| 65 | // Kill the container |
| 66 | result = RunWslc(std::format(L"container kill {}", containerId)); |
| 67 | result.Verify({.Stdout = std::format(L"{}\r\n", containerId), .Stderr = L"", .ExitCode = 0}); |
| 68 | |
| 69 | // Verify the container is no longer running |
| 70 | VerifyContainerIsListed(containerId, L"exited"); |
| 71 | } |
| 72 | |
| 73 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_ByName) |
| 74 | { |
| 75 | // Run a container in the background |
| 76 | auto result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag())); |
| 77 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 78 | const auto containerId = result.GetStdoutOneLine(); |
| 79 | VERIFY_IS_FALSE(containerId.empty()); |
| 80 | |
| 81 | // Verify container is running |
| 82 | VerifyContainerIsListed(containerId, L"running"); |
| 83 | |
| 84 | // Kill by container name |
| 85 | result = RunWslc(std::format(L"container kill {}", WslcContainerName)); |
| 86 | result.Verify({.Stdout = std::format(L"{}\r\n", WslcContainerName), .Stderr = L"", .ExitCode = 0}); |
| 87 | |
| 88 | // Verify container is no longer running |
| 89 | VerifyContainerIsListed(containerId, L"exited"); |
| 90 | } |
| 91 | |
| 92 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_NotFound) |
| 93 | { |
| 94 | VerifyContainerIsNotListed(WslcContainerName); |
| 95 | |
| 96 | auto result = RunWslc(std::format(L"container kill {}", WslcContainerName)); |
| 97 | result.Verify( |
| 98 | {.Stderr = |
| 99 | FormatErrorMessage(std::format(L"Container '{}' not found.", WslcContainerName), L"WSLC_E_CONTAINER_NOT_FOUND"), |
| 100 | .ExitCode = 1}); |
| 101 | } |
| 102 | |
| 103 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_InvalidSignal) |
| 104 | { |
| 105 | auto result = RunWslc(std::format(L"container run --name {} {}", WslcContainerName, DebianImage.NameAndTag())); |
| 106 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 107 | |
| 108 | { |
| 109 | result = RunWslc(std::format(L"container kill {} -s 0", WslcContainerName)); |
| 110 | result.Verify({.Stdout = L"", .ExitCode = 1}); |
| 111 | VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Invalid signal value: 0 is out of valid range (1-31).")); |
| 112 | } |
| 113 | |
| 114 | { |
| 115 | result = RunWslc(std::format(L"container kill {} -s 32", WslcContainerName)); |
| 116 | result.Verify({.Stdout = L"", .ExitCode = 1}); |
| 117 | VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Invalid signal value: 32 is out of valid range (1-31).")); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_TargetedContainerOnly) |
| 122 | { |
| 123 | // Run first container in background |
| 124 | auto result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag())); |
| 125 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 126 | const auto firstContainerId = result.GetStdoutOneLine(); |
| 127 | VERIFY_IS_FALSE(firstContainerId.empty()); |
| 128 | |
| 129 | // Run second container in background |
| 130 | result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName2, DebianImage.NameAndTag())); |
| 131 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 132 | const auto secondContainerId = result.GetStdoutOneLine(); |
| 133 | VERIFY_IS_FALSE(secondContainerId.empty()); |
| 134 | |
| 135 | // Verify both are running |
| 136 | VerifyContainerIsListed(firstContainerId, L"running"); |
| 137 | VerifyContainerIsListed(secondContainerId, L"running"); |
| 138 | |
| 139 | // Kill only the first container |
| 140 | result = RunWslc(std::format(L"container kill {}", firstContainerId)); |
| 141 | result.Verify({.Stdout = std::format(L"{}\r\n", firstContainerId), .Stderr = L"", .ExitCode = 0}); |
| 142 | |
| 143 | // Verify first exited, second still running |
| 144 | VerifyContainerIsListed(firstContainerId, L"exited"); |
| 145 | VerifyContainerIsListed(secondContainerId, L"running"); |
| 146 | } |
| 147 | |
| 148 | WSLC_TEST_METHOD(WSLCE2E_Container_Kill_ContinuesAfterFailure) |
| 149 | { |
| 150 | // Run first container in background |
| 151 | auto result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName, DebianImage.NameAndTag())); |
| 152 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 153 | const auto firstContainerId = result.GetStdoutOneLine(); |
| 154 | VERIFY_IS_FALSE(firstContainerId.empty()); |
| 155 | |
| 156 | // Run second container in background |
| 157 | result = RunWslc(std::format(L"container run -d --name {} {} sleep infinity", WslcContainerName2, DebianImage.NameAndTag())); |
| 158 | result.Verify({.Stderr = L"", .ExitCode = 0}); |
| 159 | const auto secondContainerId = result.GetStdoutOneLine(); |
| 160 | VERIFY_IS_FALSE(secondContainerId.empty()); |
| 161 | |
| 162 | // A container that cannot be killed is reported without skipping the ones after it |
| 163 | result = RunWslc(std::format(L"container kill {} {} {}", firstContainerId, InvalidContainerName, secondContainerId)); |
| 164 | result.Verify( |
| 165 | {.Stdout = std::format(L"{}\r\n{}\r\n", firstContainerId, secondContainerId), |
| 166 | .Stderr = FormatErrorMessage( |
| 167 | std::format(L"Container '{}' not found.", InvalidContainerName), L"WSLC_E_CONTAINER_NOT_FOUND"), |
| 168 | .ExitCode = 1}); |
| 169 | |
| 170 | VerifyContainerIsListed(firstContainerId, L"exited"); |
| 171 | VerifyContainerIsListed(secondContainerId, L"exited"); |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | const std::wstring WslcContainerName = L"wslc-test-container"; |
| 176 | const std::wstring WslcContainerName2 = L"wslc-test-container-2"; |
| 177 | const std::wstring InvalidContainerName = L"wslc-nonexistent-container-for-kill"; |
| 178 | const TestImage& DebianImage = DebianTestImage(); |
| 179 | }; |
| 180 | } // namespace WSLCE2ETests |