2026-01-19 08:34:17 +01:00
|
|
|
use std::sync::Arc;
|
2026-01-14 12:44:31 +01:00
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
use async_trait::async_trait;
|
|
|
|
|
use testing_framework_core::{
|
|
|
|
|
scenario::{
|
2026-01-14 12:44:31 +01:00
|
|
|
BlockFeed, BlockFeedTask, Deployer, DynError, Metrics, NodeClients, NodeControlCapability,
|
2026-01-19 08:34:17 +01:00
|
|
|
RunContext, Runner, Scenario, ScenarioError, spawn_block_feed,
|
2025-12-01 12:48:39 +01:00
|
|
|
},
|
2025-12-18 22:23:02 +01:00
|
|
|
topology::{
|
2026-01-19 13:27:01 +01:00
|
|
|
config::TopologyConfig,
|
2025-12-18 22:23:02 +01:00
|
|
|
deployment::{SpawnTopologyError, Topology},
|
|
|
|
|
readiness::ReadinessError,
|
|
|
|
|
},
|
2025-12-01 12:48:39 +01:00
|
|
|
};
|
|
|
|
|
use thiserror::Error;
|
2025-12-11 10:08:49 +01:00
|
|
|
use tracing::{debug, info};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2026-01-19 13:27:01 +01:00
|
|
|
use crate::{
|
|
|
|
|
manual::{LocalManualCluster, ManualClusterError},
|
|
|
|
|
node_control::{LocalDynamicNodes, LocalDynamicSeed},
|
|
|
|
|
};
|
2026-01-25 10:11:16 +02:00
|
|
|
/// Spawns validators as local processes, reusing the existing
|
2025-12-01 12:48:39 +01:00
|
|
|
/// integration harness.
|
|
|
|
|
#[derive(Clone)]
|
2026-01-19 02:28:49 +01:00
|
|
|
pub struct LocalDeployer {}
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
/// Errors surfaced by the local deployer while driving a scenario.
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
pub enum LocalDeployerError {
|
2025-12-18 22:23:02 +01:00
|
|
|
#[error("failed to spawn local topology: {source}")]
|
|
|
|
|
Spawn {
|
|
|
|
|
#[source]
|
|
|
|
|
source: SpawnTopologyError,
|
|
|
|
|
},
|
2025-12-01 12:48:39 +01:00
|
|
|
#[error("readiness probe failed: {source}")]
|
|
|
|
|
ReadinessFailed {
|
|
|
|
|
#[source]
|
|
|
|
|
source: ReadinessError,
|
|
|
|
|
},
|
|
|
|
|
#[error("workload failed: {source}")]
|
|
|
|
|
WorkloadFailed {
|
|
|
|
|
#[source]
|
|
|
|
|
source: DynError,
|
|
|
|
|
},
|
|
|
|
|
#[error("expectations failed: {source}")]
|
|
|
|
|
ExpectationsFailed {
|
|
|
|
|
#[source]
|
|
|
|
|
source: DynError,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<ScenarioError> for LocalDeployerError {
|
|
|
|
|
fn from(value: ScenarioError) -> Self {
|
|
|
|
|
match value {
|
|
|
|
|
ScenarioError::Workload(source) => Self::WorkloadFailed { source },
|
|
|
|
|
ScenarioError::ExpectationCapture(source) | ScenarioError::Expectations(source) => {
|
|
|
|
|
Self::ExpectationsFailed { source }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl Deployer<()> for LocalDeployer {
|
|
|
|
|
type Error = LocalDeployerError;
|
|
|
|
|
|
|
|
|
|
async fn deploy(&self, scenario: &Scenario<()>) -> Result<Runner, Self::Error> {
|
|
|
|
|
info!(
|
|
|
|
|
validators = scenario.topology().validators().len(),
|
|
|
|
|
"starting local deployment"
|
|
|
|
|
);
|
2026-01-19 02:28:49 +01:00
|
|
|
let topology = Self::prepare_topology(scenario).await?;
|
2025-12-01 12:48:39 +01:00
|
|
|
let node_clients = NodeClients::from_topology(scenario.topology(), &topology);
|
|
|
|
|
|
|
|
|
|
let (block_feed, block_feed_guard) = spawn_block_feed_with(&node_clients).await?;
|
|
|
|
|
|
|
|
|
|
let context = RunContext::new(
|
|
|
|
|
scenario.topology().clone(),
|
|
|
|
|
Some(topology),
|
|
|
|
|
node_clients,
|
|
|
|
|
scenario.duration(),
|
|
|
|
|
Metrics::empty(),
|
|
|
|
|
block_feed,
|
|
|
|
|
None,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(Runner::new(context, Some(Box::new(block_feed_guard))))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 12:44:31 +01:00
|
|
|
#[async_trait]
|
|
|
|
|
impl Deployer<NodeControlCapability> for LocalDeployer {
|
|
|
|
|
type Error = LocalDeployerError;
|
|
|
|
|
|
|
|
|
|
async fn deploy(
|
|
|
|
|
&self,
|
|
|
|
|
scenario: &Scenario<NodeControlCapability>,
|
|
|
|
|
) -> Result<Runner, Self::Error> {
|
|
|
|
|
info!(
|
|
|
|
|
validators = scenario.topology().validators().len(),
|
|
|
|
|
"starting local deployment with node control"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let topology = Self::prepare_topology(scenario).await?;
|
|
|
|
|
let node_clients = NodeClients::from_topology(scenario.topology(), &topology);
|
2026-01-19 08:34:17 +01:00
|
|
|
let node_control = Arc::new(LocalDynamicNodes::new_with_seed(
|
2026-01-14 12:44:31 +01:00
|
|
|
scenario.topology().clone(),
|
|
|
|
|
node_clients.clone(),
|
2026-01-19 08:34:17 +01:00
|
|
|
LocalDynamicSeed::from_topology(scenario.topology()),
|
2026-01-14 12:44:31 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
let (block_feed, block_feed_guard) = spawn_block_feed_with(&node_clients).await?;
|
|
|
|
|
|
|
|
|
|
let context = RunContext::new(
|
|
|
|
|
scenario.topology().clone(),
|
|
|
|
|
Some(topology),
|
|
|
|
|
node_clients,
|
|
|
|
|
scenario.duration(),
|
|
|
|
|
Metrics::empty(),
|
|
|
|
|
block_feed,
|
|
|
|
|
Some(node_control),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Ok(Runner::new(context, Some(Box::new(block_feed_guard))))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
impl LocalDeployer {
|
|
|
|
|
#[must_use]
|
2026-01-19 02:28:49 +01:00
|
|
|
/// Construct a local deployer.
|
2025-12-01 12:48:39 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self::default()
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 13:27:01 +01:00
|
|
|
/// Build a manual cluster using this deployer's local implementation.
|
|
|
|
|
pub fn manual_cluster(
|
|
|
|
|
&self,
|
|
|
|
|
config: TopologyConfig,
|
|
|
|
|
) -> Result<LocalManualCluster, ManualClusterError> {
|
|
|
|
|
LocalManualCluster::from_config(config)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn prepare_topology<Caps>(
|
|
|
|
|
scenario: &Scenario<Caps>,
|
|
|
|
|
) -> Result<Topology, LocalDeployerError> {
|
2025-12-01 12:48:39 +01:00
|
|
|
let descriptors = scenario.topology();
|
2026-01-19 13:27:01 +01:00
|
|
|
|
2025-12-11 10:08:49 +01:00
|
|
|
info!(
|
|
|
|
|
validators = descriptors.validators().len(),
|
2026-01-25 10:11:16 +02:00
|
|
|
"spawning local validators"
|
2025-12-11 10:08:49 +01:00
|
|
|
);
|
2026-01-19 13:27:01 +01:00
|
|
|
|
2025-12-18 22:23:02 +01:00
|
|
|
let topology = descriptors
|
|
|
|
|
.clone()
|
|
|
|
|
.spawn_local()
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|source| LocalDeployerError::Spawn { source })?;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2026-01-19 02:28:49 +01:00
|
|
|
wait_for_readiness(&topology).await.map_err(|source| {
|
|
|
|
|
debug!(error = ?source, "local readiness failed");
|
|
|
|
|
LocalDeployerError::ReadinessFailed { source }
|
|
|
|
|
})?;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
info!("local nodes are ready");
|
2026-01-19 13:27:01 +01:00
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
Ok(topology)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for LocalDeployer {
|
|
|
|
|
fn default() -> Self {
|
2026-01-19 02:28:49 +01:00
|
|
|
Self {}
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 02:28:49 +01:00
|
|
|
async fn wait_for_readiness(topology: &Topology) -> Result<(), ReadinessError> {
|
2025-12-01 12:48:39 +01:00
|
|
|
info!("waiting for local network readiness");
|
2026-01-19 13:27:01 +01:00
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
topology.wait_network_ready().await?;
|
2026-01-19 02:28:49 +01:00
|
|
|
Ok(())
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn spawn_block_feed_with(
|
|
|
|
|
node_clients: &NodeClients,
|
|
|
|
|
) -> Result<(BlockFeed, BlockFeedTask), LocalDeployerError> {
|
2025-12-11 10:08:49 +01:00
|
|
|
debug!(
|
|
|
|
|
validators = node_clients.validator_clients().len(),
|
|
|
|
|
"selecting validator client for local block feed"
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-14 12:44:31 +01:00
|
|
|
let Some(block_source_client) = node_clients.random_validator() else {
|
2025-12-15 23:19:13 +01:00
|
|
|
return Err(LocalDeployerError::WorkloadFailed {
|
2025-12-01 12:48:39 +01:00
|
|
|
source: "block feed requires at least one validator".into(),
|
2025-12-15 23:19:13 +01:00
|
|
|
});
|
|
|
|
|
};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
|
|
|
|
info!("starting block feed");
|
2025-12-15 20:43:25 +01:00
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
spawn_block_feed(block_source_client)
|
|
|
|
|
.await
|
2025-12-15 23:19:13 +01:00
|
|
|
.map_err(workload_error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn workload_error(source: impl Into<DynError>) -> LocalDeployerError {
|
|
|
|
|
LocalDeployerError::WorkloadFailed {
|
|
|
|
|
source: source.into(),
|
|
|
|
|
}
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|