2021-12-15 10:30:00 +01:00
|
|
|
#![allow(dead_code)]
|
2022-03-21 10:50:55 +01:00
|
|
|
|
2021-03-24 11:03:01 +01:00
|
|
|
use std::path::Path;
|
2022-10-26 11:09:44 +02:00
|
|
|
use std::time::Duration;
|
2021-03-24 11:03:01 +01:00
|
|
|
|
2022-10-20 18:00:07 +02:00
|
|
|
use actix_http::body::MessageBody;
|
|
|
|
use actix_web::dev::ServiceResponse;
|
2021-03-10 14:43:10 +01:00
|
|
|
use actix_web::http::StatusCode;
|
2021-02-18 19:50:52 +01:00
|
|
|
use byte_unit::{Byte, ByteUnit};
|
2022-10-20 18:00:07 +02:00
|
|
|
use clap::Parser;
|
2022-12-07 08:20:47 -07:00
|
|
|
use meilisearch::option::{IndexerOpts, MaxMemory, Opt};
|
|
|
|
use meilisearch::{analytics, create_app, setup_meilisearch};
|
2021-09-28 18:10:09 +02:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-26 11:09:44 +02:00
|
|
|
use serde_json::{json, Value};
|
2021-09-28 18:10:09 +02:00
|
|
|
use tempfile::TempDir;
|
2022-10-26 11:09:44 +02:00
|
|
|
use tokio::time::sleep;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
|
|
|
use super::index::Index;
|
|
|
|
use super::service::Service;
|
2022-10-20 18:00:07 +02:00
|
|
|
use crate::common::encoder::Encoder;
|
2021-02-18 19:50:52 +01:00
|
|
|
|
|
|
|
pub struct Server {
|
2021-02-24 09:30:51 +01:00
|
|
|
pub service: Service,
|
2021-03-24 11:03:01 +01:00
|
|
|
// hold ownership to the tempdir while we use the server instance.
|
2021-09-28 18:10:09 +02:00
|
|
|
_dir: Option<TempDir>,
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 18:31:27 +01:00
|
|
|
pub static TEST_TEMP_DIR: Lazy<TempDir> = Lazy::new(|| TempDir::new().unwrap());
|
2021-09-28 18:10:09 +02:00
|
|
|
|
2021-02-18 19:50:52 +01:00
|
|
|
impl Server {
|
|
|
|
pub async fn new() -> Self {
|
2021-09-28 18:10:09 +02:00
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
std::env::set_var("TMP", TEST_TEMP_DIR.path());
|
|
|
|
} else {
|
|
|
|
std::env::set_var("TMPDIR", TEST_TEMP_DIR.path());
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2021-09-28 18:10:09 +02:00
|
|
|
let options = default_settings(dir.path());
|
2021-02-18 19:50:52 +01:00
|
|
|
|
2022-10-18 15:14:18 +02:00
|
|
|
let (index_scheduler, auth) = setup_meilisearch(&options).unwrap();
|
2022-10-25 16:28:33 +02:00
|
|
|
let service = Service { index_scheduler, auth, options, api_key: None };
|
2022-10-20 18:00:07 +02:00
|
|
|
|
|
|
|
Server { service, _dir: Some(dir) }
|
2021-03-24 11:03:01 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:08:26 +02:00
|
|
|
pub async fn new_auth_with_options(mut options: Opt, dir: TempDir) -> Self {
|
2021-12-15 14:52:33 +01:00
|
|
|
if cfg!(windows) {
|
|
|
|
std::env::set_var("TMP", TEST_TEMP_DIR.path());
|
|
|
|
} else {
|
|
|
|
std::env::set_var("TMPDIR", TEST_TEMP_DIR.path());
|
|
|
|
}
|
|
|
|
|
|
|
|
options.master_key = Some("MASTER_KEY".to_string());
|
|
|
|
|
2022-10-18 15:14:18 +02:00
|
|
|
let (index_scheduler, auth) = setup_meilisearch(&options).unwrap();
|
2022-10-25 16:28:33 +02:00
|
|
|
let service = Service { index_scheduler, auth, options, api_key: None };
|
2022-10-20 18:00:07 +02:00
|
|
|
|
|
|
|
Server { service, _dir: Some(dir) }
|
2021-12-15 14:52:33 +01:00
|
|
|
}
|
|
|
|
|
2022-06-09 18:08:26 +02:00
|
|
|
pub async fn new_auth() -> Self {
|
|
|
|
let dir = TempDir::new().unwrap();
|
|
|
|
let options = default_settings(dir.path());
|
|
|
|
Self::new_auth_with_options(options, dir).await
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:48:57 +02:00
|
|
|
pub async fn new_with_options(options: Opt) -> Result<Self, anyhow::Error> {
|
2022-10-18 15:14:18 +02:00
|
|
|
let (index_scheduler, auth) = setup_meilisearch(&options)?;
|
2022-10-25 16:28:33 +02:00
|
|
|
let service = Service { index_scheduler, auth, options, api_key: None };
|
2022-10-20 18:00:07 +02:00
|
|
|
|
|
|
|
Ok(Server { service, _dir: None })
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2022-10-18 15:14:18 +02:00
|
|
|
pub async fn init_web_app(
|
|
|
|
&self,
|
|
|
|
) -> impl actix_web::dev::Service<
|
|
|
|
actix_http::Request,
|
|
|
|
Response = ServiceResponse<impl MessageBody>,
|
|
|
|
Error = actix_web::Error,
|
|
|
|
> {
|
|
|
|
actix_web::test::init_service(create_app(
|
|
|
|
self.service.index_scheduler.clone().into(),
|
|
|
|
self.service.auth.clone(),
|
|
|
|
self.service.options.clone(),
|
|
|
|
analytics::MockAnalytics::new(&self.service.options),
|
|
|
|
true,
|
|
|
|
))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:50:52 +01:00
|
|
|
/// Returns a view to an index. There is no guarantee that the index exists.
|
2021-05-31 16:03:39 +02:00
|
|
|
pub fn index(&self, uid: impl AsRef<str>) -> Index<'_> {
|
2022-10-09 19:43:51 +02:00
|
|
|
self.index_with_encoder(uid, Encoder::Plain)
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:20:20 +01:00
|
|
|
pub async fn create_index(&self, body: Value) -> (Value, StatusCode) {
|
|
|
|
self.service.post("/indexes", body).await
|
|
|
|
}
|
|
|
|
|
2022-10-09 19:43:51 +02:00
|
|
|
pub fn index_with_encoder(&self, uid: impl AsRef<str>, encoder: Encoder) -> Index<'_> {
|
2022-10-20 18:00:07 +02:00
|
|
|
Index { uid: uid.as_ref().to_string(), service: &self.service, encoder }
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:41:37 +01:00
|
|
|
pub async fn multi_search(&self, queries: Value) -> (Value, StatusCode) {
|
|
|
|
self.service.post("/multi-search", queries).await
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:20:20 +01:00
|
|
|
pub async fn list_indexes_raw(&self, parameters: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.get(format!("/indexes{parameters}")).await
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:29:03 +02:00
|
|
|
pub async fn list_indexes(
|
|
|
|
&self,
|
|
|
|
offset: Option<usize>,
|
|
|
|
limit: Option<usize>,
|
|
|
|
) -> (Value, StatusCode) {
|
|
|
|
let (offset, limit) = (
|
|
|
|
offset.map(|offset| format!("offset={offset}")),
|
|
|
|
limit.map(|limit| format!("limit={limit}")),
|
|
|
|
);
|
|
|
|
let query_parameter = offset
|
|
|
|
.as_ref()
|
|
|
|
.zip(limit.as_ref())
|
|
|
|
.map(|(offset, limit)| format!("{offset}&{limit}"))
|
|
|
|
.or_else(|| offset.xor(limit));
|
|
|
|
if let Some(query_parameter) = query_parameter {
|
2022-10-20 18:00:07 +02:00
|
|
|
self.service.get(format!("/indexes?{query_parameter}")).await
|
2022-05-24 11:29:03 +02:00
|
|
|
} else {
|
|
|
|
self.service.get("/indexes").await
|
|
|
|
}
|
2021-02-19 19:14:25 +01:00
|
|
|
}
|
2021-03-15 19:08:19 +01:00
|
|
|
|
|
|
|
pub async fn version(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/version").await
|
|
|
|
}
|
2021-04-01 21:54:37 +03:00
|
|
|
|
|
|
|
pub async fn stats(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/stats").await
|
|
|
|
}
|
2021-12-02 16:03:26 +01:00
|
|
|
|
|
|
|
pub async fn tasks(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.get("/tasks").await
|
|
|
|
}
|
2021-12-15 10:30:00 +01:00
|
|
|
|
2023-01-12 13:55:53 +01:00
|
|
|
pub async fn tasks_filter(&self, filter: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.get(format!("/tasks?{}", filter)).await
|
2022-11-28 16:27:41 +01:00
|
|
|
}
|
|
|
|
|
2021-12-15 10:30:00 +01:00
|
|
|
pub async fn get_dump_status(&self, uid: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.get(format!("/dumps/{}/status", uid)).await
|
|
|
|
}
|
2022-10-26 11:09:44 +02:00
|
|
|
|
|
|
|
pub async fn create_dump(&self) -> (Value, StatusCode) {
|
|
|
|
self.service.post("/dumps", json!(null)).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn index_swap(&self, value: Value) -> (Value, StatusCode) {
|
|
|
|
self.service.post("/swap-indexes", value).await
|
|
|
|
}
|
|
|
|
|
2023-01-12 13:55:53 +01:00
|
|
|
pub async fn cancel_tasks(&self, value: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.post(format!("/tasks/cancel?{}", value), json!(null)).await
|
2022-10-26 11:09:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-12 13:55:53 +01:00
|
|
|
pub async fn delete_tasks(&self, value: &str) -> (Value, StatusCode) {
|
|
|
|
self.service.delete(format!("/tasks?{}", value)).await
|
2022-10-26 11:23:51 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 11:09:44 +02:00
|
|
|
pub async fn wait_task(&self, update_id: u64) -> Value {
|
|
|
|
// try several times to get status, or panic to not wait forever
|
|
|
|
let url = format!("/tasks/{}", update_id);
|
|
|
|
for _ in 0..100 {
|
|
|
|
let (response, status_code) = self.service.get(&url).await;
|
|
|
|
assert_eq!(200, status_code, "response: {}", response);
|
|
|
|
|
|
|
|
if response["status"] == "succeeded" || response["status"] == "failed" {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait 0.5 second.
|
|
|
|
sleep(Duration::from_millis(500)).await;
|
|
|
|
}
|
|
|
|
panic!("Timeout waiting for update id");
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_task(&self, update_id: u64) -> (Value, StatusCode) {
|
|
|
|
let url = format!("/tasks/{}", update_id);
|
|
|
|
self.service.get(url).await
|
|
|
|
}
|
2021-02-18 19:50:52 +01:00
|
|
|
}
|
2021-03-24 11:03:01 +01:00
|
|
|
|
|
|
|
pub fn default_settings(dir: impl AsRef<Path>) -> Opt {
|
|
|
|
Opt {
|
|
|
|
db_path: dir.as_ref().join("db"),
|
2022-12-05 16:56:28 +01:00
|
|
|
dump_dir: dir.as_ref().join("dumps"),
|
2021-03-24 11:03:01 +01:00
|
|
|
env: "development".to_owned(),
|
2021-06-16 17:12:49 +02:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
2022-01-12 15:57:31 +01:00
|
|
|
no_analytics: true,
|
2022-06-07 13:56:55 +02:00
|
|
|
max_index_size: Byte::from_unit(100.0, ByteUnit::MiB).unwrap(),
|
|
|
|
max_task_db_size: Byte::from_unit(1.0, ByteUnit::GiB).unwrap(),
|
2021-03-24 11:03:01 +01:00
|
|
|
http_payload_size_limit: Byte::from_unit(10.0, ByteUnit::MiB).unwrap(),
|
|
|
|
snapshot_dir: ".".into(),
|
2021-09-06 13:46:19 +02:00
|
|
|
indexer_options: IndexerOpts {
|
|
|
|
// memory has to be unlimited because several meilisearch are running in test context.
|
2022-03-24 18:52:36 +00:00
|
|
|
max_indexing_memory: MaxMemory::unlimited(),
|
2022-03-21 12:10:12 +01:00
|
|
|
..Parser::parse_from(None as Option<&str>)
|
2021-09-06 13:46:19 +02:00
|
|
|
},
|
2022-08-29 12:36:54 +02:00
|
|
|
#[cfg(feature = "metrics")]
|
2022-08-24 17:03:43 +02:00
|
|
|
enable_metrics_route: true,
|
2022-03-21 10:50:55 +01:00
|
|
|
..Parser::parse_from(None as Option<&str>)
|
2021-03-24 11:03:01 +01:00
|
|
|
}
|
|
|
|
}
|