diff --git a/plugin_template/_Cargo.toml b/plugin_template/_Cargo.toml index 540a39d..d951df3 100644 --- a/plugin_template/_Cargo.toml +++ b/plugin_template/_Cargo.toml @@ -5,8 +5,13 @@ edition = "2021" publish = false [dependencies] -serde = { version = "1", features = ["derive"] } picodata-plugin = "24.6.1" +serde = { version = "1", features = ["derive"] } +log = "0.4" + +[dev-dependencies] +picodata-pike = { path = "/home/lowit/_work/picodata/cargo-pike" } # TODO: change after publish +reqwest = { version = "0.12", features = ["blocking"] } [build-dependencies] liquid = "0.26" diff --git a/plugin_template/tests/helpers/mod.rs b/plugin_template/tests/helpers/mod.rs new file mode 100644 index 0000000..724868f --- /dev/null +++ b/plugin_template/tests/helpers/mod.rs @@ -0,0 +1,63 @@ +use log::info; +use std::{ + fs::{self}, + io::ErrorKind, + path::{Path, PathBuf}, +}; + +// TODO: check in workspaces +pub const TMP_DIR: &str = "tmp/"; +pub const TOPOLOGY_PATH: &str = "topology.toml"; +pub const TARGET_DIR: &str = "target"; + +pub struct Cluster {} + +impl Drop for Cluster { + fn drop(&mut self) { + let data_dir = PathBuf::from(TMP_DIR.to_owned()); + pike::cluster::stop(&data_dir).unwrap(); + } +} + +impl Cluster { + pub fn new() -> Cluster { + info!("cleaning artefacts from previous run"); + + match fs::remove_file(Path::new(TMP_DIR).join("instance.log")) { + Ok(()) => info!("Clearing logs."), + Err(e) if e.kind() == ErrorKind::NotFound => { + info!("instance.log not found, skipping cleanup"); + }, + Err(e) => panic!("failed to delete instance.log: {e}"), + } + + match fs::remove_dir_all(TMP_DIR) { + Ok(()) => info!("clearing test plugin dir."), + Err(e) if e.kind() == ErrorKind::NotFound => { + info!("plugin dir not found, skipping cleanup"); + }, + Err(e) => panic!("failed to delete plugin_dir: {e}"), + } + + Cluster {} + } +} + +pub fn run_cluster() -> Cluster { + let cluster_handle = Cluster::new(); + let data_dir = PathBuf::from(TMP_DIR.to_owned()); + let topology_path = PathBuf::from(TOPOLOGY_PATH.to_owned()); + let target_dir = PathBuf::from(TARGET_DIR.to_owned()); + pike::cluster::run( + &topology_path, + &data_dir, + false, + 8000, + &PathBuf::from("picodata".to_owned()), + 5432, + false, + &target_dir, + ) + .unwrap(); + cluster_handle +} diff --git a/plugin_template/tests/metrics.rs b/plugin_template/tests/metrics.rs new file mode 100644 index 0000000..bf070e8 --- /dev/null +++ b/plugin_template/tests/metrics.rs @@ -0,0 +1,11 @@ +mod helpers; + +use helpers::run_cluster; +use reqwest::blocking as req; + +#[test] +fn test_metrics() { + let _cluster_handle = run_cluster(); + let resp = req::get("http://localhost:8001/metrics").unwrap(); + assert!(resp.status().is_success()); +}