Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/scylla-graphql/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
}
};
// Announce the address actually bound, so `host:0` (an OS-picked port) is usable.
let addr = server
.server_addr()
.to_ip()
.map_or(addr, |bound| bound.to_string());

let context = Context::new(session);
let root = schema();
Expand Down
38 changes: 5 additions & 33 deletions crates/scylla-graphql/tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,19 @@
//! introspection, and fail-closed error surfacing (a rejected mutation is a GraphQL `errors`, not a
//! silent `ok: false`).

use std::net::TcpListener;
use std::process::{Child, Command, Stdio};
mod common;

use std::ffi::OsStr;
use std::time::{Duration, Instant};

use scylla_port::{Session, Zoom};

const ARTIFACT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib.scylla"
);

fn free_port() -> u16 {
TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port()
}

struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
use common::ARTIFACT;

#[test]
fn token_gates_and_the_write_path_round_trips() {
let port_num = free_port();
let addr = format!("127.0.0.1:{port_num}");
let (_srv, addr) = common::spawn_server(&[("SCYLLA_GRAPHQL_TOKEN", OsStr::new("secret"))]);
let base = format!("http://{addr}");
let _srv = Server(
Command::new(env!("CARGO_BIN_EXE_scylla-graphql"))
.args([ARTIFACT, &addr])
.env("SCYLLA_GRAPHQL_TOKEN", "secret")
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn scylla-graphql"),
);

// Wait until serving — readiness probe must carry the bearer, since every request is gated.
let deadline = Instant::now() + Duration::from_secs(10);
Expand Down
58 changes: 58 additions & 0 deletions crates/scylla-graphql/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Shared spawn helper for the `scylla-graphql` integration tests.
//!
//! The server is started on `127.0.0.1:0`, so the OS hands it a port nothing else holds, and the
//! test reads the address it actually bound from its startup line. Picking a "free" port in the
//! test and passing it in raced: the port was released before the server bound it, so a parallel
//! test's server could take it and answer in its place.

use std::ffi::OsStr;
use std::io::{BufRead, BufReader};
use std::net::SocketAddr;
use std::process::{Child, Command, Stdio};

pub const ARTIFACT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib.scylla"
);

/// A spawned server, killed on drop (pass or fail).
pub struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}

/// Start `scylla-graphql` on `ARTIFACT` with these extra environment variables, and return it
/// with the `host:port` it bound. The server is listening by the time this returns.
pub fn spawn_server(envs: &[(&str, &OsStr)]) -> (Server, String) {
let mut child = Command::new(env!("CARGO_BIN_EXE_scylla-graphql"))
.args([ARTIFACT, "127.0.0.1:0"])
.envs(envs.iter().copied())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.expect("spawn scylla-graphql");
let stderr = child.stderr.take().expect("piped stderr");
let server = Server(child);

// The startup line reads `scylla-graphql: <artifact> on <scheme>://<host:port>/graphql — ...`.
let mut lines = BufReader::new(stderr).lines();
let addr = loop {
let line = lines
.next()
.expect("scylla-graphql exited before announcing its address")
.expect("read scylla-graphql stderr");
let announced = line
.split_once("/graphql — ")
.and_then(|(head, _)| head.rsplit_once("://"))
.and_then(|(_, addr)| addr.parse::<SocketAddr>().ok());
if let Some(addr) = announced {
break addr.to_string();
}
};
// Keep draining stderr so the server never blocks on a full pipe.
std::thread::spawn(move || lines.for_each(drop));
(server, addr)
}
37 changes: 5 additions & 32 deletions crates/scylla-graphql/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,21 @@
//! verb. Expectations are derived from the port in-process; no frozen golden numbers. If the head
//! drifts from the body, these fail.

use std::net::TcpListener;
use std::process::{Child, Command, Stdio};
mod common;

use std::time::{Duration, Instant};

use base64::engine::general_purpose::STANDARD as B64;
use base64::Engine;
use scylla_port::{Session, Zoom};

const ARTIFACT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib.scylla"
);
use common::ARTIFACT;

const PATCHED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib_patched.scylla"
);

fn free_port() -> u16 {
TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port()
}

struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}

