use crate::fixtures::agent::*; use crate::helpers::{jobs::retry_with_interval, Panel}; use rstest::rstest; use serde_json::to_string; use u_lib::config::AGENT_ITERATION_INTERVAL; use u_lib::models::*; #[rstest] #[tokio::test] async fn registration(registered_agent: &RegisteredAgent) { let agents: Vec = Panel::check_output("agents read"); let found = agents.iter().find(|v| v.id == registered_agent.id); assert!(found.is_some()); Panel::check_status(format!("agents delete {}", registered_agent.id)); } #[tokio::test] async fn setup_jobs() { let agents: Vec = Panel::check_output("agents read"); let agent_id = agents[0].id; let job_alias = "passwd_contents"; let job = RawJob::default() .with_alias(job_alias) .with_raw_payload("cat /etc/passwd") .with_shell("/bin/bash {}") .with_target_platforms("*linux*") .try_into_job() .unwrap(); let job_id = job.meta.id; Panel::check_status(["jobs", "create", &to_string(&RawJob::from(job)).unwrap()]); let assigned = AssignedJobById { agent_id, job_id, ..Default::default() }; Panel::check_status(["map", "create", &to_string(&[assigned]).unwrap()]); retry_with_interval(5, AGENT_ITERATION_INTERVAL, || { let result = Panel::check_output::>(format!("map read {}", job_id)).remove(0); if result.state == JobState::Finished { eprintln!("{}", result.to_str_result()); assert!(result.to_str_result().contains("root:x:0:0")); Ok(()) } else { Err("job didn't finish") } }) .await; } #[tokio::test] async fn large_payload() { let agent = &Panel::check_output::>("agents read")[0]; let agent_id = agent.id; let job_alias = "large_payload"; let job = RawJob::default() .with_alias(job_alias) .with_payload_path("./tests/bin/echoer") .with_shell("{} type echo") .with_target_platforms(&agent.platform) .try_into_job() .unwrap(); let job_id = job.meta.id; Panel::check_status(["jobs", "create", &to_string(&RawJob::from(job)).unwrap()]); let assigned = AssignedJobById { agent_id, job_id, ..Default::default() }; Panel::check_status(["map", "create", &to_string(&[assigned]).unwrap()]); retry_with_interval(5, AGENT_ITERATION_INTERVAL, || { let result = Panel::check_output::>(format!("map read {}", job_id)).remove(0); if result.state == JobState::Finished { assert_eq!(result.to_str_result(), "type echo\n"); Ok(()) } else { Err("job didn't finish") } }) .await; } #[tokio::test] async fn gather_stats() { let agent = &Panel::check_output::>("agents read")[0]; let job_alias = "stats"; let job = RawJob::default() .with_type(JobType::Stats) .with_alias(job_alias) .with_target_platforms(&agent.platform) .try_into_job() .unwrap(); let job_id = job.meta.id; let agent_id = agent.id; Panel::check_status(["jobs", "create", &to_string(&RawJob::from(job)).unwrap()]); let assigned = AssignedJobById { agent_id, job_id, ..Default::default() }; Panel::check_status(["map", "create", &to_string(&[assigned]).unwrap()]); let result = retry_with_interval(5, AGENT_ITERATION_INTERVAL, || { let result = Panel::check_output::>(["map", "read", &job_id.to_string()]).remove(0); if result.state == JobState::Finished { Ok(result) } else { Err("job didn't finish") } }) .await; let stats = result.deserialize::().unwrap(); assert_eq!(stats.agent_id, agent_id); assert!(stats.cached_jobs.contains(&job_id)); assert!(stats .scheduled_jobs .iter() .find(|j| j.job_ident == "error_reporting") .is_some()); assert!(stats .scheduled_jobs .iter() .find(|j| j.job_ident == "agent_loop") .is_some()) }