Test working

This commit is contained in:
Blue 2025-12-15 17:48:42 -08:00
parent bce826f620
commit 9be104f72d
3 changed files with 27 additions and 13 deletions

View File

@ -24,6 +24,8 @@ static std::vector<std::string> defaultNerdctlCreateArgs{//"--pull=never", // TO
"--ulimit",
"nofile=65536:65536"};
static std::vector<std::string> defaultNerdctlEnv{"PATH=/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin"};
namespace {
auto ProcessPortMappings(const WSLA_CONTAINER_OPTIONS& options, WSLAVirtualMachine& vm, std::vector<std::string>& args)
{
@ -43,6 +45,7 @@ auto ProcessPortMappings(const WSLA_CONTAINER_OPTIONS& options, WSLAVirtualMachi
auto errorCleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() {
if (!vmPorts.empty())
{
// TODO vmPorts is invalid once moved.
LOG_IF_FAILED(wil::ResultFromException([&]() { vm.ReleasePorts(vmPorts); }));
}
@ -97,7 +100,7 @@ auto ProcessPortMappings(const WSLA_CONTAINER_OPTIONS& options, WSLAVirtualMachi
{
THROW_IF_FAILED(vm.MapPort(e.Family, e.HostPort, e.VmPort, false));
args.push_back("-p");
args.push_back(std::format("{}{}:{}", e.Family == AF_INET6 ? "[::]:" : "", e.HostPort, e.VmPort));
args.push_back(std::format("{}{}:{}", e.Family == AF_INET6 ? "[::]:" : "", e.VmPort, e.ContainerPort));
}
return std::make_pair(mappedPorts, std::move(errorCleanup));
@ -137,7 +140,7 @@ void WSLAContainer::Start(const WSLA_CONTAINER_OPTIONS& Options)
m_name.c_str(),
m_state);
ServiceProcessLauncher launcher(nerdctlPath, {nerdctlPath, "start", "-a", m_id}, {}, common::ProcessFlags::None);
ServiceProcessLauncher launcher(nerdctlPath, {nerdctlPath, "start", "-a", m_id}, defaultNerdctlEnv, common::ProcessFlags::None);
for (auto i = 0; i < Options.InitProcessOptions.FdsCount; i++)
{
launcher.AddFd(Options.InitProcessOptions.Fds[i]);
@ -210,7 +213,7 @@ try
m_name.c_str(),
m_state);
ServiceProcessLauncher launcher(
nerdctlPath, {nerdctlPath, "stop", m_name, "--time", std::to_string(static_cast<ULONG>(std::round(TimeoutMs / 1000)))});
nerdctlPath, {nerdctlPath, "stop", m_name, "--time", std::to_string(static_cast<ULONG>(std::round(TimeoutMs / 1000)))}, defaultNerdctlEnv);
// TODO: Figure out how we want to handle custom signals.
// nerdctl stop has a --time and a --signal option that can be used
@ -236,7 +239,7 @@ try
m_name.c_str(),
m_state);
ServiceProcessLauncher launcher(nerdctlPath, {nerdctlPath, "rm", "-f", m_name});
ServiceProcessLauncher launcher(nerdctlPath, {nerdctlPath, "rm", "-f", m_name}, defaultNerdctlEnv);
auto result = launcher.Launch(*m_parentVM).WaitAndCaptureOutput();
THROW_HR_IF_MSG(E_FAIL, result.Code != 0, "%hs", launcher.FormatResult(result).c_str());
@ -316,7 +319,7 @@ try
args.emplace_back(Options->CommandLine[i]);
}
ServiceProcessLauncher launcher(nerdctlPath, args, {}, common::ProcessFlags::None);
ServiceProcessLauncher launcher(nerdctlPath, args, defaultNerdctlEnv, common::ProcessFlags::None);
for (auto i = 0; i < Options->FdsCount; i++)
{
launcher.AddFd(Options->Fds[i]);
@ -390,7 +393,7 @@ Microsoft::WRL::ComPtr<WSLAContainer> WSLAContainer::Create(
auto args = PrepareNerdctlCreateCommand(containerOptions, std::move(inputOptions));
ServiceProcessLauncher launcher(nerdctlPath, args, {});
ServiceProcessLauncher launcher(nerdctlPath, args, defaultNerdctlEnv);
auto result = launcher.Launch(parentVM).WaitAndCaptureOutput();
// TODO: Have better error codes.

View File

@ -56,7 +56,7 @@ WSLASession::WSLASession(ULONG id, const WSLA_SESSION_SETTINGS& Settings, WSLAUs
ServiceProcessLauncher launcher{
"/usr/bin/containerd",
{"/usr/bin/containerd"},
{{"PATH=/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:."}},
{{"PATH=/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin"}},
common::ProcessFlags::Stdout | common::ProcessFlags::Stderr};
m_containerdThread = std::thread(&WSLASession::MonitorContainerd, this, launcher.Launch(*m_virtualMachine.Get()));

View File

@ -594,7 +594,6 @@ class WSLATests
LogError("Process exited, output: %hs", output.c_str());
VERIFY_FAIL();
}
LogInfo("Buffer: %hs", output.c_str());
index += bytesRead;
if (output.find(Content) != std::string::npos)
@ -1649,7 +1648,7 @@ class WSLATests
}
}
void ExpectHttpResponse(LPCWSTR Url, const std::wstring& ExpectedContent)
void ExpectHttpResponse(LPCWSTR Url, std::optional<int> expectedCode)
{
const winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter filter;
filter.CacheControl().WriteBehavior(winrt::Windows::Web::Http::Filters::HttpCacheWriteBehavior::NoCache);
@ -1657,9 +1656,15 @@ class WSLATests
const winrt::Windows::Web::Http::HttpClient client(filter);
auto response = client.GetAsync(winrt::Windows::Foundation::Uri(Url));
auto content = response.get().Content().ReadAsStringAsync().get();
std::wstring contentString{content.data(), content.data() + content.size()};
VERIFY_ARE_EQUAL(contentString, ExpectedContent);
if (expectedCode.has_value())
{
VERIFY_ARE_EQUAL(static_cast<int>(response.get().StatusCode()), expectedCode.value());
}
else
{
// Validate that port isn't bound.
}
}
TEST_METHOD(PortMappingsBridged)
@ -1673,7 +1678,13 @@ class WSLATests
auto session = CreateSession(settings);
WSLAContainerLauncher launcher(
"python:3.12-alpine", "test-ports", {}, {"python3", "-m", "http.server"}, {}, WSLA_CONTAINER_NETWORK_BRIDGE, ProcessFlags::Stdout);
"python:3.12-alpine",
"test-ports",
{},
{"python3", "-m", "http.server"},
{"PYTHONUNBUFFERED=1"},
WSLA_CONTAINER_NETWORK_BRIDGE,
ProcessFlags::Stdout | ProcessFlags::Stderr);
launcher.AddPort(1234, 8000, AF_INET);
@ -1684,6 +1695,6 @@ class WSLATests
// Wait for the container bind() to be completed.
WaitForOutput(stdoutHandle.get(), "Serving HTTP on 0.0.0.0 port 8000");
ExpectHttpResponse(L"http://localhost:1234", L"Directory listing for /\n");
ExpectHttpResponse(L"http://localhost:1234", 200);
}
};