-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWSLCE2EContainerRunTests.cpp
More file actions
906 lines (742 loc) · 40 KB
/
Copy pathWSLCE2EContainerRunTests.cpp
File metadata and controls
906 lines (742 loc) · 40 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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/*++
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);
EnsureVolumeDoesNotExist(WslcVolumeName);
EnsureNetworkDoesNotExist(TestNetworkName);
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);
EnsureVolumeDoesNotExist(WslcVolumeName);
EnsureNetworkDoesNotExist(TestNetworkName);
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_CIDFile_Valid)
{
// 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()));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_CIDFile_AlreadyExists)
{
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_FILE_EXISTS\r\n", EscapePath(cidFilePath.wstring())),
.ExitCode = 1});
VerifyContainerIsNotListed(WslcContainerName);
}
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)
{
// Start a container with an ephemeral host port mapping (-p 8080 means host picks a random port)
auto result = RunWslc(std::format(
L"container run -d --name {} -p {} {} {}", WslcContainerName, ContainerTestPort, PythonImage.NameAndTag(), GetPythonHttpServerScript(ContainerTestPort)));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Inspect the container to find the allocated host port
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());
auto hostPort = std::stoi(portBindings[0].HostPort);
VERIFY_IS_TRUE(hostPort > 0);
// Verify we can connect to the server on the ephemeral port
ExpectHttpResponse(std::format(L"http://127.0.0.1:{}", hostPort).c_str(), HTTP_STATUS_OK, true);
}
// 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 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 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});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_WorkDir)
{
auto result = RunWslc(std::format(L"container run --rm --workdir /tmp {} pwd", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"/tmp\n", .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Hostname)
{
auto result = RunWslc(std::format(L"container run --rm --hostname my-test-host {} hostname", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"my-test-host\n", .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Domainname)
{
auto result = RunWslc(std::format(L"container run --rm --domainname my-test-domain {} dnsdomainname", DebianImage.NameAndTag()));
result.Verify({.Stdout = L"my-test-domain\n", .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_DNS)
{
auto result =
RunWslc(std::format(L"container run --rm --dns 1.1.1.1 --dns 8.8.8.8 {} cat /etc/resolv.conf", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"nameserver 1.1.1.1") != std::wstring::npos);
VERIFY_IS_TRUE(result.Stdout->find(L"nameserver 8.8.8.8") != std::wstring::npos);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_DNSSearch)
{
auto result = RunWslc(std::format(
L"container run --rm --dns-search example.com --dns-search test.local {} cat /etc/resolv.conf", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"search example.com test.local") != std::wstring::npos);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_DNSOption)
{
auto result = RunWslc(std::format(
L"container run --rm --dns-option ndots:5 --dns-option timeout:3 {} cat /etc/resolv.conf", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"options ndots:5 timeout:3") != std::wstring::npos);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Network_DefaultIsBridge)
{
auto result = RunWslc(std::format(L"container run --name {} {} true", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
const auto inspect = InspectContainer(WslcContainerName);
VERIFY_ARE_EQUAL(std::string("bridge"), inspect.HostConfig.NetworkMode);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Network_HostMode)
{
auto result =
RunWslc(std::format(L"container run --name {} --network host {} true", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
const auto inspect = InspectContainer(WslcContainerName);
VERIFY_ARE_EQUAL(std::string("host"), inspect.HostConfig.NetworkMode);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Network_UserDefinedNetwork)
{
auto result = RunWslc(std::format(L"network create --driver bridge {}", TestNetworkName));
result.Verify({.Stderr = L"", .ExitCode = 0});
auto cleanupNetwork = wil::scope_exit([&] { EnsureNetworkDoesNotExist(TestNetworkName); });
result = RunWslc(std::format(
L"container run --name {} --network {} {} true", WslcContainerName, TestNetworkName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
const auto inspect = InspectContainer(WslcContainerName);
VERIFY_ARE_EQUAL(wsl::shared::string::WideToMultiByte(TestNetworkName), inspect.HostConfig.NetworkMode);
VERIFY_IS_TRUE(inspect.NetworkSettings.Networks.contains(wsl::shared::string::WideToMultiByte(TestNetworkName)));
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Network_EmptyValue_Rejected)
{
auto result =
RunWslc(std::format(L"container run --rm --network \"\" --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid network value: network name cannot be empty or whitespace\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Network_NonexistentNetwork_Rejected)
{
auto result = RunWslc(std::format(
L"container run --rm --network does-not-exist --name {} {} true", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Network not found: 'does-not-exist'\r\nError code: WSLC_E_NETWORK_NOT_FOUND\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Volume_NamedVolume_Success)
{
// Create a named volume
auto result = RunWslc(std::format(L"volume create {}", WslcVolumeName));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Create a container with --rm that uses the named volume and writes a file to it
result = RunWslc(std::format(
L"container run --rm --volume {}:/data {} sh -c \"echo -n 'WSLC Named Volume Test' > /data/test.txt\"",
WslcVolumeName,
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Create another container that mounts the same named volume and verify the file content
result = RunWslc(std::format(L"container run --rm --volume {}:/data {} cat /data/test.txt", WslcVolumeName, DebianImage.NameAndTag()));
result.Verify({.Stdout = L"WSLC Named Volume Test", .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_Volume_NamedVolume_AutoCreate)
{
auto result = RunWslc(std::format(
L"container run --rm --volume {}:/data {} sh -c \"echo -n 'WSLC Named Volume Test' > /data/test.txt\"",
WslcVolumeName,
DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
// Verify the volume was auto-created by removing it (fails if it doesn't exist).
result = RunWslc(std::format(L"volume rm {}", WslcVolumeName));
result.Verify({.Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_WithLabel_Success)
{
auto result = RunWslc(std::format(
L"container run --name {} --label A=1 --label B=2 {} echo hello", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stdout = L"hello\n", .Stderr = L"", .ExitCode = 0});
auto inspect = InspectContainer(WslcContainerName);
VERIFY_ARE_EQUAL("1", inspect.Labels["A"]);
VERIFY_ARE_EQUAL("2", inspect.Labels["B"]);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_StopSignal)
{
constexpr int ExpectedExitCode = 42;
auto result = RunWslc(std::format(
LR"(container run -d --stop-signal SIGUSR1 --name {} {} bash -c "trap 'exit {}' SIGUSR1; while true; do sleep 1; done")",
WslcContainerName,
DebianImage.NameAndTag(),
ExpectedExitCode));
result.Verify({.Stderr = L"", .ExitCode = 0});
result = RunWslc(std::format(L"container stop {}", WslcContainerName));
result.Verify({.Stderr = L"", .ExitCode = 0});
const auto inspect = InspectContainer(WslcContainerName);
VERIFY_IS_FALSE(inspect.State.Running);
VERIFY_ARE_EQUAL(ExpectedExitCode, inspect.State.ExitCode);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_ShmSize)
{
auto result = RunWslc(std::format(L"container run --rm --shm-size 128M {} df -h /dev/shm", DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
VERIFY_IS_TRUE(result.Stdout->find(L"128M") != std::wstring::npos);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_ShmSize_Invalid)
{
{
auto result =
RunWslc(std::format(L"container run --rm --shm-size invalid --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid shm-size argument value: 'invalid'. Expected a memory size (e.g. 256M, 1G)\r\n", .ExitCode = 1});
EnsureContainerDoesNotExist(WslcContainerName);
}
{
auto result =
RunWslc(std::format(L"container run --rm --shm-size 128X --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid shm-size argument value: '128X'. Expected a memory size (e.g. 256M, 1G)\r\n", .ExitCode = 1});
EnsureContainerDoesNotExist(WslcContainerName);
}
}
WSLC_TEST_METHOD(WSLCE2E_Container_Run_StopSignal_Invalid)
{
{
auto result = RunWslc(
std::format(L"container run --rm --stop-signal SIGINVALID --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid stop-signal value: SIGINVALID is not a recognized signal name or number (Example: SIGKILL, kill, or 9).\r\n", .ExitCode = 1});
EnsureContainerDoesNotExist(WslcContainerName);
}
{
auto result =
RunWslc(std::format(L"container run --rm --stop-signal 0 --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid stop-signal value: 0 is out of valid range (1-31).\r\n", .ExitCode = 1});
EnsureContainerDoesNotExist(WslcContainerName);
}
{
auto result =
RunWslc(std::format(L"container run --rm --stop-signal 99 --name {} {}", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"Invalid stop-signal value: 99 is out of valid range (1-31).\r\n", .ExitCode = 1});
EnsureContainerDoesNotExist(WslcContainerName);
}
}
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;
// Test named volume
const std::wstring WslcVolumeName = L"wslc-test-volume";
// Test user-defined network
const std::wstring TestNetworkName = L"wslc-test-network";
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" --cidfile Write the container ID to the provided path\r\n"
<< L" -d,--detach Run container in detached mode\r\n"
<< L" --dns IP address of the DNS nameserver in resolv.conf\r\n"
<< L" --dns-option Set DNS options\r\n"
<< L" --dns-search Set DNS search domains\r\n"
<< L" --domainname Container domain name\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" --gpus Add GPU devices to the container ('all' to pass all GPUs)\r\n"
<< L" -h,--hostname Container host name\r\n"
<< L" -i,--interactive Attach to stdin and keep it open\r\n"
<< L" -l,--label Set metadata on an object\r\n"
<< L" --name Name of the container\r\n"
<< L" --network Connect a container to a network\r\n"
<< L" -p,--publish Publish a port from a container to host\r\n"
<< L" -P,--publish-all Publish all exposed ports to random host ports\r\n"
<< L" --rm Remove the container after it stops\r\n"
<< L" --session Specify the session to use\r\n"
<< L" --shm-size Size of /dev/shm (e.g. 64M, 1G)\r\n"
<< L" --stop-signal Signal to stop the container\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" -w,--workdir Working directory inside the container\r\n"
<< L" -?,--help Shows help about the selected command\r\n"
<< L"\r\n";
return options.str();
}
};
} // namespace WSLCE2ETests