diff --git a/.cargo/config.toml b/.cargo/config.toml index bf72e8c..21748f1 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,2 @@ [build] -rustflags = ["-L", "/home/ortem/src/rust/unki/static/lib"] -target = "x86_64-unknown-linux-musl" \ No newline at end of file +rustflags = ["-L", "/home/ortem/src/rust/unki/static/lib"] \ No newline at end of file diff --git a/Makefile.toml b/Makefile.toml index 7e2d27f..256e798 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -2,6 +2,7 @@ default_to_workspace = false [env] +TARGET = "x86_64-unknown-linux-musl" CARGO = "cargo" PREFIX = "${CARGO_MAKE_WORKING_DIRECTORY}/static" PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL = "true" @@ -19,12 +20,12 @@ args = ["clean"] [tasks.debug] dependencies = ["build_static_libs"] command = "${CARGO}" -args = ["build", "${@}"] +args = ["build", "--target", "${TARGET}","${@}"] [tasks.release] dependencies = ["build_static_libs"] command = "${CARGO}" -args = ["build", "--release", "${@}"] +args = ["build", "--target", "${TARGET}", "--release", "${@}"] [tasks.run] script = ''' @@ -34,7 +35,7 @@ exit 1 [tasks.unit] command = "${CARGO}" -args = ["test", "--lib", "${@}"] +args = ["test", "--target", "${TARGET}", "--lib", "--", "${@}"] [tasks.integration] script = ''' diff --git a/integration/tests/behaviour.rs b/integration/tests/behaviour.rs index 616c7a9..38e88f2 100644 --- a/integration/tests/behaviour.rs +++ b/integration/tests/behaviour.rs @@ -5,7 +5,7 @@ use rstest::rstest; use std::error::Error; use std::time::Duration; use tokio::time::sleep; -use u_lib::{messaging::Empty, models::*}; +use u_lib::models::*; use uuid::Uuid; type TestResult = Result>; @@ -17,9 +17,8 @@ async fn test_registration(#[future] register_agent: RegisteredAgent) -> TestRes let agents: Vec = Panel::check_output("agents list"); let found = agents.iter().find(|v| v.id == agent.uid); assert!(found.is_some()); - //teardown - Panel::check_status::(&format!("agents delete {}", agent.uid)); - Ok(()) //TODO: ______^^^^^ REMOV + Panel::check_status(format!("agents delete {}", agent.uid)); + Ok(()) } #[tokio::test] @@ -29,7 +28,7 @@ async fn test_setup_tasks() -> TestResult { let agent_uid = agents[0].id; let job_alias = "passwd_contents"; let cmd = format!("jobs add --alias {} 'cat /etc/passwd'", job_alias); - Panel::check_status::(&cmd); + Panel::check_status(cmd); let cmd = format!("jobmap add {} {}", agent_uid, job_alias); let assigned_uids: Vec = Panel::check_output(cmd); for _ in 0..3 { diff --git a/integration/tests/helpers/panel.rs b/integration/tests/helpers/panel.rs index 2c61245..713c60a 100644 --- a/integration/tests/helpers/panel.rs +++ b/integration/tests/helpers/panel.rs @@ -1,8 +1,8 @@ use serde::de::DeserializeOwned; -use serde_json::from_slice; +use serde_json::{from_slice, Value}; use shlex::split; use std::process::{Command, Output}; -use u_lib::{datatypes::DataResult, messaging::AsMsg, utils::VecDisplay}; +use u_lib::{datatypes::DataResult, utils::VecDisplay}; const PANEL_BINARY: &str = "/u_panel"; @@ -19,8 +19,8 @@ impl Panel { .unwrap() } - pub fn output_argv(args: &[&str]) -> PanelResult { - let result = Self::run(args); + pub fn output_argv(argv: &[&str]) -> PanelResult { + let result = Self::run(argv); from_slice(&result.stdout).map_err(|e| { eprintln!( "Failed to decode panel response: '{}'", @@ -30,7 +30,7 @@ impl Panel { }) } - pub fn output(args: impl Into) -> PanelResult { + pub fn output(args: impl Into) -> PanelResult { let splitted = split(args.into().as_ref()).unwrap(); Self::output_argv( splitted @@ -41,19 +41,19 @@ impl Panel { ) } - fn status_is_ok(data: PanelResult) -> T { + fn status_is_ok(data: PanelResult) -> T { match data.unwrap() { DataResult::Ok(r) => r, DataResult::Err(e) => panic!("Panel failed: {}", e), } } - pub fn check_status<'s, T: AsMsg + DeserializeOwned>(args: &'s str) { - let result: PanelResult = Self::output(args); + pub fn check_status(args: impl Into) { + let result: PanelResult = Self::output(args); Self::status_is_ok(result); } - pub fn check_output(args: impl Into) -> T { + pub fn check_output(args: impl Into) -> T { let result = Self::output(args); Self::status_is_ok(result) } diff --git a/lib/u_lib/src/utils/tempfile.rs b/lib/u_lib/src/utils/tempfile.rs index 209ea46..a5a510e 100644 --- a/lib/u_lib/src/utils/tempfile.rs +++ b/lib/u_lib/src/utils/tempfile.rs @@ -26,6 +26,7 @@ impl TempFile { pub fn write_exec(data: &[u8]) -> UResult { let this = Self::new(); let path = this.get_path(); + dbg!(&path); this.write_all(data)?; let perms = fs::Permissions::from_mode(0o555); fs::set_permissions(&path, perms).map_err(|e| UError::FSError(path, e.to_string()))?; @@ -35,6 +36,37 @@ impl TempFile { impl Drop for TempFile { fn drop(&mut self) { - fs::remove_file(&self.path).ok(); + fs::remove_file(&self.path).unwrap(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::utils::bytes_to_string; + use std::path::Path; + use std::process::Command; + + #[test] + fn test_file_is_not_busy() { + let binary = include_bytes!("../../tests/fixtures/echoer"); + for _ in 0..100 { + let executable = TempFile::write_exec(binary).unwrap(); + let path = executable.get_path(); + let result = Command::new(path).arg("qwe").output().unwrap(); + assert_eq!(bytes_to_string(result.stdout.as_ref()).trim(), "qwe"); + } + } + + #[test] + fn test_file_removed_after_dropping() { + let path; + { + let file = TempFile::new(); + file.write_all(b"asdqwe").unwrap(); + path = file.get_path(); + assert!(Path::new(&path).exists()) + } + assert!(!Path::new(&path).exists()) } }