parent
7735596bf0
commit
3e0c9ecd77
22 changed files with 195 additions and 108 deletions
@ -0,0 +1,3 @@ |
|||||||
|
ADMIN_AUTH_TOKEN=464af63dbd241969baa1e94b2461d94d |
||||||
|
POSTGRES_PASSWORD=12348756 |
||||||
|
DATABASE_URL=postgres://postgres:${POSTGRES_PASSWORD}@u_db/u_db |
@ -1,15 +1,18 @@ |
|||||||
.PHONY: build run clean |
.PHONY: _pre_build debug release run clean |
||||||
|
|
||||||
CARGO=./scripts/cargo_musl.sh
|
CARGO=./scripts/cargo_musl.sh
|
||||||
|
|
||||||
clean: |
clean: |
||||||
${CARGO} clean
|
${CARGO} clean
|
||||||
|
|
||||||
build: |
_pre_build: |
||||||
cd muslrust
|
|
||||||
docker build -t unki/musllibs ./muslrust
|
docker build -t unki/musllibs ./muslrust
|
||||||
cd -
|
|
||||||
${CARGO} build $@
|
debug: _pre_build |
||||||
|
${CARGO} build
|
||||||
|
|
||||||
|
release: _pre_build |
||||||
|
${CARGO} build --release
|
||||||
|
|
||||||
run: build |
run: build |
||||||
${CARGO} run
|
${CARGO} run
|
@ -1,2 +0,0 @@ |
|||||||
PG_PASSWORD=12348756 |
|
||||||
DATABASE_URL=postgres://postgres:${PG_PASSWORD}@u_db/u_db |
|
@ -1,34 +1,38 @@ |
|||||||
use futures::Future; |
use crate::helpers::client::AgentClient; |
||||||
use reqwest::{Response, Result as RResult, Url}; |
use serde_json::json; |
||||||
use serde_json::{from_str, Value}; |
|
||||||
use uuid::Uuid; |
use uuid::Uuid; |
||||||
|
|
||||||
const SERVER: &str = "u_server"; |
|
||||||
const PORT: &str = "63714"; |
|
||||||
|
|
||||||
type TestResult<R = ()> = Result<R, Box<dyn std::error::Error>>; |
type TestResult<R = ()> = Result<R, Box<dyn std::error::Error>>; |
||||||
|
|
||||||
fn url<S: AsRef<str>>(url: S) -> Url { |
async fn register_agent() -> Uuid { |
||||||
Url::parse(&format!("http://{}:{}/{}", SERVER, PORT, url.as_ref())).unwrap() |
let cli = AgentClient::new(); |
||||||
} |
let agent_uid = Uuid::new_v4(); |
||||||
|
let resp = cli.get(format!("get_agent_jobs/{}", agent_uid)).await; |
||||||
async fn unpack(req: impl Future<Output = RResult<Response>>) -> Value { |
let job_id = &resp["job_id"]; |
||||||
let resp = req.await.unwrap().text().await.unwrap(); |
let resp = cli.get(format!("get_jobs/{}", job_id)).await; |
||||||
let resp: Value = from_str(&resp).unwrap(); |
assert_eq!(&resp["alias"], "agent_hello"); |
||||||
resp.get("inner").unwrap().get(0).unwrap().clone() |
let agent_data = json! { |
||||||
} |
{"id": &agent_uid,"inner":[ |
||||||
|
{"Agent": |
||||||
async fn get<S: AsRef<str>>(_url: S) -> Value { |
{"alias":null, |
||||||
let req = reqwest::get(url(_url)); |
"hostname":"3b1030fa6324", |
||||||
unpack(req).await |
"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] |
#[tokio::test] |
||||||
async fn test_first_connection() -> TestResult { |
async fn test_first_connection() -> TestResult { |
||||||
let agent_uid = Uuid::new_v4(); |
register_agent().await; |
||||||
let resp = get(format!("get_agent_jobs/{}", agent_uid)).await; |
|
||||||
let job_id = &resp["job_id"]; |
|
||||||
let resp = get(format!("get_jobs/{}", job_id)).await; |
|
||||||
assert_eq!(&resp["alias"], "agent_hello"); |
|
||||||
Ok(()) |
Ok(()) |
||||||
} |
} |
||||||
|
@ -0,0 +1,48 @@ |
|||||||
|
use reqwest::{Client, RequestBuilder, Url}; |
||||||
|
use serde::Serialize; |
||||||
|
use serde_json::{from_str, json, Value}; |
||||||
|
|
||||||
|
const SERVER: &str = "u_server"; |
||||||
|
const PORT: &str = "63714"; |
||||||
|
|
||||||
|
pub struct AgentClient { |
||||||
|
client: Client, |
||||||
|
base_url: Url, |
||||||
|
} |
||||||
|
|
||||||
|
impl AgentClient { |
||||||
|
pub fn new() -> Self { |
||||||
|
Self { |
||||||
|
client: Client::new(), |
||||||
|
base_url: Url::parse(&format!("http://{}:{}", SERVER, PORT)).unwrap(), |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async fn process_request(&self, req: RequestBuilder, resp_needed: bool) -> Value { |
||||||
|
let resp = req.send().await.unwrap(); |
||||||
|
if let Err(e) = resp.error_for_status_ref() { |
||||||
|
panic!( |
||||||
|
"Server responded with code {}\nError: {}", |
||||||
|
e.status() |
||||||
|
.map(|s| s.to_string()) |
||||||
|
.unwrap_or(String::from("<none>")), |
||||||
|
e.to_string() |
||||||
|
); |
||||||
|
} |
||||||
|
if !resp_needed { |
||||||
|
return json!([]); |
||||||
|
} |
||||||
|
let resp: Value = from_str(&resp.text().await.unwrap()).unwrap(); |
||||||
|
resp.get("inner").unwrap().get(0).unwrap().clone() |
||||||
|
} |
||||||
|
|
||||||
|
pub async fn get<S: AsRef<str>>(&self, url: S) -> Value { |
||||||
|
let req = self.client.get(self.base_url.join(url.as_ref()).unwrap()); |
||||||
|
self.process_request(req, true).await |
||||||
|
} |
||||||
|
|
||||||
|
pub async fn post<S: AsRef<str>, B: Serialize>(&self, url: S, body: &B) -> Value { |
||||||
|
let req = self.client.post(self.base_url.join(url.as_ref()).unwrap()); |
||||||
|
self.process_request(req.json(body), false).await |
||||||
|
} |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
pub mod client; |
@ -0,0 +1,3 @@ |
|||||||
|
const BINARY: &str = "/u_panel"; |
||||||
|
|
||||||
|
pub struct Panel; |
@ -1 +1,2 @@ |
|||||||
mod behaviour; |
mod behaviour; |
||||||
|
mod helpers; |
||||||
|
Loading…
Reference in new issue