-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWSLCE2EContainerRunTests.cpp
More file actions
306 lines (242 loc) · 11.7 KB
/
WSLCE2EContainerRunTests.cpp
File metadata and controls
306 lines (242 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WSLCE2EContainerRunTests.cpp
Abstract:
This file contains end-to-end tests for WSLC.
--*/
#include "precomp.h"
#include "windows/Common.h"
#include "WSLCExecutor.h"
#include "WSLCE2EHelpers.h"
namespace WSLCE2ETests {
class WSLCE2EContainerRunTests
{
WSLC_TEST_CLASS(WSLCE2EContainerRunTests)
TEST_CLASS_SETUP(ClassSetup)
{
EnsureImageIsLoaded(DebianImage);
return true;
}
TEST_CLASS_CLEANUP(ClassCleanup)
{
EnsureContainerDoesNotExist(WslcContainerName);
EnsureImageIsDeleted(DebianImage);
return true;
}
TEST_METHOD_SETUP(TestMethodSetup)
{
EnsureContainerDoesNotExist(WslcContainerName);
return true;
}
TEST_METHOD(WSLCE2E_Container_Run_HelpCommand)
{
WSL2_TEST_ONLY();
auto result = RunWslc(L"container run --help");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Container_With_Command)
{
WSL2_TEST_ONLY();
VerifyContainerIsNotListed(WslcContainerName);
auto command = L"echo echo_from_container";
auto result = RunWslc(std::format(L"container run --name {} {} {}", WslcContainerName, DebianImage.NameAndTag(), command));
result.Verify({.Stdout = L"echo_from_container\n", .Stderr = L"", .ExitCode = 0});
VerifyContainerIsListed(WslcContainerName, L"exited");
}
TEST_METHOD(WSLCE2E_Container_Run_CIDFile_Valid)
{
WSL2_TEST_ONLY();
// Prepare a CID file path that does not exist
const auto cidFilePath = wsl::windows::common::filesystem::GetTempFilename();
VERIFY_IS_TRUE(DeleteFileW(cidFilePath.c_str()));
auto deleteCidFile = wil::scope_exit([&]() { VERIFY_IS_TRUE(DeleteFileW(cidFilePath.c_str())); });
auto result = RunWslc(std::format(
L"container run -d --cidfile \"{}\" --name {} {} sleep infinity",
EscapePath(cidFilePath.wstring()),
WslcContainerName,
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
const auto containerId = result.GetStdoutOneLine();
VERIFY_IS_TRUE(std::filesystem::exists(cidFilePath));
VERIFY_ARE_EQUAL(containerId, ReadFileContent(cidFilePath.wstring()));
}
TEST_METHOD(WSLCE2E_Container_Run_CIDFile_AlreadyExists)
{
WSL2_TEST_ONLY();
const auto cidFilePath = wsl::windows::common::filesystem::GetTempFilename();
auto deleteCidFile = wil::scope_exit([&]() { VERIFY_IS_TRUE(DeleteFileW(cidFilePath.c_str())); });
auto result = RunWslc(std::format(
L"container run --cidfile \"{}\" --name {} {}", EscapePath(cidFilePath.wstring()), WslcContainerName, DebianImage.NameAndTag()));
result.Verify(
{.Stderr = std::format(L"CID file '{}' already exists\r\nError code: ERROR_ALREADY_EXISTS\r\n", EscapePath(cidFilePath.wstring())),
.ExitCode = 1});
VerifyContainerIsNotListed(WslcContainerName);
}
TEST_METHOD(WSLCE2E_Container_Run_Entrypoint)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm --entrypoint /bin/whoami {}", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"root\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_And_Arguments)
{
WSL2_TEST_ONLY();
auto result = RunWslc(
std::format(L"container run --rm --entrypoint /bin/echo {} hello from entrypoint with args", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"hello from entrypoint with args\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_Invalid_Path)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm --entrypoint /bin/does-not-exist {}", DebianImage.NameAndTag()));
result.Verify(
{.Stdout = L"", .Stderr = L"failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: \"/bin/does-not-exist\": stat /bin/does-not-exist: no such file or directory: unknown\r\nError code: E_INVALIDARG\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_Detach_Lifecycle)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(
L"container run --name {} -d --entrypoint /bin/sleep {} infinity", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VerifyContainerIsListed(WslcContainerName, L"running");
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_NameRoot)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u root {} sh -c \"id -un; id -u; id -g\"", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"root\n0\n0\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_UidRoot)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u 0 {} id -u", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"0\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_UidGidRoot)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u 0:0 {} sh -c \"id -u; id -g\"", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"0\n0\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_UnknownUser_Fails)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u user_does_not_exist {} id -u", DebianImage.NameAndTag()));
result.Verify(
{.Stderr = L"unable to find user user_does_not_exist: no matching entries in passwd file\r\nError code: E_FAIL\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_UnknownGroup_Fails)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u root:badgid {} id -u", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"unable to find group badgid: no matching entries in group file\r\nError code: E_FAIL\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_NameGroupRoot)
{
WSL2_TEST_ONLY();
auto result =
RunWslc(std::format(L"container run --rm -u root:root {} sh -c \"id -un; id -u; id -g\"", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"root\n0\n0\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_UserOption_NonRootUser_Succeeds)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm -u nobody {} sh -c \"id -un; id -u; id -g\"", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"nobody\n65534\n65534\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Tmpfs)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(
L"container run --rm --tmpfs /wslc-tmpfs {} sh -c \"echo -n 'tmpfs_test' > /wslc-tmpfs/data && cat "
L"/wslc-tmpfs/data\"",
DebianImage.NameAndTag()));
result.Verify({.Stdout = L"tmpfs_test", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_With_Options)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(
L"container run --rm --tmpfs /wslc-tmpfs:size=64k {} sh -c \"mount | grep -q ' on /wslc-tmpfs type tmpfs ' && echo "
L"mounted\"",
DebianImage.NameAndTag()));
result.Verify({.Stdout = L"mounted\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_Multiple_With_Options)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(
L"container run --rm --tmpfs /wslc-tmpfs1:size=64k --tmpfs /wslc-tmpfs2:size=128k {} sh -c \"mount | grep -q ' on "
L"/wslc-tmpfs1 type tmpfs ' && mount | grep -q ' on /wslc-tmpfs2 type tmpfs ' && echo mounted\"",
DebianImage.NameAndTag()));
result.Verify({.Stdout = L"mounted\n", .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_RelativePath_Fails)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm --tmpfs wslc-tmpfs {}", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"invalid mount path: 'wslc-tmpfs' mount path must be absolute\r\nError code: E_FAIL\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_EmptyDestination_Fails)
{
WSL2_TEST_ONLY();
auto result = RunWslc(std::format(L"container run --rm --tmpfs :size=64k {}", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"invalid mount path: '' mount path must be absolute\r\nError code: E_FAIL\r\n", .ExitCode = 1});
}
private:
const std::wstring WslcContainerName = L"wslc-test-container";
const TestImage& DebianImage = DebianTestImage();
std::wstring GetHelpMessage() const
{
std::wstringstream output;
output << GetWslcHeader() //
<< GetDescription() //
<< GetUsage() //
<< GetAvailableCommands() //
<< GetAvailableOptions();
return output.str();
}
std::wstring GetDescription() const
{
return L"Runs a container. By default, the container is started in the background; use --detach to run in the "
L"foreground.\r\n\r\n";
}
std::wstring GetUsage() const
{
return L"Usage: wslc container run [<options>] <image> [<command>] [<arguments>...]\r\n\r\n";
}
std::wstring GetAvailableCommands() const
{
std::wstringstream commands;
commands << L"The following arguments are available:\r\n"
<< L" image Image name\r\n"
<< L" command The command to run\r\n"
<< L" arguments Arguments to pass to container's init process\r\n"
<< L"\r\n";
return commands.str();
}
std::wstring GetAvailableOptions() const
{
std::wstringstream options;
options << L"The following options are available:\r\n"
<< L" --cidfile Write the container ID to the provided path\r\n"
<< L" -d,--detach Run container in detached mode\r\n"
<< L" --entrypoint Specifies the container init process executable\r\n"
<< L" -e,--env Key=Value pairs for environment variables\r\n"
<< L" --env-file File containing key=value pairs of env variables\r\n"
<< L" -i,--interactive Attach to stdin and keep it open\r\n"
<< L" --name Name of the container\r\n"
<< L" -p,--publish Publish a port from a container to host\r\n"
<< L" --rm Remove the container after it stops\r\n"
<< L" --session Specify the session to use\r\n"
<< L" --tmpfs Mount tmpfs to the container at the given path\r\n"
<< L" -t,--tty Open a TTY with the container process.\r\n"
<< L" -u,--user User ID for the process (name|uid|uid:gid)\r\n"
<< L" -v,--volume Bind mount a volume to the container\r\n"
<< L" -h,--help Shows help about the selected command\r\n"
<< L"\r\n";
return options.str();
}
};
} // namespace WSLCE2ETests