diff --git a/Cargo.lock b/Cargo.lock index 2e6eb3c..df6cb9f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1900,6 +1900,16 @@ dependencies = [ "cipher 0.4.4", ] +[[package]] +name = "ctrlc" +version = "3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b467862cc8610ca6fc9a1532d7777cee0804e678ab45410897b9396495994a0b" +dependencies = [ + "nix 0.27.1", + "windows-sys 0.52.0", +] + [[package]] name = "cumulus-client-cli" version = "0.1.0" @@ -6451,6 +6461,17 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags 2.4.2", + "cfg-if", + "libc", +] + [[package]] name = "nmt-rs" version = "0.1.0" @@ -10200,7 +10221,7 @@ dependencies = [ "log", "netlink-packet-route", "netlink-proto", - "nix", + "nix 0.24.3", "thiserror", "tokio", ] @@ -15614,6 +15635,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "ctrlc", "duct", "tracing", "tracing-subscriber 0.3.18", diff --git a/Cargo.toml b/Cargo.toml index 8ec57c8..d8807f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -182,3 +182,4 @@ pallet-ikura-length-fee-adjustment = { path = "ikura/chain/pallets/length-fee-ad # xtask duct = { version = "0.13.7" } +ctrlc = { version = "3.4.2" } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 443fe5d..6fca116 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -15,3 +15,4 @@ clap = { workspace = true, features = ["derive"] } anyhow = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } +ctrlc = { workspace = true } diff --git a/xtask/src/build.rs b/xtask/src/build.rs index 36b7c34..dd684bb 100644 --- a/xtask/src/build.rs +++ b/xtask/src/build.rs @@ -1,4 +1,4 @@ -use crate::{cli::test::BuildParams, logging::create_with_logs}; +use crate::{cli::BuildParams, logging::create_with_logs}; use duct::cmd; // TODO: https://github.com/thrumdev/blobs/issues/225 diff --git a/xtask/src/cli.rs b/xtask/src/cli.rs index 941d252..639a982 100644 --- a/xtask/src/cli.rs +++ b/xtask/src/cli.rs @@ -10,12 +10,51 @@ pub struct Cli { #[derive(Subcommand, Debug)] pub enum Commands { Test(test::Params), + Zombienet(zombienet::Params), +} + +#[derive(clap::Args, Debug, Clone)] +pub struct ZombienetParams { + /// Zombienet process stdout and stderr are redirected into this file + /// + /// Relative paths will be treated as relative to the root project directory + /// and not relative to where it is called + #[arg( + long = "zombienet-log-path", + value_name = "log-path", + id = "zombienet.log-path" + )] + #[clap(default_value = "test_log/zombienet.log")] + pub log_path: String, +} + +#[derive(clap::Args, Debug, Clone)] +pub struct BuildParams { + /// Skip building required binaries + /// (ikura-node, ikura-shim, sov-demo-rollup and sov-cli) + #[clap(default_value = "false")] + #[arg(long = "skip-build", value_name = "skip", id = "build.skip")] + pub skip: bool, + + /// Build process stdout and stderr are redirected into this file + /// + /// Relative paths will be treated as relative to the root project directory + /// and not relative to where it is called + #[arg( + long = "build-log-path", + value_name = "log-path", + id = "build.log-path" + )] + #[clap(default_value = "test_log/build.log")] + pub log_path: String, } pub mod test { + use super::{BuildParams, ZombienetParams}; // TODO: https://github.com/thrumdev/blobs/issues/224 use clap::Args; + #[derive(Debug, Args)] pub struct Params { /// If the test is executed in CI @@ -35,27 +74,6 @@ pub mod test { pub sovereign: SovereignParams, } - #[derive(clap::Args, Debug, Clone)] - pub struct BuildParams { - /// Skip building required binaries - /// (ikura-node, ikura-shim, sov-demo-rollup and sov-cli) - #[clap(default_value = "false")] - #[arg(long = "skip-build", value_name = "skip", id = "build.skip")] - pub skip: bool, - - /// Build process stdout and stderr are redirected into this file - /// - /// Relative paths will be treated as relative to the root project directory - /// and not relative to where it is called - #[arg( - long = "build-log-path", - value_name = "log-path", - id = "build.log-path" - )] - #[clap(default_value = "test_log/build.log")] - pub log_path: String, - } - #[derive(clap::Args, Debug, Clone)] pub struct ShimParams { /// Shim process stdout and stderr are redirected into this file @@ -67,21 +85,6 @@ pub mod test { pub log_path: String, } - #[derive(clap::Args, Debug, Clone)] - pub struct ZombienetParams { - /// Zombienet process stdout and stderr are redirected into this file - /// - /// Relative paths will be treated as relative to the root project directory - /// and not relative to where it is called - #[arg( - long = "zombienet-log-path", - value_name = "log-path", - id = "zombienet.log-path" - )] - #[clap(default_value = "test_log/zombienet.log")] - pub log_path: String, - } - #[derive(clap::Args, Debug, Clone)] pub struct SovereignParams { /// Sovereign rollup process stdout and stderr are redirected into this file @@ -97,3 +100,21 @@ pub mod test { pub log_path: String, } } + +pub mod zombienet { + use super::{BuildParams, ZombienetParams}; + use clap::Args; + + #[derive(Debug, Args)] + pub struct Params { + /// If the test is executed in CI + #[clap(long, default_value = "false")] + pub ci: bool, + + #[clap(flatten)] + pub build: BuildParams, + + #[clap(flatten)] + pub zombienet: ZombienetParams, + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 14f6930..2c35c7a 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -7,7 +7,10 @@ mod zombienet; use clap::Parser; use cli::{test, Cli, Commands}; -use std::path::Path; +use std::{ + path::{Path, PathBuf}, + str, +}; fn main() -> anyhow::Result<()> { init_logging()?; @@ -15,22 +18,14 @@ fn main() -> anyhow::Result<()> { match cli.command { Commands::Test(params) => test(params)?, + Commands::Zombienet(params) => zombienet(params)?, } Ok(()) } fn test(params: test::Params) -> anyhow::Result<()> { - // extract project path - #[rustfmt::skip] - let project_path = duct::cmd!( - "sh", "-c", - "cargo metadata --format-version 1 | jq -r '.workspace_root'" - ) - .stdout_capture() - .run()?; - - let project_path = Path::new(std::str::from_utf8(&project_path.stdout)?.trim()); + let project_path = obtain_project_path()?; init_env(&project_path, params.ci)?; @@ -38,10 +33,8 @@ fn test(params: test::Params) -> anyhow::Result<()> { // the variables must be kept alive and not dropped // otherwise the child process will be killed - #[allow(unused)] - let zombienet = zombienet::Zombienet::try_new(&project_path, params.zombienet)?; - #[allow(unused)] - let shim = shim::Shim::try_new(&project_path, params.shim)?; + let _zombienet = zombienet::Zombienet::try_new(&project_path, params.zombienet)?; + let _shim = shim::Shim::try_new(&project_path, params.shim)?; let sovereign = sovereign::Sovereign::try_new(&project_path, params.sovereign)?; // TODO: https://github.com/thrumdev/blobs/issues/226 @@ -53,6 +46,36 @@ fn test(params: test::Params) -> anyhow::Result<()> { Ok(()) } +fn zombienet(params: crate::cli::zombienet::Params) -> anyhow::Result<()> { + let project_path = obtain_project_path()?; + build::build(&project_path, params.build)?; + let _zombienet = zombienet::Zombienet::try_new(&project_path, params.zombienet)?; + wait_interrupt(); + Ok(()) +} + +fn obtain_project_path() -> anyhow::Result { + #[rustfmt::skip] + let project_path = duct::cmd!( + "sh", "-c", + "cargo metadata --format-version 1 | jq -r '.workspace_root'" + ) + .stdout_capture() + .run()?; + Ok(PathBuf::from(str::from_utf8(&project_path.stdout)?.trim())) +} + +/// Blocks until ^C signal is delivered to this process. Uses global resource, don't proliferate. +fn wait_interrupt() { + use std::sync::mpsc; + let (tx, rx) = mpsc::channel(); + ctrlc::set_handler(move || { + let _ = tx.send(()); + }) + .unwrap(); + let _ = rx.recv(); +} + // Set up environment variables needed by the compilation and testing process. // // If ci flag is specified, all binaries are added to PATH env variable diff --git a/xtask/src/zombienet.rs b/xtask/src/zombienet.rs index 856a480..9eef5de 100644 --- a/xtask/src/zombienet.rs +++ b/xtask/src/zombienet.rs @@ -1,4 +1,4 @@ -use crate::{check_binary, cli::test::ZombienetParams, logging::create_with_logs}; +use crate::{check_binary, cli::ZombienetParams, logging::create_with_logs}; use duct::cmd; use std::path::Path; use tracing::info;