//! Scripted Kubernetes apiserver and node-api doubles for substrate tests. //! //! The apiserver double answers each request from a test-supplied closure and //! records it. The node-api double serves `WorkerApi`, `WorkerApiInternal` and //! gRPC health on an ephemeral loopback port: methods a test scripts are //! answered from the script, every other method is forwarded to the node-api //! simulator (or to [`SequencedWakeWorker`] for `WorkerApiInternal`), and //! every call is recorded in one ordered log so tests can assert which node //! steps ran and in what order. use std::collections::HashMap; use std::convert::Infallible; use std::sync::atomic::AtomicUsize; use std::sync::{Arc, Mutex}; use std::task::{Context, Poll}; use axum::http::{self, HeaderMap, HeaderValue, Method, StatusCode}; use futures_util::future::BoxFuture; use http_body_util::BodyExt; use hyper::body::{Bytes, Frame}; use kube::client::{Body as KubeBody, ClientBuilder}; use pplx_space_node_api_client::proto::worker_api_internal_server::WorkerApiInternalServer; use pplx_space_node_api_client::proto::worker_api_server::WorkerApiServer; use pplx_space_node_api_sim::{SimState, SimWorkerApiService}; use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; use tonic::transport::{Server, server::TcpIncoming}; use super::tests::SequencedWakeWorker; use super::*; pub(super) type BoxError = Box; /// The Kubernetes node the scripted node-api reports as its own. pub(super) const WORKER_NODE: &str = "node-a"; // ── Scripted Kubernetes apiserver ─────────────────────────────────────────── #[derive(Debug, Clone)] pub(crate) struct KubeRequest { pub(crate) method: Method, pub(crate) path: String, /// The query string, when the request has one. pub(crate) query: Option, pub(crate) body: Option, } pub(crate) enum KubeReply { Json(StatusCode, serde_json::Value), /// A watch that sends these `{"type": ..., "object": ...}` events, then /// ends. WatchEvents(Vec), /// An HTTP 200 watch stream that stays open and yields newline-delimited /// watch frames as the test sends them, ending when the sender drops. WatchStream(tokio::sync::mpsc::UnboundedReceiver), /// Never answer, for callers that bound the read with their own budget. Hang, } pub(crate) type KubeLog = Arc>>; /// A kube client whose every request is recorded and answered by `reply`. pub(crate) fn scripted_kube_client( reply: impl Fn(&KubeRequest) -> KubeReply + Send + Sync + 'static, ) -> (Client, KubeLog) { scripted_kube_client_async(move |request| std::future::ready(reply(&request))) } /// [`scripted_kube_client`] for replies that must first observe or change /// other test state, such as the database, at the moment a request arrives. pub(crate) fn scripted_kube_client_async(reply: F) -> (Client, KubeLog) where F: Fn(KubeRequest) -> Fut + Send + Sync + 'static, Fut: std::future::Future + Send + 'static, { let log: KubeLog = Arc::default(); let reply = Arc::new(reply); let service_log = Arc::clone(&log); let service = tower::service_fn(move |req: http::Request| { let log = Arc::clone(&service_log); let reply = Arc::clone(&reply); async move { let method = req.method().clone(); let path = req.uri().path().to_string(); let query = req.uri().query().map(str::to_owned); let bytes = req.into_body().collect().await?.to_bytes(); let body = (!bytes.is_empty()) .then(|| serde_json::from_slice(&bytes)) .transpose()?; let request = KubeRequest { method, path, query, body, }; log.lock().unwrap().push(request.clone()); let once_body = |bytes: Vec| { http_body_util::Full::new(Bytes::from(bytes)) .map_err(|never: Infallible| -> BoxError { match never {} }) .boxed_unsync() }; match reply(request).await { KubeReply::Json(status, body) => Ok::<_, BoxError>( http::Response::builder() .status(status) .header("content-type", "application/json") .body(once_body(serde_json::to_vec(&body)?))?, ), KubeReply::WatchEvents(events) => { let mut lines = Vec::new(); for event in &events { serde_json::to_writer(&mut lines, event)?; lines.push(b'\n'); } Ok(http::Response::builder() .status(StatusCode::OK) .header("content-type", "application/json") .body(once_body(lines))?) } KubeReply::WatchStream(rx) => { let stream = futures_util::stream::unfold(rx, |mut rx| async move { let event = rx.recv().await?; let frame = serde_json::to_vec(&event) .map(|mut line| { line.push(b'\n'); Frame::data(Bytes::from(line)) }) .map_err(Into::::into); Some((frame, rx)) }); Ok(http::Response::builder() .status(StatusCode::OK) .header("content-type", "application/json") .body(http_body_util::StreamBody::new(stream).boxed_unsync())?) } KubeReply::Hang => std::future::pending().await, } } }); (ClientBuilder::new(service, "space").build(), log) } pub(crate) fn kube_status(code: StatusCode, reason: &str) -> KubeReply { KubeReply::Json( code, serde_json::json!({ "kind": "Status", "apiVersion": "v1", "status": if code.is_success() { "Success" } else { "Failure" }, "message": reason, "reason": reason, "code": code.as_u16(), }), ) } pub(crate) fn pod_reply(pod: &Pod) -> KubeReply { KubeReply::Json( StatusCode::OK, serde_json::to_value(pod).expect("pod serializes"), ) } pub(super) fn pod_path(pod_name: &str) -> String { format!("/api/v1/namespaces/space/pods/{pod_name}") } pub(super) fn requests_matching(log: &KubeLog, method: &Method) -> Vec { log.lock() .unwrap() .iter() .filter(|request| request.method == *method) .cloned() .collect() } /// Decoded `fieldSelector` values from a Kubernetes query string. pub(crate) fn query_field_selectors(query: Option<&str>) -> Vec { query .unwrap_or_default() .split('&') .filter(|pair| !pair.is_empty()) .filter_map(|pair| { let (key, value) = pair.split_once('=').unwrap_or((pair, "")); let key = percent_encoding::percent_decode_str(key).decode_utf8_lossy(); (key == "fieldSelector").then(|| { percent_encoding::percent_decode_str(value) .decode_utf8_lossy() .into_owned() }) }) .collect() } // ── Scripted node-api ─────────────────────────────────────────────────────── /// One node-api call, named `Service/Method` without the proto package (for /// example `WorkerApi/UpdateSandbox` or `Health/Check`), with its encoded /// request message. #[derive(Debug, Clone)] pub(super) struct GrpcCall { pub(super) method: String, pub(super) message: Bytes, } type CallLog = Arc>>; #[derive(Clone)] pub(super) enum ScriptedReply { Status(tonic::Status), Message(Bytes), /// Never answer, for callers that bound the call with their own budget. Hang, /// Answer with the inner reply after the delay, as a node that /// long-polls the guest before it answers does. Delayed(Duration, Arc), /// Answers the n-th call to its method with the n-th reply and every /// later call with the last one. An empty sequence scripts nothing. Sequence(Arc<[ScriptedReply]>), } impl ScriptedReply { pub(super) fn message(message: &impl prost::Message) -> Self { Self::Message(Bytes::from(message.encode_to_vec())) } /// The reply to the call numbered `attempt`, counting from zero. fn for_attempt(&self, attempt: usize) -> &Self { match self { Self::Sequence(replies) => replies .get(attempt.min(replies.len().saturating_sub(1))) .map_or(self, |reply| reply.for_attempt(attempt)), reply => reply, } } } /// Runs when a call to its method arrives, before the call is answered, so a /// test can observe other state, such as the database, at that moment. pub(super) type CallObserver = Arc BoxFuture<'static, ()> + Send + Sync>; /// Records every call to `inner`, answers the scripted ones itself and /// forwards the rest. #[derive(Clone)] struct RecordingGrpcFront { inner: S, replies: Arc>, observers: Arc>, calls: CallLog, } impl tonic::server::NamedService for RecordingGrpcFront { const NAME: &'static str = S::NAME; } impl tower::Service> for RecordingGrpcFront where S: tower::Service< http::Request, Response = http::Response, Error = Infallible, > + Clone + Send + 'static, S::Future: Send + 'static, { type Response = http::Response; type Error = Infallible; type Future = BoxFuture<'static, Result>; fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, request: http::Request) -> Self::Future { let mut segments = request.uri().path().trim_start_matches('/').split('/'); let service = segments.next().unwrap_or_default(); let service = service.rsplit('.').next().unwrap_or(service); let method = format!("{service}/{}", segments.next().unwrap_or_default()); let reply = self.replies.get(method.as_str()).cloned(); let observer = self.observers.get(method.as_str()).cloned(); let calls = Arc::clone(&self.calls); let mut inner = self.inner.clone(); Box::pin(async move { let (parts, body) = request.into_parts(); let framed = body .collect() .await .map(|body| body.to_bytes()) .unwrap_or_default(); let attempt = { let mut calls = calls.lock().unwrap(); let attempt = calls.iter().filter(|call| call.method == method).count(); // Strip the 5-byte gRPC length-prefix frame header. calls.push(GrpcCall { method, message: framed.slice(5.min(framed.len())..), }); attempt }; if let Some(observer) = observer { observer().await; } let mut reply = reply.as_ref().map(|reply| reply.for_attempt(attempt)); while let Some(ScriptedReply::Delayed(delay, inner)) = reply { tokio::time::sleep(*delay).await; reply = Some(inner.for_attempt(attempt)); } match reply { Some(ScriptedReply::Status(status)) => Ok(status.clone().into_http()), Some(ScriptedReply::Message(message)) => Ok(grpc_unary_ok(message.clone())), Some(ScriptedReply::Hang) => std::future::pending().await, Some(ScriptedReply::Delayed(..)) => unreachable!("every delay was waited out"), Some(ScriptedReply::Sequence(_)) | None => { let request = http::Request::from_parts( parts, tonic::body::Body::new(http_body_util::Full::new(framed)), ); tower::Service::call(&mut inner, request).await } } }) } } fn grpc_unary_ok(message: Bytes) -> http::Response { let mut framed = Vec::with_capacity(message.len() + 5); framed.push(0); framed.extend_from_slice( &u32::try_from(message.len()) .expect("scripted message fits a gRPC frame") .to_be_bytes(), ); framed.extend_from_slice(&message); let mut trailers = HeaderMap::new(); trailers.insert("grpc-status", HeaderValue::from_static("0")); let frames = futures_util::stream::iter([ Ok::<_, Infallible>(Frame::data(Bytes::from(framed))), Ok(Frame::trailers(trailers)), ]); http::Response::builder() .header("content-type", "application/grpc") .body(tonic::body::Body::new(http_body_util::StreamBody::new( frames, ))) .expect("static gRPC response builds") } pub(super) struct ScriptedWorkerConfig { /// Replies keyed by `Service/Method`. pub(super) replies: HashMap<&'static str, ScriptedReply>, /// Observers keyed by `Service/Method`, for scripted and forwarded calls /// alike. pub(super) observers: HashMap<&'static str, CallObserver>, pub(super) advertise_operation_receipts: bool, /// Unscripted `WorkerApiInternal/WakeSandbox` calls go to a /// [`SequencedWakeWorker`] that echoes the requested route identity with /// the worker's node id from this attempt on, and reports the runtime not /// ready before it. pub(super) wake_succeeds_from_attempt: usize, } impl ScriptedWorkerConfig { pub(super) fn new(replies: impl IntoIterator) -> Self { Self { replies: replies.into_iter().collect(), observers: HashMap::new(), advertise_operation_receipts: true, wake_succeeds_from_attempt: 1, } } } pub(super) struct ScriptedWorker { pub(super) port: u16, /// State behind every forwarded `WorkerApi` method. pub(super) sim: SimState, calls: CallLog, shutdown: CancellationToken, task: JoinHandle<()>, _dir: tempfile::TempDir, } impl Drop for ScriptedWorker { fn drop(&mut self) { self.shutdown.cancel(); self.task.abort(); } } impl ScriptedWorker { /// Encoded request messages of every call to `method`, in arrival order. pub(super) fn received(&self, method: &str) -> Vec { self.calls .lock() .unwrap() .iter() .filter(|call| call.method == method) .map(|call| call.message.clone()) .collect() } /// Arrival index of the first call to `method`. pub(super) fn first_call(&self, method: &str) -> Option { self.calls .lock() .unwrap() .iter() .position(|call| call.method == method) } } pub(super) async fn start_scripted_worker( config: ScriptedWorkerConfig, ) -> Result { let dir = tempfile::tempdir()?; let sim = SimState::new( dir.path().join("snapshots"), dir.path().join("sandboxes"), WORKER_NODE.to_string(), ) .as_sandbox_pod(); let calls: CallLog = Arc::default(); let replies = Arc::new(config.replies); let observers = Arc::new(config.observers); let (health_reporter, health_service) = pplx_space_grpc_server::grpc_health_service(); health_reporter .set_service_status( pplx_space_sbx_mgr_client::ACTIVATION_POLICY_HEALTH_SERVICE, tonic_health::ServingStatus::Serving, ) .await; health_reporter .set_service_status( pplx_space_sbx_mgr_client::OPERATION_ACTIVATION_RECEIPT_HEALTH_SERVICE, if config.advertise_operation_receipts { tonic_health::ServingStatus::Serving } else { tonic_health::ServingStatus::NotServing }, ) .await; let health = RecordingGrpcFront { inner: health_service, replies: Arc::clone(&replies), observers: Arc::clone(&observers), calls: Arc::clone(&calls), }; let worker_api = RecordingGrpcFront { inner: WorkerApiServer::new(SimWorkerApiService::new(sim.clone())), replies: Arc::clone(&replies), observers: Arc::clone(&observers), calls: Arc::clone(&calls), }; let internal_api = RecordingGrpcFront { inner: WorkerApiInternalServer::new(SequencedWakeWorker { wake_attempts: Arc::new(AtomicUsize::new(0)), succeed_after_attempt: Some(config.wake_succeeds_from_attempt), echo_route_identity: true, received: Arc::default(), rejection: Arc::default(), }), replies: Arc::clone(&replies), observers: Arc::clone(&observers), calls: Arc::clone(&calls), }; let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?; let port = listener.local_addr()?.port(); let shutdown = CancellationToken::new(); let server_shutdown = shutdown.clone(); let task = tokio::spawn(async move { let result = Server::builder() .add_service(health) .add_service(worker_api) .add_service(internal_api) .serve_with_incoming_shutdown( TcpIncoming::from(listener).with_nodelay(Some(true)), server_shutdown.cancelled_owned(), ) .await; if let Err(error) = result { tracing::warn!(%error, "scripted worker server exited with error"); } }); Ok(ScriptedWorker { port, sim, calls, shutdown, task, _dir: dir, }) }