Skip to content

Commit

Permalink
xtask: utility to launch zombienet
Browse files Browse the repository at this point in the history
  • Loading branch information
pepyakin committed Feb 21, 2024
1 parent 73ad15d commit ae356a8
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 54 deletions.
24 changes: 23 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
1 change: 1 addition & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
93 changes: 57 additions & 36 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
}
}
53 changes: 38 additions & 15 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,34 @@ 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()?;
let cli = Cli::parse();

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)?;

build::build(&project_path, params.build)?;

// 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
Expand All @@ -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<PathBuf> {
#[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
Expand Down
2 changes: 1 addition & 1 deletion xtask/src/zombienet.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit ae356a8

Please sign in to comment.