Skip to content

Commit

Permalink
template_tests: add test example in plugin template
Browse files Browse the repository at this point in the history
closes #8
  • Loading branch information
lowitea committed Dec 20, 2024
1 parent 77cc35e commit faece96
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion plugin_template/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
63 changes: 63 additions & 0 deletions plugin_template/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions plugin_template/tests/metrics.rs
Original file line number Diff line number Diff line change
@@ -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());
}

0 comments on commit faece96

Please sign in to comment.