Skip to content

Commit

Permalink
update test-container's version
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 committed Oct 16, 2024
1 parent 9a25ef9 commit 34d8fd7
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 139 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
"jscearcy.rust-doc-viewer",
"serayuzgur.crates",
"fill-labs.dependi",
"belfz.search-crates-io",
"vadimcn.vscode-lldb",
"swellaby.vscode-rust-test-adapter",
Expand Down
6 changes: 2 additions & 4 deletions examples/cache/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ use std::env;
use std::time::Duration;
use tardis::basic::result::TardisResult;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::tokio::time::sleep;
use tardis::TardisFuns;

#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let redis_container = TardisTestContainer::redis_custom(&docker);
let port = redis_container.get_host_port_ipv4(6379);
let redis_container = TardisTestContainer::redis_custom().await?;
let port = redis_container.get_host_port_ipv4(6379).await?;
let url = format!("redis://127.0.0.1:{port}/0");
env::set_var("TARDIS_FW.CACHE.URL", url.clone());
env::set_var("TARDIS_FW.CACHE.MODULES.M1.URL", url.clone());
Expand Down
6 changes: 2 additions & 4 deletions examples/mq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ use tokio::time::sleep;

use tardis::basic::result::TardisResult;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::TardisFuns;

#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let rabbit_container = TardisTestContainer::rabbit_custom(&docker);
let port = rabbit_container.get_host_port_ipv4(5672);
let rabbit_container = TardisTestContainer::rabbit_custom().await?;
let port = rabbit_container.get_host_port_ipv4(5672).await?;
let url = format!("amqp://guest:[email protected]:{port}/%2f");
env::set_var("TARDIS_FW.MQ.URL", url);

Expand Down
6 changes: 2 additions & 4 deletions examples/multi-apps/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;

use tardis::basic::result::TardisResult;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::TardisFuns;

Expand All @@ -26,9 +25,8 @@ mod initializer;
#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let mysql_container = TardisTestContainer::mysql_custom(None, &docker);
let port = mysql_container.get_host_port_ipv4(3306);
let mysql_container = TardisTestContainer::mysql_custom(None).await?;
let port = mysql_container.get_host_port_ipv4(3306).await?;
let url = format!("mysql://root:123456@localhost:{port}/test");
env::set_var("TARDIS_FW.DB.URL", url.clone());
env::set_var("TARDIS_FW.DB.MODULES.TAG.URL", url);
Expand Down
6 changes: 2 additions & 4 deletions examples/pg-graph-search/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ use std::vec;

use tardis::basic::result::TardisResult;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::TardisFuns;

#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let mysql_container = TardisTestContainer::postgres_custom(None, &docker);
let port = mysql_container.get_host_port_ipv4(5432);
let mysql_container = TardisTestContainer::postgres_custom(None).await?;
let port = mysql_container.get_host_port_ipv4(5432).await?;
let url = format!("postgres://postgres:123456@localhost:{port}/test");
env::set_var("TARDIS_FW.DB.URL", url);

Expand Down
6 changes: 2 additions & 4 deletions examples/reldb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use tardis::basic::result::TardisResult;
use tardis::db::sea_orm::*;
use tardis::log::info;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::TardisFuns;

Expand All @@ -14,9 +13,8 @@ mod domain;
#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let mysql_container = TardisTestContainer::mysql_custom(None, &docker);
let port = mysql_container.get_host_port_ipv4(3306);
let mysql_container = TardisTestContainer::mysql_custom(None).await?;
let port = mysql_container.get_host_port_ipv4(3306).await?;
let url = format!("mysql://root:123456@localhost:{port}/test");
env::set_var("TARDIS_FW.DB.URL", url);

Expand Down
6 changes: 2 additions & 4 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::env;

use tardis::basic::result::TardisResult;
use tardis::test::test_container::TardisTestContainer;
use tardis::testcontainers::clients;
use tardis::tokio;
use tardis::TardisFuns;

