Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(saas_mode): share core startup helper and log tail

Steven Enamakel262a25177e85
diff --git a/tests/saas_mode/cluster.rs b/tests/saas_mode/cluster.rs
index e43769de97..28e0ae8def 100644
--- a/tests/saas_mode/cluster.rs
+++ b/tests/saas_mode/cluster.rs
@@ -42,6 +42,17 @@ fn hanging_backend() -> u16 {
port
}
+/// The last lines of a core's log, without the polling RPCs that drown
+/// everything else out.
+fn log_tail_of(path: &Path) -> String {
+ let log = std::fs::read_to_string(path).unwrap_or_default();
+ let lines: Vec<&str> = log
+ .lines()
+ .filter(|line| !line.contains("rpc_handler [rpc]"))
+ .collect();
+ lines[lines.len().saturating_sub(80)..].join("\n")
+}
+
/// One core of a deployment.
struct Node {
name: String,
@@ -52,13 +63,7 @@ struct Node {
impl Node {
fn log_tail(&self) -> String {
- let log = std::fs::read_to_string(&self.log).unwrap_or_default();
- // The polling RPCs drown everything else out.
- let lines: Vec<&str> = log
- .lines()
- .filter(|line| !line.contains("rpc_handler [rpc]"))
- .collect();
- lines[lines.len().saturating_sub(80)..].join("\n")
+ log_tail_of(&self.log)
}
fn kill(&mut self) {
@@ -76,72 +81,56 @@ fn start_node(
extra: &str,
backend: Option<u16>,
) -> Node {
- let port = free_port();
- let base = format!("http://127.0.0.1:{port}");
- let mut config = format!(
- "root = {:?}\nnode_id = \"core-{name}\"\noperator_dir = {:?}\nlease_ttl_secs = 3\n",
- d.root.display().to_string(),
- d.tmp
- .path()
- .join(format!("operator-{name}"))
- .display()
- .to_string(),
- );
- if let Some(url) = storage_url {
- config.push_str(&format!(
- "storage_url = {url:?}\nadvertise_url = {base:?}\n"
- ));
- }
- config.push_str(extra);
- let config_path = d.tmp.path().join(format!("operator-{name}.toml"));
- std::fs::write(&config_path, config).unwrap();
-
let log = d.tmp.path().join(format!("core-{name}.log"));
- let mut cmd = Command::new(env!("CARGO_BIN_EXE_openhuman-core"));
- cmd.args(["run", "--mode", "saas", "--saas-config"])
- .arg(&config_path)
- .args(["--port", &port.to_string()])
- .env("HOME", d.tmp.path())
- .env("USERPROFILE", d.tmp.path())
- .env("RUST_LOG", "info,openhuman::storage::lease=debug")
- .stdout(std::fs::File::create(&log).unwrap())
- .stderr(Stdio::null());
- for var in SCRUBBED_ENV {
- cmd.env_remove(var);
- }
- match backend {
- Some(port) => cmd.env("BACKEND_URL", format!("http://127.0.0.1:{port}")),
- None => cmd.env("BACKEND_URL", "http://127.0.0.1:9"),
- };
- let child: Child = cmd.spawn().expect("spawn openhuman-core");
- let mut node = Node {
+ let config_path = d.tmp.path().join(format!("operator-{name}.toml"));
+ let (server, base) = spawn_core_with(
+ |port| {
+ // The operator file names the port (`advertise_url`), so it is
+ // written again for each port tried.
+ let base = format!("http://127.0.0.1:{port}");
+ let mut config = format!(
+ "root = {:?}\nnode_id = \"core-{name}\"\noperator_dir = {:?}\nlease_ttl_secs = 3\n",
+ d.root.display().to_string(),
+ d.tmp
+ .path()
+ .join(format!("operator-{name}"))
+ .display()
+ .to_string(),
+ );
+ if let Some(url) = storage_url {
+ config.push_str(&format!(
+ "storage_url = {url:?}\nadvertise_url = {base:?}\n"
+ ));
+ }
+ config.push_str(extra);
+ std::fs::write(&config_path, config).unwrap();
+
+ let mut cmd = Command::new(env!("CARGO_BIN_EXE_openhuman-core"));
+ cmd.args(["run", "--mode", "saas", "--saas-config"])
+ .arg(&config_path)
+ .args(["--port", &port.to_string()])
+ .env("HOME", d.tmp.path())
+ .env("USERPROFILE", d.tmp.path())
+ .env("RUST_LOG", "info,openhuman::storage::lease=debug")
+ .stdout(std::fs::File::create(&log).unwrap())
+ .stderr(Stdio::null());
+ for var in SCRUBBED_ENV {
+ cmd.env_remove(var);
+ }
+ match backend {
+ Some(port) => cmd.env("BACKEND_URL", format!("http://127.0.0.1:{port}")),
+ None => cmd.env("BACKEND_URL", "http://127.0.0.1:9"),
+ };
+ cmd
+ },
+ || format!("core {name}:\n{}", log_tail_of(&log)),
+ );
+ Node {
name: name.to_string(),
- server: Server(child),
+ server,
base,
log,
- };
- let client = client();
- let deadline = Instant::now() + Duration::from_secs(120);
- while !client
- .get(format!("{}/health", node.base))
- .send()
- .is_ok_and(|r| r.status().is_success())
- {
- if let Ok(Some(status)) = node.server.0.try_wait() {
- panic!(
- "core {} exited before serving: {status}\n{}",
- node.name,
- node.log_tail()
- );
- }
- assert!(
- Instant::now() < deadline,
- "core {} never became healthy",
- name
- );
- std::thread::sleep(Duration::from_millis(250));
}
- node
}
fn client() -> reqwest::blocking::Client {
diff --git a/tests/saas_mode_e2e.rs b/tests/saas_mode_e2e.rs
index d8d3b0ea48..d4f5d50df6 100644
--- a/tests/saas_mode_e2e.rs
+++ b/tests/saas_mode_e2e.rs
@@ -222,7 +222,15 @@ fn wait_until_serving(
/// ago: a parallel test's core (or any socket) can take it before this core
/// binds, and the core then silently listens elsewhere. When another process
/// answers, this core is killed and started again on a fresh port.
-fn spawn_core(mut command: impl FnMut(u16) -> Command) -> (Server, String) {
+fn spawn_core(command: impl FnMut(u16) -> Command) -> (Server, String) {
+ spawn_core_with(command, String::new)
+}
+
+/// [`spawn_core`], appending `context()` (a log tail) to a startup failure.
+fn spawn_core_with(
+ mut command: impl FnMut(u16) -> Command,
+ context: impl Fn() -> String,
+) -> (Server, String) {
const ATTEMPTS: usize = 5;
let client = reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(10))
@@ -241,7 +249,7 @@ fn spawn_core(mut command: impl FnMut(u16) -> Command) -> (Server, String) {
"[saas-e2e] port {port} is held by pid {pid:?}, not our core; retrying ({attempt}/{ATTEMPTS})"
);
}
- Err(error) => panic!("{error}"),
+ Err(error) => panic!("{error}\n{}", context()),
}
}
panic!("no free port held for a SaaS core after {ATTEMPTS} attempts");