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 115d47d
Show file tree
Hide file tree
Showing 3 changed files with 136 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"]
65 changes: 55 additions & 10 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,32 @@ pub enum BuildType {
Debug,
}

pub struct CmdArguments {
pub run_args: Vec<String>,
pub build_args: Vec<String>,
pub plugin_args: Vec<String>,
pub stop_args: Vec<String>,
}

Check failure on line 29 in tests/helpers/mod.rs

View workflow job for this annotation

GitHub Actions / lint_check

all fields have the same postfix: `args`

error: all fields have the same postfix: `args` --> tests/helpers/mod.rs:24:1 | 24 | / pub struct CmdArguments { 25 | | pub run_args: Vec<String>, 26 | | pub build_args: Vec<String>, 27 | | pub plugin_args: Vec<String>, 28 | | pub stop_args: Vec<String>, 29 | | } | |_^ | = help: remove the postfixes = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#struct_field_names = note: `-D clippy::struct-field-names` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::struct_field_names)]`

Check failure on line 29 in tests/helpers/mod.rs

View workflow job for this annotation

GitHub Actions / lint_check

all fields have the same postfix: `args`

error: all fields have the same postfix: `args` --> tests/helpers/mod.rs:24:1 | 24 | / pub struct CmdArguments { 25 | | pub run_args: Vec<String>, 26 | | pub build_args: Vec<String>, 27 | | pub plugin_args: Vec<String>, 28 | | pub stop_args: Vec<String>, 29 | | } | |_^ | = help: remove the postfixes = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#struct_field_names = note: `-D clippy::struct-field-names` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::struct_field_names)]`

impl Default for CmdArguments {
fn default() -> CmdArguments {
CmdArguments {
run_args: vec![],
build_args: vec![],
plugin_args: vec![],
stop_args: vec![],
}
}
}

Check failure on line 40 in tests/helpers/mod.rs

View workflow job for this annotation

GitHub Actions / lint_check

this `impl` can be derived

error: this `impl` can be derived --> tests/helpers/mod.rs:31:1 | 31 | / impl Default for CmdArguments { 32 | | fn default() -> CmdArguments { 33 | | CmdArguments { 34 | | run_args: vec![], ... | 39 | | } 40 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls = note: `-D clippy::derivable-impls` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]` = help: remove the manual implementation... help: ...and instead derive it | 24 + #[derive(Default)] 25 | pub struct CmdArguments { |

Check failure on line 40 in tests/helpers/mod.rs

View workflow job for this annotation

GitHub Actions / lint_check

this `impl` can be derived

error: this `impl` can be derived --> tests/helpers/mod.rs:31:1 | 31 | / impl Default for CmdArguments { 32 | | fn default() -> CmdArguments { 33 | | CmdArguments { 34 | | run_args: vec![], ... | 39 | | } 40 | | } | |_^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls = note: `-D clippy::derivable-impls` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::derivable_impls)]` = help: remove the manual implementation... help: ...and instead derive it | 24 + #[derive(Default)] 25 | pub struct CmdArguments { |

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 +55,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 +74,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 +157,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 +240,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 +264,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: vec!["--release", "--data-dir", "new_data_dir"]

Check failure on line 23 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:23:19 | 23 | run_args: vec!["--release", "--data-dir", "new_data_dir"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--release", "--data-dir", "new_data_dir"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec = note: `-D clippy::useless-vec` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`

Check failure on line 23 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:23:19 | 23 | run_args: vec!["--release", "--data-dir", "new_data_dir"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--release", "--data-dir", "new_data_dir"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec = note: `-D clippy::useless-vec` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`
.iter()
.map(|&s| s.into())
.collect(),
stop_args: vec!["--data-dir", "new_data_dir"]

Check failure on line 27 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:27:20 | 27 | stop_args: vec!["--data-dir", "new_data_dir"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--data-dir", "new_data_dir"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec

Check failure on line 27 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:27:20 | 27 | stop_args: vec!["--data-dir", "new_data_dir"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--data-dir", "new_data_dir"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
.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: vec![
"-d",
"--topology",
"../../assets/topology.toml",
"--base-http-port",
"8001",
"--base-pg-port",
"5430",
"--target-dir",
"tmp_target",
]

Check failure on line 52 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:42:19 | 42 | run_args: vec![ | ___________________^ 43 | | "-d", 44 | | "--topology", 45 | | "../../assets/topology.toml", ... | 51 | | "tmp_target", 52 | | ] | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec help: you can use an array directly | 42 ~ run_args: ["-d", 43 + "--topology", 44 + "../../assets/topology.toml", 45 + "--base-http-port", 46 + "8001", 47 + "--base-pg-port", 48 + "5430", 49 + "--target-dir", 50 + "tmp_target"] |

Check failure on line 52 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:42:19 | 42 | run_args: vec![ | ___________________^ 43 | | "-d", 44 | | "--topology", 45 | | "../../assets/topology.toml", ... | 51 | | "tmp_target", 52 | | ] | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec help: you can use an array directly | 42 ~ run_args: ["-d", 43 + "--topology", 44 + "../../assets/topology.toml", 45 + "--base-http-port", 46 + "8001", 47 + "--base-pg-port", 48 + "5430", 49 + "--target-dir", 50 + "tmp_target"] |
.iter()
.map(|&s| s.into())
.collect(),
build_args: vec!["--target-dir", "tmp_target"]

Check failure on line 56 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:56:21 | 56 | build_args: vec!["--target-dir", "tmp_target"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--target-dir", "tmp_target"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec

Check failure on line 56 in tests/run.rs

View workflow job for this annotation

GitHub Actions / lint_check

useless use of `vec!`

error: useless use of `vec!` --> tests/run.rs:56:21 | 56 | build_args: vec!["--target-dir", "tmp_target"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `["--target-dir", "tmp_target"]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_vec
.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 115d47d

Please sign in to comment.