-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The test creates, indexes, searches and then drops a small hdfs logs file. Using `testcontainers-rs`, the test runs a postgres instance inside a container.
- Loading branch information
Showing
12 changed files
with
606 additions
and
47 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{ | ||
fs::canonicalize, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use color_eyre::Result; | ||
use sqlx::{migrate::Migrator, postgres::PgPoolOptions, PgPool}; | ||
use testcontainers::{runners::AsyncRunner, ContainerAsync}; | ||
use testcontainers_modules::postgres::Postgres as PostgresContainer; | ||
|
||
static MIGRATOR: Migrator = sqlx::migrate!(); | ||
|
||
const MAX_DB_CONNECTIONS: u32 = 100; | ||
|
||
pub struct Postgres { | ||
/// Keep container alive (container is deleted on drop). | ||
_container: ContainerAsync<PostgresContainer>, | ||
|
||
/// The underlying sqlx connection to the postgres inside the container. | ||
pub pool: PgPool, | ||
} | ||
|
||
async fn open_db_pool(url: &str) -> Result<PgPool> { | ||
Ok(PgPoolOptions::new() | ||
.max_connections(MAX_DB_CONNECTIONS) | ||
.connect(url) | ||
.await?) | ||
} | ||
|
||
pub async fn run_postgres() -> Result<Postgres> { | ||
let container = PostgresContainer::default().start().await?; | ||
let pool = open_db_pool(&format!( | ||
"postgres://postgres:[email protected]:{}/postgres", | ||
container.get_host_port_ipv4(5432).await? | ||
)) | ||
.await?; | ||
|
||
MIGRATOR.run(&pool).await?; | ||
|
||
Ok(Postgres { | ||
_container: container, | ||
pool, | ||
}) | ||
} | ||
|
||
pub fn get_test_file_path(test_file: &str) -> PathBuf { | ||
canonicalize(&Path::new(file!())) | ||
.unwrap() | ||
.parent() | ||
.unwrap() | ||
.parent() | ||
.unwrap() | ||
.join("test_files") | ||
.join(test_file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
mod common; | ||
|
||
use std::str::FromStr; | ||
|
||
use clap::Parser; | ||
use color_eyre::Result; | ||
use ctor::ctor; | ||
use pretty_env_logger::formatted_timed_builder; | ||
use tokio::sync::mpsc; | ||
use toshokan::{ | ||
args::{DropArgs, IndexArgs, SearchArgs}, | ||
commands::{ | ||
create::run_create_from_config, drop::run_drop, index::run_index, | ||
search::run_search_with_callback, | ||
}, | ||
config::IndexConfig, | ||
}; | ||
|
||
use crate::common::{get_test_file_path, run_postgres}; | ||
|
||
#[ctor] | ||
fn init() { | ||
color_eyre::install().unwrap(); | ||
|
||
let mut log_builder = formatted_timed_builder(); | ||
log_builder.parse_filters("toshokan=trace,opendal::services=info"); | ||
log_builder.try_init().unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_example_config() -> Result<()> { | ||
let postgres = run_postgres().await?; | ||
let config = IndexConfig::from_str(include_str!("../example_config.yaml"))?; | ||
|
||
run_create_from_config(&config, &postgres.pool).await?; | ||
|
||
run_index( | ||
IndexArgs::parse_from([ | ||
"", | ||
&config.name, | ||
&get_test_file_path("hdfs-logs-multitenants-2.json").to_string_lossy(), | ||
]), | ||
&postgres.pool, | ||
) | ||
.await?; | ||
|
||
let (tx, mut rx) = mpsc::channel(1); | ||
run_search_with_callback( | ||
SearchArgs::parse_from([ | ||
"", | ||
&config.name, | ||
"tenant_id:>50 AND severity_text:INFO", | ||
"--limit", | ||
"1", | ||
]), | ||
&postgres.pool, | ||
Box::new(move |doc| { | ||
tx.try_send(doc).unwrap(); | ||
}), | ||
) | ||
.await?; | ||
|
||
assert_eq!( | ||
rx.recv().await.unwrap(), | ||
r#"{"attributes":{"class":"org.apache.hadoop.hdfs.server.datanode.DataNode"},"body":"PacketResponder: BP-108841162-10.10.34.11-1440074360971:blk_1074072698_331874, type=HAS_DOWNSTREAM_IN_PIPELINE terminating","resource":{"service":"datanode/01"},"severity_text":"INFO","tenant_id":58,"timestamp":"2016-04-13T06:46:53Z"}"# | ||
); | ||
|
||
run_drop(DropArgs::parse_from(["", &config.name]), &postgres.pool).await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{"timestamp":1460530013,"severity_text":"INFO","body":"PacketResponder: BP-108841162-10.10.34.11-1440074360971:blk_1074072698_331874, type=HAS_DOWNSTREAM_IN_PIPELINE terminating","resource":{"service":"datanode/01"},"attributes":{"class":"org.apache.hadoop.hdfs.server.datanode.DataNode"},"tenant_id":58} | ||
{"timestamp":1460530014,"severity_text":"INFO","body":"Receiving BP-108841162-10.10.34.11-1440074360971:blk_1074072706_331882 src: /10.10.34.33:42666 dest: /10.10.34.11:50010","resource":{"service":"datanode/01"},"attributes":{"class":"org.apache.hadoop.hdfs.server.datanode.DataNode"},"tenant_id":46} |