/// The port, loaded the same way the head loads it — the source of truth for the contract.
fn port(path: &str) -> Session {
Session::from_artifact(&std::fs::read(path).expect("read artifact")).expect("load artifact")
Expand All @@ -47,17 +29,8 @@ fn graphql_head_conforms_to_the_port() {
let p = port(ARTIFACT);
let prog = p.program();

let port_num = free_port();
let addr = format!("127.0.0.1:{port_num}");
let (_srv, addr) = common::spawn_server(&[]);
let base = format!("http://{addr}");
let _srv = Server(
Command::new(env!("CARGO_BIN_EXE_scylla-graphql"))
.args([ARTIFACT, &addr])
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn scylla-graphql"),
);

// Wait until it's serving.
let deadline = Instant::now() + Duration::from_secs(10);
Expand Down
58 changes: 58 additions & 0 deletions crates/scylla-rpc/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Shared spawn helper for the `scylla-rpc` integration tests.
//!
//! The server is started on `127.0.0.1:0`, so the OS hands it a port nothing else holds, and the
//! test reads the address it actually bound from its startup line. Picking a "free" port in the
//! test and passing it in raced: the port was released before the server bound it, so a parallel
//! test's server could take it and answer in its place.

use std::ffi::OsStr;
use std::io::{BufRead, BufReader};
use std::net::SocketAddr;
use std::process::{Child, Command, Stdio};

pub const ARTIFACT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib.scylla"
);

/// A spawned server, killed on drop (pass or fail).
pub struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}

/// Start `scylla-rpc-serve` on `ARTIFACT` with these extra environment variables, and return it
/// with the `host:port` it bound. The server is listening by the time this returns.
pub fn spawn_server(envs: &[(&str, &OsStr)]) -> (Server, String) {
let mut child = Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve"))
.args([ARTIFACT, "127.0.0.1:0"])
.envs(envs.iter().copied())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.expect("spawn scylla-rpc-serve");
let stderr = child.stderr.take().expect("piped stderr");
let server = Server(child);

// The startup line reads `scylla-rpc-serve: <artifact> on <host:port> — <auth>, ...`.
let mut lines = BufReader::new(stderr).lines();
let addr = loop {
let line = lines
.next()
.expect("scylla-rpc-serve exited before announcing its address")
.expect("read scylla-rpc-serve stderr");
let announced = line
.split_once(" — ")
.and_then(|(head, _)| head.rsplit_once(" on "))
.and_then(|(_, addr)| addr.parse::<SocketAddr>().ok());
if let Some(addr) = announced {
break addr.to_string();
}
};
// Keep draining stderr so the server never blocks on a full pipe.
std::thread::spawn(move || lines.for_each(drop));
(server, addr)
}
38 changes: 6 additions & 32 deletions crates/scylla-rpc/tests/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,20 @@
//! the same artifact — verb by verb. Expectations are derived from the port in-process; no frozen
//! golden numbers (those live in `remote.rs`). A head drifting from the body fails here.

use std::net::TcpListener;
use std::process::{Child, Command, Stdio};
mod common;

use std::process::Command;
use std::time::{Duration, Instant};

use scylla_port::{Session, Zoom};

const ARTIFACT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib.scylla"
);
use common::ARTIFACT;

const PATCHED: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../scylla-wasm/web/mathlib_patched.scylla"
);

fn free_port() -> u16 {
TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port()
}

struct Server(Child);
impl Drop for Server {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}

/// Run the remote client (no token — the server runs open here), returning `(exit, stdout)`.
fn connect(addr: &str, args: &[&str]) -> (i32, String) {
let mut full = vec![addr];
Expand Down Expand Up @@ -80,16 +63,7 @@ fn rpc_head_conforms_to_the_port() {
let p = port(ARTIFACT);
let prog = p.program();

let pnum = free_port();
let addr = format!("127.0.0.1:{pnum}");
let _srv = Server(
Command::new(env!("CARGO_BIN_EXE_scylla-rpc-serve"))
.args([ARTIFACT, &addr])
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("spawn scylla-rpc-serve"),
);
let (_srv, addr) = common::spawn_server(&[]);

// Wait until the server accepts (info round-trips).
let deadline = Instant::now() + Duration::from_secs(10);
Expand Down
Loading
Loading