From e730a80b59a92f2c82f956a886ebf659b40a856b Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Tue, 24 Sep 2024 13:35:11 +0100 Subject: [PATCH 1/4] Add testing utility crate --- Cargo.lock | 264 ++++++++++++++++++++++++++++++----- Cargo.toml | 5 +- testing-utils/Cargo.toml | 13 ++ testing-utils/src/cargo.rs | 127 +++++++++++++++++ testing-utils/src/lib.rs | 9 ++ testing-utils/src/network.rs | 25 ++++ testing-utils/src/podman.rs | 75 ++++++++++ 7 files changed, 485 insertions(+), 33 deletions(-) create mode 100644 testing-utils/Cargo.toml create mode 100644 testing-utils/src/cargo.rs create mode 100644 testing-utils/src/lib.rs create mode 100644 testing-utils/src/network.rs create mode 100644 testing-utils/src/podman.rs diff --git a/Cargo.lock b/Cargo.lock index ceb817ff..cdef57ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -207,7 +207,7 @@ dependencies = [ "pin-project-lite", "rustversion", "serde", - "sync_wrapper", + "sync_wrapper 0.1.2", "tower", "tower-layer", "tower-service", @@ -390,7 +390,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -832,6 +832,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" +[[package]] +name = "encoding_rs" +version = "0.8.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b45de904aa0b010bce2ab45264d0631681847fa7b6f2eaa7dab7619943bc4f59" +dependencies = [ + "cfg-if", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -1275,6 +1284,23 @@ dependencies = [ "want", ] +[[package]] +name = "hyper-rustls" +version = "0.27.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08afdbb5c31130e3034af566421053ab03787c640246a446327f550d11bcb333" +dependencies = [ + "futures-util", + "http 1.1.0", + "hyper 1.3.1", + "hyper-util", + "rustls 0.23.10", + "rustls-pki-types", + "tokio", + "tokio-rustls 0.26.0", + "tower-service", +] + [[package]] name = "hyper-timeout" version = "0.4.1" @@ -1860,6 +1886,18 @@ dependencies = [ "tracing", ] +[[package]] +name = "nix" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" +dependencies = [ + "bitflags 2.5.0", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "noisy_float" version = "0.2.0" @@ -2271,7 +2309,7 @@ dependencies = [ "libc", "redox_syscall 0.5.1", "smallvec", - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -2696,6 +2734,49 @@ dependencies = [ "bytecheck", ] +[[package]] +name = "reqwest" +version = "0.12.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8f4955649ef5c38cc7f9e8aa41761d48fb9677197daea9984dc54f56aad5e63" +dependencies = [ + "base64 0.22.1", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2 0.4.5", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.3.1", + "hyper-rustls", + "hyper-tls", + "hyper-util", + "ipnet", + "js-sys", + "log", + "mime", + "native-tls", + "once_cell", + "percent-encoding", + "pin-project-lite", + "rustls-pemfile", + "serde", + "serde_json", + "serde_urlencoded", + "sync_wrapper 1.0.1", + "system-configuration", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "windows-registry", +] + [[package]] name = "ring" version = "0.17.8" @@ -2798,6 +2879,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls" +version = "0.23.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" +dependencies = [ + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + [[package]] name = "rustls-native-certs" version = "0.7.0" @@ -2955,6 +3049,18 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_with" version = "3.8.1" @@ -3197,6 +3303,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "sync_wrapper" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +dependencies = [ + "futures-core", +] + [[package]] name = "synstructure" version = "0.13.1" @@ -3208,6 +3323,27 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "system-configuration" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c879d448e9d986b661742763247d3693ed13609438cf3d006f51f5368a5ba6b" +dependencies = [ + "bitflags 2.5.0", + "core-foundation", + "system-configuration-sys", +] + +[[package]] +name = "system-configuration-sys" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e1d1b10ced5ca923a1fcb8d03e96b8d3268065d724548c0211415ff6ac6bac4" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "taos" version = "0.10.27" @@ -3329,6 +3465,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "testing-utils" +version = "0.1.0" +dependencies = [ + "nix", + "reqwest", + "tokio", + "tracing", +] + [[package]] name = "thiserror" version = "1.0.63" @@ -3426,6 +3572,7 @@ dependencies = [ "libc", "mio 1.0.1", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.52.0", @@ -3468,7 +3615,18 @@ version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" dependencies = [ - "rustls", + "rustls 0.22.4", + "rustls-pki-types", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" +dependencies = [ + "rustls 0.23.10", "rustls-pki-types", "tokio", ] @@ -3560,7 +3718,7 @@ dependencies = [ "rustls-pemfile", "rustls-pki-types", "tokio", - "tokio-rustls", + "tokio-rustls 0.25.0", "tokio-stream", "tower", "tower-layer", @@ -3930,6 +4088,18 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.92" @@ -4007,7 +4177,37 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-registry" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" +dependencies = [ + "windows-result", + "windows-strings", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", ] [[package]] @@ -4025,7 +4225,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.5", + "windows-targets 0.52.6", ] [[package]] @@ -4045,18 +4245,18 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.5", - "windows_aarch64_msvc 0.52.5", - "windows_i686_gnu 0.52.5", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc 0.52.5", - "windows_x86_64_gnu 0.52.5", - "windows_x86_64_gnullvm 0.52.5", - "windows_x86_64_msvc 0.52.5", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] [[package]] @@ -4067,9 +4267,9 @@ checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" @@ -4079,9 +4279,9 @@ checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_aarch64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" @@ -4091,15 +4291,15 @@ checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" @@ -4109,9 +4309,9 @@ checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_i686_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" @@ -4121,9 +4321,9 @@ checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnu" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" @@ -4133,9 +4333,9 @@ checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" @@ -4145,9 +4345,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "windows_x86_64_msvc" -version = "0.52.5" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" diff --git a/Cargo.toml b/Cargo.toml index 02d668be..5910b76f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ "nexus-writer", "simulator", "streaming-types", + "testing-utils", "trace-archiver-hdf5", "trace-archiver-tdengine", "trace-reader", @@ -34,6 +35,7 @@ metrics = "0.22.3" metrics-exporter-prometheus = "0.14.0" ndarray = "0.15.6" ndarray-stats = "0.5.1" +nix = { version = "0.29.0", features = ["process", "signal"] } num = "0.4.3" opentelemetry = "0.22.0" opentelemetry-otlp = { version = "0.15.0", features = ["trace", "tonic", "tls-roots"] } @@ -43,12 +45,13 @@ rand_distr = "0.4.3" ratatui = "0.22.0" rayon = "1.10.0" rdkafka = { version = "0.31.0", features = [ "cmake-build", "ssl", "gssapi", "sasl", ] } +reqwest = "0.12.7" serde = { version = "1", features = ["derive"] } serde_json = { version = "1.0.127" } supermusr-common = { path = "./common" } supermusr-streaming-types = { path = "./streaming-types" } taos = { version = "0.10.27", default_features = false, features = ["ws"] } -tokio = { version = "1.40", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.40", features = ["macros", "rt-multi-thread", "process"] } thiserror = "1.0.63" tracing = "0.1.40" tracing-opentelemetry = "0.23.0" diff --git a/testing-utils/Cargo.toml b/testing-utils/Cargo.toml new file mode 100644 index 00000000..da986753 --- /dev/null +++ b/testing-utils/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "testing-utils" +license.workspace = true +version.workspace = true +edition.workspace = true + +[dependencies] +nix.workspace = true +reqwest.workspace = true +tokio.workspace = true +tracing.workspace = true + +[dev-dependencies] diff --git a/testing-utils/src/cargo.rs b/testing-utils/src/cargo.rs new file mode 100644 index 00000000..47b9c30a --- /dev/null +++ b/testing-utils/src/cargo.rs @@ -0,0 +1,127 @@ +use nix::{ + sys::signal::{self, Signal}, + unistd::{self, Pid}, +}; +use std::{ + path::PathBuf, + process::Stdio, + sync::{Arc, Mutex}, +}; +use tokio::{ + io::{AsyncBufReadExt, BufReader}, + process::Command, + task::JoinHandle, +}; +use tracing::{debug, info}; + +type SharedPid = Arc>>; + +pub struct CargoBinaryRunner { + pid: SharedPid, + handle: Option>, +} + +impl CargoBinaryRunner { + pub fn new(binary: String, args: Vec, env: Vec<(String, String)>) -> Self { + let pid = SharedPid::default(); + + let mut workspace_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + workspace_dir.pop(); + + let handle = { + let name = binary.clone(); + + let pid = pid.clone(); + + Some(tokio::spawn(async move { + let mut cargo_process = unsafe { + Command::new("cargo") + .current_dir(workspace_dir) + .envs(env) + // Run the specified binary + .arg("run") + .arg("--bin") + .arg(binary) + // In release mode + .arg("--release") + // With the specified arguments + .arg("--") + .args(args) + // Do nothing with stdin + .stdin(Stdio::null()) + // Capture stdout and stderr + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + // Call setsid, required for correct exit signal handling + .pre_exec(|| { + unistd::setsid()?; + Ok(()) + }) + .spawn() + .expect("process should be started") + }; + + // Get and store the PID + *pid.lock().unwrap() = Some(Pid::from_raw( + cargo_process.id().expect("process should have a PID") as i32, + )); + + let stdout = cargo_process.stdout.take().unwrap(); + let stderr = cargo_process.stderr.take().unwrap(); + + let mut stdout_reader = BufReader::new(stdout).lines(); + let mut stderr_reader = BufReader::new(stderr).lines(); + + loop { + tokio::select! { + line = stdout_reader.next_line() => { + match line { + Ok(Some(line)) => debug!("{name} stdout: {line}"), + Err(_) => break, + _ => (), + } + } + line = stderr_reader.next_line() => { + match line { + Ok(Some(line)) => debug!("{name} stderr: {line}"), + Err(_) => break, + _ => (), + } + } + // Wait for process to exit + result = cargo_process.wait() => { + info!("{name} cargo exited, ok={}", result.is_ok()); + *pid.lock().unwrap() = None; + break; + } + } + } + })) + }; + + Self { pid, handle } + } + + pub fn stop(&self) { + const EXIT_SIGNAL: Signal = Signal::SIGINT; + + // Request process to terminate + info!("Sending {} to process", EXIT_SIGNAL); + if let Some(pid) = *self.pid.lock().unwrap() { + signal::kill(pid, EXIT_SIGNAL).unwrap(); + } + } + + pub async fn wait(&mut self) { + // Wait for process to exit + if let Some(handle) = self.handle.take() { + let _ = handle.await; + } + } +} + +impl Drop for CargoBinaryRunner { + fn drop(&mut self) { + self.stop(); + } +} diff --git a/testing-utils/src/lib.rs b/testing-utils/src/lib.rs new file mode 100644 index 00000000..aacef000 --- /dev/null +++ b/testing-utils/src/lib.rs @@ -0,0 +1,9 @@ +mod cargo; +mod network; +mod podman; + +pub use self::{ + cargo::CargoBinaryRunner, + network::wait_for_url, + podman::PodmanDriver, +}; diff --git a/testing-utils/src/network.rs b/testing-utils/src/network.rs new file mode 100644 index 00000000..a8c48f34 --- /dev/null +++ b/testing-utils/src/network.rs @@ -0,0 +1,25 @@ +use tokio::time::{Duration, Instant}; +use tracing::{error, info}; + +pub async fn wait_for_url(url: &str, timeout: Duration) -> Result<(), ()> { + let client = reqwest::Client::new(); + let start = Instant::now(); + + loop { + let spent = Instant::now() - start; + + if spent > timeout { + error!("Timeout waiting for URL: {}", url); + return Err(()); + } + + if client.get(url).send().await.is_ok() { + info!("URL {} is available after {}s", url, spent.as_secs()); + break; + } + + tokio::time::sleep(Duration::from_secs(1)).await; + } + + Ok(()) +} diff --git a/testing-utils/src/podman.rs b/testing-utils/src/podman.rs new file mode 100644 index 00000000..38b6a5b2 --- /dev/null +++ b/testing-utils/src/podman.rs @@ -0,0 +1,75 @@ +use std::process::Command; +use tracing::{debug, info}; + +pub struct PodmanDriver { + container_id: String, +} + +impl PodmanDriver { + pub fn new( + image: &str, + ports: &[&str], + env_vars: &[&str], + volumes: &[&str], + args: &[&str], + ) -> Self { + // Build Podman args + let mut podman_args = vec![ + "run".to_string(), + "--detach".to_string(), + "--rm".to_string(), + ]; + + for port in ports.iter() { + podman_args.push("-p".to_string()); + podman_args.push(port.to_string()); + } + + for var in env_vars.iter() { + podman_args.push("-e".to_string()); + podman_args.push(var.to_string()); + } + + for volume in volumes.iter() { + podman_args.push("-v".to_string()); + podman_args.push(volume.to_string()); + } + + podman_args.push(image.to_string()); + + for arg in args.iter() { + podman_args.push(arg.to_string()); + } + + // Start the container + let container_start = Command::new("podman").args(podman_args).output().unwrap(); + debug!("Container start: {:?}", container_start); + + if container_start.status.success() && container_start.status.code().unwrap() == 0 { + let container_id = String::from_utf8(container_start.stdout) + .unwrap() + .trim() + .to_string(); + info!("Container ID: {container_id}"); + + Self { container_id } + } else { + panic!("Failed to start container"); + } + } + + fn stop(&self) { + // Stop the container + let container_stop = Command::new("podman") + .args(vec!["stop", &self.container_id]) + .output() + .unwrap(); + debug!("Container stop: {:?}", container_stop); + } +} + +impl Drop for PodmanDriver { + fn drop(&mut self) { + self.stop(); + } +} From c15618045abe8b438e7892a60f140d5ca28032ee Mon Sep 17 00:00:00 2001 From: Dan Nixon Date: Tue, 24 Sep 2024 13:39:42 +0100 Subject: [PATCH 2/4] fixup! Add testing utility crate --- testing-utils/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testing-utils/src/lib.rs b/testing-utils/src/lib.rs index aacef000..49fb02be 100644 --- a/testing-utils/src/lib.rs +++ b/testing-utils/src/lib.rs @@ -2,8 +2,4 @@ mod cargo; mod network; mod podman; -pub use self::{ - cargo::CargoBinaryRunner, - network::wait_for_url, - podman::PodmanDriver, -}; +pub use self::{cargo::CargoBinaryRunner, network::wait_for_url, podman::PodmanDriver}; From c6b6745f27d50edc50f0daae5c0ffeea6490be21 Mon Sep 17 00:00:00 2001 From: Modularius Date: Wed, 25 Sep 2024 13:13:27 +0000 Subject: [PATCH 3/4] Replaced unwraps with expects --- testing-utils/src/cargo.rs | 12 ++++++------ testing-utils/src/podman.rs | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/testing-utils/src/cargo.rs b/testing-utils/src/cargo.rs index 47b9c30a..0f708512 100644 --- a/testing-utils/src/cargo.rs +++ b/testing-utils/src/cargo.rs @@ -62,12 +62,12 @@ impl CargoBinaryRunner { }; // Get and store the PID - *pid.lock().unwrap() = Some(Pid::from_raw( + *pid.lock().expect("pid mutex should lock") = Some(Pid::from_raw( cargo_process.id().expect("process should have a PID") as i32, )); - let stdout = cargo_process.stdout.take().unwrap(); - let stderr = cargo_process.stderr.take().unwrap(); + let stdout = cargo_process.stdout.take().expect("stdout should exist"); + let stderr = cargo_process.stderr.take().expect("stderr should exist"); let mut stdout_reader = BufReader::new(stdout).lines(); let mut stderr_reader = BufReader::new(stderr).lines(); @@ -91,7 +91,7 @@ impl CargoBinaryRunner { // Wait for process to exit result = cargo_process.wait() => { info!("{name} cargo exited, ok={}", result.is_ok()); - *pid.lock().unwrap() = None; + *pid.lock().expect("pid mutex should lock") = None; break; } } @@ -107,8 +107,8 @@ impl CargoBinaryRunner { // Request process to terminate info!("Sending {} to process", EXIT_SIGNAL); - if let Some(pid) = *self.pid.lock().unwrap() { - signal::kill(pid, EXIT_SIGNAL).unwrap(); + if let Some(pid) = *self.pid.lock().expect("pid mutex should lock") { + signal::kill(pid, EXIT_SIGNAL).expect("Kill signal should send to process"); } } diff --git a/testing-utils/src/podman.rs b/testing-utils/src/podman.rs index 38b6a5b2..4f7109c0 100644 --- a/testing-utils/src/podman.rs +++ b/testing-utils/src/podman.rs @@ -42,12 +42,12 @@ impl PodmanDriver { } // Start the container - let container_start = Command::new("podman").args(podman_args).output().unwrap(); + let container_start = Command::new("podman").args(podman_args).output().expect("Podman start command should execute"); debug!("Container start: {:?}", container_start); - if container_start.status.success() && container_start.status.code().unwrap() == 0 { + if container_start.status.success() && container_start.status.code().expect("Exit code should exist") == 0 { let container_id = String::from_utf8(container_start.stdout) - .unwrap() + .expect("stdout should be convertable to utf8") .trim() .to_string(); info!("Container ID: {container_id}"); @@ -63,7 +63,7 @@ impl PodmanDriver { let container_stop = Command::new("podman") .args(vec!["stop", &self.container_id]) .output() - .unwrap(); + .expect("Podman stop command should execute"); debug!("Container stop: {:?}", container_stop); } } From a17fe1bb14031f01a239dd4a9b3130f2c4bd0a2d Mon Sep 17 00:00:00 2001 From: Modularius Date: Wed, 25 Sep 2024 13:20:14 +0000 Subject: [PATCH 4/4] Formatting --- testing-utils/src/podman.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/testing-utils/src/podman.rs b/testing-utils/src/podman.rs index 4f7109c0..47fde163 100644 --- a/testing-utils/src/podman.rs +++ b/testing-utils/src/podman.rs @@ -42,10 +42,19 @@ impl PodmanDriver { } // Start the container - let container_start = Command::new("podman").args(podman_args).output().expect("Podman start command should execute"); + let container_start = Command::new("podman") + .args(podman_args) + .output() + .expect("Podman start command should execute"); debug!("Container start: {:?}", container_start); - if container_start.status.success() && container_start.status.code().expect("Exit code should exist") == 0 { + if container_start.status.success() + && container_start + .status + .code() + .expect("Exit code should exist") + == 0 + { let container_id = String::from_utf8(container_start.stdout) .expect("stdout should be convertable to utf8") .trim()