forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWSLCE2EContainerRunTests.cpp
More file actions
635 lines (514 loc) · 26.9 KB
/
WSLCE2EContainerRunTests.cpp
File metadata and controls
635 lines (514 loc) · 26.9 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*++
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);
EnsureImageIsLoaded(PythonImage);
VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName.c_str(), HostEnvVariableValue.c_str()));
VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName2.c_str(), HostEnvVariableValue2.c_str()));
// Initialize Winsock for loopback connectivity tests
WSADATA wsaData{};
const int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
THROW_HR_IF(HRESULT_FROM_WIN32(result), result != 0);
return true;
}
TEST_CLASS_CLEANUP(ClassCleanup)
{
EnsureContainerDoesNotExist(WslcContainerName);
EnsureContainerDoesNotExist(WslcContainerName2);
EnsureImageIsDeleted(DebianImage);
EnsureImageIsDeleted(PythonImage);
VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName.c_str(), nullptr));
VERIFY_IS_TRUE(::SetEnvironmentVariableW(HostEnvVariableName2.c_str(), nullptr));
// Cleanup Winsock
WSACleanup();
return true;
}
TEST_METHOD_SETUP(TestMethodSetup)
{
EnsureContainerDoesNotExist(WslcContainerName);
EnsureContainerDoesNotExist(WslcContainerName2);
EnvTestFile1 = wsl::windows::common::filesystem::GetTempFilename();
EnvTestFile2 = wsl::windows::common::filesystem::GetTempFilename();
return true;
}
TEST_METHOD_CLEANUP(TestMethodCleanup)
{
DeleteFileW(EnvTestFile1.c_str());
DeleteFileW(EnvTestFile2.c_str());
return true;
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_HelpCommand)
{
auto result = RunWslc(L"container run --help");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Container_With_Command)
{
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");
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Entrypoint)
{
auto result = RunWslc(std::format(L"container run --rm --entrypoint /bin/whoami {}", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"root\n", .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_And_Arguments)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_Invalid_Path)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Entrypoint_Detach_Lifecycle)
{
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");
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Remove)
{
VerifyContainerIsNotListed(WslcContainerName);
// Run the container with a valid image
auto result = RunWslc(std::format(L"container run --rm --name {} {} echo hello", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Run should be deleted on return so no retry.
VerifyContainerIsNotListed(WslcContainerName);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} -e {}=A {} env", WslcContainerName, HostEnvVariableName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}=A", HostEnvVariableName)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption_MultipleValues)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} -e {}=A -e {}=B {} env",
WslcContainerName,
HostEnvVariableName,
HostEnvVariableName2,
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}=A", HostEnvVariableName)));
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}=B", HostEnvVariableName2)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption_KeyOnly_UsesHostValue)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} -e {} {} env", WslcContainerName, HostEnvVariableName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}={}", HostEnvVariableName, HostEnvVariableValue)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption_KeyOnly_MultipleValues_UsesHostValues)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} -e {} -e {} {} env",
WslcContainerName,
HostEnvVariableName,
HostEnvVariableName2,
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}={}", HostEnvVariableName, HostEnvVariableValue)));
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}={}", HostEnvVariableName2, HostEnvVariableValue2)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption_EmptyValue)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} -e {}= {} env", WslcContainerName, HostEnvVariableName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(std::format(L"{}=", HostEnvVariableName)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_FILE_A=env-file-a", "WSLC_TEST_ENV_FILE_B=env-file-b"});
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_A=env-file-a"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_B=env-file-b"));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvOption_MixedWithEnvFile)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_MIX_FILE_A=from-file-a", "WSLC_TEST_ENV_MIX_FILE_B=from-file-b"});
auto result = RunWslc(std::format(
L"container run --rm --name {} -e WSLC_TEST_ENV_MIX_CLI=from-cli --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_MIX_FILE_A=from-file-a"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_MIX_FILE_B=from-file-b"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_MIX_CLI=from-cli"));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile_MultipleFiles)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_FILE_MULTI_A=file1-a", "WSLC_TEST_ENV_FILE_MULTI_B=file1-b"});
WriteTestFile(EnvTestFile2, {"WSLC_TEST_ENV_FILE_MULTI_C=file2-c", "WSLC_TEST_ENV_FILE_MULTI_D=file2-d"});
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
EscapePath(EnvTestFile2.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_MULTI_A=file1-a"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_MULTI_B=file1-b"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_MULTI_C=file2-c"));
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_FILE_MULTI_D=file2-d"));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile_MissingFile)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file ENV_FILE_NOT_FOUND {} env", WslcContainerName, DebianImage.NameAndTag()));
result.Verify(
{.Stderr = L"Environment file 'ENV_FILE_NOT_FOUND' cannot be opened for reading\r\nError code: E_INVALIDARG\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile_InvalidContent)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_VALID=ok", "BAD KEY=value"});
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Environment variable key 'BAD KEY' cannot contain whitespace\r\nError code: E_INVALIDARG\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile_DuplicateKeys_Precedence)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_DUP=from-file-1"});
WriteTestFile(EnvTestFile2, {"WSLC_TEST_ENV_DUP=from-file-2"});
// Later --env-file should win over earlier --env-file for duplicate keys
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
EscapePath(EnvTestFile2.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_DUP=from-file-2"));
// Explicit -e should win over env-file value for duplicate keys
result = RunWslc(std::format(
L"container run --rm --name {} -e WSLC_TEST_ENV_DUP=from-cli --env-file {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
EscapePath(EnvTestFile2.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_DUP=from-cli"));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_EnvFile_ValueContainsEquals)
{
VerifyContainerIsNotListed(WslcContainerName);
WriteTestFile(EnvTestFile1, {"WSLC_TEST_ENV_EQUALS=value=with=equals"});
auto result = RunWslc(std::format(
L"container run --rm --name {} --env-file {} {} env",
WslcContainerName,
EscapePath(EnvTestFile1.wstring()),
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.StdoutContainsLine(L"WSLC_TEST_ENV_EQUALS=value=with=equals"));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_NameRoot)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_UidRoot)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_UidGidRoot)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_UnknownUser_Fails)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_UnknownGroup_Fails)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_NameGroupRoot)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_UserOption_NonRootUser_Succeeds)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_PortMultipleMappings)
{
// Start a container with a simple server listening on a port
// Map two host ports to the same container port
auto result = RunWslc(std::format(
L"container run -d --name {} -p {}:{} -p {}:{} {} {}",
WslcContainerName,
HostTestPort1,
ContainerTestPort,
HostTestPort2,
ContainerTestPort,
PythonImage.NameAndTag(),
GetPythonHttpServerScript(ContainerTestPort)));
result.Verify({.Stderr = L"", .ExitCode = 0});
// From the host side, verify we can connect to both ports
ExpectHttpResponse(std::format(L"http://127.0.0.1:{}", HostTestPort1).c_str(), HTTP_STATUS_OK, true);
ExpectHttpResponse(std::format(L"http://127.0.0.1:{}", HostTestPort2).c_str(), HTTP_STATUS_OK, true);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_PortAlreadyInUse)
{
// Start a container with a simple server listening on a port
auto result1 = RunWslc(std::format(
L"container run -d --name {} -p {}:{} {} {}",
WslcContainerName,
HostTestPort1,
ContainerTestPort,
PythonImage.NameAndTag(),
GetPythonHttpServerScript(ContainerTestPort)));
result1.Verify({.Stderr = L"", .ExitCode = 0});
// Create a second container mapping the same host port to validate the full error message
auto createResult =
RunWslc(std::format(L"container create -p {}:{} {}", HostTestPort1, ContainerTestPort, DebianImage.NameAndTag()));
createResult.Verify({.Stderr = L"", .ExitCode = 0});
auto containerId = createResult.GetStdoutOneLine();
// Attempt to start — should fail with port conflict
auto startResult = RunWslc(std::format(L"container start {}", containerId));
startResult.Verify(
{.Stderr = std::format(
L"Port 127.0.0.1:{}/tcp is already in use, cannot start container {}\r\nError code: ERROR_ALREADY_EXISTS\r\n", HostTestPort1, containerId),
.ExitCode = 1});
// Clean up the created container
RunWslc(std::format(L"container rm {}", containerId)).Verify({.Stderr = L"", .ExitCode = 0});
// Verify 'container run' auto-cleans up on port conflict (no ghost container)
auto runResult = RunWslc(std::format(
L"container run --name {} -p {}:{} {}", WslcContainerName2, HostTestPort1, ContainerTestPort, DebianImage.NameAndTag()));
runResult.Verify({.ExitCode = 1});
VerifyContainerIsNotListed(WslcContainerName2);
}
// https://github.com/microsoft/WSL/issues/14433
WSLC_TEST_METHOD(WSLCE2E_Container_Run_PortEphemeral_NotSupported)
{
auto result = RunWslc(std::format(L"container run -p 80 {}", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Port mappings with ephemeral host ports, specific host IPs, or UDP protocol are not currently supported\r\nError code: ERROR_NOT_SUPPORTED\r\n", .ExitCode = 1});
}
// https://github.com/microsoft/WSL/issues/14433
WSLC_TEST_METHOD(WSLCE2E_Container_Run_PortUdp_NotSupported)
{
auto result = RunWslc(std::format(L"container run -p 80:80/udp {}", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Port mappings with ephemeral host ports, specific host IPs, or UDP protocol are not currently supported\r\nError code: ERROR_NOT_SUPPORTED\r\n", .ExitCode = 1});
}
// https://github.com/microsoft/WSL/issues/14433
WSLC_TEST_METHOD(WSLCE2E_Container_Run_PortHostIP_NotSupported)
{
auto result = RunWslc(std::format(L"container run -p 127.0.0.1:80:80 {}", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Port mappings with ephemeral host ports, specific host IPs, or UDP protocol are not currently supported\r\nError code: ERROR_NOT_SUPPORTED\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Port_TCP)
{
// Start a container with a simple server listening on a port
auto result = RunWslc(std::format(
L"container run -d --name {} -p {}:{} {} {}",
WslcContainerName,
HostTestPort1,
ContainerTestPort,
PythonImage.NameAndTag(),
GetPythonHttpServerScript(ContainerTestPort)));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Verify we can connect to the server from the host side
ExpectHttpResponse(std::format(L"http://127.0.0.1:{}", HostTestPort1).c_str(), HTTP_STATUS_OK, true);
// Verify the port mapping is correct in the container inspect data
auto inspectContainer = InspectContainer(WslcContainerName);
auto portKey = std::to_string(ContainerTestPort) + "/tcp";
VERIFY_IS_TRUE(inspectContainer.Ports.contains(portKey));
auto portBindings = inspectContainer.Ports[portKey];
VERIFY_ARE_EQUAL(1u, portBindings.size());
VERIFY_ARE_EQUAL(std::to_string(HostTestPort1), portBindings[0].HostPort);
VERIFY_ARE_EQUAL("127.0.0.1", portBindings[0].HostIp);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Interactive_TTY)
{
VerifyContainerIsNotListed(WslcContainerName);
const auto& prompt = ">";
auto session = RunWslcInteractive(
std::format(L"container run -it -e PS1={} --name {} {} bash --norc", prompt, WslcContainerName, DebianImage.NameAndTag()));
VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");
const auto& expectedPrompt = VT::BuildContainerPrompt(prompt);
session.ExpectStdout(expectedPrompt);
session.WriteLine("echo hello");
session.ExpectCommandEcho("echo hello");
session.ExpectStdout("hello\r\n");
session.ExpectStdout(expectedPrompt);
session.WriteLine("whoami");
session.ExpectCommandEcho("whoami");
session.ExpectStdout("root\r\n");
session.ExpectStdout(expectedPrompt);
auto exitCode = session.ExitAndVerifyNoErrors();
VERIFY_ARE_EQUAL(0, exitCode);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Interactive_NoTTY)
{
VerifyContainerIsNotListed(WslcContainerName);
auto session = RunWslcInteractive(std::format(L"container run -i --name {} {} cat", WslcContainerName, DebianImage.NameAndTag()));
VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");
session.WriteLine("test line 1");
session.ExpectStdout("test line 1\n");
session.WriteLine("test line 2");
session.ExpectStdout("test line 2\n");
// Close stdin to signal EOF to cat
session.CloseStdin();
// Wait for cat to exit with code 0
auto exitCode = session.Wait(10000);
VERIFY_ARE_EQUAL(0, exitCode, L"Cat should exit with code 0 after receiving EOF");
session.VerifyNoErrors();
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Tmpfs)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_With_Options)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_Multiple_With_Options)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_RelativePath_Fails)
{
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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Tmpfs_EmptyDestination_Fails)
{
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:
// Test container name
const std::wstring WslcContainerName = L"wslc-test-container";
const std::wstring WslcContainerName2 = L"wslc-test-container-2";
// Test environment variables
const std::wstring HostEnvVariableName = L"WSLC_TEST_HOST_ENV";
const std::wstring HostEnvVariableName2 = L"WSLC_TEST_HOST_ENV2";
const std::wstring HostEnvVariableValue = L"wslc-host-env-value";
const std::wstring HostEnvVariableValue2 = L"wslc-host-env-value2";
// Test images
const TestImage& DebianImage = DebianTestImage();
const TestImage& PythonImage = PythonTestImage();
// Test environment variable files
std::filesystem::path EnvTestFile1;
std::filesystem::path EnvTestFile2;
// Test ports
const uint16_t ContainerTestPort = 8080;
const uint16_t HostTestPort1 = 1234;
const uint16_t HostTestPort2 = 1235;
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 foreground; use --detach to run in the "
L"background.\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" -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