Expand All @@ -18,9 +17,8 @@ mod processor;
#[tokio::main]
async fn main() -> TardisResult<()> {
// Here is a demonstration of using docker to start a mysql simulation scenario.
let docker = clients::Cli::default();
let mysql_container = TardisTestContainer::mysql_custom(None, &docker);
let port = mysql_container.get_host_port_ipv4(3306);
let mysql_container = TardisTestContainer::mysql_custom(None).await?;
let port = mysql_container.get_host_port_ipv4(3306).await?;
let url = format!("mysql://root:123456@localhost:{port}/test");
env::set_var("TARDIS_FW.DB.URL", url);

Expand Down
11 changes: 8 additions & 3 deletions tardis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ sea-orm = { version = "1", features = [
"with-json",
"debug-print",
], optional = true }
sqlx = { version = "0.7", features = ["any"], optional = true }
sqlx = { version = "0.8", features = ["any"], optional = true }
sqlparser = { version = "0", optional = true }

# Web Server
Expand Down Expand Up @@ -232,10 +232,15 @@ anyhow = { version = "1.0", optional = true }

# Test
# update this may cause break changes
testcontainers = { version = "0.15", optional = true }
testcontainers-modules = { version = "0.3", features = [
testcontainers = { version = "0.23", optional = true }
testcontainers-modules = { version = "0.11", features = [
"minio",
"redis",
"rabbitmq",
"mysql",
"postgres",
"elastic_search",

], optional = true }

# Debug
Expand Down
1 change: 1 addition & 0 deletions tardis/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub use sea_orm;
pub mod domain;
pub mod reldb_client;
pub use sqlx;
122 changes: 55 additions & 67 deletions tardis/src/test/test_container.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
use std::env;
use std::future::Future;

use testcontainers::clients;
use testcontainers::clients::Cli;
use testcontainers::core::Container;
use testcontainers::core::WaitFor;
use testcontainers::GenericImage;
use testcontainers::core::Mount;
use testcontainers::runners::AsyncRunner;
use testcontainers::ContainerAsync as Container;
use testcontainers::ImageExt;
use testcontainers_modules::elastic_search::ElasticSearch;
use testcontainers_modules::minio::MinIO;
use testcontainers_modules::mysql::Mysql;
use testcontainers_modules::postgres::Postgres;
use testcontainers_modules::rabbitmq::RabbitMq;
use testcontainers_modules::redis::Redis;

use crate::basic::error::TardisError;
use crate::basic::result::TardisResult;

impl From<testcontainers::TestcontainersError> for TardisError {
fn from(e: testcontainers::TestcontainersError) -> Self {
TardisError::internal_error(&e.to_string(), "testcontainers-error")
}
}

pub struct TardisTestContainer;

impl TardisTestContainer {
Expand All @@ -22,15 +32,15 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("redis://127.0.0.1:6379/0".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::redis_custom(&docker);
let port = node.get_host_port_ipv4(6379);
let node = TardisTestContainer::redis_custom().await?;
let port = node.get_host_port_ipv4(6379).await?;
fun(format!("redis://127.0.0.1:{port}/0")).await
}
}

pub fn redis_custom(docker: &Cli) -> Container<Redis> {
docker.run(Redis)
pub async fn redis_custom() -> TardisResult<Container<Redis>> {
let result = Redis::default().start().await?;
Ok(result)
}

pub async fn rabbit<F, T>(fun: F) -> TardisResult<()>
Expand All @@ -41,15 +51,15 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("amqp://guest:[email protected]:5672/%2f".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::rabbit_custom(&docker);
let port = node.get_host_port_ipv4(5672);
let node = TardisTestContainer::rabbit_custom().await?;
let port = node.get_host_port_ipv4(5672).await?;
fun(format!("amqp://guest:[email protected]:{port}/%2f")).await
}
}

pub fn rabbit_custom(docker: &Cli) -> Container<GenericImage> {
docker.run(GenericImage::new("rabbitmq", "management").with_wait_for(WaitFor::message_on_stdout("Server startup complete")))
pub async fn rabbit_custom() -> TardisResult<Container<RabbitMq>> {
let rabbit_mq = RabbitMq::default().start().await?;
Ok(rabbit_mq)
}

pub async fn mysql<F, T>(init_script_path: Option<&str>, fun: F) -> TardisResult<()>
Expand All @@ -60,36 +70,26 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("mysql://root:[email protected]:3306/test".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::mysql_custom(init_script_path, &docker);
let port = node.get_host_port_ipv4(3306);
let node = TardisTestContainer::mysql_custom(init_script_path).await?;
let port = node.get_host_port_ipv4(3306).await?;
fun(format!("mysql://root:[email protected]:{port}/test")).await
}
}

pub fn mysql_custom<'a>(init_script_path: Option<&str>, docker: &'a Cli) -> Container<'a, GenericImage> {
if let Some(init_script_path) = init_script_path {
pub async fn mysql_custom(init_script_path: Option<&str>) -> TardisResult<Container<Mysql>> {
let mut mysql = Mysql::default().with_env_var("MYSQL_ROOT_PASSWORD", "123456").with_env_var("MYSQL_DATABASE", "test");
mysql = if let Some(init_script_path) = init_script_path {
let path = env::current_dir()
.expect("[Tardis.Test_Container] Current path get error")
.join(std::path::Path::new(init_script_path))
.to_str()
.unwrap_or_else(|| panic!("[Tardis.Test_Container] Script Path [{init_script_path}] get error"))
.to_string();
docker.run(
GenericImage::new("mysql", "8")
.with_env_var("MYSQL_ROOT_PASSWORD", "123456")
.with_env_var("MYSQL_DATABASE", "test")
.with_volume(path, "/docker-entrypoint-initdb.d/")
.with_wait_for(WaitFor::message_on_stderr("port: 3306 MySQL Community Server - GPL")),
)
mysql.with_mount(Mount::volume_mount(path, "/docker-entrypoint-initdb.d/"))
} else {
docker.run(
GenericImage::new("mysql", "8")
.with_env_var("MYSQL_ROOT_PASSWORD", "123456")
.with_env_var("MYSQL_DATABASE", "test")
.with_wait_for(WaitFor::message_on_stderr("port: 3306 MySQL Community Server - GPL")),
)
}
mysql
};
Ok(mysql.start().await?)
}

pub async fn postgres<F, T>(init_script_path: Option<&str>, fun: F) -> TardisResult<()>
Expand All @@ -100,36 +100,28 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("postgres://postgres:[email protected]:5432/test".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::postgres_custom(init_script_path, &docker);
let port = node.get_host_port_ipv4(5432);
let node = TardisTestContainer::postgres_custom(init_script_path).await?;
let port = node.get_host_port_ipv4(5432).await?;
fun(format!("postgres://postgres:[email protected]:{port}/test")).await
}
}

pub fn postgres_custom<'a>(init_script_path: Option<&str>, docker: &'a Cli) -> Container<'a, GenericImage> {
if let Some(init_script_path) = init_script_path {
pub async fn postgres_custom(init_script_path: Option<&str>) -> TardisResult<Container<Postgres>> {
let mut postgres = Postgres::default().with_env_var("POSTGRES_PASSWORD", "123456").with_env_var("POSTGRES_DB", "test");

postgres = if let Some(init_script_path) = init_script_path {
let path = env::current_dir()
.expect("[Tardis.Test_Container] Current path get error")
.join(std::path::Path::new(init_script_path))
.to_str()
.unwrap_or_else(|| panic!("[Tardis.Test_Container] Script Path [{init_script_path}] get error"))
.to_string();
docker.run(
GenericImage::new("postgres", "alpine")
.with_env_var("POSTGRES_PASSWORD", "123456")
.with_env_var("POSTGRES_DB", "test")
.with_volume(path, "/docker-entrypoint-initdb.d/")
.with_wait_for(WaitFor::message_on_stderr("database system is ready to accept connections")),
)
postgres.with_mount(Mount::volume_mount(path, "/docker-entrypoint-initdb.d/"))
} else {
docker.run(
GenericImage::new("postgres", "alpine")
.with_env_var("POSTGRES_PASSWORD", "123456")
.with_env_var("POSTGRES_DB", "test")
.with_wait_for(WaitFor::message_on_stderr("database system is ready to accept connections")),
)
}
postgres
};
let postgres = postgres.start().await?;
Ok(postgres)
}

