fixed rust-analyzer fail on cargo check, added & improved tests

pull/1/head
plazmoid 3 years ago
parent 745dcb7ff8
commit ac20f1b343
  1. 3
      .cargo/config.toml
  2. 7
      Makefile.toml
  3. 9
      integration/tests/behaviour.rs
  4. 18
      integration/tests/helpers/panel.rs
  5. 34
      lib/u_lib/src/utils/tempfile.rs

@ -1,3 +1,2 @@
[build] [build]
rustflags = ["-L", "/home/ortem/src/rust/unki/static/lib"] rustflags = ["-L", "/home/ortem/src/rust/unki/static/lib"]
target = "x86_64-unknown-linux-musl"

@ -2,6 +2,7 @@
default_to_workspace = false default_to_workspace = false
[env] [env]
TARGET = "x86_64-unknown-linux-musl"
CARGO = "cargo" CARGO = "cargo"
PREFIX = "${CARGO_MAKE_WORKING_DIRECTORY}/static" PREFIX = "${CARGO_MAKE_WORKING_DIRECTORY}/static"
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL = "true" PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL = "true"
@ -19,12 +20,12 @@ args = ["clean"]
[tasks.debug] [tasks.debug]
dependencies = ["build_static_libs"] dependencies = ["build_static_libs"]
command = "${CARGO}" command = "${CARGO}"
args = ["build", "${@}"] args = ["build", "--target", "${TARGET}","${@}"]
[tasks.release] [tasks.release]
dependencies = ["build_static_libs"] dependencies = ["build_static_libs"]
command = "${CARGO}" command = "${CARGO}"
args = ["build", "--release", "${@}"] args = ["build", "--target", "${TARGET}", "--release", "${@}"]
[tasks.run] [tasks.run]
script = ''' script = '''
@ -34,7 +35,7 @@ exit 1
[tasks.unit] [tasks.unit]
command = "${CARGO}" command = "${CARGO}"
args = ["test", "--lib", "${@}"] args = ["test", "--target", "${TARGET}", "--lib", "--", "${@}"]
[tasks.integration] [tasks.integration]
script = ''' script = '''

@ -5,7 +5,7 @@ use rstest::rstest;
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
use tokio::time::sleep; use tokio::time::sleep;
use u_lib::{messaging::Empty, models::*}; use u_lib::models::*;
use uuid::Uuid; use uuid::Uuid;
type TestResult<R = ()> = Result<R, Box<dyn Error>>; type TestResult<R = ()> = Result<R, Box<dyn Error>>;
@ -17,9 +17,8 @@ async fn test_registration(#[future] register_agent: RegisteredAgent) -> TestRes
let agents: Vec<Agent> = Panel::check_output("agents list"); let agents: Vec<Agent> = Panel::check_output("agents list");
let found = agents.iter().find(|v| v.id == agent.uid); let found = agents.iter().find(|v| v.id == agent.uid);
assert!(found.is_some()); assert!(found.is_some());
//teardown Panel::check_status(format!("agents delete {}", agent.uid));
Panel::check_status::<i32>(&format!("agents delete {}", agent.uid)); Ok(())
Ok(()) //TODO: ______^^^^^ REMOV
} }
#[tokio::test] #[tokio::test]
@ -29,7 +28,7 @@ async fn test_setup_tasks() -> TestResult {
let agent_uid = agents[0].id; let agent_uid = agents[0].id;
let job_alias = "passwd_contents"; let job_alias = "passwd_contents";
let cmd = format!("jobs add --alias {} 'cat /etc/passwd'", job_alias); let cmd = format!("jobs add --alias {} 'cat /etc/passwd'", job_alias);
Panel::check_status::<Empty>(&cmd); Panel::check_status(cmd);
let cmd = format!("jobmap add {} {}", agent_uid, job_alias); let cmd = format!("jobmap add {} {}", agent_uid, job_alias);
let assigned_uids: Vec<Uuid> = Panel::check_output(cmd); let assigned_uids: Vec<Uuid> = Panel::check_output(cmd);
for _ in 0..3 { for _ in 0..3 {

@ -1,8 +1,8 @@
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde_json::from_slice; use serde_json::{from_slice, Value};
use shlex::split; use shlex::split;
use std::process::{Command, Output}; 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"; const PANEL_BINARY: &str = "/u_panel";
@ -19,8 +19,8 @@ impl Panel {
.unwrap() .unwrap()
} }
pub fn output_argv<T: AsMsg + DeserializeOwned>(args: &[&str]) -> PanelResult<T> { pub fn output_argv<T: DeserializeOwned>(argv: &[&str]) -> PanelResult<T> {
let result = Self::run(args); let result = Self::run(argv);
from_slice(&result.stdout).map_err(|e| { from_slice(&result.stdout).map_err(|e| {
eprintln!( eprintln!(
"Failed to decode panel response: '{}'", "Failed to decode panel response: '{}'",
@ -30,7 +30,7 @@ impl Panel {
}) })
} }
pub fn output<T: AsMsg + DeserializeOwned>(args: impl Into<String>) -> PanelResult<T> { pub fn output<T: DeserializeOwned>(args: impl Into<String>) -> PanelResult<T> {
let splitted = split(args.into().as_ref()).unwrap(); let splitted = split(args.into().as_ref()).unwrap();
Self::output_argv( Self::output_argv(
splitted splitted
@ -41,19 +41,19 @@ impl Panel {
) )
} }
fn status_is_ok<T: AsMsg + DeserializeOwned>(data: PanelResult<T>) -> T { fn status_is_ok<T: DeserializeOwned>(data: PanelResult<T>) -> T {
match data.unwrap() { match data.unwrap() {
DataResult::Ok(r) => r, DataResult::Ok(r) => r,
DataResult::Err(e) => panic!("Panel failed: {}", e), DataResult::Err(e) => panic!("Panel failed: {}", e),
} }
} }
pub fn check_status<'s, T: AsMsg + DeserializeOwned>(args: &'s str) { pub fn check_status(args: impl Into<String>) {
let result: PanelResult<T> = Self::output(args); let result: PanelResult<Value> = Self::output(args);
Self::status_is_ok(result); Self::status_is_ok(result);
} }
pub fn check_output<T: AsMsg + DeserializeOwned>(args: impl Into<String>) -> T { pub fn check_output<T: DeserializeOwned>(args: impl Into<String>) -> T {
let result = Self::output(args); let result = Self::output(args);
Self::status_is_ok(result) Self::status_is_ok(result)
} }

@ -26,6 +26,7 @@ impl TempFile {
pub fn write_exec(data: &[u8]) -> UResult<Self> { pub fn write_exec(data: &[u8]) -> UResult<Self> {
let this = Self::new(); let this = Self::new();
let path = this.get_path(); let path = this.get_path();
dbg!(&path);
this.write_all(data)?; this.write_all(data)?;
let perms = fs::Permissions::from_mode(0o555); let perms = fs::Permissions::from_mode(0o555);
fs::set_permissions(&path, perms).map_err(|e| UError::FSError(path, e.to_string()))?; fs::set_permissions(&path, perms).map_err(|e| UError::FSError(path, e.to_string()))?;
@ -35,6 +36,37 @@ impl TempFile {
impl Drop for TempFile { impl Drop for TempFile {
fn drop(&mut self) { 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())
} }
} }

Loading…
Cancel
Save