fix(test): isolate Valkey in the CI job network and bound readiness

This commit is contained in:
Hermes DevOps committed 2026-10-25 10:59:18 +00:00
1 parent e57ab47c05
commit c00e7d95bd
2 files changed
+96 -29

No files matched your search

@@ -1,18 +1,56 @@
use super::*; use super::*;
use std::process::Command; use std::{
env,
io::{Read, Write},
net::{TcpListener, TcpStream, ToSocketAddrs},
process::Command,
thread,
time::{Duration, Instant},
};
pub(super) struct TestValkey(String); pub(super) struct TestValkey(String);
impl TestValkey { impl TestValkey {
pub(super) fn start() -> (Self, u16) { pub(super) fn start() -> (Self, u16) {
let output = Command::new("docker") let in_docker_job = if std::path::Path::new("/.dockerenv").exists() {
.args([ let hostname = env::var("HOSTNAME").unwrap_or_default();
"run", assert!(
"--detach", matches!(hostname.len(), 12 | 64)
"--publish", && hostname.bytes().all(|byte| byte.is_ascii_hexdigit()),
"127.0.0.1::6379", "Docker job hostname must be a full 12- or 64-character container ID"
"valkey/valkey:8-alpine", );
]) Some(hostname)
} else {
None
};
let port = if in_docker_job.is_some() {
let listener = TcpListener::bind(("127.0.0.1", 0))
.expect("test-owned Valkey loopback port must be available");
let port = listener.local_addr().unwrap().port();
drop(listener);
Some(port)
} else {
None
};
let mut docker = Command::new("docker");
docker.args([
"run",
"--detach",
"--label",
"crank.test.fixture=static-resources-valkey",
]);
if let (Some(hostname), Some(port)) = (&in_docker_job, port) {
docker
.arg("--network")
.arg(format!("container:{hostname}"))
.arg("valkey/valkey:8-alpine")
.args(["valkey-server", "--port", &port.to_string()]);
} else {
docker.args(["--publish", "127.0.0.1::6379", "valkey/valkey:8-alpine"]);
}
let output = docker
.output() .output()
.expect("Docker CLI must start the test-owned Valkey container"); .expect("Docker CLI must start the test-owned Valkey container");
assert!( assert!(
@@ -25,22 +63,29 @@ impl TestValkey {
.trim() .trim()
.to_owned(); .to_owned();
let valkey = Self(id); let valkey = Self(id);
let port_output = Command::new("docker") let port = match port {
.args(["port", &valkey.0, "6379/tcp"]) Some(port) => port,
.output() None => {
.expect("Docker CLI must report the test-owned port"); let port_output = Command::new("docker")
assert!( .args(["port", &valkey.0, "6379/tcp"])
port_output.status.success(), .output()
"test-owned Valkey port must be available" .expect("Docker CLI must report the test-owned port");
); assert!(
let address = String::from_utf8(port_output.stdout).expect("Docker port must be UTF-8"); port_output.status.success(),
let port = address "test-owned Valkey port must be available"
.trim() );
.rsplit(':') let address =
.next() String::from_utf8(port_output.stdout).expect("Docker port must be UTF-8");
.unwrap() address
.parse() .trim()
.expect("Docker mapped port must be numeric"); .rsplit(':')
.next()
.unwrap()
.parse()
.expect("Docker mapped port must be numeric")
}
};
wait_for_valkey(port);
(valkey, port) (valkey, port)
} }
@@ -53,6 +98,30 @@ impl TestValkey {
} }
} }
fn wait_for_valkey(port: u16) {
let address = ("127.0.0.1", port)
.to_socket_addrs()
.expect("test-owned Valkey loopback address must resolve")
.next()
.unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
if let Ok(mut stream) = TcpStream::connect_timeout(&address, Duration::from_millis(250)) {
let _ = stream.set_read_timeout(Some(Duration::from_millis(250)));
let _ = stream.set_write_timeout(Some(Duration::from_millis(250)));
let mut reply = [0_u8; 7];
if stream.write_all(b"*1\r\n$4\r\nPING\r\n").is_ok()
&& stream.read_exact(&mut reply).is_ok()
&& &reply == b"+PONG\r\n"
{
return;
}
}
thread::sleep(Duration::from_millis(100));
}
panic!("test-owned Valkey did not answer PING on 127.0.0.1:{port} within 10 seconds");
}
impl Drop for TestValkey { impl Drop for TestValkey {
fn drop(&mut self) { fn drop(&mut self) {
let _ = Command::new("docker").args(["rm", "-f", &self.0]).output(); let _ = Command::new("docker").args(["rm", "-f", &self.0]).output();
+2 -4
View File
@@ -29,9 +29,7 @@ run_atdd() {
return 1 return 1
} }
# Accepted recovery scope: the story ATDD published by this branch is Story 3.16 (composite # Keep Story 3.16 prompt ATDD with Story 3.17 completion and its bound-template regression.
# Prompts with published Resources) and Story 3.17 (bounded MCP completion and the resources/list
# admission of a bound template). Add only AC-required affected security/contract cases.
run_atdd "MCP prompt composition" -p mcp-server --test integration mcp_prompts::composition -- --test-threads=1 run_atdd "MCP prompt composition" -p mcp-server --test integration mcp_prompts::composition -- --test-threads=1
run_atdd "MCP completion" -p mcp-server --test integration mcp_completion -- --test-threads=1 run_atdd "MCP completion" -p mcp-server --test integration mcp_completion -- --test-threads=1
run_atdd "static resource list" -p mcp-server --test integration static_resources::list -- --test-threads=1 run_atdd "bound template resources/list regression" -p mcp-server --test integration static_resources::list::list_stays_available_when_a_published_template_is_bound -- --test-threads=1