Skip to content

Commit

Permalink
Get e2e tests working by using threads and assert_command
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofl committed Dec 30, 2023
1 parent 0fbd531 commit 8b02990
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
21 changes: 14 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
// #![allow(warnings)]


use log::{error, info, warn};

use std::path::PathBuf;
use std::ops::Deref;
use std::sync::Arc;





mod tests;
mod utils;
mod servers;
use crate::servers::{*};

use clap::{Parser};
extern crate ctrlc;
extern crate core;

#[derive(Parser, Debug)]
#[command(author, version, about = "Any-serve", long_about = "Developers swiss-knife of quick file serving")]
Expand Down Expand Up @@ -219,3 +213,16 @@ async fn main() {
futures::future::join_all(spawned_runners).await;
return;
}

#[cfg(test)]
mod tests {
use predicates::prelude::*;
use assert_cmd::Command;

#[test]
fn test_cli_help() {
let mut cmd = Command::cargo_bin("any-serve").unwrap();
cmd.arg("--help");
cmd.assert().success().stdout(predicate::str::contains("Usage: any-serve"));
}
}
51 changes: 38 additions & 13 deletions src/servers/ftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,48 @@ impl FTPRunner for Server {
/////////////////////////////////////////////////////////////////////////////////////
// TESTS //
/////////////////////////////////////////////////////////////////////////////////////
// #[cfg(test)]
// mod tests {
// #[tokio::test]
// async fn test_e2e() {
// let bind_ip = String::from("127.0.0.1");
// let port: u16 = 2121;
// let (temp_dir_path, file_name) =
// crate::tests::common::test_server::mkfile().await.expect("Failed to create temp file...");
//
// let s = Arc::new(<Server as FTPRunner>::new(temp_dir_path.clone(), bind_ip.clone(), port));
// let cmd = format!("wget -t2 -T1 {}://{}:{}/{} -O /tmp/out.txt",
// s.protocol.to_string(), bind_ip.clone(), port, file_name);
//
// crate::tests::common::test_server::test_server_e2e(s, cmd).await;
// }
// }


#[cfg(test)]
mod tests {
use crate::servers::{Server, FTPRunner};
use std::{sync::Arc};
use std::string::String;
use std::time::Duration;
use assert_cmd::Command;
use std::thread;

#[tokio::test]
async fn test_e2e() {
let bind_ip = String::from("127.0.0.1");
let port: u16 = 2121;
let (temp_dir_path, file_name) =
crate::tests::common::test_server::mkfile().await.expect("Failed to create temp file...");
#[test]
fn test_e2e() {
let server = thread::spawn(|| {
let mut cmd = Command::cargo_bin("any-serve").unwrap();
cmd.timeout(Duration::from_secs(2));
cmd.args(&["--ftp", "-v"]);
cmd.unwrap()
});

let s = Arc::new(<Server as FTPRunner>::new(temp_dir_path.clone(), bind_ip.clone(), port));
let cmd = format!("wget -t2 -T1 {}://{}:{}/{} -O /tmp/out.txt",
s.protocol.to_string(), bind_ip.clone(), port, file_name);
let client = thread::spawn(|| {
thread::sleep(Duration::from_millis(1000));
let mut cmd = Command::new("wget");
cmd.env("PATH", "/bin");
cmd.args(&["-t2", "-T1", "ftp://127.0.0.1:2121/in.txt", "-O", "/tmp/out.txt"]);
cmd.unwrap()
});

crate::tests::common::test_server::test_server_e2e(s, cmd).await;
let _ = server.join();
client.join().unwrap();
}
}
32 changes: 19 additions & 13 deletions src/servers/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,27 @@ impl HTTPRunner for Server {

#[cfg(test)]
mod tests {
use crate::servers::{Server, HTTPRunner};
use std::{sync::Arc};
use std::string::String;
use std::time::Duration;
use assert_cmd::Command;
use std::thread;

#[tokio::test]
async fn test_e2e() {
let bind_ip = String::from("127.0.0.1");
let port: u16 = 8080;
let (temp_dir_path, file_name) =
crate::tests::common::test_server::mkfile().await.expect("Failed to create temp file...");
#[test]
fn test_e2e() {
let server = thread::spawn(|| {
let mut cmd = Command::cargo_bin("any-serve").unwrap();
cmd.timeout(Duration::from_secs(1));
cmd.args(&["--http", "-v"]);
cmd.unwrap()
});

let s = Arc::new(<Server as HTTPRunner>::new(temp_dir_path.clone(), bind_ip.clone(), port));
let cmd = format!("wget -t2 -T1 {}://{}:{}/{} -O /tmp/out.txt",
s.protocol.to_string(), bind_ip.clone(), port, file_name);
let client = thread::spawn(|| {
let mut cmd = Command::new("wget");
cmd.env("PATH", "/bin");
cmd.args(&["-t2", "-T1", "http://127.0.0.1:8080/in.txt", "-O", "/tmp/out.txt"]);
cmd.unwrap()
});

crate::tests::common::test_server::test_server_e2e(s, cmd).await;
let _ = server.join();
client.join().unwrap();
}
}

0 comments on commit 8b02990

Please sign in to comment.