diff --git a/apps/mcp-server/tests/integration/static_resources/list/template_discovery_binary.rs b/apps/mcp-server/tests/integration/static_resources/list/template_discovery_binary.rs index 29b35d4..1d62bb8 100644 --- a/apps/mcp-server/tests/integration/static_resources/list/template_discovery_binary.rs +++ b/apps/mcp-server/tests/integration/static_resources/list/template_discovery_binary.rs @@ -1,18 +1,56 @@ 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); impl TestValkey { pub(super) fn start() -> (Self, u16) { - let output = Command::new("docker") - .args([ - "run", - "--detach", - "--publish", - "127.0.0.1::6379", - "valkey/valkey:8-alpine", - ]) + let in_docker_job = if std::path::Path::new("/.dockerenv").exists() { + let hostname = env::var("HOSTNAME").unwrap_or_default(); + assert!( + matches!(hostname.len(), 12 | 64) + && hostname.bytes().all(|byte| byte.is_ascii_hexdigit()), + "Docker job hostname must be a full 12- or 64-character container ID" + ); + 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() .expect("Docker CLI must start the test-owned Valkey container"); assert!( @@ -25,22 +63,29 @@ impl TestValkey { .trim() .to_owned(); let valkey = Self(id); - let port_output = Command::new("docker") - .args(["port", &valkey.0, "6379/tcp"]) - .output() - .expect("Docker CLI must report the test-owned port"); - assert!( - port_output.status.success(), - "test-owned Valkey port must be available" - ); - let address = String::from_utf8(port_output.stdout).expect("Docker port must be UTF-8"); - let port = address - .trim() - .rsplit(':') - .next() - .unwrap() - .parse() - .expect("Docker mapped port must be numeric"); + let port = match port { + Some(port) => port, + None => { + let port_output = Command::new("docker") + .args(["port", &valkey.0, "6379/tcp"]) + .output() + .expect("Docker CLI must report the test-owned port"); + assert!( + port_output.status.success(), + "test-owned Valkey port must be available" + ); + let address = + String::from_utf8(port_output.stdout).expect("Docker port must be UTF-8"); + address + .trim() + .rsplit(':') + .next() + .unwrap() + .parse() + .expect("Docker mapped port must be numeric") + } + }; + wait_for_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 { fn drop(&mut self) { let _ = Command::new("docker").args(["rm", "-f", &self.0]).output(); diff --git a/scripts/ci-story-checks.sh b/scripts/ci-story-checks.sh index 258d888..8db715d 100755 --- a/scripts/ci-story-checks.sh +++ b/scripts/ci-story-checks.sh @@ -29,9 +29,7 @@ run_atdd() { return 1 } -# Accepted recovery scope: the story ATDD published by this branch is Story 3.16 (composite -# 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. +# Keep Story 3.16 prompt ATDD with Story 3.17 completion and its bound-template regression. 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 "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