#[cfg(test)]
mod testsolr {
use crate::args::{Cli, SOLR_COPY_DIR, SOLR_COPY_URL};
use chrono::offset::Local;
use clap::Parser;
use glob::glob;
use log::error;
use std::fs::remove_file;
fn test_command_line_args_for(args: &[&str]) {
let parsed = Cli::parse_from(args);
execute_command_for(args, parsed);
}
#[cfg(not(feature = "testsolr"))]
fn execute_command_for(args: &[&str], parsed: Cli) {
assert_eq!(args.len() > 0, true);
assert_eq!(parsed.arguments.validate(), Ok(()));
}
#[cfg(feature = "testsolr")]
fn execute_command_for(args: &[&str], parsed: Cli) {
let res = crate::wrangle::command_exec(&parsed.arguments);
if let Err(err) = res {
let res = format!("Command: {}", args.join(" "));
let msg = format!("Failure: {:?}", err);
assert_eq!(res, msg, "\n Tip: Check it the core has any documents indexed.");
}
}
fn get_solr_url() -> String {
std::env::var(SOLR_COPY_URL).unwrap_or_else(|_| "http://localhost:8983/solr".into())
}
fn get_output_dir() -> String {
std::env::var(SOLR_COPY_DIR).unwrap_or_else(|_| "target".into())
}
fn cleanup_output_dir() {
let listed = glob("target/demo*").unwrap();
let found = listed.filter_map(Result::ok).collect::<Vec<_>>();
for file in found {
if let Err(e) = remove_file(&file) {
error!("Failed to remove file {}: {}", file.display(), e);
}
}
}
fn check_exec_backup(url: &str, dir: &str, core: &str, comp: &str) {
let test_args = &[
"solrcopy",
"backup",
"--url",
url,
"--core",
core,
"--dir",
dir,
"--archive-compression",
comp,
];
test_command_line_args_for(test_args);
}
fn check_exec_restore(url: &str, dir: &str, core: &str) {
let test_args = &[
"solrcopy",
"restore",
"--url",
url,
"--core",
core,
"--search",
"demo*.zip",
"--dir",
dir,
];
test_command_line_args_for(test_args);
}
#[test]
fn check_exec_solrcopy_with_solr_running() {
let (uri, out) = (get_solr_url(), get_output_dir());
let (url, dir) = (uri.as_str(), out.as_str());
cleanup_output_dir();
check_exec_backup(url, dir, "demo", "zip");
check_exec_delete(url, "target");
check_exec_restore(url, dir, "target");
check_exec_backup(url, dir, "demo", "zstd");
}
#[test]
fn check_exec_commit() {
let uri = get_solr_url();
let url = uri.as_str();
let test_args =
&["solrcopy", "commit", "--url", url, "--core", "demo", "--log-level", "debug"];
test_command_line_args_for(test_args);
}
#[test]
fn check_exec_info() {
let uri = get_solr_url();
let url = uri.as_str();
let test_args =
&["solrcopy", "info", "--url", url, "--core", "demo", "--log-level", "debug"];
test_command_line_args_for(test_args);
}
#[test]
fn check_exec_create() {
let uri = get_solr_url();
let url = uri.as_str();
let now = Local::now().timestamp();
let added = format!("added_{}", now);
let test_args =
&["solrcopy", "create", "--url", url, "--core", &added, "--log-level", "debug"];
test_command_line_args_for(test_args);
}
fn check_exec_delete(url: &str, core: &str) {
let test_args = &[
"solrcopy",
"delete",
"--url",
url,
"--core",
core,
"--query",
"*:*",
"--flush",
"hard",
"--log-level",
"debug",
];
test_command_line_args_for(test_args);
}
#[test]
fn check_exec_generate() {
let out = get_output_dir();
let dir = out.as_str();
let test_args = &["solrcopy", "generate", "--all", "--output-dir", dir];
test_command_line_args_for(test_args);
}
}