From 3e12e95f660d8fda0dc83319ac73a5d7bf9d458b Mon Sep 17 00:00:00 2001 From: Herbert Wolverson Date: Thu, 19 Jan 2023 17:52:19 +0000 Subject: [PATCH] Create lqos_utils as a repository for useful macros. Move the string table macro. Start working on some command wrappers for easier use. --- src/rust/Cargo.lock | 8 ++ src/rust/Cargo.toml | 1 + src/rust/lqos_queue_tracker/Cargo.toml | 1 + .../lqos_queue_tracker/src/queue_types/mod.rs | 1 - .../src/queue_types/tc_cake.rs | 2 +- src/rust/lqos_utils/Cargo.toml | 7 ++ src/rust/lqos_utils/src/commands.rs | 96 +++++++++++++++++++ src/rust/lqos_utils/src/lib.rs | 2 + .../src}/string_table_enum.rs | 0 9 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/rust/lqos_utils/Cargo.toml create mode 100644 src/rust/lqos_utils/src/commands.rs create mode 100644 src/rust/lqos_utils/src/lib.rs rename src/rust/{lqos_queue_tracker/src/queue_types => lqos_utils/src}/string_table_enum.rs (100%) diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index 3725d20e..3cad7e30 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -1337,6 +1337,7 @@ dependencies = [ "lqos_bus", "lqos_config", "lqos_sys", + "lqos_utils", "notify", "parking_lot 0.12.1", "rayon", @@ -1363,6 +1364,13 @@ dependencies = [ "nix", ] +[[package]] +name = "lqos_utils" +version = "0.1.0" +dependencies = [ + "serde", +] + [[package]] name = "lqosd" version = "0.1.0" diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index d4c5a2d0..0b561ecd 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -21,4 +21,5 @@ members = [ "lqos_node_manager", # A lightweight web interface for management and local monitoring "lqos_python", # Python bindings for using the Rust bus directly "webusers", # CLI control for managing the web user list + "lqos_utils", # A collection of macros and helpers we find useful ] \ No newline at end of file diff --git a/src/rust/lqos_queue_tracker/Cargo.toml b/src/rust/lqos_queue_tracker/Cargo.toml index d4b8ef66..cc0e859b 100644 --- a/src/rust/lqos_queue_tracker/Cargo.toml +++ b/src/rust/lqos_queue_tracker/Cargo.toml @@ -10,6 +10,7 @@ serde_json = "1" lqos_bus = { path = "../lqos_bus" } lqos_config = { path = "../lqos_config" } lqos_sys = { path = "../lqos_sys" } +lqos_utils = { path = "../lqos_utils" } log = "0" lazy_static = "1.4" parking_lot = "0" diff --git a/src/rust/lqos_queue_tracker/src/queue_types/mod.rs b/src/rust/lqos_queue_tracker/src/queue_types/mod.rs index b83742f4..dcf820d7 100644 --- a/src/rust/lqos_queue_tracker/src/queue_types/mod.rs +++ b/src/rust/lqos_queue_tracker/src/queue_types/mod.rs @@ -1,4 +1,3 @@ -mod string_table_enum; mod tc_cake; mod tc_fq_codel; mod tc_htb; diff --git a/src/rust/lqos_queue_tracker/src/queue_types/tc_cake.rs b/src/rust/lqos_queue_tracker/src/queue_types/tc_cake.rs index 1a7908e2..144e70c6 100644 --- a/src/rust/lqos_queue_tracker/src/queue_types/tc_cake.rs +++ b/src/rust/lqos_queue_tracker/src/queue_types/tc_cake.rs @@ -2,7 +2,7 @@ use anyhow::{Error, Result}; use lqos_bus::TcHandle; use serde::{Deserialize, Serialize}; use serde_json::Value; -use crate::{string_table_enum, dashy_table_enum}; +use lqos_utils::{string_table_enum, dashy_table_enum}; string_table_enum!(DiffServ, besteffort, diffserv3, diffserv4, diffserv8, precedence); dashy_table_enum!(AckFilter, none, ack_filter, ack_filter_aggressive); diff --git a/src/rust/lqos_utils/Cargo.toml b/src/rust/lqos_utils/Cargo.toml new file mode 100644 index 00000000..e7c7f48f --- /dev/null +++ b/src/rust/lqos_utils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lqos_utils" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0", features = ["derive"] } \ No newline at end of file diff --git a/src/rust/lqos_utils/src/commands.rs b/src/rust/lqos_utils/src/commands.rs new file mode 100644 index 00000000..2661e1c5 --- /dev/null +++ b/src/rust/lqos_utils/src/commands.rs @@ -0,0 +1,96 @@ +/// `run_success` is a macro that wraps `std::process::Command`, and +/// obtains the status code. The macro returns `true` if the called +/// program returned success (0) and wasn't killed, and `false` if +/// anything went wrong. +/// +/// # Examples +/// +/// ```rust +/// use lqos_utils::run_success; +/// assert_eq!(run_success!("/bin/true"), true); +/// ``` +/// +/// ```rust +/// use lqos_utils::run_success; +/// assert!(run_success!("/bin/echo", "Hello World")); +/// assert!(run_success!("/bin/echo", "Hello", "World")); +/// ``` +#[macro_export] +macro_rules! run_success { + ($command:expr, $($arg:expr),*) => { + { + let status = std::process::Command::new($command) + $( + .arg($arg) + )* + .status().unwrap(); + status.success() + } + }; + + ($command: expr) => { + { + let status = std::process::Command::new($command) + .status().unwrap(); + status.success() + } + }; +} + +/// Executes the `run_success` macro with the added caveat that it +/// will panic on failure. This is intended ONLY for times that running +/// the command successfully is absolutely critical. +/// +/// ## Examples +/// +/// ```rust +/// use lqos_utils::*; +/// run_or_panic!("/bin/true", !"It's not true"); +/// ``` +#[macro_export] +macro_rules! run_or_panic { + ($command: expr, !$error: expr) => { + if !run_success!($command) { + panic!($error); + } + }; + + ($command:expr, $($arg:expr),*, ?$error: expr) => { + if !run_success!($command, $($arg),*) { + panic!($error); + } + }; +} + +#[cfg(test)] +mod test { + use crate::run_success; + + #[test] + fn test_true() { + assert_eq!(run_success!("/bin/true"), true); + } + + #[test] + fn test_echo() { + assert!(run_success!("/bin/echo", "Hello World")); + assert!(run_success!("/bin/echo", "Hello", "World")); + } + + #[test] + fn test_expressions() { + const ECHO: &str = "/bin/echo"; + const HELLO_WORLD: &str = "Hello World"; + assert!(run_success!(ECHO, HELLO_WORLD)); + } + + #[test] + fn test_true_not_panic() { + run_or_panic!("/bin/true", !"It's not true"); + } + + #[test] + fn test_echo_not_panic() { + run_or_panic!("/bin/echo", "Hello", "World", ?"Echo isn't working"); + } +} \ No newline at end of file diff --git a/src/rust/lqos_utils/src/lib.rs b/src/rust/lqos_utils/src/lib.rs new file mode 100644 index 00000000..f8b9ed4e --- /dev/null +++ b/src/rust/lqos_utils/src/lib.rs @@ -0,0 +1,2 @@ +mod string_table_enum; +mod commands; \ No newline at end of file diff --git a/src/rust/lqos_queue_tracker/src/queue_types/string_table_enum.rs b/src/rust/lqos_utils/src/string_table_enum.rs similarity index 100% rename from src/rust/lqos_queue_tracker/src/queue_types/string_table_enum.rs rename to src/rust/lqos_utils/src/string_table_enum.rs