forked from github-starred/komodo
create test bin for testing system with monitor client
This commit is contained in:
16
.vscode/tasks.json
vendored
16
.vscode/tasks.json
vendored
@@ -28,14 +28,6 @@
|
||||
"cwd": "${workspaceFolder}/core"
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run periphery",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/periphery"
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
@@ -55,17 +47,17 @@
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run builder",
|
||||
"label": "run cli",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/builder"
|
||||
"cwd": "${workspaceFolder}/cli"
|
||||
},
|
||||
},
|
||||
{
|
||||
"type": "cargo",
|
||||
"command": "run",
|
||||
"label": "run cli",
|
||||
"label": "run tests",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/cli"
|
||||
"cwd": "${workspaceFolder}/tests"
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
45
Cargo.lock
generated
45
Cargo.lock
generated
@@ -367,7 +367,7 @@ dependencies = [
|
||||
"helpers",
|
||||
"hmac",
|
||||
"jwt",
|
||||
"monitor_types 0.1.0",
|
||||
"monitor_types",
|
||||
"mungos",
|
||||
"oauth2",
|
||||
"periphery_client",
|
||||
@@ -516,7 +516,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
"monitor_types 0.1.0",
|
||||
"monitor_types",
|
||||
"mungos",
|
||||
]
|
||||
|
||||
@@ -794,7 +794,7 @@ dependencies = [
|
||||
"async_timing_util",
|
||||
"axum",
|
||||
"bollard",
|
||||
"monitor_types 0.1.0",
|
||||
"monitor_types",
|
||||
"rand",
|
||||
"run_command",
|
||||
"serde",
|
||||
@@ -1245,7 +1245,7 @@ name = "monitor_client"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"monitor_types 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"monitor_types",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
@@ -1264,20 +1264,6 @@ dependencies = [
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "monitor_types"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d94342b29a6ae3fd7fd603f1a904fd233008f426b844cd1f18fc8bfe71a74d79"
|
||||
dependencies = [
|
||||
"async_timing_util",
|
||||
"mungos",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"strum",
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mungos"
|
||||
version = "0.2.24"
|
||||
@@ -1492,7 +1478,7 @@ dependencies = [
|
||||
"dotenv",
|
||||
"envy",
|
||||
"helpers",
|
||||
"monitor_types 0.1.0",
|
||||
"monitor_types",
|
||||
"run_command",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
@@ -1509,7 +1495,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"helpers",
|
||||
"monitor_types 0.1.0",
|
||||
"monitor_types",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -2149,6 +2135,20 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tests"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"dotenv",
|
||||
"envy",
|
||||
"monitor_client",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.36"
|
||||
@@ -2215,9 +2215,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.21.1"
|
||||
version = "1.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0020c875007ad96677dcc890298f4b942882c5d4eb7cc8f439fc3bf813dc9c95"
|
||||
checksum = "d76ce4a75fb488c605c54bf610f221cea8b0dafb53333c1a67e8ee199dcd2ae3"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"bytes",
|
||||
@@ -2225,7 +2225,6 @@ dependencies = [
|
||||
"memchr",
|
||||
"mio",
|
||||
"num_cpus",
|
||||
"once_cell",
|
||||
"parking_lot",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
|
||||
@@ -4,6 +4,7 @@ members = [
|
||||
"cli",
|
||||
"core",
|
||||
"periphery",
|
||||
"tests",
|
||||
"lib/db_client",
|
||||
"lib/helpers",
|
||||
"lib/periphery_client",
|
||||
|
||||
@@ -9,7 +9,7 @@ use super::JwtExtension;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct SecretLoginBody {
|
||||
user_id: String,
|
||||
username: String,
|
||||
secret: String,
|
||||
}
|
||||
|
||||
@@ -23,14 +23,15 @@ pub fn router() -> Router {
|
||||
pub async fn login(
|
||||
Extension(db): DbExtension,
|
||||
Extension(jwt): JwtExtension,
|
||||
Json(SecretLoginBody { user_id, secret }): Json<SecretLoginBody>,
|
||||
Json(SecretLoginBody { username, secret }): Json<SecretLoginBody>,
|
||||
) -> anyhow::Result<String> {
|
||||
let user = db
|
||||
.users
|
||||
.find_one_by_id(&user_id)
|
||||
.find_one(doc! { "username": &username }, None)
|
||||
.await
|
||||
.context("failed at mongo query")?
|
||||
.ok_or(anyhow!("did not find user with id {user_id}"))?;
|
||||
.ok_or(anyhow!("did not find user with username {username}"))?;
|
||||
let user_id = user.id.unwrap().to_string();
|
||||
let ts = unix_timestamp_ms() as i64;
|
||||
for s in user.secrets {
|
||||
if let Some(expires) = s.expires {
|
||||
|
||||
@@ -9,7 +9,8 @@ license = "MIT"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
monitor_types = "0.1.0"
|
||||
# monitor_types = "0.1.0"
|
||||
monitor_types = { path = "../types" }
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
anyhow = "1.0"
|
||||
serde = "1.0"
|
||||
|
||||
1
lib/monitor_client/src/build.rs
Normal file
1
lib/monitor_client/src/build.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
1
lib/monitor_client/src/deployment.rs
Normal file
1
lib/monitor_client/src/deployment.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use anyhow::{anyhow, Context};
|
||||
use reqwest::StatusCode;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
@@ -5,6 +7,12 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||
pub use monitor_types as types;
|
||||
use serde_json::json;
|
||||
|
||||
mod build;
|
||||
mod deployment;
|
||||
mod permissions;
|
||||
mod secret;
|
||||
mod server;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MonitorClient {
|
||||
http_client: reqwest::Client,
|
||||
@@ -21,30 +29,38 @@ impl MonitorClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_with_credentials(
|
||||
pub async fn new_with_password(
|
||||
url: &str,
|
||||
username: impl Into<String>,
|
||||
password: impl Into<String>,
|
||||
) -> anyhow::Result<MonitorClient> {
|
||||
let mut client = MonitorClient::new_with_token(url, "");
|
||||
client.login(username, password).await?;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
async fn login(
|
||||
&mut self,
|
||||
username: impl Into<String>,
|
||||
password: impl Into<String>,
|
||||
) -> anyhow::Result<()> {
|
||||
let token = self
|
||||
let token = client
|
||||
.post_string(
|
||||
"/auth/local/login",
|
||||
json!({ "username": username.into(), "password": password.into() }),
|
||||
)
|
||||
.await
|
||||
.context("failed to log in")?;
|
||||
self.token = token;
|
||||
Ok(())
|
||||
.context("failed to log in with password")?;
|
||||
client.token = token;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
pub async fn new_with_secret(
|
||||
url: &str,
|
||||
username: impl Into<String>,
|
||||
secret: impl Into<String>,
|
||||
) -> anyhow::Result<MonitorClient> {
|
||||
let mut client = MonitorClient::new_with_token(url, "");
|
||||
let token = client
|
||||
.post_string(
|
||||
"/auth/secret/login",
|
||||
json!({ "username": username.into(), "secret": secret.into() }),
|
||||
)
|
||||
.await
|
||||
.context("failed to log in with secret")?;
|
||||
client.token = token;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
async fn get<R: DeserializeOwned>(&self, endpoint: &str) -> anyhow::Result<R> {
|
||||
|
||||
1
lib/monitor_client/src/permissions.rs
Normal file
1
lib/monitor_client/src/permissions.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
1
lib/monitor_client/src/secret.rs
Normal file
1
lib/monitor_client/src/secret.rs
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
27
lib/monitor_client/src/server.rs
Normal file
27
lib/monitor_client/src/server.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use monitor_types::{Server, SystemStats};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::MonitorClient;
|
||||
|
||||
impl MonitorClient {
|
||||
pub async fn list_servers(&self) -> anyhow::Result<Vec<Server>> {
|
||||
self.get("/api/server/list").await
|
||||
}
|
||||
|
||||
pub async fn create_server(&self, name: &str, address: &str) -> anyhow::Result<()> {
|
||||
self.post(
|
||||
"/api/server/create",
|
||||
json!({ "name": name, "address": address }),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_server(&self, id: &str) -> anyhow::Result<()> {
|
||||
self.delete::<(), _>(&format!("/api/server/delete/{id}"), None)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_server_stats(&self, id: &str) -> anyhow::Result<SystemStats> {
|
||||
self.get(&format!("/api/server/stats/{id}")).await
|
||||
}
|
||||
}
|
||||
16
tests/Cargo.toml
Normal file
16
tests/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "tests"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "1.22", features = ["full"] }
|
||||
monitor_client = { path = "../lib/monitor_client" }
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_derive = "1.0"
|
||||
envy = "0.4"
|
||||
anyhow = "1.0"
|
||||
dotenv = "0.15"
|
||||
34
tests/src/config.rs
Normal file
34
tests/src/config.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use dotenv::dotenv;
|
||||
use monitor_client::MonitorClient;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Env {
|
||||
pub url: String,
|
||||
pub token: Option<String>,
|
||||
pub username: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub secret: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn load() -> MonitorClient {
|
||||
dotenv().ok();
|
||||
let env: Env = envy::from_env().expect("failed to parse env");
|
||||
if let Some(token) = env.token {
|
||||
MonitorClient::new_with_token(&env.url, token)
|
||||
} else if let Some(username) = env.username {
|
||||
if let Some(password) = env.password {
|
||||
MonitorClient::new_with_password(&env.url, username, password)
|
||||
.await
|
||||
.expect("failed to log in with username / password")
|
||||
} else if let Some(secret) = env.secret {
|
||||
MonitorClient::new_with_secret(&env.url, username, secret)
|
||||
.await
|
||||
.expect("failed to log in with username / secret")
|
||||
} else {
|
||||
panic!("must provide either password or secret to login in along with username")
|
||||
}
|
||||
} else {
|
||||
panic!("must provide either token, username / password, or username / secret")
|
||||
}
|
||||
}
|
||||
16
tests/src/main.rs
Normal file
16
tests/src/main.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
#![allow(unused)]
|
||||
|
||||
mod config;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let monitor = config::load().await;
|
||||
let servers = monitor
|
||||
.list_servers()
|
||||
.await
|
||||
.expect("failed at list servers");
|
||||
let server = servers.get(0).unwrap();
|
||||
let server_id = server.id.unwrap().to_string();
|
||||
let stats = monitor.get_server_stats(&server_id).await.unwrap();
|
||||
println!("{stats:#?}")
|
||||
}
|
||||
Reference in New Issue
Block a user