diff --git a/nix/vm-tests/k8s-services.nix b/nix/vm-tests/k8s-services.nix index 8d57816a3..fb6d7ff5d 100644 --- a/nix/vm-tests/k8s-services.nix +++ b/nix/vm-tests/k8s-services.nix @@ -1826,6 +1826,19 @@ pkgs.testers.runNixOSTest { body, _, code = out.rpartition("\n") return int(code or 0), body + def wait_gateway_metrics() -> None: + """Waits for Grafana's `/metrics` through the gateway to answer with the + credential in place. A reload is graceful: workers on the old + configuration (an empty include file, a missing one) go on answering + until they drain, so a request right after it can still get the 401.""" + deadline = time.monotonic() + 30 + while True: + status, body = grafana_request("/metrics") + if status == 200 and "grafana_" in body: + return + assert time.monotonic() < deadline, ("the gateway does not send the credential", status, body[:200]) + time.sleep(1) + def grafana_head(path: str, *args: str) -> tuple[str, str]: """The status and the redirect target of one request through the gateway.""" out = machine.succeed(f"curl -sS --max-time 20 -o /dev/null -w '%{{http_code}} %{{redirect_url}}' {' '.join(args)} '{GRAFANA_GATEWAY}{path}'") @@ -1950,8 +1963,7 @@ pkgs.testers.runNixOSTest { status, body = grafana_request("/metrics", f'--user "swarm-store:$(cat {GRAFANA_PASSWORD})"', gateway=False) assert status == 200 and "grafana_" in body, (status, body[:200]) # Through the gateway the same request needs no credential of the caller's. - status, body = grafana_request("/metrics") - assert status == 200 and "grafana_" in body, (status, body[:200]) + wait_gateway_metrics() # A gateway without the credential gets 401 from the pod. saved = machine.succeed(f"cat {GRAFANA_CONF}") assert "proxy_set_header Authorization" in saved @@ -1966,8 +1978,8 @@ pkgs.testers.runNixOSTest { machine.succeed("systemctl restart swarm-bao-store-auth.service") store_credential_published() machine.succeed("systemctl reload logs-gateway") - assert machine.succeed(f"cat {GRAFANA_CONF}") == saved - assert grafana_request("/metrics")[0] == 200 + assert machine.succeed(f"cat {GRAFANA_CONF}") == saved, "the unit wrote another include file than the first run did" + wait_gateway_metrics() # The health route and the health check of the kubelet stay open. assert grafana_request("/api/health", gateway=False)[0] == 200