master
cpp 132 lines 4.79 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 WSLCE2EContainerPruneTests.cpp
8
9 Abstract:
10
11 This file contains end-to-end tests for WSLC container prune command.
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 WSLCE2EContainerPruneTests
24 {
25 WSLC_TEST_CLASS(WSLCE2EContainerPruneTests)
26
27 TEST_CLASS_SETUP(ClassSetup)
28 {
29 TestImageRegistry::Instance().EnsureLoaded(DebianImage);
30
31 // Clean up any leftover containers from previous failed runs
32 EnsureContainerDoesNotExist(L"prune-test-container");
33 EnsureContainerDoesNotExist(L"prune-running-test");
34 EnsureContainerDoesNotExist(L"prune-multi-1");
35 EnsureContainerDoesNotExist(L"prune-multi-2");
36 return true;
37 }
38
39 TEST_CLASS_CLEANUP(ClassCleanup)
40 {
41 // Clean up any leftover containers
42 RunWslc(L"container prune");
43 return true;
44 }
45
46 WSLC_TEST_METHOD(WSLCE2E_Container_Prune_HelpCommand)
47 {
48 const auto result = RunWslc(L"container prune --help");
49 result.Verify({.Stderr = L"", .ExitCode = 0});
50 VERIFY_IS_FALSE(result.Stdout.value().empty());
51 }
52
53 WSLC_TEST_METHOD(WSLCE2E_Container_Prune_NoStoppedContainers)
54 {
55 // Establish a clean baseline first so this does not depend on what other tests left behind.
56 RunWslc(L"container prune").Verify({.Stderr = L"", .ExitCode = 0});
57
58 const auto result = RunWslc(L"container prune");
59
60 // With nothing pruned docker emits only the reclaimed-space line, with no header or leading blank line.
61 result.Verify({.Stdout = L"Total reclaimed space: 0B\r\n", .Stderr = L"", .ExitCode = 0});
62 }
63
64 WSLC_TEST_METHOD(WSLCE2E_Container_Prune_StoppedContainer)
65 {
66 // Create and stop a container, then prune it
67 auto createResult = RunWslc(std::format(L"container create --name prune-test-container {}", DebianImage.NameAndTag()));
68 createResult.Verify({.Stderr = L"", .ExitCode = 0});
69 auto containerId = createResult.GetStdoutOneLine();
70
71 auto cleanup = wil::scope_exit([&]() { RunWslc(L"container prune"); });
72
73 // The created container is in stopped state, so prune should remove it
74 const auto result = RunWslc(L"container prune");
75 result.Verify({.Stderr = L"", .ExitCode = 0});
76
77 // Verify pruned container ID is in output
78 VERIFY_IS_TRUE(result.StdoutContainsSubstring(containerId));
79 VERIFY_IS_TRUE(result.StdoutContainsSubstring(Localization::WSLCCLI_ContainerPruneDeletedHeader()));
80
81 // Verify the container is actually removed
82 VerifyContainerIsNotListed(L"prune-test-container");
83 }
84
85 WSLC_TEST_METHOD(WSLCE2E_Container_Prune_RunningContainerNotPruned)
86 {
87 // Start a running container, verify prune does NOT remove it
88 auto runResult = RunWslc(std::format(L"container run --detach --name prune-running-test {} sleep 300", DebianImage.NameAndTag()));
89 runResult.Verify({.Stderr = L"", .ExitCode = 0});
90
91 auto cleanup = wil::scope_exit([&]() {
92 RunWslc(L"container kill prune-running-test");
93 RunWslc(L"container remove --force prune-running-test");
94 });
95
96 // Prune should not remove a running container
97 const auto pruneResult = RunWslc(L"container prune");
98 pruneResult.Verify({.Stderr = L"", .ExitCode = 0});
99
100 // Verify the running container is still present
101 VerifyContainerIsListed(L"prune-running-test", L"running");
102 }
103
104 WSLC_TEST_METHOD(WSLCE2E_Container_Prune_MultipleStopped)
105 {
106 // Create multiple stopped containers and verify all are pruned
107 auto create1 = RunWslc(std::format(L"container create --name prune-multi-1 {}", DebianImage.NameAndTag()));
108 create1.Verify({.Stderr = L"", .ExitCode = 0});
109 auto containerId1 = create1.GetStdoutOneLine();
110
111 auto create2 = RunWslc(std::format(L"container create --name prune-multi-2 {}", DebianImage.NameAndTag()));
112 create2.Verify({.Stderr = L"", .ExitCode = 0});
113 auto containerId2 = create2.GetStdoutOneLine();
114
115 auto cleanup = wil::scope_exit([&]() { RunWslc(L"container prune"); });
116
117 const auto result = RunWslc(L"container prune");
118 result.Verify({.Stderr = L"", .ExitCode = 0});
119
120 // Verify pruned container IDs are in output
121 VERIFY_IS_TRUE(result.StdoutContainsSubstring(containerId1));
122 VERIFY_IS_TRUE(result.StdoutContainsSubstring(containerId2));
123
124 // Verify both containers are removed
125 VerifyContainerIsNotListed(L"prune-multi-1");
126 VerifyContainerIsNotListed(L"prune-multi-2");
127 }
128
129 private:
130 const TestImage& DebianImage = DebianTestImage();
131 };
132 } // namespace WSLCE2ETests