feat(test): give a test that sets Test::wayland a compositor of its own
A test that opens a window opened it on the display of whoever ran the suite: every `crafter-build test` of Crafter.Graphics put the windows of its window tests on the desktop, and its other GPU tests connect to the session's compositor too, since Device::Initialize calls wl_display_connect whether a window follows or not. A test that sets the new Test::wayland (TestBuilder::Wayland) now runs with a headless compositor started for it alone. RunSingleTest starts the compositor through sh, with XDG_RUNTIME_DIR naming a directory made for it and without WAYLAND_DISPLAY, WAYLAND_SOCKET or DISPLAY, waits up to 30 s for a wayland-<n> socket in that directory, runs the test with WAYLAND_DISPLAY set to the socket's path and with DISPLAY and WAYLAND_SOCKET unset, then stops the compositor's process group and removes the directory. The test's command is env with those changes, and env's command is sh, which execs the test's words: env takes every word with an '=' in it ahead of its command for an assignment, a binary's path among them. The compositor runs under timeout(1) as long as the test may, the wait and 30 s more, and with no limit for a test that has none, a timeout of 0. It is weston on its headless backend with its GL renderer, unless CRAFTER_BUILD_COMPOSITOR or the new --compositor= names another command; `host` gives the tests the session's own display instead, to watch them. A compositor that ends before it listens, sh not finding it included, or does not listen in time fails the test with what it printed. A test that may run where its compositor is missing declares it in requires_ (tool:weston for the default), the check RunTests makes before the build, so no skip of its own is added, and no probe of the command: the command is any sh command, whose first word need not be the compositor. A test that fails, crashes or times out keeps what its compositor printed (TestResult::compositorLog), which RunTests writes to build/test-logs/<name>.compositor.log, and says when the compositor ended while it ran. One compositor per test rather than one per run: no two tests share one, so tests that run at once cannot cover each other's windows or take each other's focus, for weston's start-up on every test. weston's GL renderer, because it imports the dmabufs that a GPU's Vulkan driver presents, as Mesa's and NVIDIA's do, and the headless backend's default renderer and pixman import none: against pixman, the NVIDIA driver reports no format for a window's surface, and Crafter.Graphics's FrameLoopSync stops at its formatCount > 0 assertion. weston cannot run the test itself, as `weston -- <test>` exits 0 whatever the test returned, so crafter-build runs the compositor beside it. :Platform gains BackgroundCommand, the compositor's process, and PrivateDirectory, its directory. The process is forked with PR_SET_PDEATHSIG and runs under `timeout`, which passes the SIGTERM it gets when crafter-build dies on to the compositor's group, so a crafter-build killed by Ctrl-C leaves no compositor running. The directory is made in $XDG_RUNTIME_DIR, or in /tmp where that is not a directory, rather than in the temp directory, since a socket's path holds 107 bytes, and is flocked while in use; one that a killed crafter-build left is removed when the next is made beside it, once it has not changed for a minute. New members and a parameter with a default, nothing removed: a minor release.
This commit is contained in:
parent
cb8914d387
commit
dc8e9e6f9e
7 changed files with 418 additions and 13 deletions
|
|
@ -64,12 +64,77 @@ namespace {
|
|||
}
|
||||
}
|
||||
|
||||
void WriteLog(const fs::path& projectPath, const std::string& name, const std::string& output) {
|
||||
// Writes build/test-logs/<name>.log and returns its path, or nothing
|
||||
// when the directory cannot be made.
|
||||
std::optional<fs::path> WriteLog(const fs::path& projectPath, const std::string& name, const std::string& output) {
|
||||
fs::path logDir = projectPath / "build" / "test-logs";
|
||||
std::error_code ec;
|
||||
fs::create_directories(logDir, ec);
|
||||
if (ec) return;
|
||||
std::ofstream(logDir / (std::format("{}.log", name))) << output;
|
||||
if (ec) return std::nullopt;
|
||||
const fs::path log = logDir / std::format("{}.log", name);
|
||||
std::ofstream(log) << output;
|
||||
return log;
|
||||
}
|
||||
|
||||
// weston on its headless backend: the compositor a test that sets
|
||||
// Test::wayland runs with unless --compositor= or CRAFTER_BUILD_COMPOSITOR
|
||||
// names another. Its GL renderer takes the dmabufs that hardware Vulkan
|
||||
// and EGL drivers present, which the headless backend's default, a
|
||||
// renderer that draws nothing, and pixman do not. --no-config keeps the
|
||||
// user's weston.ini out of the test, and --idle-time=0 keeps the output
|
||||
// from going idle after the default 300 s, which would stop frame
|
||||
// callbacks.
|
||||
constexpr std::string_view DefaultCompositor = "weston --backend=headless --renderer=gl --no-config --idle-time=0";
|
||||
// The compositor named for none of the test's own: the test gets the
|
||||
// environment crafter-build has, display included, to watch its windows.
|
||||
constexpr std::string_view HostDisplay = "host";
|
||||
// How long a compositor may take to listen. weston needs a fraction of a
|
||||
// second; this is for a machine running a test on every hardware thread.
|
||||
constexpr std::chrono::seconds CompositorStartup{30};
|
||||
|
||||
std::string CompositorCommand(const std::optional<std::string>& compositorOverride) {
|
||||
if (compositorOverride && !compositorOverride->empty()) return *compositorOverride;
|
||||
const char* named = std::getenv("CRAFTER_BUILD_COMPOSITOR");
|
||||
if (named != nullptr && *named != '\0') return named;
|
||||
return std::string(DefaultCompositor);
|
||||
}
|
||||
|
||||
// The wayland-<n> socket a compositor made in `directory`. Any number:
|
||||
// libwayland's wl_display_add_socket_auto starts at 0 and weston at 1.
|
||||
std::optional<fs::path> WaylandSocket(const fs::path& directory) {
|
||||
constexpr std::string_view prefix = "wayland-";
|
||||
std::error_code ec;
|
||||
for (const fs::directory_entry& entry : fs::directory_iterator(directory, ec)) {
|
||||
const std::string name = entry.path().filename().string();
|
||||
if (!name.starts_with(prefix) || name.size() == prefix.size()) continue;
|
||||
if (!std::ranges::all_of(name.substr(prefix.size()), [](char c) { return c >= '0' && c <= '9'; })) continue;
|
||||
std::error_code statError;
|
||||
if (fs::is_socket(entry.symlink_status(statError))) return entry.path();
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// Where the directory one test's compositor gets as its XDG_RUNTIME_DIR
|
||||
// is made: in $XDG_RUNTIME_DIR, where a session's sockets live, when
|
||||
// that is a directory, else in /tmp. A socket's path holds at most 107
|
||||
// bytes, so the directory needs a short path, which a project's, a
|
||||
// scratch directory's or the temp directory TMPDIR names need not be.
|
||||
fs::path CompositorParent() {
|
||||
const char* runtime = std::getenv("XDG_RUNTIME_DIR");
|
||||
std::error_code ec;
|
||||
if (runtime != nullptr && *runtime != '\0' && fs::is_directory(runtime, ec)) return runtime;
|
||||
return "/tmp";
|
||||
}
|
||||
|
||||
// That directory, which also holds the compositor's log.
|
||||
struct CompositorDirectory : PrivateDirectory {
|
||||
CompositorDirectory() : PrivateDirectory(CompositorParent(), "crafter-build-wayland-") {}
|
||||
fs::path Log() const { return path / "compositor.log"; }
|
||||
};
|
||||
|
||||
std::string ReadWhole(const fs::path& file) {
|
||||
std::ifstream in(file, std::ios::binary);
|
||||
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||||
}
|
||||
|
||||
void PrintResult(const TestResult& r, std::string_view runnerName) {
|
||||
|
|
@ -259,27 +324,82 @@ TestRunner TestRunner::FromEnv(std::string_view target, TestRunner fallback) {
|
|||
return fallback;
|
||||
}
|
||||
|
||||
TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std::chrono::seconds timeout) {
|
||||
TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std::chrono::seconds timeout, const std::optional<std::string>& compositorOverride) {
|
||||
TestResult result;
|
||||
result.name = test.config.name;
|
||||
|
||||
std::map<std::string, std::string> ph;
|
||||
ph["{args}"] = JoinAndQuoteArgs(test.args);
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
CommandResult r;
|
||||
|
||||
std::string cmd;
|
||||
if (test.runner.exec.empty()) {
|
||||
// Local runner: spawn the binary directly through the host shell.
|
||||
std::string cmd = std::format("{} {}", ShellQuote(binary.string()), ph["{args}"]);
|
||||
r = RunCommandWithTimeout(cmd, timeout);
|
||||
cmd = std::format("{} {}", ShellQuote(binary.string()), ph["{args}"]);
|
||||
} else {
|
||||
// Prefix runner (qemu-user, wasmtime, wine, ...): templated exec
|
||||
// wraps the local binary.
|
||||
ph["{bin}"] = binary.string();
|
||||
r = RunCommandWithTimeout(Substitute(test.runner.exec, ph), timeout);
|
||||
cmd = Substitute(test.runner.exec, ph);
|
||||
}
|
||||
|
||||
// The test's own compositor, which no other test shares, so that no
|
||||
// two tests cover each other's windows or take each other's focus. Its
|
||||
// directory goes after it: declared first, it is destroyed last.
|
||||
const std::string compositor = test.wayland ? CompositorCommand(compositorOverride) : std::string();
|
||||
std::optional<CompositorDirectory> directory;
|
||||
std::optional<BackgroundCommand> process;
|
||||
if (test.wayland && compositor != HostDisplay) {
|
||||
directory.emplace();
|
||||
// Without a WAYLAND_DISPLAY, which the compositor would take for a
|
||||
// session to open a window in (wlroots does), or a DISPLAY.
|
||||
const std::array<EnvironmentChange, 4> environment = {{
|
||||
{"XDG_RUNTIME_DIR", directory->path.string()},
|
||||
{"WAYLAND_DISPLAY", std::nullopt},
|
||||
{"WAYLAND_SOCKET", std::nullopt},
|
||||
{"DISPLAY", std::nullopt},
|
||||
}};
|
||||
// As long as the test may run, the wait for its socket and 30 s
|
||||
// more; none for a test with none, a timeout of 0, which timeout(1)
|
||||
// takes for no limit, the test's as the compositor's.
|
||||
const std::chrono::seconds lifetime = timeout.count() > 0 ? timeout + CompositorStartup + std::chrono::seconds(30) : std::chrono::seconds(0);
|
||||
process.emplace(compositor, environment, directory->Log(), lifetime);
|
||||
|
||||
// weston makes its socket once its backend and renderer are up, so a
|
||||
// socket means a compositor that takes clients.
|
||||
const auto deadline = std::chrono::steady_clock::now() + CompositorStartup;
|
||||
std::optional<fs::path> socket = WaylandSocket(directory->path);
|
||||
std::optional<std::int32_t> ended;
|
||||
while (!socket && !(ended = process->Exited()) && std::chrono::steady_clock::now() < deadline) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
socket = WaylandSocket(directory->path);
|
||||
}
|
||||
if (!socket) {
|
||||
process->Stop(std::chrono::seconds(5));
|
||||
std::string printed = ReadWhole(directory->Log());
|
||||
if (!printed.empty() && !printed.ends_with('\n')) printed.push_back('\n');
|
||||
// A compositor that is missing fails the test as any other that
|
||||
// does not start does. The one skip is the test's own requires_,
|
||||
// tool:weston for the default, which RunTests checks before the
|
||||
// build; a probe of the command here would not do, since it is any
|
||||
// sh command, whose first word need not be the compositor.
|
||||
result.outcome = TestOutcome::Fail;
|
||||
result.exitCode = -1;
|
||||
result.output = ended
|
||||
? std::format("the headless compositor ({}) ended with status {} before it listened for clients; it printed:\n{}", compositor, *ended, printed)
|
||||
: std::format("the headless compositor ({}) made no wayland-<n> socket in {} within {} s; it printed:\n{}", compositor, directory->path.string(), CompositorStartup.count(), printed);
|
||||
// sh's status for a command it cannot find.
|
||||
if (ended == 127) result.output += "install weston, name another compositor with CRAFTER_BUILD_COMPOSITOR or --compositor=, or skip a test that may run without it with requires_, tool:weston for weston\n";
|
||||
return result;
|
||||
}
|
||||
// env's command is sh, which execs the test's words as they are: env
|
||||
// takes every word with an '=' in it ahead of its command for an
|
||||
// assignment, the binary's path among them where it has one (a test
|
||||
// named a=b), and that test never ran.
|
||||
cmd = std::format("env -u DISPLAY -u WAYLAND_SOCKET WAYLAND_DISPLAY={} sh -c 'exec \"$@\"' sh {}", ShellQuote(socket->string()), cmd);
|
||||
}
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
CommandResult r = RunCommandWithTimeout(cmd, timeout);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
result.duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
result.output = std::move(r.output);
|
||||
|
|
@ -292,6 +412,19 @@ TestResult Crafter::RunSingleTest(const Test& test, const fs::path& binary, std:
|
|||
else if (r.exitCode != 0) result.outcome = TestOutcome::Fail;
|
||||
else result.outcome = TestOutcome::Pass;
|
||||
|
||||
if (process) {
|
||||
// Asked before Stop() ends it, which would make every compositor one
|
||||
// that ended.
|
||||
const std::optional<std::int32_t> ended = process->Exited();
|
||||
process->Stop(std::chrono::seconds(5));
|
||||
if (result.outcome != TestOutcome::Pass && result.outcome != TestOutcome::Skipped) {
|
||||
if (ended) {
|
||||
if (!result.output.empty() && !result.output.ends_with('\n')) result.output.push_back('\n');
|
||||
result.output += std::format("the headless compositor ({}) ended with status {} while the test ran\n", compositor, *ended);
|
||||
}
|
||||
result.compositorLog = ReadWhole(directory->Log());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -447,6 +580,7 @@ TestBuilder& TestBuilder::Requires(std::string r) { Ref().requires_.pu
|
|||
TestBuilder& TestBuilder::Dependencies(std::vector<Configuration*> d) { Ref().config.dependencies = std::move(d); return *this; }
|
||||
TestBuilder& TestBuilder::LinkFlag(std::string f) { Ref().config.linkFlags.push_back(std::move(f)); return *this; }
|
||||
TestBuilder& TestBuilder::CompileFlag(std::string f) { Ref().config.compileFlags.push_back(std::move(f)); return *this; }
|
||||
TestBuilder& TestBuilder::Wayland(bool w) { Ref().wayland = w; return *this; }
|
||||
|
||||
void Crafter::UnpackageTests(Configuration& cfg) {
|
||||
for (Test& test : cfg.tests) test.config.packaging = Packaging::None;
|
||||
|
|
@ -692,13 +826,21 @@ TestSummary Crafter::RunTests(std::span<Configuration* const> configurations, co
|
|||
// wasmtime without the .wasm it was linked as.
|
||||
fs::path binary = ExpectedOutput(t.config);
|
||||
try {
|
||||
r = RunSingleTest(t, binary, timeout);
|
||||
r = RunSingleTest(t, binary, timeout, opts.compositorOverride);
|
||||
} catch (const std::exception& e) {
|
||||
r.outcome = TestOutcome::Fail;
|
||||
r.output = std::format("runner threw: {}", e.what());
|
||||
r.exitCode = -1;
|
||||
}
|
||||
|
||||
// Beside the test's own log rather than in it: weston prints some
|
||||
// 70 lines starting up, which would bury what the test said.
|
||||
if (!r.compositorLog.empty()) {
|
||||
if (const std::optional<fs::path> log = WriteLog(owners[i]->path, std::format("{}.compositor", r.name), r.compositorLog)) {
|
||||
if (!r.output.empty() && !r.output.ends_with('\n')) r.output.push_back('\n');
|
||||
r.output += std::format("the headless compositor's output is in {}\n", log->string());
|
||||
}
|
||||
}
|
||||
if (r.outcome != TestOutcome::Pass && r.outcome != TestOutcome::Skipped && !r.output.empty()) {
|
||||
WriteLog(owners[i]->path, r.name, r.output);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue