#![cfg(unix)]
use assert_cmd::cargo::cargo_bin;
use std::{process::Command, fs, path::PathBuf};
use tempfile::{tempdir, TempDir};
use regex::Regex;
pub mod common;
fn contains_error(logged_output: &str) -> bool {
logged_output.contains("Error")
}
struct ExportImportRevertExecutor<'a> {
base_path: &'a TempDir,
exported_blocks_file: &'a PathBuf,
db_path: &'a PathBuf,
num_exported_blocks: Option<u64>,
}
enum FormatOpt {
Json,
Binary,
}
enum SubCommand {
ExportBlocks,
ImportBlocks,
}
impl ToString for SubCommand {
fn to_string(&self) -> String {
match self {
SubCommand::ExportBlocks => String::from("export-blocks"),
SubCommand::ImportBlocks => String::from("import-blocks"),
}
}
}
impl<'a> ExportImportRevertExecutor<'a> {
fn new(
base_path: &'a TempDir,
exported_blocks_file: &'a PathBuf,
db_path: &'a PathBuf
) -> Self {
Self {
base_path,
exported_blocks_file,
db_path,
num_exported_blocks: None,
}
}
fn run_block_command(&self,
sub_command: SubCommand,
format_opt: FormatOpt,
expected_to_fail: bool
) -> String {
let sub_command_str = sub_command.to_string();
let arguments: Vec<&str> = match format_opt {
FormatOpt::Binary => vec![&sub_command_str, "--dev", "--pruning", "archive", "--binary", "-d"],
FormatOpt::Json => vec![&sub_command_str, "--dev", "--pruning", "archive", "-d"],
};
let tmp: TempDir;
let base_path = match sub_command {
SubCommand::ExportBlocks => &self.base_path.path(),
SubCommand::ImportBlocks => {
tmp = tempdir().unwrap();
tmp.path()
}
};
let output = Command::new(cargo_bin("tetcore"))
.args(&arguments)
.arg(&base_path)
.arg(&self.exported_blocks_file)
.output()
.unwrap();
let logged_output = String::from_utf8_lossy(&output.stderr).to_string();
if expected_to_fail {
assert!(contains_error(&logged_output), "expected to error but did not error!");
assert!(!output.status.success());
} else {
assert!(!contains_error(&logged_output), "expected not to error but error'd!");
assert!(output.status.success());
}
logged_output
}
fn run_export(&mut self, fmt_opt: FormatOpt) {
let log = self.run_block_command(SubCommand::ExportBlocks, fmt_opt, false);
let re = Regex::new(r"Exporting blocks from #\d* to #(?P<exported_blocks>\d*)").unwrap();
let caps = re.captures(&log).unwrap();
self.num_exported_blocks = Some(caps["exported_blocks"].parse::<u64>().unwrap());
let metadata = fs::metadata(&self.exported_blocks_file).unwrap();
assert!(metadata.len() > 0, "file exported_blocks should not be empty");
let _ = fs::remove_dir_all(&self.db_path);
}
fn run_import(&mut self, fmt_opt: FormatOpt, expected_to_fail: bool) {
let log = self.run_block_command(SubCommand::ImportBlocks, fmt_opt, expected_to_fail);
if !expected_to_fail {
let re = Regex::new(r"Imported (?P<imported>\d*) blocks. Best: #(?P<best>\d*)").unwrap();
let caps = re.captures(&log).expect("capture should have succeeded");
let imported = caps["imported"].parse::<u64>().unwrap();
let best = caps["best"].parse::<u64>().unwrap();
assert_eq!(
imported,
best,
"numbers of blocks imported and best number differs"
);
assert_eq!(
best,
self.num_exported_blocks.expect("number of exported blocks cannot be None; qed"),
"best block number and number of expected blocks should not differ"
);
}
self.num_exported_blocks = None;
}
fn run_revert(&self) {
let output = Command::new(cargo_bin("tetcore"))
.args(&["revert", "--dev", "--pruning", "archive", "-d"])
.arg(&self.base_path.path())
.output()
.unwrap();
let logged_output = String::from_utf8_lossy(&output.stderr).to_string();
assert!(!contains_error(&logged_output));
assert!(output.status.success());
}
fn run(&mut self, export_fmt: FormatOpt, import_fmt: FormatOpt, expected_to_fail: bool) {
self.run_export(export_fmt);
self.run_import(import_fmt, expected_to_fail);
self.run_revert();
}
}
#[test]
fn export_import_revert() {
let base_path = tempdir().expect("could not create a temp dir");
let exported_blocks_file = base_path.path().join("exported_blocks");
let db_path = base_path.path().join("db");
common::run_dev_node_for_a_while(base_path.path());
let mut executor = ExportImportRevertExecutor::new(
&base_path,
&exported_blocks_file,
&db_path,
);
executor.run(FormatOpt::Binary, FormatOpt::Binary, false);
executor.run(FormatOpt::Binary, FormatOpt::Json, true);
executor.run(FormatOpt::Json, FormatOpt::Json, false);
executor.run(FormatOpt::Json, FormatOpt::Binary, true);
}