Files
openwaf/dotnet/OpenWaf.Tests.Integration/AotJsonContractTests.cs
T
Pavlo Ratushnyi 0a3d28184f
ci / lint the workflow and the shell (push) Failing after 54s
ci / build, test, smoke (push) Successful in 6m4s
ci / the operator page in a real browser (push) Failing after 4m53s
ci / build the image and smoke it (push) Failing after 10m29s
ci / the same smoke on an image with no base (push) Failing after 9m2s
the aot edge serves traffic now, and the shape still missing a contract was its own healthcheck
Measured on this tree with scripts/aot-probe.sh (osx-arm64, a 49,609,680-byte
native binary, 10 IL warnings, 0 errors): the AOT edge reaches "Now listening
on:", creates its 12 tables from the DDL it carries, proxies a request to the
upstream for 200, and its own --healthcheck mode exits 0. The API wall that
stood between the first cut and a serving edge came down in two edits, and the
json-refusal count went 55 -> 1 -> 0.

The 55 were one shape. The anonymous 8-property /healthz literal had no
compiled contract, so the edge was serving traffic while the endpoint a scratch
container's HEALTHCHECK asks answered 500 - an edge that serves and is restarted
anyway. Health/HealthDtos.cs names it (HealthDto, ReadinessDto). The 1 after
that was Microsoft.AspNetCore.Mvc.ProblemDetails, the body Results.Problem
writes and nobody's DTO in this project: its visible cost was a /readyz 500
naming nothing where a 503 naming the failure is the truth. It now says
"Query wasn't precompiled and dynamic code isn't supported with NativeAOT."

The probe's scoreboard is split the way the finding is. Three assertions that
need no query are gated (healthz 200, proxy 200, --healthcheck exit 0), and the
host must be listening with no response refused for want of a contract. Five
that do need a query are recorded with the answer a working edge would give -
operator block 503 where 204 is right, the streak ban never forms, /readyz 503
where 200 is right - and any of them that ever matches prints MOVED, which is
the signal that the query wall moved. queries refused at run time: 32 in that
window, 30 in the run before it; it counts a window, not a total, because the
background refresh loop retries on its own clock.

Two probe bugs surfaced doing this. It sent "Authorization: Bearer" to the admin
API, which AdminAuth does not read, so three of its assertions were measuring a
401 rather than the query wall; and it asserted nothing about whether the host
had listened at all.

Gates on this tree: build 0 warnings, unit 378/378, integration 408/408 in 51 s
(the new one demands a contract for ProblemDetails and ValidationProblemDetails),
smoke 256/256.

Two stale figures corrected rather than left standing. The registered-type count
was 24 in README and ARCHITECTURE while the file at that commit holds 25 -
CountrySummaryDto arrived without the count being re-derived - and it is 29 now.
The anonymous-literal count is 29 in 23 shapes: 22 response shapes, and 7 inside
an EF query's Select, which belong to the query wall rather than this one. The
"48.8 MB" native binary in ARCHITECTURE matches no measured binary of this tree.
2026-10-07 19:28:22 +02:00

100 lines
4.6 KiB
C#

using Microsoft.AspNetCore.Http.Metadata;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using OpenWaf.Edge.Admin;
namespace OpenWaf.Tests.Integration;
/// <summary>
/// The AOT half of the admin API, checked on the tree that still ships JIT.
///
/// Measured on 2026-10-06, the NativeAOT edge does not reach the query wall at
/// all: the host refuses to start with <c>JsonTypeInfo metadata for type
/// 'OpenWaf.Edge.Admin.NewBlockRequest' was not provided by TypeInfoResolver</c>
/// (3 refusals in the probe window, first type named). A JIT build never notices,
/// because it falls back to a reflection resolver that an AOT binary does not
/// have — so the failure is invisible here until it is a dead container there.
///
/// This test asks the running edge which body types its endpoints accept, and
/// demands a compiled contract for each one. It reads the answer from the live
/// endpoint table rather than from a list kept by hand, so an endpoint added
/// without a contract is red here and not in the weekly probe.
///
/// The count assertion is not decoration: a guard that walks an empty set passes,
/// and the empty set is exactly what a metadata change would produce. The number
/// below is the measured count of distinct request-body types this edge accepts.
/// </summary>
public sealed class AotJsonContractTests
{
private const int ExpectedBodyTypeCount = 5;
[Fact]
public async Task Every_request_body_the_edge_accepts_has_a_compiled_json_contract()
{
await using var f = new EdgeFixture(threshold: 9);
var source = f.Factory.Services.GetRequiredService<EndpointDataSource>();
var bodies = new HashSet<Type>();
foreach (var endpoint in source.Endpoints)
foreach (var metadata in endpoint.Metadata)
if (metadata is IAcceptsMetadata accepts && accepts.RequestType is { } body)
bodies.Add(body);
// The set has to be non-empty and the right size, or the assertion below
// is vacuous. 5: NewBlockRequest, NewAllowRequest, NewRuleRequest,
// NewRateRuleRequest, PolicyUpdateRequest — measured, not assumed.
Assert.Equal(ExpectedBodyTypeCount, bodies.Count);
var missing = bodies
.Where(t => EdgeJson.Default.GetTypeInfo(t) is null)
.Select(t => t.FullName)
.OrderBy(n => n, StringComparer.Ordinal)
.ToList();
Assert.True(missing.Count == 0,
"these request bodies have no source-generated contract, so a NativeAOT edge "
+ "will not start on them: " + string.Join(", ", missing));
}
[Fact]
public void The_problem_body_every_handler_falls_back_to_has_a_contract()
{
// Results.Problem is the error path of /readyz and of any handler that
// catches. It writes ProblemDetails, which is nobody's DTO in this
// project, so nothing else in the build asks for a contract for it — and
// a NativeAOT binary has no resolver to discover one. Measured
// 2026-10-07, after the /healthz shape came down: this was the single
// remaining JsonTypeInfo refusal in the probe window, and its visible
// effect was that a /readyz 503 explaining the failure became a 500
// explaining nothing.
Assert.NotNull(EdgeJson.Default.GetTypeInfo(typeof(Microsoft.AspNetCore.Mvc.ProblemDetails)));
Assert.NotNull(EdgeJson.Default.GetTypeInfo(typeof(Microsoft.AspNetCore.Mvc.ValidationProblemDetails)));
}
[Fact]
public void The_generated_contract_uses_the_wire_names_the_operator_reads()
{
// The camelCase generation option is load-bearing. Without it the same
// record answers {"Error": …} and every consumer of this API — the
// console, the smoke gate, the operator's scripts — reads a body with no
// error in it. Asserted on the contract itself, so the regression is
// caught without waiting for a page to go blank.
var info = EdgeJson.Default.GetTypeInfo(typeof(ErrorDto));
Assert.NotNull(info);
var names = info!.Properties
.Select(p => p.Name)
.OrderBy(n => n, StringComparer.Ordinal)
.ToList();
Assert.Equal(new[] { "error" }, names);
var refusal = EdgeJson.Default.GetTypeInfo(typeof(ReadOnlyRefusalDto));
Assert.NotNull(refusal);
Assert.Equal(
new[] { "error", "tokenLevel" },
refusal!.Properties.Select(p => p.Name)
.OrderBy(n => n, StringComparer.Ordinal)
.ToArray());
}
}