2021-08-31 03:46:51 +00:00
|
|
|
use crate::cli::utils;
|
2021-08-29 06:03:18 +00:00
|
|
|
use assert_cmd::Command;
|
|
|
|
use distant_core::*;
|
|
|
|
use rstest::*;
|
2021-09-14 17:54:45 +00:00
|
|
|
use std::{ffi::OsStr, net::SocketAddr, thread};
|
2021-08-29 07:00:39 +00:00
|
|
|
use tokio::{runtime::Runtime, sync::mpsc};
|
2021-08-30 06:01:08 +00:00
|
|
|
|
2021-09-05 22:20:47 +00:00
|
|
|
const LOG_PATH: &str = "/tmp/test.distant.server.log";
|
2021-08-29 06:03:18 +00:00
|
|
|
|
|
|
|
/// Context for some listening distant server
|
|
|
|
pub struct DistantServerCtx {
|
|
|
|
pub addr: SocketAddr,
|
|
|
|
pub auth_key: String,
|
2021-08-29 07:00:39 +00:00
|
|
|
done_tx: mpsc::Sender<()>,
|
2021-08-29 06:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DistantServerCtx {
|
2021-08-29 07:14:25 +00:00
|
|
|
pub fn initialize() -> Self {
|
|
|
|
let ip_addr = "127.0.0.1".parse().unwrap();
|
|
|
|
let (done_tx, mut done_rx) = mpsc::channel(1);
|
|
|
|
let (started_tx, mut started_rx) = mpsc::channel(1);
|
|
|
|
|
|
|
|
// NOTE: We spawn a dedicated thread that runs our tokio runtime separately
|
|
|
|
// from our test itself because using assert_cmd blocks the thread
|
|
|
|
// and prevents our runtime from working unless we make the tokio
|
|
|
|
// test multi-threaded using `tokio::test(flavor = "multi_thread", worker_threads = 1)`
|
|
|
|
// which isn't great because we're only using async tests for our
|
|
|
|
// server itself; so, we hide that away since our test logic doesn't need to be async
|
|
|
|
thread::spawn(move || match Runtime::new() {
|
|
|
|
Ok(rt) => {
|
|
|
|
rt.block_on(async move {
|
2021-08-30 06:01:08 +00:00
|
|
|
let logger = utils::init_logging(LOG_PATH);
|
2021-09-04 04:49:37 +00:00
|
|
|
let opts = DistantServerOptions {
|
|
|
|
shutdown_after: None,
|
|
|
|
max_msg_capacity: 100,
|
|
|
|
};
|
2021-09-14 17:54:45 +00:00
|
|
|
let key = SecretKey::default();
|
|
|
|
let key_hex_string = key.unprotected_to_hex_key();
|
|
|
|
let codec = XChaCha20Poly1305Codec::from(key);
|
2021-09-04 04:49:37 +00:00
|
|
|
let (_server, port) =
|
2021-09-14 17:54:45 +00:00
|
|
|
DistantServer::bind(ip_addr, "0".parse().unwrap(), codec, opts)
|
2021-09-04 04:49:37 +00:00
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-09-14 17:54:45 +00:00
|
|
|
started_tx.send(Ok((port, key_hex_string))).await.unwrap();
|
2021-08-29 07:14:25 +00:00
|
|
|
|
|
|
|
let _ = done_rx.recv().await;
|
2021-08-30 06:01:08 +00:00
|
|
|
logger.flush();
|
|
|
|
logger.shutdown();
|
2021-08-29 07:14:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
Err(x) => {
|
|
|
|
started_tx.blocking_send(Err(x)).unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Extract our server startup data if we succeeded
|
|
|
|
let (port, auth_key) = started_rx.blocking_recv().unwrap().unwrap();
|
|
|
|
|
|
|
|
Self {
|
|
|
|
addr: SocketAddr::new(ip_addr, port),
|
|
|
|
auth_key,
|
|
|
|
done_tx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-29 06:03:18 +00:00
|
|
|
/// 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();
|
|
|
|
cmd.arg(subcommand)
|
|
|
|
.args(&["--session", "environment"])
|
|
|
|
.env("DISTANT_HOST", self.addr.ip().to_string())
|
|
|
|
.env("DISTANT_PORT", self.addr.port().to_string())
|
2021-08-29 18:04:49 +00:00
|
|
|
.env("DISTANT_AUTH_KEY", self.auth_key.as_str());
|
2021-08-29 06:03:18 +00:00
|
|
|
cmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for DistantServerCtx {
|
2021-08-29 07:00:39 +00:00
|
|
|
/// Kills server upon drop
|
2021-08-29 06:03:18 +00:00
|
|
|
fn drop(&mut self) {
|
2021-08-29 07:14:25 +00:00
|
|
|
let _ = self.done_tx.send(());
|
2021-08-29 06:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[fixture]
|
2021-08-29 07:14:25 +00:00
|
|
|
pub fn ctx() -> &'static DistantServerCtx {
|
2021-08-29 18:04:49 +00:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref CTX: DistantServerCtx = DistantServerCtx::initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
&CTX
|
2021-08-29 07:14:25 +00:00
|
|
|
}
|
2021-08-29 06:03:18 +00:00
|
|
|
|
2021-08-29 18:04:49 +00:00
|
|
|
#[fixture]
|
|
|
|
pub fn action_cmd(ctx: &'_ DistantServerCtx) -> Command {
|
|
|
|
ctx.new_cmd("action")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[fixture]
|
|
|
|
pub fn lsp_cmd(ctx: &'_ DistantServerCtx) -> Command {
|
|
|
|
ctx.new_cmd("lsp")
|
2021-08-29 06:03:18 +00:00
|
|
|
}
|