From eb677209c0b837c3797c951ec1eb937965e40418 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 20 Apr 2026 14:10:10 +0200 Subject: zen-test: add CLI integration harness + TestArtifactProvider + CI host stats (#985) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Establishes a new end-to-end integration test harness for the `zen` CLI, the shared fetcher it uses to pull test artifacts, and the CI plumbing that feeds both. Also lowers the default test-harness log level and broadens the artifact fetcher's credential resolution. ### `zen-test` executable (`src/zen-test/`) - New binary modeled on `zenserver-test`, built only in debug. - `zen-test.{h,cpp}` harness: spawns `zen.exe` via `CreateProc` and captures combined stdout/stderr into a `ZenCommandResult` for assertion. - Registered with `scripts/test.lua` under the short name `zen` (`xmake test --run=zen`) and enabled for `--kill-stale-processes`. - Prints a clear console message when invoked from a release build (tests disabled), so misconfiguration is easy to spot. - Documented in `CLAUDE.md` (test-suite naming table + test projects section) and `README.md`. - Test cases in the `zen.artifactprovider` suite: - `probe.lyra_cook_rpc_recording` — probe against a canonical Lyra cook RPC recording that skips with a diagnostic `MESSAGE` when no artifact source is configured. - `probe.s3_readme` — probes the configured S3 bucket for `README.md` using a fresh temp cache to force the request through to S3; skips on macOS without static creds (no EC2 Mac runners in our fleet). - `zen.utility-cmd` suite: new integration tests exercising `zen print`, `zen wipe`, and `zen copy`. ### `TestArtifactProvider` (`src/zenutil/testartifactprovider.{h,cpp}`) - `Ref` factory returning a local-only or S3-backed provider, selected from env vars: - `ZEN_TEST_ARTIFACTS_PATH` — local directory to serve from (write-through cache for remote fetches). - `ZEN_TEST_ARTIFACTS_S3` — S3 URL to fetch from. - `AWS_DEFAULT_REGION` / `AWS_REGION`, `AWS_ENDPOINT_URL`, `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` — standard AWS config. - `Exists(path)` / `Fetch(path)` API with a `TestArtifactFetchResult` return carrying the content buffer and a diagnostic error string. Content is cached on disk across test runs. - **IMDS credential fallback**: when no static `AWS_ACCESS_KEY_ID` is present, attaches an `ImdsCredentialProvider` so self-hosted EC2 runners with an attached IAM role can sign S3 requests without static credentials (mirrors the pattern in `zenserver/hub/hydration.cpp`). - **IMDS opt-out**: honors the standard `AWS_EC2_METADATA_DISABLED=true` env var, and skips IMDS by default on macOS where the link-local probe would just emit noise. ### Test harness log level (`src/zencore/testing.cpp`) - `TestRunner::ApplyCommandLine` now defaults the global log level to `Info` (was effectively `Trace`), cutting the noise from `xmake test --run=all` now that the suite has grown. Applies uniformly to `zencore-test`, `zenhttp-test`, `zenstore-test`, `zenutil-test`, `zenserver-test`, `zen-test`, etc. `--debug` (Debug) and `--verbose` (Trace) still opt back in when chasing failures. ### CI (`.github/workflows/validate.yml`) - **Runner info step** on all three platforms (Windows/Linux/macOS): prints host, CPU topology, memory, and disk usage before the build/test step, so flakes that correlate with a particular runner or low disk space are easy to spot. - **Artifact env wiring**: passes `ZEN_TEST_ARTIFACTS_S3` and `AWS_DEFAULT_REGION` into the debug Build & Test step on all three platforms so the probe can reach its source when the repo variable is configured. The probe skips cleanly when unset. --- src/zen-test/utility-cmd-tests.cpp | 179 +++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 src/zen-test/utility-cmd-tests.cpp (limited to 'src/zen-test/utility-cmd-tests.cpp') diff --git a/src/zen-test/utility-cmd-tests.cpp b/src/zen-test/utility-cmd-tests.cpp new file mode 100644 index 000000000..aa49d7299 --- /dev/null +++ b/src/zen-test/utility-cmd-tests.cpp @@ -0,0 +1,179 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include + +#if ZEN_WITH_TESTS + +# include "zen-test.h" + +# include +# include +# include +# include +# include + +namespace zen::tests { + +TEST_SUITE_BEGIN("zen.utility-cmd"); + +TEST_CASE("print.cbo_to_json") +{ + ScopedTemporaryDirectory TempDir; + + // Build a small compact binary object covering a few primitive field types. + CbObjectWriter Writer; + Writer << "Name" + << "ZenPrintTest"; + Writer << "Count" << int32_t(42); + Writer << "Enabled" << true; + + IoBuffer Payload = Writer.Save().GetBuffer().AsIoBuffer(); + REQUIRE(Payload.GetSize() > 0); + + const std::filesystem::path CboPath = TempDir.Path() / "object.cbo"; + WriteFile(CboPath, Payload); + + const ZenCommandResult Result = RunZen(fmt::format(R"(print --source "{}")", CboPath.string())); + + REQUIRE_MESSAGE(Result.ExitCode == 0, fmt::format("zen print exited with code {}", Result.ExitCode)); + + // CompactBinaryToJson emits a JSON object; field names are quoted and values appear verbatim. + CHECK(Result.Output.find("\"Name\"") != std::string::npos); + CHECK(Result.Output.find("\"ZenPrintTest\"") != std::string::npos); + CHECK(Result.Output.find("\"Count\"") != std::string::npos); + CHECK(Result.Output.find("42") != std::string::npos); + CHECK(Result.Output.find("\"Enabled\"") != std::string::npos); + CHECK(Result.Output.find("true") != std::string::npos); +} + +TEST_CASE("print.show_type_info_annotates_output") +{ + ScopedTemporaryDirectory TempDir; + + CbObjectWriter Writer; + Writer << "Count" << int32_t(7); + + IoBuffer Payload = Writer.Save().GetBuffer().AsIoBuffer(); + const std::filesystem::path CboPath = TempDir.Path() / "object.cbo"; + WriteFile(CboPath, Payload); + + const ZenCommandResult Plain = RunZen(fmt::format(R"(print --source "{}")", CboPath.string())); + const ZenCommandResult Typed = RunZen(fmt::format(R"(print --show-type-info --source "{}")", CboPath.string())); + + REQUIRE(Plain.ExitCode == 0); + REQUIRE(Typed.ExitCode == 0); + + // --show-type-info prefixes values with type annotations like [IntegerPositive] that the plain variant omits. + CHECK(Typed.Output.size() > Plain.Output.size()); + CHECK(Typed.Output.find("[IntegerPositive]") != std::string::npos); + CHECK(Plain.Output.find("[IntegerPositive]") == std::string::npos); +} + +TEST_CASE("print.missing_source_fails") +{ + const ZenCommandResult Result = RunZen(R"(print)"); + + CHECK(Result.ExitCode != 0); +} + +TEST_CASE("print.non_cbo_file_fails") +{ + ScopedTemporaryDirectory TempDir; + + const std::filesystem::path GarbagePath = TempDir.Path() / "garbage.cbo"; + const std::string_view Garbage = "this is not compact binary data"; + WriteFile(GarbagePath, IoBuffer(IoBuffer::Wrap, Garbage.data(), Garbage.size())); + + const ZenCommandResult Result = RunZen(fmt::format(R"(print --source "{}")", GarbagePath.string())); + + CHECK(Result.ExitCode != 0); +} + +//////////////////////////////////////////////////////////////////////////////// +// wipe + +namespace { + + // Drop a small fixture tree of files and nested directories inside Root so wipe has + // something non-trivial to recurse over. + void PopulateWipeFixture(const std::filesystem::path& Root) + { + std::filesystem::create_directories(Root / "nested" / "deep"); + const std::string_view Contents = "hello"; + const IoBuffer Payload(IoBuffer::Wrap, Contents.data(), Contents.size()); + WriteFile(Root / "top.txt", Payload); + WriteFile(Root / "nested" / "mid.txt", Payload); + WriteFile(Root / "nested" / "deep" / "leaf.txt", Payload); + } + + bool DirectoryIsEmpty(const std::filesystem::path& Path) + { + std::error_code Ec; + return std::filesystem::is_empty(Path, Ec) && !Ec; + } + +} // namespace + +TEST_CASE("wipe.removes_directory_contents") +{ + ScopedTemporaryDirectory TempDir; + const std::filesystem::path Target = TempDir.Path() / "to-wipe"; + PopulateWipeFixture(Target); + + REQUIRE(std::filesystem::exists(Target / "top.txt")); + REQUIRE(std::filesystem::exists(Target / "nested" / "deep" / "leaf.txt")); + + const ZenCommandResult Result = RunZen(fmt::format(R"(wipe --yes --quiet --directory "{}")", Target.string())); + + REQUIRE_MESSAGE(Result.ExitCode == 0, fmt::format("zen wipe exited with code {}", Result.ExitCode)); + + CHECK_FALSE(std::filesystem::exists(Target / "top.txt")); + CHECK_FALSE(std::filesystem::exists(Target / "nested")); +} + +TEST_CASE("wipe.dryrun_keeps_files") +{ + ScopedTemporaryDirectory TempDir; + const std::filesystem::path Target = TempDir.Path() / "to-wipe"; + PopulateWipeFixture(Target); + + const ZenCommandResult Result = RunZen(fmt::format(R"(wipe --yes --quiet --dryrun --directory "{}")", Target.string())); + + REQUIRE_MESSAGE(Result.ExitCode == 0, fmt::format("zen wipe --dryrun exited with code {}", Result.ExitCode)); + + // --dryrun must leave the tree untouched. + CHECK(std::filesystem::exists(Target / "top.txt")); + CHECK(std::filesystem::exists(Target / "nested" / "mid.txt")); + CHECK(std::filesystem::exists(Target / "nested" / "deep" / "leaf.txt")); +} + +TEST_CASE("wipe.nonexistent_directory_is_noop") +{ + ScopedTemporaryDirectory TempDir; + const std::filesystem::path Missing = TempDir.Path() / "does-not-exist"; + REQUIRE_FALSE(std::filesystem::exists(Missing)); + + const ZenCommandResult Result = RunZen(fmt::format(R"(wipe --yes --quiet --directory "{}")", Missing.string())); + + // A missing directory should be silently skipped without failing. + CHECK(Result.ExitCode == 0); + CHECK_FALSE(std::filesystem::exists(Missing)); +} + +TEST_CASE("wipe.empty_directory") +{ + ScopedTemporaryDirectory TempDir; + const std::filesystem::path Target = TempDir.Path() / "empty"; + std::filesystem::create_directories(Target); + REQUIRE(DirectoryIsEmpty(Target)); + + const ZenCommandResult Result = RunZen(fmt::format(R"(wipe --yes --quiet --directory "{}")", Target.string())); + + CHECK(Result.ExitCode == 0); +} + +TEST_SUITE_END(); + +} // namespace zen::tests + +#endif -- cgit v1.2.3