Files
harmony/fleet/harmony-fleet-e2e/tests/ping.rs

71 lines
2.3 KiB
Rust

//! TDD anchor test: an in-cluster agent answers `Verb::Ping` over
//! NATS request/reply, end-to-end. First green test for the
//! `device-commands.*` protocol.
//!
//! Bring-up runs **once** for the whole test binary via
//! `harmony_fleet_e2e::shared_stack` (the same `OnceCell` pattern
//! `fleet_e2e_demo`'s walking skeleton uses). Cleanup is best-effort
//! at process exit; per-bring-up namespace is unique so concurrent
//! test runs don't collide.
//!
//! Skipped automatically when `HARMONY_FLEET_E2E=1` is not set — this
//! keeps `cargo test --workspace` cheap on machines without
//! k3d/podman. Run explicitly:
//!
//! ```bash
//! HARMONY_FLEET_E2E=1 cargo test -p harmony-fleet-e2e --test ping
//! ```
use std::time::Duration;
use harmony_fleet_e2e::{StackOptions, shared_stack};
use harmony_fleet_operator::commands::FleetCommandsClient;
const E2E_ENV: &str = "HARMONY_FLEET_E2E";
fn e2e_enabled() -> bool {
matches!(std::env::var(E2E_ENV).as_deref(), Ok("1" | "true"))
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn operator_can_ping_agent() -> anyhow::Result<()> {
if !e2e_enabled() {
eprintln!(
"skipping {E2E_ENV}-gated e2e test (set {E2E_ENV}=1 to run; \
requires k3d + podman on PATH)"
);
return Ok(());
}
let _ = tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.try_init();
let stack = shared_stack(StackOptions::default()).await?;
stack.print_debug_info();
let device_id = stack.device_ids[0].clone();
let client = FleetCommandsClient::new(stack.nats_client.clone());
// Generous outer timeout — bring-up readiness is verified
// separately; this only guards against an agent that comes up
// but never subscribes.
let reply = tokio::time::timeout(Duration::from_secs(15), client.ping(&device_id))
.await
.map_err(|_| anyhow::anyhow!("ping outer timeout"))??;
assert_eq!(
reply.device_id.to_string(),
device_id,
"agent must report back its own device_id"
);
assert!(
!reply.agent_version.is_empty(),
"agent_version must be non-empty"
);
Ok(())
}