Skip to content

Commit

Permalink
xtask: make testing cwd independent
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriele-0201 committed Feb 20, 2024
1 parent 13c3a5a commit a5ad9ef
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 48 deletions.
9 changes: 5 additions & 4 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use duct::cmd;

// TODO: https://github.com/thrumdev/blobs/issues/225

pub fn build(params: BuildParams) -> anyhow::Result<()> {
pub fn build(project_path: &std::path::Path, params: BuildParams) -> anyhow::Result<()> {
if params.skip {
return Ok(());
}

tracing::info!("Building logs redirected {}", params.log_path);
let with_logs = create_with_logs(params.log_path);
let with_logs = create_with_logs(project_path, params.log_path);

// `it is advisable to use CARGO environmental variable to get the right cargo`
// quoted by xtask readme
Expand All @@ -27,14 +27,15 @@ pub fn build(params: BuildParams) -> anyhow::Result<()> {
)
.run()?;

let sov_demo_rollup_path = project_path.join("demo/sovereign/demo-rollup/");
#[rustfmt::skip]
with_logs(
"Building sovereign demo-rollup",
cmd!(
"sh", "-c",
format!(
"cd demo/sovereign/demo-rollup/ && {} build --release",
cargo
"cd {} && {cargo} build --release",
sov_demo_rollup_path.to_string_lossy()
)
),
).run()?;
Expand Down
12 changes: 12 additions & 0 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub mod test {
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",
Expand All @@ -56,6 +59,9 @@ pub mod test {
#[derive(clap::Args, Debug, Clone)]
pub struct ShimParams {
/// Shim 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 = "shim-log-path", value_name = "log-path", id = "shim.log-path")]
#[clap(default_value = "test_log/shim.log")]
pub log_path: String,
Expand All @@ -64,6 +70,9 @@ pub mod test {
#[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",
Expand All @@ -76,6 +85,9 @@ pub mod test {
#[derive(clap::Args, Debug, Clone)]
pub struct SovereignParams {
/// Sovereign rollup 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 = "sovereign-log-path",
value_name = "log-path",
Expand Down
45 changes: 33 additions & 12 deletions xtask/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use std::io::Write;
use std::path::Path;
use std::{io::Write, path::PathBuf};
use tracing::{info, warn};

fn create_log_file(log_path: &String) -> std::io::Result<()> {
if let Some(prefix) = Path::new(&log_path).parent() {
// If log_path is relative it will be made absolute relative to the project_path
//
// The absolute path of where the log file is created is returned
fn create_log_file(project_path: &Path, log_path: &String) -> std::io::Result<PathBuf> {
let mut log_path: PathBuf = Path::new(&log_path).to_path_buf();

if log_path.is_relative() {
log_path = project_path.join(log_path);
}

if let Some(prefix) = log_path.parent() {
std::fs::create_dir_all(prefix)?;
}
std::fs::File::create(&log_path)?;
Ok(())
Ok(log_path)
}

// If the log file cannot be created due to any reasons,
Expand All @@ -18,17 +27,21 @@ fn create_log_file(log_path: &String) -> std::io::Result<()> {
// The description will be printed to both stdout and the log file, if possible, while
// to the expression will be added the redirection of the logs, if possible.
pub fn create_with_logs(
project_path: &Path,
log_path: String,
) -> Box<dyn Fn(&str, duct::Expression) -> duct::Expression> {
let without_logs = |description: &str, cmd: duct::Expression| -> duct::Expression {
info!("{description}");
cmd
};

if let Err(e) = create_log_file(&log_path) {
warn!("Impossible redirect to {log_path}, using stdout instead. Error: {e}");
return Box::new(without_logs);
}
let log_path = match create_log_file(project_path, &log_path) {
Ok(log_path) => log_path,
Err(e) => {
warn!("Impossible redirect logs, using stdout instead. Error: {e}");
return Box::new(without_logs);
}
};

let with_logs = move |description: &str, cmd: duct::Expression| -> duct::Expression {
// The file has just been created
Expand All @@ -40,10 +53,18 @@ pub fn create_with_logs(
info!("{description}");
let _ = log_file
.write(format!("{}\n", description).as_bytes())
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
let _ = log_file
.flush()
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
.map_err(|e| {
warn!(
"Error writing into {}, error: {e}",
log_path.to_string_lossy()
)
});
let _ = log_file.flush().map_err(|e| {
warn!(
"Error writing into {}, error: {e}",
log_path.to_string_lossy()
)
});
cmd.stderr_to_stdout().stdout_file(log_file)
};

Expand Down
35 changes: 19 additions & 16 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod zombienet;

use clap::Parser;
use cli::{test, Cli, Commands};
use std::path::Path;

fn main() -> anyhow::Result<()> {
init_logging()?;
Expand All @@ -20,17 +21,28 @@ fn main() -> anyhow::Result<()> {
}

fn test(params: test::Params) -> anyhow::Result<()> {
init_env(params.ci)?;
// extract project path
#[rustfmt::skip]
let project_path = duct::cmd!(
"sh", "-c",
"cargo metadata --format-version 1 | jq -r '.workspace_root'"
)
.stdout_capture()
.run()?;

build::build(params.build)?;
let project_path = Path::new(std::str::from_utf8(&project_path.stdout)?.trim());

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(params.zombienet)?;
let zombienet = zombienet::Zombienet::try_new(&project_path, params.zombienet)?;
#[allow(unused)]
let shim = shim::Shim::try_new(params.shim)?;
let sovereign = sovereign::Sovereign::try_new(params.sovereign)?;
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
// Wait for the sovereign rollup to be ready
Expand All @@ -46,25 +58,16 @@ fn test(params: test::Params) -> anyhow::Result<()> {
// If ci flag is specified, all binaries are added to PATH env variable
// and the sovereign constant manifest position is specified through the
// CONSTANTS_MANIFEST new env variable
fn init_env(ci: bool) -> anyhow::Result<()> {
fn init_env(project_path: &Path, ci: bool) -> anyhow::Result<()> {
if ci {
#[rustfmt::skip]
let project_dir = duct::cmd!(
"sh", "-c",
"cargo locate-project | jq -r '.root' | grep -oE '^.*/'"
)
.stdout_capture()
.run()?;
let project_dir = std::str::from_utf8(&project_dir.stdout)?.trim();

let path = std::env::var("PATH").unwrap_or_else(|_| "".to_string());

// `cargo_target` is the target used in ci by cargo as destination
// for all intermediate and final artifacts
let new_path = format!("/cargo_target/release/:{}", path);
std::env::set_var("PATH", new_path);

let path = std::path::Path::new(project_dir).join("demo/sovereign/constants.json");
let path = project_path.join("demo/sovereign/constants.json");
if !path.exists() {
anyhow::bail!(
"The `constants.json` file for Sovereign does not exist,\n \
Expand Down
4 changes: 2 additions & 2 deletions xtask/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ pub struct Shim(duct::Handle);

impl Shim {
// Try launching the shim, it requires an up an running ikura-node
pub fn try_new(params: ShimParams) -> anyhow::Result<Self> {
pub fn try_new(project_path: &std::path::Path, params: ShimParams) -> anyhow::Result<Self> {
check_binary(
"ikura-shim",
"'ikura-node' is not found in PATH. \n \
cd to 'ikura/shim' and run 'cargo build --release' and add the result into your PATH.",
)?;

tracing::info!("Shim logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path);
let with_logs = create_with_logs(project_path, params.log_path);

// Wait for the shim to be connected, which indicates that the network is ready
with_logs(
Expand Down
29 changes: 20 additions & 9 deletions xtask/src/sovereign.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use crate::{check_binary, cli::test::SovereignParams, logging::create_with_logs};
use anyhow::bail;
use duct::cmd;
use std::path::{Path, PathBuf};
use tracing::info;

pub struct Sovereign {
process: duct::Handle,
with_logs: Box<dyn Fn(&str, duct::Expression) -> duct::Expression>,
project_path: PathBuf,
}

impl Sovereign {
// Try launching the sovereing rollup using zombienet
pub fn try_new(params: SovereignParams) -> anyhow::Result<Self> {
pub fn try_new(project_path: &Path, params: SovereignParams) -> anyhow::Result<Self> {
info!("Deleting rollup db if it already exists");
cmd!("rm", "-r", "demo/sovereign/demo-rollup/demo_data")
.unchecked()
.stderr_null()
.stdout_null()
.run()?;

let sovereign_demo_data = project_path.join("demo/sovereign/demo-rollup/demo_data");
if sovereign_demo_data.as_path().exists() {
cmd!("rm", "-r", sovereign_demo_data).run()?;
}

check_binary(
"sov-demo-rollup",
Expand All @@ -25,21 +27,26 @@ impl Sovereign {
)?;

info!("Sovereign logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path.clone());
let with_logs = create_with_logs(project_path, params.log_path.clone());

let sov_demo_rollup_path = project_path.join("demo/sovereign/demo-rollup/");
//TODO: https://github.com/thrumdev/blobs/issues/227
#[rustfmt::skip]
let sovereign_handle = with_logs(
"Launching sovereign rollup",
cmd!(
"sh", "-c",
"cd demo/sovereign/demo-rollup && sov-demo-rollup"
format!(
"cd {} && sov-demo-rollup",
sov_demo_rollup_path.to_string_lossy()
)
),
).start()?;

Ok(Self {
process: sovereign_handle,
with_logs,
project_path: project_path.to_path_buf(),
})
}

Expand All @@ -54,12 +61,16 @@ impl Sovereign {
info!("Running sovereign rollup test");

//TODO: https://github.com/thrumdev/blobs/issues/227
let sov_demo_rollup_path = self.project_path.join("demo/sovereign/demo-rollup/");
let test_data_path = "../test-data/";
let run_cli_cmd =
|description: &str, args: &str| -> std::io::Result<std::process::Output> {
let args = [
"-c",
&format!("cd demo/sovereign/demo-rollup/ && sov-cli {}", args),
&format!(
"cd {} && sov-cli {args}",
sov_demo_rollup_path.to_string_lossy()
),
];

(self.with_logs)(description, duct::cmd("sh", args)).run()
Expand Down
16 changes: 11 additions & 5 deletions xtask/src/zombienet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{check_binary, cli::test::ZombienetParams, logging::create_with_logs};
use duct::cmd;
use std::path::Path;
use tracing::info;

pub struct Zombienet(duct::Handle);
Expand All @@ -10,12 +11,14 @@ impl Zombienet {
// The binaries for zombienet and polkadot are expected to be in the PATH,
// while polkadot-execute-worker and polkadot-prepare-worker
// need to be in the same directory as the polkadot binary.
pub fn try_new(params: ZombienetParams) -> anyhow::Result<Self> {
pub fn try_new(project_path: &Path, params: ZombienetParams) -> anyhow::Result<Self> {
info!("Deleting the zombienet folder if it already exists");
cmd!("rm", "-r", "zombienet").unchecked().run()?;
let zombienet_folder = project_path.join("zombienet");
if zombienet_folder.as_path().exists() {
cmd!("rm", "-r", zombienet_folder).run()?;
}

info!("Checking binaries availability");

check_binary(
"zombienet",
"'zombienet' is not found in PATH. Install zombienet. \n \
Expand All @@ -35,12 +38,15 @@ impl Zombienet {
)?;

tracing::info!("Zombienet logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path);
let with_logs = create_with_logs(project_path, params.log_path);

#[rustfmt::skip]
let zombienet_handle = with_logs(
"Launching zombienet",
cmd!("zombienet", "spawn", "-p", "native", "--dir", "zombienet", "testnet.toml"),
cmd!(
"sh", "-c",
format!("cd {} && zombienet spawn -p native --dir zombienet testnet.toml", project_path.to_string_lossy())
),
).start()?;

Ok(Self(zombienet_handle))
Expand Down

0 comments on commit a5ad9ef

Please sign in to comment.