You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.5 KiB
81 lines
2.5 KiB
3 years ago
|
mod helpers;
|
||
|
|
||
|
use helpers::{AgentClient, Panel};
|
||
|
|
||
|
use serde_json::json;
|
||
|
use std::thread::sleep;
|
||
|
use std::time::Duration;
|
||
|
use uuid::Uuid;
|
||
|
|
||
|
type TestResult<R = ()> = Result<R, Box<dyn std::error::Error>>;
|
||
|
|
||
|
async fn register_agent() -> Uuid {
|
||
|
let cli = AgentClient::new();
|
||
|
let agent_uid = Uuid::new_v4();
|
||
|
let resp = cli.get(format!("get_agent_jobs/{}", agent_uid)).await;
|
||
|
let job_id = &resp["job_id"];
|
||
|
let resp = cli.get(format!("get_jobs/{}", job_id)).await;
|
||
|
assert_eq!(&resp["alias"], "agent_hello");
|
||
|
let agent_data = json! {
|
||
|
{"id": &agent_uid,"inner":[
|
||
|
{"Agent":
|
||
|
{"alias":null,
|
||
|
"hostname":"3b1030fa6324",
|
||
|
"id":&agent_uid,
|
||
|
"is_root":false,
|
||
|
"is_root_allowed":false,
|
||
|
"last_active":{"secs_since_epoch":1625271265,"nanos_since_epoch":92814921},
|
||
|
"platform":"x86_64-unknown-linux-gnu",
|
||
|
"regtime":{"secs_since_epoch":1625271265,"nanos_since_epoch":92814945},
|
||
|
"state":"New",
|
||
|
"token":null,
|
||
|
"username":"root"}
|
||
|
}
|
||
|
]}
|
||
|
};
|
||
|
cli.post("report", &agent_data).await;
|
||
|
agent_uid
|
||
|
}
|
||
|
|
||
|
#[tokio::test]
|
||
|
async fn test_registration() -> TestResult {
|
||
|
let agent_uid = register_agent().await;
|
||
|
let agents = Panel::check_output("agents list");
|
||
|
let found = agents
|
||
|
.iter()
|
||
|
.find(|v| v["id"].as_str().unwrap() == agent_uid.to_string());
|
||
|
assert!(found.is_some());
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[tokio::test]
|
||
|
async fn test_setup_tasks() -> TestResult {
|
||
|
let agent_uid = Panel::check_output("agents list")[0]["id"].clone();
|
||
|
dbg!(&agent_uid);
|
||
|
let job_alias = "passwd_contents";
|
||
|
let cmd = format!("jobs add --alias {} 'cat /etc/passwd'", job_alias);
|
||
|
Panel::check_status(cmd);
|
||
|
let cmd = format!("jobmap add {} {}", agent_uid, job_alias);
|
||
|
let assigned_uids = Panel::check_output(cmd);
|
||
|
dbg!(&assigned_uids);
|
||
|
loop {
|
||
|
let result = Panel::check_output(format!("jobmap list {}", assigned_uids[0]));
|
||
|
dbg!(&result);
|
||
|
match result.get(0) {
|
||
|
Some(entry) if entry["state"] == "Finished" => {
|
||
|
println!("{}", result[0]);
|
||
|
break;
|
||
|
}
|
||
|
None => {
|
||
|
eprintln!("jobmap list is empty (bad bad bad)");
|
||
|
continue;
|
||
|
}
|
||
|
_ => {
|
||
|
sleep(Duration::from_secs(1));
|
||
|
eprintln!("waiting for task");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
Ok(())
|
||
|
}
|