Skip to content

Commit f2281c3

Browse files
committed
Add ipv6 outbound connectivity test, small test refactor
1 parent dc80254 commit f2281c3

4 files changed

Lines changed: 111 additions & 34 deletions

File tree

‎test/windows/wslc/e2e/WSLCE2EHelpers.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Module Name:
1919
#include "WSLCExecutor.h"
2020
#include "WSLCE2EHelpers.h"
2121
#include <JsonUtils.h>
22+
#include <WSLCProcessLauncher.h>
2223
#include <wslutil.h>
2324

2425
extern std::wstring g_testDataPath;
@@ -557,6 +558,33 @@ wil::com_ptr<IWSLCSession> OpenDefaultElevatedSession()
557558
return std::move(session);
558559
}
559560

561+
std::string RunDockerInSession(IWSLCSession& session, std::vector<std::string>&& args)
562+
{
563+
wsl::windows::common::WSLCProcessLauncher launcher("/usr/bin/docker", args);
564+
auto result = launcher.Launch(session).WaitAndCaptureOutput();
565+
VERIFY_ARE_EQUAL(0, result.Code);
566+
567+
auto output = result.Output[1];
568+
output.erase(output.find_last_not_of(" \n\r") + 1);
569+
return output;
570+
}
571+
572+
void RemoveDockerContainerNoThrow(const std::string& containerId)
573+
try
574+
{
575+
wil::com_ptr<IWSLCSessionManager> sessionManager;
576+
THROW_IF_FAILED(CoCreateInstance(__uuidof(WSLCSessionManager), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&sessionManager)));
577+
wsl::windows::common::security::ConfigureForCOMImpersonation(sessionManager.get());
578+
579+
wil::com_ptr<IWSLCSession> session;
580+
THROW_IF_FAILED(sessionManager->OpenSessionByName(nullptr, &session));
581+
wsl::windows::common::security::ConfigureForCOMImpersonation(session.get());
582+
583+
wsl::windows::common::WSLCProcessLauncher launcher("/usr/bin/docker", {"/usr/bin/docker", "rm", "-f", containerId});
584+
launcher.Launch(*session).WaitAndCaptureOutput();
585+
}
586+
CATCH_LOG()
587+
560588
std::pair<RunningWSLCContainer, std::string> StartLocalRegistry(
561589
IWSLCSession& session, const std::string& username, const std::string& password, USHORT port, const std::wstring& tlsCertDir)
562590
{

‎test/windows/wslc/e2e/WSLCE2EHelpers.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ inline void VerifyContainerIsNotListed(const std::wstring& containerNameOrId)
207207

208208
wil::com_ptr<IWSLCSession> OpenDefaultElevatedSession();
209209

210+
std::string RunDockerInSession(IWSLCSession& session, std::vector<std::string>&& args);
211+
void RemoveDockerContainerNoThrow(const std::string& containerId);
212+
210213
void VerifyPseudoConsoleTtySize(WSLCInteractiveSession& session, SHORT columns, SHORT rows);
211214

212215
// Waits for a substring to appear in the session's pseudo console output.

‎test/windows/wslc/e2e/WSLCE2ENetworkCreateTests.cpp‎

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@ class WSLCE2ENetworkCreateTests
2323
{
2424
WSLC_TEST_CLASS(WSLCE2ENetworkCreateTests)
2525

26+
TEST_CLASS_SETUP(ClassSetup)
27+
{
28+
THROW_IF_WIN32_ERROR(WSAStartup(MAKEWORD(2, 2), &m_wsaData));
29+
EnsureImageIsLoaded(PythonImage);
30+
return true;
31+
}
32+
2633
TEST_METHOD_SETUP(MethodSetup)
2734
{
35+
EnsureContainerDoesNotExist(ClientContainerName);
36+
RemoveDockerContainerNoThrow(wsl::shared::string::WideToMultiByte(ServerContainerName));
2837
EnsureNetworkDoesNotExist(TestNetworkName);
2938
return true;
3039
}
3140

3241
TEST_CLASS_CLEANUP(ClassCleanup)
3342
{
43+
EnsureContainerDoesNotExist(ClientContainerName);
44+
RemoveDockerContainerNoThrow(wsl::shared::string::WideToMultiByte(ServerContainerName));
3445
EnsureNetworkDoesNotExist(TestNetworkName);
46+
EnsureImageIsDeleted(PythonImage);
47+
WSACleanup();
3548
return true;
3649
}
3750

@@ -128,8 +141,7 @@ class WSLCE2ENetworkCreateTests
128141

129142
WSLC_TEST_METHOD(WSLCE2E_Network_Create_Ipv6_Success)
130143
{
131-
const std::wstring subnet = L"fd00:172:53::/64";
132-
auto result = RunWslc(std::format(L"network create --ipv6 --subnet {} {}", subnet, TestNetworkName));
144+
auto result = RunWslc(std::format(L"network create --ipv6 --subnet {} {}", Ipv6Subnet, TestNetworkName));
133145
result.Verify({.Stderr = L"", .ExitCode = 0});
134146
VERIFY_ARE_EQUAL(TestNetworkName, result.GetStdoutOneLine());
135147

@@ -139,10 +151,67 @@ class WSLCE2ENetworkCreateTests
139151
VERIFY_IS_TRUE(inspect.EnableIPv6);
140152
VERIFY_IS_TRUE(inspect.IPAM.Config.has_value());
141153
VERIFY_IS_TRUE(std::ranges::any_of(*inspect.IPAM.Config, [&](const auto& config) {
142-
return config.Subnet == wsl::shared::string::WideToMultiByte(subnet);
154+
return config.Subnet == wsl::shared::string::WideToMultiByte(Ipv6Subnet);
143155
}));
144156
}
145157

158+
WSLC_TEST_METHOD(WSLCE2E_Network_Create_Ipv6_OutboundConnectivity)
159+
{
160+
auto result = RunWslc(std::format(L"network create --ipv6 --subnet {} {}", Ipv6Subnet, TestNetworkName));
161+
result.Verify({.Stderr = L"", .ExitCode = 0});
162+
163+
auto session = OpenDefaultElevatedSession();
164+
const auto serverName = wsl::shared::string::WideToMultiByte(ServerContainerName);
165+
const auto serverScript = std::format(
166+
"import socket;"
167+
"s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);"
168+
"s.setsockopt(socket.IPPROTO_IPV6,socket.IPV6_V6ONLY,1);"
169+
"s.bind(('::',{}));"
170+
"s.listen(1);"
171+
"print('SERVER READY',flush=True);"
172+
"c,_=s.accept();"
173+
"c.sendall(b'ipv6-ok');"
174+
"c.close()",
175+
Ipv6TestPort);
176+
177+
RunDockerInSession(
178+
*session,
179+
{"/usr/bin/docker",
180+
"run",
181+
"-d",
182+
"--network",
183+
"host",
184+
"--name",
185+
serverName,
186+
wsl::shared::string::WideToMultiByte(PythonImage.NameAndTag()),
187+
"python3",
188+
"-u",
189+
"-c",
190+
serverScript});
191+
auto cleanupServer = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { RemoveDockerContainerNoThrow(serverName); });
192+
193+
VERIFY_NO_THROW(wsl::shared::retry::RetryWithTimeout<void>(
194+
[&]() {
195+
const auto logs = RunDockerInSession(*session, {"/usr/bin/docker", "logs", serverName});
196+
THROW_HR_IF(E_PENDING, logs.find("SERVER READY") == std::string::npos);
197+
},
198+
std::chrono::milliseconds(200),
199+
std::chrono::seconds(10)));
200+
201+
const auto clientCommand = std::format(
202+
L"python3 -c \"import socket;"
203+
L"s=socket.socket(socket.AF_INET6,socket.SOCK_STREAM);"
204+
L"s.settimeout(10);"
205+
L"s.connect(('{}',{}));"
206+
L"print(s.recv(16).decode())\"",
207+
Ipv6Gateway,
208+
Ipv6TestPort);
209+
210+
result = RunWslc(std::format(
211+
L"container run --rm --network {} --name {} {} {}", TestNetworkName, ClientContainerName, PythonImage.NameAndTag(), clientCommand));
212+
result.Verify({.Stdout = L"ipv6-ok\n", .Stderr = L"", .ExitCode = 0});
213+
}
214+
146215
WSLC_TEST_METHOD(WSLCE2E_Network_Create_Subnet_Success)
147216
{
148217
const std::wstring subnet = L"172.45.0.0/16";
@@ -230,6 +299,13 @@ class WSLCE2ENetworkCreateTests
230299
}
231300

232301
private:
302+
const std::wstring ClientContainerName = L"wslc-e2e-network-ipv6-client";
303+
const std::wstring ServerContainerName = L"wslc-e2e-network-ipv6-server";
233304
const std::wstring TestNetworkName = L"wslc-e2e-network-create";
305+
const std::wstring Ipv6Subnet = L"fd00:172:53::/64";
306+
const std::wstring Ipv6Gateway = L"fd00:172:53::1";
307+
const uint16_t Ipv6TestPort = 18080;
308+
const TestImage& PythonImage = PythonTestImage();
309+
WSADATA m_wsaData{};
234310
};
235311
} // namespace WSLCE2ETests

