-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWSLCE2EGlobalTests.cpp
More file actions
477 lines (382 loc) · 19.3 KB
/
WSLCE2EGlobalTests.cpp
File metadata and controls
477 lines (382 loc) · 19.3 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WSLCE2EGlobalTests.cpp
Abstract:
This file contains end-to-end tests for WSLC.
--*/
#include "precomp.h"
#include "windows/Common.h"
#include "WSLCCLITestHelpers.h"
#include "WSLCExecutor.h"
#include "WSLCE2EHelpers.h"
#include "Argument.h"
using namespace WEX::Logging;
namespace WSLCE2ETests {
using namespace wsl::shared;
class WSLCE2EGlobalTests
{
WSLC_TEST_CLASS(WSLCE2EGlobalTests)
wil::unique_couninitialize_call m_coinit = wil::CoInitializeEx();
TEST_CLASS_SETUP(TestClassSetup)
{
return true;
}
TEST_CLASS_CLEANUP(TestClassCleanup)
{
return true;
}
TEST_METHOD(WSLCE2E_HelpCommand)
{
WSL2_TEST_ONLY();
RunWslcAndVerify(L"--help", {.Stdout = GetHelpMessage(), .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_InvalidCommand_DisplaysErrorMessage)
{
WSL2_TEST_ONLY();
RunWslcAndVerify(L"INVALID_CMD", {.Stdout = GetHelpMessage(), .Stderr = L"Unrecognized command: 'INVALID_CMD'\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_VersionCommand)
{
WSL2_TEST_ONLY();
RunWslcAndVerify(L"version", {.Stdout = GetVersionMessage(), .Stderr = L"", .ExitCode = 0});
}
TEST_METHOD(WSLCE2E_Session_DefaultElevated)
{
WSL2_TEST_ONLY();
// Run container list to create the default elevated session
auto result = RunWslc(L"container list", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the admin session name
result = RunWslc(L"session list", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
}
TEST_METHOD(WSLCE2E_Session_DefaultNonElevated)
{
WSL2_TEST_ONLY();
// Run container list non-elevated to create the default non-elevated session
auto result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the non-admin session name
result = RunWslc(L"session list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
// The "\r\n" after session name is important to differentiate it from the admin session.
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
}
TEST_METHOD(WSLCE2E_Session_NonElevatedCannotAccessAdminSession)
{
WSL2_TEST_ONLY();
// First ensure admin session is created by running container list.
auto result = RunWslc(L"container list", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Try to explicitly target the admin session from non-elevated process
result = RunWslc(L"container list --session wslc-cli-admin", ElevationType::NonElevated);
// Should fail with access denied.
result.Verify({.Stderr = L"The requested operation requires elevation. \r\nError code: ERROR_ELEVATION_REQUIRED\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Session_ElevatedCanAccessNonElevatedSession)
{
WSL2_TEST_ONLY();
// First ensure non-elevated session is created by running container list.
auto result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Elevated user should be able to explicitly target the non-admin session
result = RunWslc(L"container list --session wslc-cli", ElevationType::Elevated);
// This should work - elevated users can access non-elevated sessions
result.Verify({.Stderr = L"", .ExitCode = S_OK});
}
TEST_METHOD(WSLCE2E_Session_CreateMixedElevation_Fails)
{
WSL2_TEST_ONLY();
EnsureSessionIsTerminated(L"wslc-cli");
EnsureSessionIsTerminated(L"wslc-cli-admin");
// Ensure elevated cannot create the non-elevated session.
auto result = RunWslc(L"container list --session wslc-cli", ElevationType::Elevated);
result.Verify({.Stderr = L"Element not found. \r\nError code: ERROR_NOT_FOUND\r\n", .ExitCode = 1});
// Ensure non-elevated cannot create the elevated session.
result = RunWslc(L"container list --session wslc-cli-admin", ElevationType::NonElevated);
result.Verify({.Stderr = L"Element not found. \r\nError code: ERROR_NOT_FOUND\r\n", .ExitCode = 1});
}
TEST_METHOD(WSLCE2E_Session_Terminate_Implicit)
{
WSL2_TEST_ONLY();
// Run container list to create the default session if it does not already exist
auto result = RunWslc(L"container list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the admin session name
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
// Terminate the session
result = RunWslc(L"session terminate");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session no longer shows up
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_FALSE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
// Repeat test for non-elevated session.
// Run container list to create the default session if it does not already exist
result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the non-elevated session name
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
// Terminate the session
result = RunWslc(L"session terminate", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session no longer shows up
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_FALSE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
}
TEST_METHOD(WSLCE2E_Session_Terminate_Explicit)
{
WSL2_TEST_ONLY();
// Run container list to create the default session if it does not already exist
auto result = RunWslc(L"container list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the admin session name
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
// Terminate the session
result = RunWslc(L"session terminate wslc-cli-admin");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session no longer shows up
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_FALSE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
// Repeat test for non-elevated session.
// Run container list to create the default session if it does not already exist
result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows the non-elevated session name
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
// Terminate the session
result = RunWslc(L"session terminate wslc-cli", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session no longer shows up
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_FALSE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
}
TEST_METHOD(WSLCE2E_Session_Terminate_MixedElevation)
{
WSL2_TEST_ONLY();
// Run container list to create the default sessions if they do not already exist.
auto result = RunWslc(L"container list", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify session list shows both sessions.
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli-admin") != std::wstring::npos);
VERIFY_IS_TRUE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
// Attempt to terminate the admin session from the non-elevated process and fail.
result = RunWslc(L"session terminate wslc-cli-admin", ElevationType::NonElevated);
result.Verify({.Stderr = L"The requested operation requires elevation. \r\nError code: ERROR_ELEVATION_REQUIRED\r\n", .ExitCode = 1});
// Terminate the non-elevated session from the elevated process.
result = RunWslc(L"session terminate wslc-cli", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify non-elevated session no longer shows up
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
VERIFY_IS_TRUE(result.Stdout.has_value());
VERIFY_IS_FALSE(result.Stdout->find(L"wslc-cli\r\n") != std::wstring::npos);
}
TEST_METHOD(WSLCE2E_Session_Targeting)
{
WSL2_TEST_ONLY();
// Generate a unique session name to avoid conflicts with previous runs or concurrent tests.
// Use only a short portion of the GUID to avoid MAX_PATH issues.
GUID sessionGuid;
VERIFY_SUCCEEDED(CoCreateGuid(&sessionGuid));
auto guidStr = wsl::shared::string::GuidToString<wchar_t>(sessionGuid, wsl::shared::string::GuidToStringFlags::None);
const auto sessionName = std::format(L"wslc-test-{}", guidStr.substr(0, 8));
auto session = TestSession::Create(sessionName);
// Load the Debian image into the test session to avoid hitting Docker Hub rate limits.
EnsureImageIsLoaded(DebianTestImage(), session.Name());
// Verify targeting a non-existent session fails.
auto result = RunWslc(L"container list --session INVALID_SESSION_NAME");
result.Verify({.Stdout = L"", .Stderr = L"Element not found. \r\nError code: ERROR_NOT_FOUND\r\n", .ExitCode = 1});
// Verify session list
result = RunWslc(L"session list");
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Verify there is a session with the name of the test session in the session list output.
VERIFY_IS_TRUE(result.Stdout.has_value());
auto findResult = result.Stdout->find(session.Name());
VERIFY_ARE_NOT_EQUAL(findResult, std::wstring::npos);
// Run container list in the test session, which should succeed if the session is valid.
result = RunWslc(std::format(L"container list --session {}", session.Name()));
result.Verify({.Stderr = L"", .ExitCode = S_OK});
// Add a container to the new session.
result = RunWslc(
std::format(L"container create --session {} --name {} {}", session.Name(), L"test-cont", DebianTestImage().NameAndTag()));
result.Dump(); // Dump so it is easier to find any potential issues with the pull in the test output.
result.Verify({.ExitCode = S_OK});
// Verify container exists in the custom session
VerifyContainerIsListed(L"test-cont", L"created", session.Name());
// Verify container does not exist in the default CLI session.
VerifyContainerIsNotListed(L"test-cont");
}
TEST_METHOD(WSLCE2E_Session_Shell)
{
WSL2_TEST_ONLY();
// Ensure sessions are created by running container list elevated and non-elevated.
auto result = RunWslc(L"container list", ElevationType::NonElevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
result = RunWslc(L"container list", ElevationType::Elevated);
result.Verify({.Stderr = L"", .ExitCode = S_OK});
{
Log::Comment(L"Testing elevated interactive session");
// Session shell should attach to the correct default session.
// Test should be elevated, therefore this should be the admin session.
auto session = RunWslcInteractive(L"session shell");
VERIFY_IS_TRUE(session.IsRunning(), L"Session should be running");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("echo hello");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("echo hello");
session.ExpectStdout("hello\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("whoami");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("whoami");
session.ExpectStdout("root\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.ExitAndVerifyNoErrors();
auto exitCode = session.Wait();
VERIFY_ARE_EQUAL(0, exitCode);
}
{
Log::Comment(L"Testing non-elevated interactive session with explicit session name");
// Non-Elevated session shell should attach to the wslc by name also.
auto session = RunWslcInteractive(L"session shell wslc-cli", ElevationType::NonElevated);
VERIFY_IS_TRUE(session.IsRunning(), L"Session should be running");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("echo hello");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("echo hello");
session.ExpectStdout("hello\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("whoami");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("whoami");
session.ExpectStdout("root\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.ExitAndVerifyNoErrors();
auto exitCode = session.Wait();
VERIFY_ARE_EQUAL(0, exitCode);
}
{
Log::Comment(L"Testing elevated interactive session with explicit admin session name");
// Elevated session shell should attach to the wslc by name also.
auto session = RunWslcInteractive(L"session shell wslc-cli-admin", ElevationType::Elevated);
VERIFY_IS_TRUE(session.IsRunning(), L"Session should be running");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("echo hello");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("echo hello");
session.ExpectStdout("hello\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.WriteLine("whoami");
session.ExpectStdout(VT::RESET);
session.ExpectCommandEcho("whoami");
session.ExpectStdout("root\r\n");
session.ExpectStdout(VT::SESSION_PROMPT);
session.ExitAndVerifyNoErrors();
auto exitCode = session.Wait();
VERIFY_ARE_EQUAL(0, exitCode);
}
}
private:
std::wstring GetHelpMessage() const
{
std::wstringstream output;
output << GetWslcHeader() //
<< GetDescription() //
<< GetUsage() //
<< GetAvailableCommands() //
<< GetAvailableOptions();
return output.str();
}
std::wstring GetVersionMessage() const
{
return std::format(L"wslc {}\r\n", WSL_PACKAGE_VERSION);
}
std::wstring GetDescription() const
{
return L"WSLC is the Windows Subsystem for Linux Container CLI tool. It enables management and interaction with WSL "
L"containers from the command line.\r\n\r\n";
}
std::wstring GetUsage() const
{
return L"Usage: wslc [<command>] [<options>]\r\n\r\n";
}
std::wstring GetAvailableCommands() const
{
std::vector<std::pair<std::wstring_view, std::wstring>> entries = {
{L"container", Localization::WSLCCLI_ContainerCommandDesc()},
{L"image", Localization::WSLCCLI_ImageCommandDesc()},
{L"session", Localization::WSLCCLI_SessionCommandDesc()},
{L"settings", Localization::WSLCCLI_SettingsCommandDesc()},
{L"attach", Localization::WSLCCLI_ContainerAttachDesc()},
{L"build", Localization::WSLCCLI_ImageBuildDesc()},
{L"create", Localization::WSLCCLI_ContainerCreateDesc()},
{L"exec", Localization::WSLCCLI_ContainerExecDesc()},
{L"images", Localization::WSLCCLI_ImageListDesc()},
{L"inspect", Localization::WSLCCLI_ContainerInspectDesc()},
{L"kill", Localization::WSLCCLI_ContainerKillDesc()},
{L"list", Localization::WSLCCLI_ContainerListDesc()},
{L"load", Localization::WSLCCLI_ImageLoadDesc()},
{L"logs", Localization::WSLCCLI_ContainerLogsDesc()},
{L"pull", Localization::WSLCCLI_ImagePullDesc()},
{L"remove", Localization::WSLCCLI_ContainerRemoveDesc()},
{L"rmi", Localization::WSLCCLI_ImageRemoveDesc()},
{L"run", Localization::WSLCCLI_ContainerRunDesc()},
{L"save", Localization::WSLCCLI_ImageSaveDesc()},
{L"start", Localization::WSLCCLI_ContainerStartDesc()},
{L"stop", Localization::WSLCCLI_ContainerStopDesc()},
{L"version", Localization::WSLCCLI_VersionDesc()},
};
size_t maxLen = 0;
for (const auto& [name, _] : entries)
{
maxLen = (std::max)(maxLen, name.size());
}
std::wstringstream commands;
commands << Localization::WSLCCLI_AvailableCommands() << L"\r\n";
for (const auto& [name, desc] : entries)
{
commands << L" " << name << std::wstring(maxLen - name.size() + 2, L' ') << desc << L"\r\n";
}
commands << L"\r\n" << Localization::WSLCCLI_HelpForDetails() << L" [" << WSLC_CLI_HELP_ARG_STRING << L"]\r\n\r\n";
return commands.str();
}
std::wstring GetAvailableOptions() const
{
std::wstringstream options;
options << L"The following options are available:\r\n"
<< L" -v,--version Show version information for this tool\r\n"
<< L" -h,--help Shows help about the selected command\r\n"
<< L"\r\n";
return options.str();
}
};
} // namespace WSLCE2ETests