Skip to content

Commit

Permalink
test: implement tests for run/stop/plugin new commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dodokek committed Feb 5, 2025
1 parent 7ca1983 commit 269a4e1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 15 deletions.
11 changes: 11 additions & 0 deletions tests/assets/topology.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tier.default]
replicasets = 2
replication_factor = 2

[plugin.test-plugin]
migration_context = [
{ name = "example_name", value = "example_value" },
]

[plugin.test-plugin.service.main]
tiers = ["default"]
56 changes: 46 additions & 10 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ pub enum BuildType {
Debug,
}

#[derive(Default)]
#[allow(clippy::struct_field_names)]
pub struct CmdArguments {
pub run_args: Vec<String>,
pub build_args: Vec<String>,
pub plugin_args: Vec<String>,
pub stop_args: Vec<String>,
}

pub struct Cluster {
run_handler: Option<Child>,
pub cmd_args: CmdArguments,
}

impl Drop for Cluster {
fn drop(&mut self) {
let mut child = run_pike(vec!["stop"], PLUGIN_DIR).unwrap();
let mut child = run_pike(vec!["stop"], PLUGIN_DIR, &self.cmd_args.stop_args).unwrap();
child.wait().unwrap();
if let Some(ref mut run_handler) = self.run_handler {
run_handler.wait().unwrap();
Expand All @@ -36,7 +46,7 @@ impl Drop for Cluster {
}

impl Cluster {
fn new() -> Cluster {
fn new(run_params: CmdArguments) -> Cluster {
info!("cleaning artefacts from previous run");

match fs::remove_file(Path::new(TESTS_DIR).join("instance.log")) {
Expand All @@ -55,7 +65,10 @@ impl Cluster {
Err(e) => panic!("failed to delete plugin_dir: {e}"),
}

Cluster { run_handler: None }
Cluster {
run_handler: None,
cmd_args: run_params,
}
}

fn set_run_handler(&mut self, handler: Child) {
Expand Down Expand Up @@ -135,24 +148,39 @@ pub fn build_plugin(build_type: &BuildType, new_version: &str) {
};
}

pub fn run_cluster(timeout: Duration, total_instances: i32) -> Result<Cluster, std::io::Error> {
pub fn run_cluster(
timeout: Duration,
total_instances: i32,
cmd_args: CmdArguments,
) -> Result<Cluster, std::io::Error> {
// Set up cleanup function
let mut cluster_handle = Cluster::new();
let mut cluster_handle = Cluster::new(cmd_args);

// Create plugin from template
let mut plugin_creation_proc =
run_pike(vec!["plugin", "new", "test-plugin"], TESTS_DIR).unwrap();
let mut plugin_creation_proc = run_pike(
vec!["plugin", "new", "test-plugin"],
TESTS_DIR,
&cluster_handle.cmd_args.plugin_args,
)
.unwrap();

wait_for_proc(&mut plugin_creation_proc, Duration::from_secs(10));

// Build the plugin
Command::new("cargo")
.args(vec!["build"])
.args(&cluster_handle.cmd_args.build_args)
.current_dir(PLUGIN_DIR)
.output()?;

// Setup the cluster
let run_handler = run_pike(vec!["run"], PLUGIN_DIR).unwrap();

let install_plugins = cluster_handle
.cmd_args
.run_args
.contains(&"--disable-install-plugins".to_string());

let run_handler = run_pike(vec!["run"], PLUGIN_DIR, &cluster_handle.cmd_args.run_args).unwrap();
cluster_handle.set_run_handler(run_handler);

let start_time = Instant::now();
Expand Down Expand Up @@ -203,15 +231,22 @@ pub fn run_cluster(timeout: Duration, total_instances: i32) -> Result<Cluster, s

picodata_admin.kill().unwrap();

if can_connect && plugin_ready && online_instances_counter == total_instances {
if can_connect
&& (plugin_ready || !install_plugins)
&& online_instances_counter == total_instances
{
return Ok(cluster_handle);
}

thread::sleep(Duration::from_secs(5));
}
}

pub fn run_pike<A, P>(args: Vec<A>, current_dir: P) -> Result<std::process::Child, std::io::Error>
pub fn run_pike<A, P>(
args: Vec<A>,
current_dir: P,
cmd_args: &Vec<String>,
) -> Result<std::process::Child, std::io::Error>
where
A: AsRef<OsStr>,
P: AsRef<Path>,
Expand All @@ -220,6 +255,7 @@ where
Command::new(format!("{root_dir}/target/debug/cargo-pike"))
.arg("pike")
.args(args)
.args(cmd_args)
.current_dir(current_dir)
.spawn()
}
Expand Down
75 changes: 70 additions & 5 deletions tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,80 @@
mod helpers;

use helpers::{
build_plugin, check_plugin_version_artefacts, run_cluster, run_pike, wait_for_proc, PLUGIN_DIR,
TESTS_DIR,
build_plugin, check_plugin_version_artefacts, run_cluster, run_pike, wait_for_proc,
CmdArguments, PLUGIN_DIR, TESTS_DIR,
};
use std::{fs, path::Path, time::Duration};
use std::{fs, path::Path, time::Duration, vec};

const TOTAL_INSTANCES: i32 = 4;
#[test]
fn test_cluster_setup_debug() {
let _cluster_handle = run_cluster(Duration::from_secs(120), TOTAL_INSTANCES).unwrap();
let _cluster_handle = run_cluster(
Duration::from_secs(120),
TOTAL_INSTANCES,
CmdArguments::default(),
)
.unwrap();
}

#[test]
fn test_cluster_setup_release() {
let run_params = CmdArguments {
run_args: ["--release", "--data-dir", "new_data_dir"]
.iter()
.map(|&s| s.into())
.collect(),
stop_args: ["--data-dir", "new_data_dir"]
.iter()
.map(|&s| s.into())
.collect(),
..Default::default()
};

let _cluster_handle =
run_cluster(Duration::from_secs(120), TOTAL_INSTANCES, run_params).unwrap();
}

// Using as much command line arguments in this test as we can
#[test]
fn test_cluster_daemon_and_arguments() {
let run_params = CmdArguments {
run_args: [
"-d",
"--topology",
"../../assets/topology.toml",
"--base-http-port",
"8001",
"--base-pg-port",
"5430",
"--target-dir",
"tmp_target",
]
.iter()
.map(|&s| s.into())
.collect(),
build_args: ["--target-dir", "tmp_target"]
.iter()
.map(|&s| s.into())
.collect(),
plugin_args: vec!["--workspace".to_string()],
..Default::default()
};

let _cluster_handle =
run_cluster(Duration::from_secs(120), TOTAL_INSTANCES, run_params).unwrap();

// Validate each instances's PID
for entry in fs::read_dir(Path::new(PLUGIN_DIR).join("tmp").join("cluster")).unwrap() {
let entry = entry.unwrap();
let pid_path = entry.path().join("pid");

assert!(pid_path.exists());

if let Ok(content) = fs::read_to_string(&pid_path) {
assert!(content.trim().parse::<u32>().is_ok());
}
}
}

#[test]
Expand All @@ -20,7 +85,7 @@ fn test_cargo_build() {
}

let mut plugin_creation_proc =
run_pike(vec!["plugin", "new", "test-plugin"], TESTS_DIR).unwrap();
run_pike(vec!["plugin", "new", "test-plugin"], TESTS_DIR, &vec![]).unwrap();

wait_for_proc(&mut plugin_creation_proc, Duration::from_secs(10));

Expand Down

0 comments on commit 269a4e1

Please sign in to comment.