no e2e feature, and don't assume 'cargo run' is a good default

This commit is contained in:
James Wilson
2021-07-13 11:12:23 +01:00
parent 66bf24dc4d
commit faedba87d4
7 changed files with 68 additions and 80 deletions
@@ -0,0 +1,36 @@
//! A pair of commands we can use when running `cargo test` style tests in this workspace
//! that want to test the current code. For more external tests, we may want to ask for the
//! commands, or connect to a running instance instead.
use super::Command;
use std::path::PathBuf;
/// Runs `cargo run` in the current workspace to start up a telemetry shard process
pub fn telemetry_shard() -> Result<Command, std::io::Error> {
telemetry_command("telemetry_shard")
}
/// Runs `cargo run` in the current workspace to start up a telemetry core process
pub fn telemetry_core() -> Result<Command, std::io::Error> {
telemetry_command("telemetry_core")
}
fn telemetry_command(bin: &'static str) -> Result<Command, std::io::Error> {
let mut workspace_dir = try_find_workspace_dir()?;
workspace_dir.push("Cargo.toml");
Ok(Command::new("cargo")
.arg("run")
.arg("--bin")
.arg(bin)
.arg("--manifest-path")
.arg(workspace_dir)
.arg("--"))
}
/// A _very_ naive way to find the workspace ("backend") directory
/// from the current path (which is assumed to be inside it).
fn try_find_workspace_dir() -> Result<PathBuf, std::io::Error> {
let mut dir = std::env::current_dir()?;
while !dir.ends_with("backend") && dir.pop() {}
Ok(dir)
}
@@ -1,30 +0,0 @@
use super::Command;
use std::path::PathBuf;
pub fn default_telemetry_shard_command() -> Result<Command, std::io::Error> {
default_telemetry_command("telemetry_shard")
}
pub fn default_telemetry_core_command() -> Result<Command, std::io::Error> {
default_telemetry_command("telemetry_core")
}
fn default_telemetry_command(bin: &'static str) -> Result<Command, std::io::Error> {
let mut workspace_dir = try_find_workspace_dir()?;
workspace_dir.push("Cargo.toml");
Ok(Command::new("cargo")
.arg("run")
.arg("--bin")
.arg(bin)
.arg("--manifest-path")
.arg(workspace_dir)
.arg("--"))
}
/// A _very_ naive way to find the workspace ("backend") directory
/// from the current path (which is assumed to be inside it).
fn try_find_workspace_dir() -> Result<PathBuf, std::io::Error> {
let mut dir = std::env::current_dir()?;
while !dir.ends_with("backend") && dir.pop() {}
Ok(dir)
}
+1 -1
View File
@@ -1,6 +1,6 @@
mod utils;
mod server;
mod default_commands;
pub mod cargo_run_commands;
pub mod channels;
pub use server::*;
+13 -28
View File
@@ -11,21 +11,12 @@ id_type! {
}
pub struct StartOpts {
/// Optional command to run to start a shard (instead of `telemetry_shard`).
/// Command to run to start a shard.
/// The `--listen` and `--log` arguments will be appended within and shouldn't be provided.
pub shard_command: Option<Command>,
/// Optional command to run to start a telemetry core process (instead of `telemetry_core`).
pub shard_command: Command,
/// Command to run to start a telemetry core process.
/// The `--listen` and `--log` arguments will be appended within and shouldn't be provided.
pub core_command: Option<Command>
}
impl Default for StartOpts {
fn default() -> Self {
StartOpts {
shard_command: None,
core_command: None
}
}
pub core_command: Command
}
pub struct ConnectToExistingOpts {
@@ -57,7 +48,9 @@ pub enum Error {
pub struct Server {
/// URI to connect a shard to core:
core_shard_submit_uri: Option<http::Uri>,
/// Command to run to start a new shard:
/// Command to run to start a new shard. Optional
/// because if we connect to running instances it'll
/// be unset.
shard_command: Option<Command>,
/// Shard processes that we can connect to
shards: DenseMap<ProcessId, ShardProcess>,
@@ -119,10 +112,10 @@ impl Server {
None => return Err(Error::CannotAddShardNoHandle)
};
let mut shard_cmd: TokioCommand = match &self.shard_command {
Some(cmd) => cmd.clone(),
None => super::default_commands::default_telemetry_shard_command()?
}.into();
let mut shard_cmd: TokioCommand = self.shard_command
.clone()
.ok_or_else(|| Error::CannotAddShardNoHandle)?
.into();
shard_cmd
.arg("--listen")
@@ -169,18 +162,10 @@ impl Server {
Ok(pid)
}
/// Start a telemetry_core process with default opts. From here, we can add/remove shards as needed.
pub async fn start_default() -> Result<Server, Error> {
Server::start(StartOpts::default()).await
}
/// Start a telemetry_core process. From here, we can add/remove shards as needed.
pub async fn start(opts: StartOpts) -> Result<Server, Error> {
let mut core_cmd: TokioCommand = match opts.core_command {
Some(cmd) => cmd,
None => super::default_commands::default_telemetry_core_command()?
}.into();
let mut core_cmd: TokioCommand = opts.core_command.into();
let mut child = core_cmd
.arg("--listen")
@@ -208,7 +193,7 @@ impl Server {
.expect("valid feed URI");
Ok(Server {
shard_command: opts.shard_command,
shard_command: Some(opts.shard_command),
core_shard_submit_uri: Some(format!("http://127.0.0.1:{}/shard_submit", core_port)
.parse()
.expect("valid shard_submit URI")),