mirror of https://github.com/chipsenkbeil/distant
Add first example of an e2e test for distant cli
parent
54a7f567a1
commit
188b5f74e4
@ -0,0 +1,77 @@
|
||||
use crate::fixtures::*;
|
||||
use assert_fs::prelude::*;
|
||||
use distant_core::{
|
||||
data::{Error, ErrorKind},
|
||||
Response, ResponseData,
|
||||
};
|
||||
use rstest::*;
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn should_print_out_file_contents(#[future] ctx: DistantServerCtx) {
|
||||
let temp = assert_fs::TempDir::new().unwrap();
|
||||
let file = temp.child("test-file");
|
||||
file.write_str("some\ntext\ncontent").unwrap();
|
||||
|
||||
ctx.await
|
||||
.new_cmd("action")
|
||||
.args(&["file-read", file.to_str().unwrap()])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("some\ntext\ncontent\n")
|
||||
.stderr("");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn should_support_json_output(#[future] ctx: DistantServerCtx) {
|
||||
let temp = assert_fs::TempDir::new().unwrap();
|
||||
let file = temp.child("test-file");
|
||||
file.write_str("some\ntext\ncontent").unwrap();
|
||||
|
||||
let cmd = ctx
|
||||
.await
|
||||
.new_cmd("action")
|
||||
.args(&["--format", "json"])
|
||||
.args(&["file-read", file.to_str().unwrap()])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr("");
|
||||
|
||||
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
|
||||
assert_eq!(
|
||||
res.payload[0],
|
||||
ResponseData::Blob {
|
||||
data: b"some\ntext\ncontent".to_vec()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
||||
async fn yield_an_error_when_fails(#[future] ctx: DistantServerCtx) {
|
||||
let temp = assert_fs::TempDir::new().unwrap();
|
||||
let file = temp.child("missing-file");
|
||||
|
||||
let cmd = ctx
|
||||
.await
|
||||
.new_cmd("action")
|
||||
.args(&["--format", "json"])
|
||||
.args(&["file-read", file.to_str().unwrap()])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr("");
|
||||
|
||||
let res: Response = serde_json::from_slice(&cmd.get_output().stdout).unwrap();
|
||||
assert!(
|
||||
matches!(
|
||||
res.payload[0],
|
||||
ResponseData::Error(Error {
|
||||
kind: ErrorKind::NotFound,
|
||||
..
|
||||
})
|
||||
),
|
||||
"Unexpected response: {:?}",
|
||||
res.payload[0]
|
||||
);
|
||||
}
|
@ -0,0 +1 @@
|
||||
mod file_read_test;
|
@ -0,0 +1,2 @@
|
||||
mod action;
|
||||
mod fixtures;
|
@ -0,0 +1,56 @@
|
||||
use assert_cmd::Command;
|
||||
use distant_core::*;
|
||||
use rstest::*;
|
||||
use std::{ffi::OsStr, net::SocketAddr, time::Duration};
|
||||
|
||||
/// Timeout to wait for a command to complete
|
||||
const TIMEOUT_SECS: u64 = 10;
|
||||
|
||||
/// Context for some listening distant server
|
||||
pub struct DistantServerCtx {
|
||||
pub addr: SocketAddr,
|
||||
pub auth_key: String,
|
||||
pub server: DistantServer,
|
||||
}
|
||||
|
||||
impl DistantServerCtx {
|
||||
/// Produces a new test command that configures some distant command
|
||||
/// configured with an environment that can talk to a remote distant server
|
||||
pub fn new_cmd(&self, subcommand: impl AsRef<OsStr>) -> Command {
|
||||
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
|
||||
|
||||
println!("DISTANT_HOST = {}", self.addr.ip());
|
||||
println!("DISTANT_PORT = {}", self.addr.port());
|
||||
println!("DISTANT_AUTH_KEY = {}", self.auth_key);
|
||||
|
||||
// NOTE: We define a command that has a timeout of 10s because the handshake
|
||||
// involved in a non-release test can take several seconds
|
||||
cmd.arg(subcommand)
|
||||
.args(&["--session", "environment"])
|
||||
.env("DISTANT_HOST", self.addr.ip().to_string())
|
||||
.env("DISTANT_PORT", self.addr.port().to_string())
|
||||
.env("DISTANT_AUTH_KEY", self.auth_key.as_str())
|
||||
.timeout(Duration::from_secs(TIMEOUT_SECS));
|
||||
cmd
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DistantServerCtx {
|
||||
fn drop(&mut self) {
|
||||
self.server.abort();
|
||||
}
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
pub async fn ctx() -> DistantServerCtx {
|
||||
let ip_addr = "127.0.0.1".parse().unwrap();
|
||||
let server = DistantServer::bind(ip_addr, "0".parse().unwrap(), None, 100)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
DistantServerCtx {
|
||||
addr: SocketAddr::new(ip_addr, server.port()),
|
||||
auth_key: server.to_unprotected_hex_auth_key(),
|
||||
server,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue