diff --git a/test/windows/Common.cpp b/test/windows/Common.cpp index 6a16948b..6fa72913 100644 --- a/test/windows/Common.cpp +++ b/test/windows/Common.cpp @@ -1464,22 +1464,6 @@ std::wstring LxssWriteWslConfig(const std::wstring& Content) return previousContent; } -// writes distro specific settings /etc/wsl.conf -std::string LxssWriteWslDistroConfig(const std::string& Content, LPCWSTR DistributionName) -{ - std::string path = std::format("\\\\wsl.localhost\\{}\\etc\\wsl.conf", DistributionName); - - std::ifstream distroConfigRead(path); - auto previousContent = std::string{std::istreambuf_iterator(distroConfigRead), {}}; - distroConfigRead.close(); - - std::ofstream distroConfig(path, std::ios_base::binary); - VERIFY_IS_TRUE(distroConfig.good()); - distroConfig.write(Content.c_str(), Content.size()); - - return previousContent; -} - // generates a sample global WSL config for the tests std::wstring LxssGenerateTestConfig(TestConfigDefaults Default) { @@ -2702,11 +2686,12 @@ UniqueWebServer::~UniqueWebServer() } } -DistroFileChange::DistroFileChange(LPCWSTR Path, bool exists) : m_path(Path) +DistroFileChange::DistroFileChange(LPCWSTR Path, bool exists, LPCWSTR DistributionName) : + m_path(Path), m_distributionName(DistributionName) { if (exists) { - m_originalContent = LxsstuLaunchWslAndCaptureOutput(std::format(L"cat '{}'", m_path)).first; + m_originalContent = LxsstuLaunchWslAndCaptureOutput(std::format(L"-d {} -u root cat '{}'", m_distributionName, m_path)).first; } } @@ -2724,7 +2709,7 @@ DistroFileChange::~DistroFileChange() void DistroFileChange::SetContent(LPCWSTR Content) { - const auto cmd = LxssGenerateWslCommandLine(std::format(L" -u root cat > '{}'", m_path).c_str()); + const auto cmd = LxssGenerateWslCommandLine(std::format(L"-d {} -u root cat > '{}'", m_distributionName, m_path).c_str()); wsl::windows::common::SubProcess process(nullptr, cmd.c_str()); auto [read, write] = CreateSubprocessPipe(true, false); @@ -2751,7 +2736,7 @@ void DistroFileChange::SetContent(LPCWSTR Content) void DistroFileChange::Delete() { - VERIFY_ARE_EQUAL(LxsstuLaunchWsl(std::format(L"-u root rm -f '{}'", m_path).c_str()), 0L); + VERIFY_ARE_EQUAL(LxsstuLaunchWsl(std::format(L"-d {} -u root rm -f '{}'", m_distributionName, m_path)), 0L); } std::string ReadToString(SOCKET Handle) diff --git a/test/windows/Common.h b/test/windows/Common.h index 41bc2f73..ccf6588c 100644 --- a/test/windows/Common.h +++ b/test/windows/Common.h @@ -36,7 +36,6 @@ using namespace std::chrono_literals; #define LXSS_DISTRO_NAME_TEST "test_distro" #define LXSS_DISTRO_NAME_TEST_L WIDEN(LXSS_DISTRO_NAME_TEST) -#define LXSST_REMOVE_DISTRO_CONF_COMMAND_LINE L"-u root -e rm /etc/wsl.conf" #define LXSST_TESTS_INSTALL_COMMAND_LINE L"/bin/bash -c 'cd /data/test; ./build_tests.sh'" // @@ -374,7 +373,7 @@ private: class DistroFileChange { public: - DistroFileChange(LPCWSTR Path, bool exists = true); + DistroFileChange(LPCWSTR Path, bool exists = true, LPCWSTR DistributionName = LXSS_DISTRO_NAME_TEST_L); ~DistroFileChange(); DistroFileChange(const DistroFileChange&) = delete; DistroFileChange(DistroFileChange&&) = delete; @@ -387,6 +386,7 @@ public: private: std::optional m_originalContent; LPCWSTR m_path{}; + std::wstring m_distributionName; }; class PartialHandleRead @@ -540,8 +540,6 @@ wil::unique_handle GetNonElevatedToken(TOKEN_TYPE Type = TokenPrimary); std::wstring LxssWriteWslConfig(const std::wstring& Content); -std::string LxssWriteWslDistroConfig(const std::string& Content, LPCWSTR DistributionName = LXSS_DISTRO_NAME_TEST_L); - enum class DrvFsMode { WSL1, @@ -624,14 +622,13 @@ void Trim(std::wstring& string); inline auto EnableSystemd(const std::string& extraConfig = "", LPCWSTR distroName = LXSS_DISTRO_NAME_TEST_L) { - // enable systemd on the test distro by editing /etc/wsl.conf - LxssWriteWslDistroConfig("[boot]\nsystemd=true\n" + extraConfig, distroName); + auto config = std::make_unique(L"/etc/wsl.conf", false, distroName); + config->SetContent(wsl::shared::string::MultiByteToWide("[boot]\nsystemd=true\n" + extraConfig).c_str()); TerminateDistribution(distroName); - return wil::scope_exit([distroName] { - // clean up wsl.conf file - LxsstuLaunchWsl(std::format(L"-d {} " LXSST_REMOVE_DISTRO_CONF_COMMAND_LINE, distroName)); - TerminateDistribution(distroName); + return wil::scope_exit([config = std::move(config), distroName = std::wstring(distroName)]() mutable { + config.reset(); + TerminateDistribution(distroName.c_str()); }); } diff --git a/test/windows/Plan9Tests.cpp b/test/windows/Plan9Tests.cpp index f927bc71..9e873998 100644 --- a/test/windows/Plan9Tests.cpp +++ b/test/windows/Plan9Tests.cpp @@ -377,11 +377,11 @@ class Plan9Tests static auto EnablePlan9Logging() { - LxssWriteWslDistroConfig("[fileServer]\nlogFile=/plan9-logs.txt\nlogTruncate=false\nlogLevel=5"); + auto config = std::make_unique(L"/etc/wsl.conf", false); + config->SetContent(L"[fileServer]\nlogFile=/plan9-logs.txt\nlogTruncate=false\nlogLevel=5"); - return wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [] { - // clean up wsl.conf file - LxsstuLaunchWsl(L"rm /etc/wsl.conf"); + return wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [config = std::move(config)]() mutable { + config.reset(); TerminateDistribution(); }); } @@ -453,12 +453,13 @@ class Plan9Tests 0u); auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { - LxsstuLaunchWsl(L"-u root rm -rf /etc/wsl.conf /tmp/plan9-group-test"); + LxsstuLaunchWsl(L"-u root rm -rf /tmp/plan9-group-test"); TerminateDistribution(); }); // Make plan9testuser the default - LxssWriteWslDistroConfig("[user]\ndefault=plan9testuser\n"); + DistroFileChange config(L"/etc/wsl.conf", false); + config.SetContent(L"[user]\ndefault=plan9testuser\n"); TerminateDistribution(); // Validate that folder isn't accessible diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index c63b386b..e5415046 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -205,13 +205,6 @@ class UnitTests WSL2_TEST_METHOD(SystemdSystem) { - auto cleanup = wil::scope_exit([] { - // clean up wsl.conf file - const std::wstring disableSystemdCmd(LXSST_REMOVE_DISTRO_CONF_COMMAND_LINE); - LxsstuLaunchWsl(disableSystemdCmd); - TerminateDistribution(); - }); - auto revert = EnableSystemd(); VERIFY_IS_TRUE(IsSystemdRunning(L"--system")); @@ -594,13 +587,7 @@ class UnitTests auto cleanupPeer = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() { LxsstuLaunchWsl(std::format(L"--unregister {}", peerDistroName)); }); - // Enable systemd in the peer distro (no helper exists for non-test distros). - VERIFY_ARE_EQUAL( - LxsstuLaunchWsl(std::format(L"-d {} -- sh -c \"mkdir -p /etc && printf '[boot]\\nsystemd=true\\n' > /etc/wsl.conf\"", peerDistroName)), - 0L); - - // Terminate so the config takes effect on next start. - TerminateDistribution(peerDistroName); + auto cleanupPeerSystemd = EnableSystemd("", peerDistroName); // Verify interop works in both distros (this also starts the peer with systemd). { @@ -2207,14 +2194,11 @@ Usage: TEST_METHOD(Hostname) { - auto cleanup = wil::scope_exit([] { - LxsstuLaunchWsl(LXSST_REMOVE_DISTRO_CONF_COMMAND_LINE); + auto cleanup = wil::scope_exit([] { TerminateDistribution(); }); + DistroFileChange config(L"/etc/wsl.conf", false); - TerminateDistribution(); - }); - - auto validate = [](const std::string& input, const std::wstring& expectedOutput) { - LxssWriteWslDistroConfig("[network]\nhostname=" + input); + auto validate = [&config](const std::string& input, const std::wstring& expectedOutput) { + config.SetContent(wsl::shared::string::MultiByteToWide("[network]\nhostname=" + input).c_str()); TerminateDistribution(); auto [output, _] = LxsstuLaunchWslAndCaptureOutput(L"hostname"); @@ -8412,6 +8396,7 @@ Distribution successfully installed. It can be launched via 'wsl.exe -d ubuntu-d std::optional systemdCleanup; std::optional systemdCleanup2; std::optional cgroupConfig; + std::optional cgroupConfig2; if (systemd) { systemdCleanup.emplace(EnableSystemd()); @@ -8422,7 +8407,8 @@ Distribution successfully installed. It can be launched via 'wsl.exe -d ubuntu-d { cgroupConfig.emplace(L"/etc/wsl.conf", false); cgroupConfig->SetContent(L"[automount]\ncgroups=v1\n"); - LxssWriteWslDistroConfig("[automount]\ncgroups=v1\n", secondDistroName); + cgroupConfig2.emplace(L"/etc/wsl.conf", false, secondDistroName); + cgroupConfig2->SetContent(L"[automount]\ncgroups=v1\n"); TerminateDistribution(); TerminateDistribution(secondDistroName);