-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
172 additions
and
139 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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); | ||
|
||
|
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub use sea_orm; | ||
pub mod domain; | ||
pub mod reldb_client; | ||
pub use sqlx; |
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 |
---|---|---|
@@ -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 { | ||
|
@@ -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<()> | ||
|
@@ -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<()> | ||
|
@@ -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<()> | ||
|
@@ -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<()> | ||
|
@@ -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<()> | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.