Files
iroh/tests/cli.rs
T
Rüdiger Klaehn ada047aa06 Allow nested directories while still keeping the flat collection / blob hierarchy (#845)
* feat(persistence): copy over directory functions from beetle

* feat: add snapshot type that deals with persistence

* test(persistence): add property based tests for db persistence

* feat(persistence): basic persistence works

* Make provider cloneable

* refactor:

provider is getting to big, so give it its own submodule and put database
in there.

* clippy

* fix: disable persistence in cli tests

* refactor: make the snapshot stuff just pub(crate)

so we don't bloat the public API

* fix: properly handle when iroh data dir does not exist yet

Also fix mixup of dir names

* fix: persist key in iroh data root if persistence is on

* feat: canonicalize before adding collection

Not saying that we should suddenly start to care about canonicalization, but
it is almost free, so we might as well do it.

* feat: allow deep directory structures

* fix: make sure deep directories are created

* refactor: use walkdir

* pr: remove unused dev dependency and dead code annotation

* test: add basic test for persistence

* pr: remove key and persist options

it will now always persist.

Also fix the test by not mixing sync and async.

* refactor: get rid of collection name

The name should be external

* refactor: make fields in Collection private

...so we can canonicalize it

* ci: change toolchain for doc to stable

* refactor: canonicalize collection on create

conceptually, a collection should be a set of name -> blob mappings that
is sorted by name. We could use a BTreeMap or a RadixTree later.

* fix: special handling for single files

* doc: add comment about traversal

* feat: properly handle windows insanity

* Add basic test for canonicalize_path

* test: add a test that sends a (tiny) hierarchy

* refactor: runtime check for duplicate names

* pr review: minor changes and comments
2023-03-22 12:14:59 +02:00

388 lines
13 KiB
Rust

#![cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use anyhow::{Context, Result};
use rand::{RngCore, SeedableRng};
use testdir::testdir;
use walkdir::WalkDir;
const ADDR: &str = "127.0.0.1:0";
fn make_rand_file(size: usize, path: &Path) -> Result<()> {
let mut content = vec![0u8; size];
rand::rngs::StdRng::seed_from_u64(1).fill_bytes(&mut content);
std::fs::write(path, content)?;
Ok(())
}
#[test]
fn cli_provide_one_file() -> Result<()> {
let dir = testdir!();
let path = dir.join("foo");
make_rand_file(1000, &path)?;
// provide a path to a file, do not pipe from stdin, do not pipe to stdout
test_provide_get_loop(&path, Input::Path, Output::Path)
}
#[test]
fn cli_provide_folder() -> Result<()> {
let dir = testdir!();
let foo_path = dir.join("foo");
let bar_path = dir.join("bar");
make_rand_file(1000, &foo_path)?;
make_rand_file(10000, &bar_path)?;
// provide a path to a folder, do not pipe from stdin, do not pipe to stdout
test_provide_get_loop(&dir, Input::Path, Output::Path)
}
#[test]
fn cli_provide_tree() -> Result<()> {
let dir = testdir!();
let foo_path = dir.join("foo");
let bar_path = dir.join("bar");
let file1 = foo_path.join("file1");
let file2 = bar_path.join("file2");
let file3 = bar_path.join("file3");
std::fs::create_dir(&foo_path)?;
std::fs::create_dir(&bar_path)?;
make_rand_file(1000, &file1)?;
make_rand_file(10000, &file2)?;
make_rand_file(5000, &file3)?;
// provide a path to a folder, do not pipe from stdin, do not pipe to stdout
test_provide_get_loop(&dir, Input::Path, Output::Path)
}
#[test]
fn cli_provide_from_stdin_to_stdout() -> Result<()> {
let dir = testdir!();
let path = dir.join("foo");
make_rand_file(1000, &path)?;
// provide a file, pipe content to the provider's stdin, pipe content to the getter's stdout
test_provide_get_loop(&path, Input::Stdin, Output::Stdout)
}
#[cfg(all(unix, feature = "cli"))]
#[test]
fn cli_provide_persistence() -> anyhow::Result<()> {
use iroh::provider::Database;
use nix::{
sys::signal::{self, Signal},
unistd::Pid,
};
let dir = testdir!();
let iroh_data_dir = dir.join("iroh_data_dir");
let foo_path = dir.join("foo");
std::fs::write(&foo_path, b"foo")?;
let bar_path = dir.join("bar");
std::fs::write(&bar_path, b"bar")?;
// spawn iroh in provide mode
let iroh_provide = |path| {
Command::new(iroh_bin())
.env("IROH_DATA_DIR", &iroh_data_dir)
// comment out to get debug output from the child process
// .env("RUST_LOG", "debug")
.stdin(Stdio::null())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.arg("provide")
.arg("--addr")
.arg(ADDR)
.arg("--rpc-port")
.arg("disabled")
.arg(path)
.spawn()
};
// provide for 1 sec, then stop with control-c
let provide_1sec = |path| {
let mut child = iroh_provide(path)?;
// wait for the provider to start
std::thread::sleep(std::time::Duration::from_secs(1));
// kill the provider via Control-C
signal::kill(Pid::from_raw(child.id() as i32), Signal::SIGINT).unwrap();
// wait for the provider to exit and make sure that it exited successfully
let status = child.wait()?;
// comment out to get debug output from the child process
std::io::copy(&mut child.stderr.unwrap(), &mut std::io::stdout())?;
assert!(status.success());
anyhow::Ok(())
};
provide_1sec(&foo_path)?;
// should have some data now
let db = Database::load_test(iroh_data_dir.clone())?;
let blobs = db.blobs().map(|x| x.1).collect::<Vec<_>>();
assert_eq!(blobs, vec![foo_path.clone()]);
provide_1sec(&bar_path)?;
// should have more data now
let db = Database::load_test(&iroh_data_dir)?;
let mut blobs = db.blobs().map(|x| x.1).collect::<Vec<_>>();
blobs.sort();
assert_eq!(blobs, vec![bar_path, foo_path]);
Ok(())
}
/// Parameter for `test_provide_get_loop`, that determines how we handle the fetched data from the
/// `iroh get` command
#[derive(Debug, PartialEq)]
enum Output {
/// Indicates we should save the content as a file in the given directory, by passing the path
/// to the `--out` argument in `iroh get`
Path,
/// Indicates we should pipe the content to `stdout` of the `iroh get` process
Stdout,
}
/// Parameter for `test_provide_get_loop`, that determines how we send the data to the `provide`
/// command.
#[derive(Debug, PartialEq)]
enum Input {
/// Indicates we should pass the content as an argument to the `iroh provide` command
Path,
/// Idincates we should pipe the content via `stdin` to the `iroh provide` command
Stdin,
}
fn iroh_bin() -> &'static str {
env!("CARGO_BIN_EXE_iroh")
}
fn make_provider(path: &Path, input: &Input, home: impl AsRef<Path>) -> Result<ProvideProcess> {
// spawn a provider & optionally provide from stdin
let mut command = Command::new(iroh_bin());
let res = command
.stderr(Stdio::null())
.stdout(Stdio::piped())
.env("RUST_LOG", "debug")
.env(
"IROH_DATA_DIR",
home.as_ref().join("iroh_data_dir").as_os_str(),
)
.stderr(Stdio::piped())
.arg("provide")
.arg(path)
.arg("--addr")
.arg(ADDR)
.arg("--rpc-port")
.arg("disabled");
let provider = match input {
Input::Stdin => {
let f = File::open(path)?;
let stdin = Stdio::from(f);
res.stdin(stdin).spawn()?
}
Input::Path => res.stdin(Stdio::null()).spawn()?,
};
// wrap in `ProvideProcess` to ensure the spawned process is killed on drop
Ok(ProvideProcess { child: provider })
}
/// Test the provide and get loop for success, stderr output, and file contents.
///
/// Can optionally pipe the given `path` content to the provider from stdin & can optionally save the output to an `out` path.
///
/// Runs the provider as a child process that stays alive until the getter has completed. Then
/// checks the output of the "provide" and "get" processes against expected regex output. Finally,
/// test the content fetched from the "get" process is the same as the "provided" content.
fn test_provide_get_loop(path: &Path, input: Input, output: Output) -> Result<()> {
let out = if output == Output::Stdout {
None
} else {
let dir = testdir!();
Some(dir.join("out"))
};
let src = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("fixtures");
let path = src.join(path);
let num_blobs = if path.is_dir() {
WalkDir::new(&path)
.into_iter()
.filter_map(|x| x.ok().filter(|x| x.file_type().is_file()))
.count()
} else {
1
};
let home = testdir!();
let mut provider = make_provider(&path, &input, home)?;
// std::io::copy(&mut provider.child.stderr.take().unwrap(), &mut std::io::stderr())?;
let stdout = provider.child.stdout.take().unwrap();
let stdout = BufReader::new(stdout);
// test provide output & get all in one ticket from stderr
let all_in_one = match_provide_output(stdout, num_blobs, input)?;
// create a `get-ticket` cmd & optionally provide out path
let mut cmd = Command::new(iroh_bin());
cmd.arg("get-ticket").arg(all_in_one);
let cmd = if let Some(ref out) = out {
cmd.arg("--out").arg(out)
} else {
&mut cmd
};
// test get stderr output
let get_output = cmd.output()?;
assert!(get_output.status.success());
// test output
match out {
None => {
assert!(!get_output.stdout.is_empty());
let expect_content = std::fs::read(path)?;
assert_eq!(expect_content, get_output.stdout);
}
Some(out) => compare_files(path, out)?,
};
assert!(!get_output.stderr.is_empty());
match_get_stderr(get_output.stderr)
}
/// Wrapping the [`Child`] process here allows us to impl the `Drop` trait ensuring the provide
/// process is killed when it goes out of scope.
struct ProvideProcess {
child: Child,
}
impl Drop for ProvideProcess {
fn drop(&mut self) {
self.child.kill().ok();
self.child.try_wait().ok();
}
}
fn compare_files(expect_path: impl AsRef<Path>, got_dir_path: impl AsRef<Path>) -> Result<()> {
let expect_path = expect_path.as_ref();
let got_dir_path = got_dir_path.as_ref();
if expect_path.is_dir() {
let paths = WalkDir::new(expect_path).into_iter().filter(|x| {
x.as_ref()
.ok()
.map(|x| x.file_type().is_file())
.unwrap_or(false)
});
for entry in paths {
let entry = entry?;
let file_path = entry.path();
let rel = file_path.strip_prefix(expect_path)?;
let expected_file_path = got_dir_path.join(rel);
let got = std::fs::read(file_path)?;
let expect = std::fs::read(expected_file_path)?;
assert_eq!(expect, got);
}
} else {
let file_name = expect_path.file_name().unwrap();
let expect = std::fs::read(expect_path)?;
let got = std::fs::read(got_dir_path.join(file_name))?;
assert_eq!(expect, got);
}
Ok(())
}
/// Looks for regex matches on stderr output for the getter.
///
/// Errors on the first regex mis-match or if the stderr output has fewer lines than expected
fn match_get_stderr(stderr: Vec<u8>) -> Result<()> {
println!("{}", String::from_utf8_lossy(&stderr[..]));
let stderr = std::io::BufReader::new(&stderr[..]);
assert_matches_line![
stderr,
r"Fetching: [\da-z]{59}"; 1,
r"\[1/3\] Connecting ..."; 1,
r"\[2/3\] Requesting ..."; 1,
r"\[3/3\] Downloading ..."; 1,
r"\d* file\(s\) with total transfer size [\d.]* ?[BKMGT]?i?B"; 1,
r"Transferred \d*.?\d*? ?[BKMGT]i?B? in \d* seconds?, \d*.?\d*? [BKMGT]iB/s"; 1
];
Ok(())
}
/// Looks for regex matches on each line of output for the provider, returning the "all in one ticket"
/// that can be used to 'get' from another process.
///
/// Errors on the first regex mismatch or if the stderr output has fewer lines than expected
fn match_provide_output<T: Read>(
reader: BufReader<T>,
num_blobs: usize,
input: Input,
) -> Result<String> {
// if we are using `stdin` we don't "read" any files, so the provider does not output any lines
// about "Reading"
let _reading_line_num = match input {
Input::Stdin => 0,
Input::Path => 1,
};
let mut caps = assert_matches_line![
reader,
r"Listening address: [\d.:]*"; 1,
r"PeerID: [_\w\d-]*"; 1,
r"Auth token: [\w\d]*"; 1,
r""; 1,
r"Adding .*"; 1,
r"- \S*: \d*.?\d*? ?[BKMGT]i?B?"; num_blobs,
r"Total: [_\w\d-]*"; 1,
r""; 1,
r"Collection: [\da-z]{59}"; 1,
r"All-in-one ticket: ([_a-zA-Z\d-]*)"; 1
];
// return the capture of the all in one ticket, should be the last capture
caps.pop().context("Expected at least one capture.")
}
#[macro_export]
/// Ensures each line of the first expression matches the regex of each following expression. Each
/// regex expression is followed by the number of consecutive lines it should match.
///
/// Returns a vec of `String`s of any captures made against the regex on each line.
///
/// # Examples
/// ```
/// let expr = b"hello world!\nNice to meet you!\n02/23/2023\n02/23/2023\n02/23/2023";
/// let buf_reader = std::io::BufReader::new(&expr[..]);
/// assert_matches_line![
/// buf_reader,
/// r"hello world!"; 1,
/// r"\S*$"; 1,
/// r"\d{2}/\d{2}/\d{4}"; 3
/// ];
/// ```
macro_rules! assert_matches_line {
( $x:expr, $( $z:expr;$a:expr ),* ) => {
{
let mut lines = $x.lines();
let mut caps = Vec::new();
$(
let rx = regex::Regex::new($z)?;
for _ in 0..$a {
let line = lines.next().context("Unexpected end of stderr reader")??;
if let Some(cap) = rx.captures(line.trim()) {
for i in 0..cap.len() {
if let Some(capture_group) = cap.get(i) {
caps.push(capture_group.as_str().to_string());
}
}
} else {
anyhow::bail!(format!("no match found\nexpected match for '{}'\ngot '{line}'", $z));
};
}
)*
caps
}
};
}