Files
buzz/crates/buzz-test-client/tests/e2e_acceptance.rs
Luke Tornquist 58695fe376 acceptance: read the probe key from a file; dial a connect address (#9773)
## Summary

Follows https://github.com/block/buzz/pull/9771 (merged), which added
the acceptance CronJob.

Some NIP-FI issuers bind each key to one identity. A deployment that
mints a new identity for each run then needs a new key for each run,
written next to the assertion before the probe starts. The probe could
only take its key from an argument or the environment, which a
Kubernetes init container can't set.

- **Probe:** `--private-key-file` (`BUZZ_PRIVATE_KEY_FILE`) reads the
key (hex or nsec) from a file. It conflicts with `--private-key`.
- **Shared read:** the key file and the assertion file use one bounded
reader, `acceptance::read_secret_file`. It needs a regular, non-empty
file under a size cap.
- **Report:** the JSON report now includes `pubkey`, the hex key the
probe ran as. An operator can match a run to the identity that was
minted for it.
- **Chart:** `acceptance.privateKeyFile` is passed as
`--private-key-file`, not as an env var, so a probe that predates the
flag fails instead of running with a new key. Exactly one of it and
`acceptance.privateKeySecret.name` must be set; rendering fails with
neither or both.
- **A new key per run needs an agent assertion** (`nip-fi-agent+jwt`
whose `nostr_owner_pubkey` is a relay member). A members-only relay
refuses a new person key, and an open relay joins it to every auto-join
channel. The README and `values.yaml` say so.
- **Connect address:** `--connect-addr` (`BUZZ_ACCEPTANCE_CONNECT_ADDR`)
dials a plaintext `host:port` for every WebSocket connection and for the
NIP-11 fetch, for a probe that can't reach the relay's public host. The
relay URL still sets the `Host` header, the NIP-42 relay tag and the
NIP-11 `Host`, so the relay serves the same community. On that path,
NIP-11 uses no proxy, follows no redirect and requires a 2xx answer. The
chart passes `acceptance.connectAddr` as `--connect-addr`, not as an env
var, so a probe that predates the flag fails the run instead of checking
the public URL. The default is empty.
`NostrWsConnection::connect_to_addr` now takes upgrade headers, so the
NIP-FI assertion goes on this path too; loadgen passes none. (Folded in
from #9830.)
- **Chart version 0.1.13** with an `artifacthub.io/changes` entry. It
publishes the acceptance CronJob from #9771, the key file and the
connect address.

- **Follow-up test from the #9767 review:** the fake relay gets an
`accept_mismatched` setting, and
`access_control_fails_when_the_relay_accepts_another_keys_write` expects
`access_control` to fail at `pubkey_mismatch_rejected`. A
`pubkey_mismatch_rejected` stage that always returns Ok now fails a
test.

## What got simpler

The assertion reader's inline bounded read is now the shared
`read_secret_file`, so both secret files follow one rule instead of two
copies of it. The probe's four connection call sites now share one
`open` helper, and `connect_to_addr` reuses the same `upgrade_request`
path as `connect_with_headers`.

## Testing

- `e2e_acceptance`:
`the_binary_authenticates_with_the_key_from_its_key_file` passes against
a live relay with the key path in `BUZZ_PRIVATE_KEY_FILE` (the same clap
argument as `--private-key-file`). It checks that the report's `pubkey`
is the file's key, so a probe that ignored the file and made a new key
fails.
- `e2e_acceptance`: `the_binary_exits_2_when_it_cannot_run` covers a bad
key, a bad key file, a missing key file, and both key flags at once.
Each exits 2 with no report and no metrics.
- Existing unit tests cover the shared reader through the assertion file
(missing, empty, oversized, directory, FIFO).
- `connect_addr_dials_the_address_and_keeps_the_relay_url`: the probe
runs at an unresolvable `wss://relay.invalid` with the fake relay's
address. Every upgrade has `Host: relay.invalid` and the assertion's
bearer header, and every AUTH relay tag is `wss://relay.invalid`.
- `nip11_goes_to_the_connect_addr_with_the_relay_host` (`GET
/relay?route=probe`, `Host: relay.invalid:8443`) and
`nip11_at_the_connect_addr_does_not_follow_a_redirect` (a 301 fails
`nip11` with no version).
- `caller_user_agent_replaces_default` now covers `connect_to_addr` too.
- `tests/acceptance_test.yaml`: `acceptance.connectAddr` renders
`--connect-addr <addr>` in the args, and the default args do not have
it. The key file renders as `--private-key-file <path>` in the args,
with neither `BUZZ_PRIVATE_KEY_FILE` nor `BUZZ_PRIVATE_KEY`, a Secret
plus a file fails, and no key source fails.

🤖 Generated with [Claude Code](https://claude.com/claude-code)



buzz-review-completed

---------

Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:27:04 -05:00

233 lines
7.9 KiB
Rust

//! The `buzz-acceptance` probe against a running relay: every check passes,
//! through the shared runner and through the binary.
//!
//! Requires a running relay, so it is `#[ignore]`d. Start the relay, then run:
//!
//! ```text
//! RELAY_URL=ws://localhost:3000 cargo test -p buzz-test-client --test e2e_acceptance -- --ignored
//! ```
use std::io::Read;
use std::net::UdpSocket;
use std::process::{Command, Output, Stdio};
use std::time::{Duration, Instant};
use buzz_test_client::acceptance::{self, Config, Outcome, CHECKS};
use nostr::Keys;
fn relay_url() -> String {
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".into())
}
#[tokio::test]
#[ignore]
async fn every_acceptance_check_passes_against_a_live_relay() {
let report = acceptance::run(&Config {
relay_url: relay_url(),
connect_addr: None,
keys: Keys::generate(),
assertion_file: None,
stage_timeout: Duration::from_secs(10),
})
.await;
let summary = serde_json::to_string_pretty(&report).unwrap();
assert!(report.passed, "{summary}");
let names: Vec<&str> = report.checks.iter().map(|c| c.check).collect();
assert_eq!(names, CHECKS, "{summary}");
for check in &report.checks {
assert_eq!(check.result, Outcome::Success, "{summary}");
}
assert!(report.relay_version.is_some(), "{summary}");
}
/// Runs the binary with `args`, metrics sent to a local listener. Returns
/// its output and every datagram that arrived.
fn probe(args: &[&str]) -> (Output, Vec<String>) {
probe_with_env(args, &[])
}
/// [`probe`] with extra environment variables. The child inherits none of the
/// probe's own settings from the test's environment.
fn probe_with_env(args: &[&str], envs: &[(&str, &str)]) -> (Output, Vec<String>) {
let agent = UdpSocket::bind("127.0.0.1:0").unwrap();
agent
.set_read_timeout(Some(Duration::from_millis(500)))
.unwrap();
let addr = agent.local_addr().unwrap().to_string();
let mut child = Command::new(env!("CARGO_BIN_EXE_buzz-acceptance"))
.args(args)
.args(["--statsd-addr", &addr, "--tag", "env:test"])
.env_remove("BUZZ_PRIVATE_KEY")
.env_remove("BUZZ_PRIVATE_KEY_FILE")
.env_remove("BUZZ_ACCEPTANCE_ASSERTION_FILE")
.env_remove("BUZZ_ACCEPTANCE_STATSD_ADDR")
.env_remove("BUZZ_ACCEPTANCE_CONNECT_ADDR")
.env_remove("BUZZ_RELAY_URL")
.envs(envs.iter().copied())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
// Drain both pipes while the child runs, so a chatty child cannot fill a
// pipe and stall before the deadline.
let stdout = drain(child.stdout.take().unwrap());
let stderr = drain(child.stderr.take().unwrap());
let deadline = Instant::now() + Duration::from_secs(120);
let status = loop {
if let Some(status) = child.try_wait().unwrap() {
break status;
}
if Instant::now() > deadline {
let _ = child.kill();
let _ = child.wait();
panic!("buzz-acceptance did not exit within 120 s");
}
std::thread::sleep(Duration::from_millis(50));
};
let output = Output {
status,
stdout: stdout.join().unwrap(),
stderr: stderr.join().unwrap(),
};
let mut lines = Vec::new();
let mut buf = [0u8; 2048];
while let Ok(n) = agent.recv(&mut buf) {
lines.push(String::from_utf8_lossy(&buf[..n]).into_owned());
}
(output, lines)
}
/// Reads `pipe` to its end on a thread, keeping at most 4 MiB.
fn drain(pipe: impl Read + Send + 'static) -> std::thread::JoinHandle<Vec<u8>> {
std::thread::spawn(move || {
let mut pipe = pipe;
let mut kept = Vec::new();
let _ = (&mut pipe).take(4 << 20).read_to_end(&mut kept);
let _ = std::io::copy(&mut pipe, &mut std::io::sink());
kept
})
}
fn report(output: &Output) -> serde_json::Value {
serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {
panic!(
"stdout is not a JSON report ({e}): {}",
String::from_utf8_lossy(&output.stdout)
)
})
}
#[test]
#[ignore]
fn the_binary_passes_against_a_live_relay_and_sends_metrics() {
let (output, lines) = probe(&["--relay-url", &relay_url()]);
let report = report(&output);
assert_eq!(output.status.code(), Some(0), "{report}");
assert_eq!(report["passed"], true);
assert!(
lines.iter().any(
|l| l.starts_with("buzz.acceptance.runs:1|c|#result:success,")
&& l.ends_with(",env:test")
),
"{lines:?}"
);
assert!(
lines.iter().any(|l| l.starts_with(
"buzz.acceptance.check.runs:1|c|#check:fanout,result:success,stage:none,"
)),
"{lines:?}"
);
}
#[test]
#[ignore]
fn the_binary_exits_1_and_reports_when_a_check_fails() {
let (output, lines) = probe(&[
"--relay-url",
"ws://127.0.0.1:1",
"--stage-timeout-secs",
"2",
]);
let report = report(&output);
assert_eq!(output.status.code(), Some(1), "{report}");
assert_eq!(report["passed"], false);
assert_eq!(report["checks"][1]["failed_stage"], "ws_connect");
assert!(
lines
.iter()
.any(|l| l == "buzz.acceptance.runs:1|c|#result:failure,env:test"),
"{lines:?}"
);
}
#[test]
#[ignore]
fn the_binary_exits_2_when_it_cannot_run() {
let dir = std::env::temp_dir().join(format!("acceptance-key-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let bad_key_file = dir.join("bad-key");
std::fs::write(&bad_key_file, "not-a-key\n").unwrap();
let bad_key_file = bad_key_file.to_str().unwrap();
let missing_key_file = dir.join("missing");
let missing_key_file = missing_key_file.to_str().unwrap();
let relay = relay_url();
// Each case must stop before the run: no report, no metrics.
let cases: [(&[&str], &str); 4] = [
(&["--private-key", "not-a-key"], "private key:"),
// The key file is read and parsed, not ignored for a new key.
(&["--private-key-file", bad_key_file], "private key:"),
(
&["--private-key-file", missing_key_file],
"read private key file",
),
(
&[
"--private-key",
"not-a-key",
"--private-key-file",
bad_key_file,
],
"cannot be used with",
),
];
for (extra, error) in cases {
let mut args = vec!["--relay-url", relay.as_str()];
args.extend_from_slice(extra);
let (output, lines) = probe(&args);
let stderr = String::from_utf8_lossy(&output.stderr);
assert_eq!(output.status.code(), Some(2), "{extra:?}: {stderr}");
assert!(stderr.contains(error), "{extra:?}: {stderr}");
assert!(output.stdout.is_empty(), "{extra:?}");
assert!(lines.is_empty(), "{extra:?}: {lines:?}");
}
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
#[ignore]
fn the_binary_authenticates_with_the_key_from_its_key_file() {
let dir = std::env::temp_dir().join(format!("acceptance-key-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let key_file = dir.join("key");
let keys = Keys::generate();
std::fs::write(
&key_file,
format!("{}\n", keys.secret_key().to_secret_hex()),
)
.unwrap();
// The chart passes the path through the environment, so this does too.
let (output, _) = probe_with_env(
&["--relay-url", &relay_url()],
&[("BUZZ_PRIVATE_KEY_FILE", key_file.to_str().unwrap())],
);
let report = report(&output);
assert_eq!(output.status.code(), Some(0), "{report}");
assert_eq!(report["passed"], true);
// A probe that ignored the file would run as a new key.
assert_eq!(report["pubkey"], keys.public_key().to_hex(), "{report}");
std::fs::remove_dir_all(dir).unwrap();
}