‎test/windows/wslc/e2e/WSLCE2EWarningTests.cpp‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Module Name:
1717
#include "windows/Common.h"
1818
#include "WSLCExecutor.h"
1919
#include "WSLCE2EHelpers.h"
20-
#include <WSLCProcessLauncher.h>
2120

2221
namespace WSLCE2ETests {
2322
using namespace wsl::shared;
@@ -43,35 +42,6 @@ class WSLCE2EWarningTests
4342
return true;
4443
}
4544

46-
static std::string RunDockerInSession(IWSLCSession& session, std::vector<std::string>&& args)
47-
{
48-
wsl::windows::common::WSLCProcessLauncher launcher("/usr/bin/docker", args);
49-
auto result = launcher.Launch(session).WaitAndCaptureOutput();
50-
VERIFY_ARE_EQUAL(0, result.Code);
51-
52-
// Trim trailing whitespace from the captured stdout (fd 1).
53-
auto output = result.Output[1];
54-
output.erase(output.find_last_not_of(" \n\r") + 1);
55-
return output;
56-
}
57-
58-
// Best-effort removal of a container from docker.
59-
static void RemoveContainerNoThrow(const std::string& containerId)
60-
try
61-
{
62-
wil::com_ptr<IWSLCSessionManager> sessionManager;
63-
THROW_IF_FAILED(CoCreateInstance(__uuidof(WSLCSessionManager), nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&sessionManager)));
64-
wsl::windows::common::security::ConfigureForCOMImpersonation(sessionManager.get());
65-
66-
wil::com_ptr<IWSLCSession> session;
67-
THROW_IF_FAILED(sessionManager->OpenSessionByName(nullptr, &session));
68-
wsl::windows::common::security::ConfigureForCOMImpersonation(session.get());
69-
70-
wsl::windows::common::WSLCProcessLauncher launcher("/usr/bin/docker", {"/usr/bin/docker", "rm", "-f", containerId});
71-
launcher.Launch(*session).WaitAndCaptureOutput();
72-
}
73-
CATCH_LOG()
74-
7545
// Injects a container with corrupt WSLC metadata into the default session's storage,
7646
// then verifies that running the wslc.exe CLI does not surface the COM service's recovery
7747
// warning on stderr: recovery runs outside the user's current command, so it is logged
@@ -95,7 +65,7 @@ class WSLCE2EWarningTests
9565
}
9666

9767
// cleanup: remove the corrupt container
98-
auto cleanup = wil::scope_exit([&]() { RemoveContainerNoThrow(corruptContainerId); });
68+
auto cleanup = wil::scope_exit([&]() { RemoveDockerContainerNoThrow(corruptContainerId); });
9969

10070
// Terminate the default session so the next wslc command recreates it and runs recovery.
10171
EnsureSessionIsTerminated();

0 commit comments

Comments
 (0)