pub async fn es<F, T>(fun: F) -> TardisResult<()>
Expand All @@ -140,19 +132,15 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("http://127.0.0.1:9200".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::es_custom(&docker);
let port = node.get_host_port_ipv4(9200);
let node = TardisTestContainer::es_custom().await?;
let port = node.get_host_port_ipv4(9200).await?;
fun(format!("http://127.0.0.1:{port}")).await
}
}

pub fn es_custom(docker: &Cli) -> Container<GenericImage> {
docker.run(
GenericImage::new("rapidfort/elasticsearch", "7.17")
.with_env_var(" ELASTICSEARCH_HEAP_SIZE", "128m")
.with_wait_for(WaitFor::message_on_stdout("Cluster health status changed from [YELLOW] to [GREEN]")),
)
pub async fn es_custom() -> TardisResult<Container<ElasticSearch>> {
let es = ElasticSearch::default().with_env_var("ELASTICSEARCH_HEAP_SIZE", "128m").start().await?;
Ok(es)
}

pub async fn minio<F, T>(fun: F) -> TardisResult<()>
Expand All @@ -163,15 +151,15 @@ impl TardisTestContainer {
if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() {
fun("http://127.0.0.1:9000".to_string()).await
} else {
let docker = clients::Cli::default();
let node = TardisTestContainer::minio_custom(&docker);
let port = node.get_host_port_ipv4(9000);
let node = TardisTestContainer::minio_custom().await?;
let port = node.get_host_port_ipv4(9000).await?;
fun(format!("http://127.0.0.1:{port}")).await
}
}

pub fn minio_custom(docker: &Cli) -> Container<MinIO> {
docker.run(MinIO::default())
pub async fn minio_custom() -> TardisResult<Container<MinIO>> {
let min_io = MinIO::default().start().await?;
Ok(min_io)
}
}

Expand Down
Loading

0 comments on commit 34d8fd7

Please sign in to comment.