-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathWSLCE2EContainerAttachTests.cpp
More file actions
165 lines (132 loc) · 5.25 KB
/
WSLCE2EContainerAttachTests.cpp
File metadata and controls
165 lines (132 loc) · 5.25 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
WSLCE2EContainerAttachTests.cpp
Abstract:
This file contains end-to-end tests for WSLC container attach command.
--*/
#include "precomp.h"
#include "windows/Common.h"
#include "WSLCExecutor.h"
#include "WSLCE2EHelpers.h"
namespace WSLCE2ETests {
class WSLCE2EContainerAttachTests
{
WSLC_TEST_CLASS(WSLCE2EContainerAttachTests)
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;
}
WSLC_TEST_METHOD(WSLCE2E_Container_Attach_HelpCommand)
{
auto result = RunWslc(L"container attach --help");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"", .ExitCode = 0});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Attach_TTY)
{
VerifyContainerIsNotListed(WslcContainerName);
const auto& prompt = ">";
auto result = RunWslc(std::format(
L"container run -itd -e PS1={} --name {} {} bash --norc", prompt, WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
auto containerId = result.GetStdoutOneLine();
const auto& expectedAttachPrompt = VT::BuildContainerAttachPrompt(prompt);
const auto& expectedPrompt = VT::BuildContainerPrompt(prompt);
auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
VERIFY_IS_TRUE(session.IsRunning(), L"Container session should be running");
// The container attach prompt appears twice.
session.ExpectStdout(expectedAttachPrompt);
session.ExpectStdout(expectedAttachPrompt);
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);
session.ExitAndVerifyNoErrors();
auto exitCode = session.Wait();
VERIFY_ARE_EQUAL(0, exitCode);
}
WSLC_TEST_METHOD(WSLCE2E_Container_Attach_NoTTY)
{
VerifyContainerIsNotListed(WslcContainerName);
auto result = RunWslc(std::format(L"container run -id --name {} {} cat", WslcContainerName, DebianImage.NameAndTag()));
result.Verify({.Stderr = L"", .ExitCode = 0});
auto containerId = result.GetStdoutOneLine();
auto session = RunWslcInteractive(std::format(L"container attach {}", containerId));
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_Attach_MissingContainerId)
{
auto result = RunWslc(L"container attach");
result.Verify({.Stdout = GetHelpMessage(), .Stderr = L"Required argument not provided: 'container-id'\r\n", .ExitCode = 1});
}
WSLC_TEST_METHOD(WSLCE2E_Container_Attach_ContainerNotFound)
{
auto result = RunWslc(std::format(L"container attach {}", WslcContainerName));
result.Verify({.Stderr = L"Element not found. \r\nError code: ERROR_NOT_FOUND\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"Attaches to a container.\r\n\r\n";
}
std::wstring GetUsage() const
{
return L"Usage: wslc container attach [<options>] <container-id>\r\n\r\n";
}
std::wstring GetAvailableCommands() const
{
std::wstringstream commands;
commands << L"The following arguments are available:\r\n" //
<< L" container-id Container ID\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" --session Specify the session to use\r\n" //
<< L" -h,--help Shows help about the selected command\r\n"
<< L"\r\n";
return options.str();
}
};
} // namespace WSLCE2ETests