master
cpp 128 lines 4.19 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 WSLCE2EContainerAttachTests.cpp
8
9 Abstract:
10
11 This file contains end-to-end tests for WSLC container attach command.
12 --*/
13
14 #include "precomp.h"
15 #include "windows/Common.h"
16 #include "WSLCExecutor.h"
17 #include "WSLCE2EHelpers.h"
18 #include "TestImageRegistry.h"
19
20 namespace WSLCE2ETests {
21
22 class WSLCE2EContainerAttachTests
23 {
24 WSLC_TEST_CLASS(WSLCE2EContainerAttachTests)
25
26 TEST_CLASS_SETUP(ClassSetup)
27 {
28 TestImageRegistry::Instance().EnsureLoaded(DebianImage);
29 return true;
30 }
31
32 TEST_CLASS_CLEANUP(ClassCleanup)
33 {
34 EnsureContainerDoesNotExist(WslcContainerName);
35 return true;
36 }
37
38 TEST_METHOD_SETUP(TestMethodSetup)
39 {
40 EnsureContainerDoesNotExist(WslcContainerName);
41 return true;
42 }
43
44 WSLC_TEST_METHOD(WSLCE2E_Container_Attach_HelpCommand)
45 {
46 auto result = RunWslc(L"container attach --help");
47 result.Verify({.Stderr = L"", .ExitCode = 0});
48 VERIFY_IS_FALSE(result.Stdout.value().empty());
49 }
50
51 WSLC_TEST_METHOD(WSLCE2E_Container_Attach_TTY)
52 {
53 VerifyContainerIsNotListed(WslcContainerName);
54
55 const auto& prompt = ">";
56 auto result = RunWslc(std::format(
57 L"container run -itd -e PS1={} --name {} {} bash --norc", prompt, WslcContainerName, DebianImage.NameAndTag()));
58 result.Verify({.Stderr = L"", .ExitCode = 0});
59 auto containerId = result.GetStdoutOneLine();
60
61 const auto& expectedPrompt = VT::BuildContainerPrompt(prompt);
62
63 auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
64 VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");
65
66 // Ignore resize-repaint messages. Those are emitted when the the tty initial size is set, which can happen before or after we start running commands.
67 session.IgnoreSequence(VT::BuildContainerAttachPrompt(prompt));
68
69 session.WriteLine("echo hello");
70 session.ExpectCommandEcho("echo hello");
71 session.ExpectStdout("hello\r\n");
72 session.ExpectStdout(expectedPrompt);
73
74 session.WriteLine("whoami");
75 session.ExpectCommandEcho("whoami");
76 session.ExpectStdout("root\r\n");
77 session.ExpectStdout(expectedPrompt);
78
79 session.ExitAndVerifyNoErrors();
80 auto exitCode = session.Wait();
81 VERIFY_ARE_EQUAL(0, exitCode);
82 }
83
84 WSLC_TEST_METHOD(WSLCE2E_Container_Attach_NoTTY)
85 {
86 VerifyContainerIsNotListed(WslcContainerName);
87 auto result = RunWslc(std::format(L"container run -id --name {} {} cat", WslcContainerName, DebianImage.NameAndTag()));
88 result.Verify({.Stderr = L"", .ExitCode = 0});
89 auto containerId = result.GetStdoutOneLine();
90
91 auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
92 VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");
93
94 session.WriteLine("test line 1");
95 session.ExpectStdout("test line 1\n");
96 session.WriteLine("test line 2");
97 session.ExpectStdout("test line 2\n");
98
99 // Close stdin to signal EOF to cat
100 session.CloseStdin();
101
102 // Wait for cat to exit with code 0
103 auto exitCode = session.Wait(10000);
104 VERIFY_ARE_EQUAL(0, exitCode, L"Cat should exit with code 0 after receiving EOF");
105 session.VerifyNoErrors();
106 }
107
108 WSLC_TEST_METHOD(WSLCE2E_Container_Attach_MissingContainerId)
109 {
110 auto result = RunWslc(L"container attach");
111 result.Verify({.Stdout = L"", .ExitCode = 1});
112 VERIFY_IS_TRUE(result.StderrContainsSubstring(L"Required argument not provided: 'container-id'"));
113 }
114
115 WSLC_TEST_METHOD(WSLCE2E_Container_Attach_ContainerNotFound)
116 {
117 auto result = RunWslc(std::format(L"container attach {}", WslcContainerName));
118 result.Verify(
119 {.Stderr =
120 FormatErrorMessage(std::format(L"Container '{}' not found.", WslcContainerName), L"WSLC_E_CONTAINER_NOT_FOUND"),
121 .ExitCode = 1});
122 }
123
124 private:
125 const std::wstring WslcContainerName = L"wslc-test-container";
126 const TestImage& DebianImage = DebianTestImage();
127 };
128 } // namespace WSLCE2ETests