From 34d8fd75d08a7141da439b55af098dae59e4e321 Mon Sep 17 00:00:00 2001 From: 4t145 Date: Wed, 16 Oct 2024 17:23:27 +0800 Subject: [PATCH] update test-container's version --- .vscode/extensions.json | 2 +- examples/cache/src/main.rs | 6 +- examples/mq/src/main.rs | 6 +- examples/multi-apps/src/main.rs | 6 +- examples/pg-graph-search/src/main.rs | 6 +- examples/reldb/src/main.rs | 6 +- examples/todos/src/main.rs | 6 +- tardis/Cargo.toml | 11 +- tardis/src/db.rs | 1 + tardis/src/test/test_container.rs | 122 ++++++++---------- .../src/test/test_container/nacos_server.rs | 26 ++-- tardis/tests/grpc/rust/helloworld.rs | 70 ++++++++-- tardis/tests/test_config_with_remote.rs | 37 +++--- tardis/tests/test_web_server.rs | 6 +- 14 files changed, 172 insertions(+), 139 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 90f5f346..23ad04bb 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -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", diff --git a/examples/cache/src/main.rs b/examples/cache/src/main.rs index ac55b8b5..cbc687b7 100644 --- a/examples/cache/src/main.rs +++ b/examples/cache/src/main.rs @@ -2,7 +2,6 @@ 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; @@ -10,9 +9,8 @@ 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()); diff --git a/examples/mq/src/main.rs b/examples/mq/src/main.rs index fab81688..a0fe21b4 100644 --- a/examples/mq/src/main.rs +++ b/examples/mq/src/main.rs @@ -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:guest@127.0.0.1:{port}/%2f"); env::set_var("TARDIS_FW.MQ.URL", url); diff --git a/examples/multi-apps/src/main.rs b/examples/multi-apps/src/main.rs index 5362f990..2f3052c2 100644 --- a/examples/multi-apps/src/main.rs +++ b/examples/multi-apps/src/main.rs @@ -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; @@ -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); diff --git a/examples/pg-graph-search/src/main.rs b/examples/pg-graph-search/src/main.rs index 6f2a33d0..23752337 100644 --- a/examples/pg-graph-search/src/main.rs +++ b/examples/pg-graph-search/src/main.rs @@ -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); diff --git a/examples/reldb/src/main.rs b/examples/reldb/src/main.rs index 6efe0b2a..f6fbd108 100644 --- a/examples/reldb/src/main.rs +++ b/examples/reldb/src/main.rs @@ -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; @@ -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); diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 33b1fc86..00be6059 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -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; @@ -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); diff --git a/tardis/Cargo.toml b/tardis/Cargo.toml index 85e0bdbe..e0fb05a6 100644 --- a/tardis/Cargo.toml +++ b/tardis/Cargo.toml @@ -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 @@ -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 diff --git a/tardis/src/db.rs b/tardis/src/db.rs index 6e9b73eb..dfe626ae 100644 --- a/tardis/src/db.rs +++ b/tardis/src/db.rs @@ -1,3 +1,4 @@ pub use sea_orm; pub mod domain; pub mod reldb_client; +pub use sqlx; diff --git a/tardis/src/test/test_container.rs b/tardis/src/test/test_container.rs index 248a754a..220936b6 100644 --- a/tardis/src/test/test_container.rs +++ b/tardis/src/test/test_container.rs @@ -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 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 { - docker.run(Redis) + pub async fn redis_custom() -> TardisResult> { + let result = Redis::default().start().await?; + Ok(result) } pub async fn rabbit(fun: F) -> TardisResult<()> @@ -41,15 +51,15 @@ impl TardisTestContainer { if std::env::var_os("TARDIS_TEST_DISABLED_DOCKER").is_some() { fun("amqp://guest:guest@127.0.0.1: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:guest@127.0.0.1:{port}/%2f")).await } } - pub fn rabbit_custom(docker: &Cli) -> Container { - docker.run(GenericImage::new("rabbitmq", "management").with_wait_for(WaitFor::message_on_stdout("Server startup complete"))) + pub async fn rabbit_custom() -> TardisResult> { + let rabbit_mq = RabbitMq::default().start().await?; + Ok(rabbit_mq) } pub async fn mysql(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:123456@127.0.0.1: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:123456@127.0.0.1:{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> { + 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(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:123456@127.0.0.1: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:123456@127.0.0.1:{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> { + 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(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 { - 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> { + let es = ElasticSearch::default().with_env_var("ELASTICSEARCH_HEAP_SIZE", "128m").start().await?; + Ok(es) } pub async fn minio(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 { - docker.run(MinIO::default()) + pub async fn minio_custom() -> TardisResult> { + let min_io = MinIO::default().start().await?; + Ok(min_io) } } diff --git a/tardis/src/test/test_container/nacos_server.rs b/tardis/src/test/test_container/nacos_server.rs index 01ce4351..5d4a903a 100644 --- a/tardis/src/test/test_container/nacos_server.rs +++ b/tardis/src/test/test_container/nacos_server.rs @@ -47,31 +47,29 @@ impl std::fmt::Display for NacosServerMode { def_container! { NacosServer { - nacos_auth_enable: bool = false, - mode: NacosServerMode = NacosServerMode::Cluster, - nacos_auth_identity_key: String = "nacos", - nacos_auth_identity_value: String = "nacos", - nacos_auth_token: String = "TARDIS-NACOS-SERVER-TEST-CONTAINER", - nacos_auth_token_expire_seconds: usize = 18000_usize + nacos_auth_enable: bool = false, + mode: NacosServerMode = NacosServerMode::Cluster, + nacos_auth_identity_key: String = "nacos", + nacos_auth_identity_value: String = "nacos", + nacos_auth_token: String = "TARDIS-NACOS-SERVER-TEST-CONTAINER", + nacos_auth_token_expire_seconds: usize = 18000_usize } } impl Image for NacosServer { - type Args = Vec; - - fn name(&self) -> String { - "nacos/nacos-server".to_string() + fn name(&self) -> &str { + "nacos/nacos-server" } - fn tag(&self) -> String { - self.tag.clone() + fn tag(&self) -> &str { + &self.tag } fn ready_conditions(&self) -> Vec { vec![WaitFor::message_on_stdout("Nacos started successfully")] } - fn env_vars(&self) -> Box + '_> { - Box::new(self.env_vars.iter()) + fn env_vars(&self) -> impl IntoIterator>, impl Into>)> { + self.env_vars.iter().map(|(k, v)| (k.as_str(), v.as_str())) } } diff --git a/tardis/tests/grpc/rust/helloworld.rs b/tardis/tests/grpc/rust/helloworld.rs index 74f1d521..7716f672 100644 --- a/tardis/tests/grpc/rust/helloworld.rs +++ b/tardis/tests/grpc/rust/helloworld.rs @@ -1,13 +1,11 @@ // This file is @generated by prost-build. /// The request message containing the user's name. -#[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct HelloRequest { #[prost(string, tag = "1")] pub name: ::prost::alloc::string::String, } /// The response message containing the greetings -#[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct HelloReply { #[prost(string, tag = "1")] @@ -55,6 +53,19 @@ impl GreeterClient { self.cli = self.cli.with(middleware); self } + /// Set the compression encoding for sending + pub fn set_send_compressed(&mut self, encoding: poem_grpc::CompressionEncoding) { + self.cli.set_send_compressed(encoding); + } + /// Set the compression encodings for accepting + pub fn set_accept_compressed( + &mut self, + encodings: impl ::std::convert::Into< + ::std::sync::Arc<[poem_grpc::CompressionEncoding]>, + >, + ) { + self.cli.set_accept_compressed(encodings); + } #[allow(dead_code)] pub async fn say_hello( &self, @@ -80,15 +91,52 @@ pub trait Greeter: Send + Sync + 'static { > + Send; } #[allow(unused_imports)] -#[derive(Clone)] -pub struct GreeterServer(::std::sync::Arc); +pub struct GreeterServer { + inner: ::std::sync::Arc, + send_compressd: ::std::option::Option, + accept_compressed: ::std::sync::Arc<[poem_grpc::CompressionEncoding]>, +} +impl ::std::clone::Clone for GreeterServer { + #[inline] + fn clone(&self) -> Self { + Self { + inner: self.inner.clone(), + send_compressd: self.send_compressd, + accept_compressed: self.accept_compressed.clone(), + } + } +} impl poem_grpc::Service for GreeterServer { const NAME: &'static str = "helloworld.Greeter"; } #[allow(dead_code)] impl GreeterServer { + /// Create a new GRPC server pub fn new(service: T) -> Self { - Self(::std::sync::Arc::new(service)) + Self { + inner: ::std::sync::Arc::new(service), + send_compressd: ::std::option::Option::None, + accept_compressed: ::std::sync::Arc::new([]), + } + } + /// Set the compression encoding for sending + pub fn send_compressed(self, encoding: poem_grpc::CompressionEncoding) -> Self { + Self { + send_compressd: Some(encoding), + ..self + } + } + /// Set the compression encodings for accepting + pub fn accept_compressed( + self, + encodings: impl ::std::convert::Into< + ::std::sync::Arc<[poem_grpc::CompressionEncoding]>, + >, + ) -> Self { + Self { + accept_compressed: encodings.into(), + ..self + } } } impl ::poem::IntoEndpoint for GreeterServer { @@ -114,16 +162,20 @@ impl ::poem::IntoEndpoint for GreeterServer { .at( "/SayHello", ::poem::endpoint::make({ - let svc = self.0.clone(); + let server = self.clone(); move |req| { - let svc = svc.clone(); + let server = server.clone(); async move { let codec = as ::std::default::Default>::default(); - poem_grpc::server::GrpcServer::new(codec) - .unary(Greetersay_helloService(svc.clone()), req) + poem_grpc::server::GrpcServer::new( + codec, + server.send_compressd, + &server.accept_compressed, + ) + .unary(Greetersay_helloService(server.inner.clone()), req) .await } } diff --git a/tardis/tests/test_config_with_remote.rs b/tardis/tests/test_config_with_remote.rs index b3de33ea..5b6f9ce9 100644 --- a/tardis/tests/test_config_with_remote.rs +++ b/tardis/tests/test_config_with_remote.rs @@ -12,9 +12,10 @@ use tardis::serde::{Deserialize, Serialize}; use tardis::test::test_container::nacos_server::NacosServer; use tardis::TardisFuns; -use testcontainers::clients::Cli; -use testcontainers::Container; -use testcontainers::GenericImage; + +use testcontainers::runners::AsyncRunner; +use testcontainers::{ContainerAsync, GenericImage}; +use testcontainers_modules::rabbitmq::RabbitMq; use testcontainers_modules::redis::Redis; use tracing::{info, warn}; @@ -39,17 +40,17 @@ impl TestApi { } #[allow(dead_code)] -struct DockerEnv<'d> { +struct DockerEnv { nacos_url: String, mq_url: String, - nacos: Container<'d, NacosServer>, - mq: Container<'d, GenericImage>, - cache: Container<'d, Redis>, + nacos: ContainerAsync, + mq: ContainerAsync, + cache: ContainerAsync, } const NACOS_TAG: &str = "v2.2.3-slim"; -fn initialize_docker_env(cli: &Cli) -> DockerEnv { +async fn initialize_docker_env() -> DockerEnv { // init nacos docker use tardis::test::test_container::nacos_server::NacosServerMode; use tardis::test::test_container::TardisTestContainer; @@ -64,29 +65,32 @@ fn initialize_docker_env(cli: &Cli) -> DockerEnv { .nacos_auth_token_expire_seconds(10) .mode(NacosServerMode::Standalone); nacos.tag = NACOS_TAG.to_string(); - let nacos = cli.run(nacos); - let nacos_url = format!("{schema}://{ip}:{port}/nacos", schema = "http", ip = "127.0.0.1", port = nacos.get_host_port_ipv4(8848)); + let nacos = nacos.start().await.expect("fail to start nacos server"); + let port = nacos.get_host_port_ipv4(8848).await.expect("fail to get nacos port"); + + let nacos_url = format!("{schema}://{ip}:{port}/nacos", schema = "http", ip = "127.0.0.1"); env::set_var("TARDIS_FW.CONF_CENTER.URL", nacos_url.clone()); - nacos.start(); + nacos.start().await.expect("fail to start nacos server"); println!("nacos server started at: {}", nacos_url); // mq - let mq = TardisTestContainer::rabbit_custom(cli); + let mq = TardisTestContainer::rabbit_custom().await.expect("fail to start rabbitmq"); + let port = mq.get_host_port_ipv4(5672).await.expect("fail to get mq port"); let mq_url = format!( "{schema}://{user}:{pswd}@{ip}:{port}/%2f", schema = "amqp", user = "guest", pswd = "guest", ip = "127.0.0.1", - port = mq.get_host_port_ipv4(5672) ); env::set_var("TARDIS_FW.MQ.URL", mq_url.clone()); env::set_var("TARDIS_FW.MQ.MODULES.M1.URL", mq_url.clone()); println!("rabbit-mq started at: {}", mq_url); // redis - let redis = TardisTestContainer::redis_custom(cli); - let redis_url = format!("redis://localhost:{port}/0", port = redis.get_host_port_ipv4(6379)); + let redis = TardisTestContainer::redis_custom().await.expect("fail to start redis"); + let port = redis.get_host_port_ipv4(6379).await.expect("fail to get redis port"); + let redis_url = format!("redis://localhost:{port}/0"); env::set_var("TARDIS_FW.CACHE.URL", redis_url.clone()); DockerEnv { @@ -103,8 +107,7 @@ async fn test_config_with_remote() -> TardisResult<()> { env::set_var("RUST_LOG", "info,tardis::config=debug"); env::set_var("PROFILE", "remote"); - let docker = testcontainers::clients::Cli::default(); - let docker_env = initialize_docker_env(&docker); + let docker_env = initialize_docker_env().await; TardisFuns::init(Some("tests/config")).await?; assert_eq!(TardisFuns::cs_config::("").project_name, "测试_romote_locale"); assert_eq!(TardisFuns::cs_config::("").level_num, 3); diff --git a/tardis/tests/test_web_server.rs b/tardis/tests/test_web_server.rs index f3ce26ef..9973d192 100644 --- a/tardis/tests/test_web_server.rs +++ b/tardis/tests/test_web_server.rs @@ -11,7 +11,6 @@ use poem::http::Method; use poem::{IntoResponse, Middleware, Response}; use serde_json::json; use tardis::web::web_server::WebServerModule; -use testcontainers::clients; use tokio::time::sleep; use tracing::info; @@ -93,9 +92,8 @@ async fn test_web_server() -> TardisResult<()> { tardis::TardisFuns::init_log(); let web_url = "https://localhost:8080"; - let docker = clients::Cli::default(); - let redis_container = TardisTestContainer::redis_custom(&docker); - let redis_port = redis_container.get_host_port_ipv4(6379); + let redis_container = TardisTestContainer::redis_custom().await?; + let redis_port = redis_container.get_host_port_ipv4(6379).await?; let redis_url = format!("redis://127.0.0.1:{redis_port}/0"); start_serv(web_url, &redis_url).await?; sleep(Duration::from_millis(500)).await;