diff --git a/Cargo.lock b/Cargo.lock index edb29e924722..e4f61724ae71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3231,6 +3231,25 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "config" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23738e11972c7643e4ec947840fc463b6a571afcd3e735bdfce7d03c7a784aca" +dependencies = [ + "async-trait", + "json5", + "lazy_static", + "nom", + "pathdiff", + "ron", + "rust-ini 0.18.0", + "serde", + "serde_json", + "toml 0.5.11", + "yaml-rust", +] + [[package]] name = "console" version = "0.15.5" @@ -3979,6 +3998,7 @@ dependencies = [ "common-storages-view", "common-tracing", "common-users", + "config", "criterion", "ctor", "dashmap", @@ -4296,6 +4316,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "dlv-list" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" + [[package]] name = "dlv-list" version = "0.5.0" @@ -7264,6 +7290,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "jsonb" version = "0.3.0" @@ -8691,13 +8728,23 @@ dependencies = [ "num-traits", ] +[[package]] +name = "ordered-multimap" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +dependencies = [ + "dlv-list 0.3.0", + "hashbrown 0.12.3", +] + [[package]] name = "ordered-multimap" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ed8acf08e98e744e5384c8bc63ceb0364e68a6854187221c18df61c4797690e" dependencies = [ - "dlv-list", + "dlv-list 0.5.0", "hashbrown 0.13.2", ] @@ -8906,6 +8953,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -8955,6 +9008,51 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "pest" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae9cee2a55a544be8b89dc6848072af97a20f2422603c10865be2a42b580fff5" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81d78524685f5ef2a3b3bd1cafbc9fcabb036253d9b1463e726a91cd16e2dfc2" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68bd1206e71118b5356dae5ddc61c8b11e28b09ef6a31acbd15ea48a28e0c227" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 2.0.29", +] + +[[package]] +name = "pest_meta" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c747191d4ad9e4a4ab9c8798f1e82a39affe7ef9648390b7e5548d18e099de6" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "petgraph" version = "0.6.3" @@ -10065,7 +10163,7 @@ dependencies = [ "rand 0.8.5", "reqwest", "rsa 0.9.2", - "rust-ini", + "rust-ini 0.19.0", "serde", "serde_json", "sha1", @@ -10287,6 +10385,17 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5864e7ef1a6b7bcf1d6ca3f655e65e724ed3b52546a0d0a663c991522f552ea" +[[package]] +name = "ron" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +dependencies = [ + "base64 0.13.1", + "bitflags 1.3.2", + "serde", +] + [[package]] name = "rsa" version = "0.7.2" @@ -10350,6 +10459,16 @@ dependencies = [ "cache-padded", ] +[[package]] +name = "rust-ini" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +dependencies = [ + "cfg-if", + "ordered-multimap 0.4.3", +] + [[package]] name = "rust-ini" version = "0.19.0" @@ -10357,7 +10476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7e2a3bcec1f113553ef1c88aae6c020a369d03d55b58de9869a0908930385091" dependencies = [ "cfg-if", - "ordered-multimap", + "ordered-multimap 0.6.0", ] [[package]] @@ -12393,6 +12512,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + [[package]] name = "uluru" version = "3.0.0" @@ -13131,6 +13256,15 @@ dependencies = [ "lzma-sys", ] +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "yansi" version = "0.5.1" diff --git a/scripts/setup/dev_setup.sh b/scripts/setup/dev_setup.sh index 205659922674..26ec4fc68a86 100755 --- a/scripts/setup/dev_setup.sh +++ b/scripts/setup/dev_setup.sh @@ -533,6 +533,8 @@ if [[ "$INSTALL_BUILD_TOOLS" == "true" ]]; then cargo quickinstall cargo-binstall cargo binstall -y sccache cargo binstall -y cargo-zigbuild + cargo install cargo-nextest + fi if [[ "$INSTALL_CHECK_TOOLS" == "true" ]]; then diff --git a/src/bendpy/src/lib.rs b/src/bendpy/src/lib.rs index b1c926435977..9dd05d3da8a0 100644 --- a/src/bendpy/src/lib.rs +++ b/src/bendpy/src/lib.rs @@ -54,7 +54,7 @@ fn databend(_py: Python, m: &PyModule) -> PyResult<()> { MetaEmbedded::init_global_meta_store(meta_dir.to_string_lossy().to_string()) .await .unwrap(); - GlobalServices::init(conf.clone()).await.unwrap(); + GlobalServices::init(&conf).await.unwrap(); // init oss license manager OssLicenseManager::init(conf.query.tenant_id.clone()).unwrap(); diff --git a/src/binaries/query/entry.rs b/src/binaries/query/entry.rs index 649fcfef2cfa..71681f43b7c3 100644 --- a/src/binaries/query/entry.rs +++ b/src/binaries/query/entry.rs @@ -78,7 +78,7 @@ pub async fn init_services(conf: &InnerConfig) -> Result<()> { )); } // Make sure global services have been inited. - GlobalServices::init(conf.clone()).await + GlobalServices::init(conf).await } async fn precheck_services(conf: &InnerConfig) -> Result<()> { diff --git a/src/binaries/tool/table_meta_inspector.rs b/src/binaries/tool/table_meta_inspector.rs index fffa341e08f7..91074ea4b421 100644 --- a/src/binaries/tool/table_meta_inspector.rs +++ b/src/binaries/tool/table_meta_inspector.rs @@ -87,7 +87,7 @@ async fn parse_input_data(config: &InspectorConfig) -> Result> { builder = builder.collect(from_file(Toml, config_file)); let read_config = builder.build()?; let inner_config: InnerConfig = read_config.clone().try_into()?; - GlobalServices::init(inner_config).await?; + GlobalServices::init(&inner_config).await?; let storage_config: StorageConfig = read_config.storage.try_into()?; init_operator(&storage_config.params)? } diff --git a/src/query/config/src/global.rs b/src/query/config/src/global.rs index 8b7600671393..51cb6e216eeb 100644 --- a/src/query/config/src/global.rs +++ b/src/query/config/src/global.rs @@ -22,8 +22,8 @@ use crate::InnerConfig; pub struct GlobalConfig; impl GlobalConfig { - pub fn init(config: InnerConfig) -> Result<()> { - GlobalInstance::set(Arc::new(config)); + pub fn init(config: &InnerConfig) -> Result<()> { + GlobalInstance::set(Arc::new(config.clone())); Ok(()) } diff --git a/src/query/ee/src/test_kits/context.rs b/src/query/ee/src/test_kits/context.rs index f0cb64d388d3..fa2086135fdb 100644 --- a/src/query/ee/src/test_kits/context.rs +++ b/src/query/ee/src/test_kits/context.rs @@ -19,14 +19,13 @@ use common_meta_app::storage::StorageFsConfig; use common_meta_app::storage::StorageParams; use databend_query::test_kits::ConfigBuilder; use databend_query::test_kits::Setup; -use databend_query::test_kits::TestGuard; use jwt_simple::algorithms::ECDSAP256KeyPairLike; use jwt_simple::prelude::Claims; use jwt_simple::prelude::Duration; use jwt_simple::prelude::ES256KeyPair; use tempfile::TempDir; -use crate::test_kits::sessions::TestGlobalServices; +use crate::test_kits::setup::TestFixture; fn build_custom_claims(license_type: String, org: String) -> LicenseInfo { LicenseInfo { @@ -74,10 +73,8 @@ impl Default for EESetup { #[async_trait::async_trait] impl Setup for EESetup { - async fn setup(&self) -> Result<(TestGuard, InnerConfig)> { - Ok(( - TestGlobalServices::setup(&self.config, self.pk.clone()).await?, - self.config.clone(), - )) + async fn setup(&self) -> Result { + TestFixture::setup(&self.config, self.pk.clone()).await?; + Ok(self.config.clone()) } } diff --git a/src/query/ee/src/test_kits/mock_services.rs b/src/query/ee/src/test_kits/mock_services.rs index d812f1acd868..fbd3ead73c84 100644 --- a/src/query/ee/src/test_kits/mock_services.rs +++ b/src/query/ee/src/test_kits/mock_services.rs @@ -29,7 +29,7 @@ use crate::virtual_column::RealVirtualColumnHandler; pub struct MockServices; impl MockServices { #[async_backtrace::framed] - pub async fn init(cfg: InnerConfig, public_key: String) -> Result<()> { + pub async fn init(cfg: &InnerConfig, public_key: String) -> Result<()> { let rm = RealLicenseManager::new(cfg.query.tenant_id.clone(), public_key); let wrapper = LicenseManagerWrapper { manager: Box::new(rm), diff --git a/src/query/ee/src/test_kits/mod.rs b/src/query/ee/src/test_kits/mod.rs index e65601cd1278..b5d252b7ab19 100644 --- a/src/query/ee/src/test_kits/mod.rs +++ b/src/query/ee/src/test_kits/mod.rs @@ -14,4 +14,4 @@ pub mod context; pub mod mock_services; -pub mod sessions; +pub mod setup; diff --git a/src/query/ee/src/test_kits/sessions.rs b/src/query/ee/src/test_kits/setup.rs similarity index 71% rename from src/query/ee/src/test_kits/sessions.rs rename to src/query/ee/src/test_kits/setup.rs index d98db7abd6f9..6b7ffeed1bdf 100644 --- a/src/query/ee/src/test_kits/sessions.rs +++ b/src/query/ee/src/test_kits/setup.rs @@ -16,33 +16,25 @@ use common_config::InnerConfig; use common_exception::Result; use common_tracing::set_panic_hook; use databend_query::clusters::ClusterDiscovery; -use databend_query::test_kits::TestGuard; use databend_query::GlobalServices; use log::info; use crate::test_kits::mock_services::MockServices; -pub struct TestGlobalServices; +pub struct TestFixture; -unsafe impl Send for TestGlobalServices {} - -unsafe impl Sync for TestGlobalServices {} - -impl TestGlobalServices { - pub async fn setup(config: &InnerConfig, public_key: String) -> Result { +impl TestFixture { + pub async fn setup(config: &InnerConfig, public_key: String) -> Result<()> { set_panic_hook(); std::env::set_var("UNIT_TEST", "TRUE"); - let thread_name = match std::thread::current().name() { - None => panic!("thread name is none"), - Some(thread_name) => thread_name.to_string(), - }; + let thread_name = std::thread::current().name().unwrap().to_string(); #[cfg(debug_assertions)] common_base::base::GlobalInstance::init_testing(&thread_name); - GlobalServices::init_with(config.clone()).await?; - MockServices::init(config.clone(), public_key).await?; + GlobalServices::init_with(config).await?; + MockServices::init(config, public_key).await?; // Cluster register. { @@ -55,6 +47,13 @@ impl TestGlobalServices { ); } - Ok(TestGuard::new(thread_name.to_string())) + Ok(()) + } + + /// Teardown the test environment. + pub async fn teardown() -> Result<()> { + // Nothing to do. + + Ok(()) } } diff --git a/src/query/ee/tests/it/aggregating_index/index_refresh.rs b/src/query/ee/tests/it/aggregating_index/index_refresh.rs index 7c1da520db48..6b1f819838c5 100644 --- a/src/query/ee/tests/it/aggregating_index/index_refresh.rs +++ b/src/query/ee/tests/it/aggregating_index/index_refresh.rs @@ -109,7 +109,7 @@ async fn refresh_index( #[tokio::test(flavor = "multi_thread")] async fn test_refresh_agg_index() -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; fixture .execute_command("CREATE TABLE t0 (a int, b int, c int) storage_format = 'parquet'") @@ -226,7 +226,7 @@ async fn test_refresh_agg_index() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_refresh_agg_index_with_limit() -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -297,7 +297,7 @@ async fn test_sync_agg_index() -> Result<()> { async fn test_sync_agg_index_after_update() -> Result<()> { // let (_guard, ctx, root) = create_ee_query_context(None).await.unwrap(); - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; fixture .default_session() .get_settings() @@ -417,7 +417,7 @@ async fn test_sync_agg_index_after_insert() -> Result<()> { // ctx.get_settings() // .set_enable_refresh_aggregating_index_after_write(true)?; - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; fixture .default_session() .get_settings() @@ -533,7 +533,7 @@ async fn test_sync_agg_index_after_insert() -> Result<()> { } async fn test_sync_agg_index_after_copy_into() -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; fixture .default_session() .get_settings() diff --git a/src/query/ee/tests/it/aggregating_index/index_scan.rs b/src/query/ee/tests/it/aggregating_index/index_scan.rs index e0481a11e6cf..ac3cdd117002 100644 --- a/src/query/ee/tests/it/aggregating_index/index_scan.rs +++ b/src/query/ee/tests/it/aggregating_index/index_scan.rs @@ -28,7 +28,7 @@ use common_sql::plans::RelOperator; use common_sql::Planner; use databend_query::interpreters::InterpreterFactory; use databend_query::sessions::QueryContext; -use databend_query::test_kits::table_test_fixture::expects_ok; +use databend_query::test_kits::fixture::expects_ok; use databend_query::test_kits::TestFixture; use enterprise_query::test_kits::context::EESetup; use futures_util::TryStreamExt; @@ -100,7 +100,7 @@ async fn drop_index(ctx: Arc, index_name: &str) -> Result<()> { } async fn test_index_scan_impl(format: &str) -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -241,7 +241,7 @@ async fn test_index_scan_impl(format: &str) -> Result<()> { } async fn test_index_scan_two_agg_funcs_impl(format: &str) -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -361,7 +361,7 @@ async fn test_index_scan_two_agg_funcs_impl(format: &str) -> Result<()> { } async fn test_projected_index_scan_impl(format: &str) -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -476,7 +476,7 @@ async fn test_projected_index_scan_impl(format: &str) -> Result<()> { } async fn test_index_scan_with_count_impl(format: &str) -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -527,7 +527,7 @@ async fn test_index_scan_with_count_impl(format: &str) -> Result<()> { } async fn test_index_scan_agg_args_are_expression_impl(format: &str) -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; // Create table fixture @@ -983,7 +983,7 @@ async fn test_fuzz_impl(format: &str, spill: bool) -> Result<()> { None }; - let fixture = TestFixture::with_setup(EESetup::new()).await?; + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; for num_blocks in [1, 10] { for num_rows_per_block in [1, 50] { let session = fixture.default_session(); @@ -1033,5 +1033,6 @@ async fn test_fuzz_impl(format: &str, spill: bool) -> Result<()> { fixture.execute_command("DROP TABLE t ALL").await?; } } + Ok(()) } diff --git a/src/query/ee/tests/it/license/license_mgr.rs b/src/query/ee/tests/it/license/license_mgr.rs index f25c23ccab0f..d82902987869 100644 --- a/src/query/ee/tests/it/license/license_mgr.rs +++ b/src/query/ee/tests/it/license/license_mgr.rs @@ -39,7 +39,7 @@ fn build_custom_claims( #[tokio::test(flavor = "multi_thread")] async fn test_parse_license() -> common_exception::Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let key_pair = ES256KeyPair::generate(); let license_mgr = RealLicenseManager::new( @@ -87,7 +87,7 @@ async fn test_parse_license() -> common_exception::Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_license_features() -> common_exception::Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let key_pair = ES256KeyPair::generate(); let license_mgr = RealLicenseManager::new( diff --git a/src/query/ee/tests/it/storages/fuse/operations/computed_columns.rs b/src/query/ee/tests/it/storages/fuse/operations/computed_columns.rs index e35585b91ee8..6d7085d51f7c 100644 --- a/src/query/ee/tests/it/storages/fuse/operations/computed_columns.rs +++ b/src/query/ee/tests/it/storages/fuse/operations/computed_columns.rs @@ -14,18 +14,20 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::test_kits::table_test_fixture::expects_ok; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::expects_ok; +use databend_query::test_kits::fixture::TestFixture; use enterprise_query::test_kits::context::EESetup; use futures::TryStreamExt; #[tokio::test(flavor = "multi_thread")] async fn test_computed_column() -> Result<()> { - let fixture = TestFixture::with_setup(EESetup::new()).await?; - let db = fixture.default_db_name(); - let tbl = fixture.default_table_name(); + let fixture = TestFixture::setup_with_custom(EESetup::new()).await?; + + fixture.create_default_database().await?; fixture.create_computed_table().await?; + let db = fixture.default_db_name(); + let tbl = fixture.default_table_name(); for i in 0..2 { let table = fixture.latest_default_table().await?; let num_blocks = 1; diff --git a/src/query/ee/tests/it/storages/fuse/operations/vacuum.rs b/src/query/ee/tests/it/storages/fuse/operations/vacuum.rs index cb2c6da926ed..d840880f9124 100644 --- a/src/query/ee/tests/it/storages/fuse/operations/vacuum.rs +++ b/src/query/ee/tests/it/storages/fuse/operations/vacuum.rs @@ -14,18 +14,21 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::test_kits::table_test_fixture::append_sample_data; -use databend_query::test_kits::table_test_fixture::check_data_dir; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::append_sample_data; +use databend_query::test_kits::fixture::check_data_dir; +use databend_query::test_kits::fixture::TestFixture; use enterprise_query::storages::fuse::do_vacuum_drop_tables; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_do_vacuum_drop_table() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture .default_session() .get_settings() .set_retention_period(0)?; + + fixture.create_default_database().await?; fixture.create_default_table().await?; let number_of_block = 1; @@ -86,5 +89,6 @@ async fn test_fuse_do_vacuum_drop_table() -> Result<()> { ) .await?; } + Ok(()) } diff --git a/src/query/ee/tests/it/storages/fuse/operations/virtual_columns.rs b/src/query/ee/tests/it/storages/fuse/operations/virtual_columns.rs index 6705cdc20cd6..3bd5d3b15c10 100644 --- a/src/query/ee/tests/it/storages/fuse/operations/virtual_columns.rs +++ b/src/query/ee/tests/it/storages/fuse/operations/virtual_columns.rs @@ -17,18 +17,20 @@ use common_exception::Result; use common_storages_fuse::io::MetaReaders; use common_storages_fuse::io::TableMetaLocationGenerator; use common_storages_fuse::FuseTable; -use databend_query::test_kits::table_test_fixture::append_variant_sample_data; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::append_variant_sample_data; +use databend_query::test_kits::fixture::TestFixture; use enterprise_query::storages::fuse::operations::virtual_columns::do_refresh_virtual_column; use storages_common_cache::LoadParams; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_do_refresh_virtual_column() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture .default_session() .get_settings() .set_retention_period(0)?; + fixture.create_default_database().await?; fixture.create_variant_table().await?; let number_of_block = 2; diff --git a/src/query/service/Cargo.toml b/src/query/service/Cargo.toml index a8fca18b6722..c7cda4ac6458 100644 --- a/src/query/service/Cargo.toml +++ b/src/query/service/Cargo.toml @@ -113,6 +113,7 @@ byte-unit = "4.0.19" byteorder = "1.4.3" chrono = { workspace = true } chrono-tz = { workspace = true } +config = { version = "0.13.4", features = [] } ctor = "0.1.26" dashmap = "5.4" ethnum = { workspace = true } diff --git a/src/query/service/src/clusters/cluster.rs b/src/query/service/src/clusters/cluster.rs index 796cf347c848..38c3284045c1 100644 --- a/src/query/service/src/clusters/cluster.rs +++ b/src/query/service/src/clusters/cluster.rs @@ -150,9 +150,9 @@ impl ClusterDiscovery { } #[async_backtrace::framed] - pub async fn init(cfg: InnerConfig) -> Result<()> { - let metastore = ClusterDiscovery::create_meta_client(&cfg).await?; - GlobalInstance::set(Self::try_create(&cfg, metastore).await?); + pub async fn init(cfg: &InnerConfig) -> Result<()> { + let metastore = ClusterDiscovery::create_meta_client(cfg).await?; + GlobalInstance::set(Self::try_create(cfg, metastore).await?); Ok(()) } diff --git a/src/query/service/src/global_services.rs b/src/query/service/src/global_services.rs index 1e102cc78cb3..7ea0d362169c 100644 --- a/src/query/service/src/global_services.rs +++ b/src/query/service/src/global_services.rs @@ -48,13 +48,13 @@ pub struct GlobalServices; impl GlobalServices { #[async_backtrace::framed] - pub async fn init(config: InnerConfig) -> Result<()> { + pub async fn init(config: &InnerConfig) -> Result<()> { GlobalInstance::init_production(); GlobalServices::init_with(config).await } #[async_backtrace::framed] - pub async fn init_with(config: InnerConfig) -> Result<()> { + pub async fn init_with(config: &InnerConfig) -> Result<()> { // app name format: node_id[0..7]@cluster_id let app_name_shuffle = format!( "databend-query-{}@{}", @@ -68,7 +68,7 @@ impl GlobalServices { // The order of initialization is very important // 1. global config init. - GlobalConfig::init(config.clone())?; + GlobalConfig::init(config)?; // 2. log init. let mut log_labels = BTreeMap::new(); @@ -83,7 +83,7 @@ impl GlobalServices { GlobalQueryRuntime::init(config.storage.num_cpus as usize)?; // 4. cluster discovery init. - ClusterDiscovery::init(config.clone()).await?; + ClusterDiscovery::init(config).await?; // TODO(xuanwo): // @@ -101,19 +101,19 @@ impl GlobalServices { (CatalogType::Hive, Arc::new(HiveCreator)), ]; - CatalogManager::init(&config, Arc::new(default_catalog), catalog_creator).await?; + CatalogManager::init(config, Arc::new(default_catalog), catalog_creator).await?; } - HttpQueryManager::init(&config).await?; + HttpQueryManager::init(config).await?; DataExchangeManager::init()?; - SessionManager::init(&config)?; + SessionManager::init(config)?; LockManager::init()?; - AuthMgr::init(&config)?; + AuthMgr::init(config)?; UserApiProvider::init( config.meta.to_meta_grpc_client_conf(), - config.query.idm, + config.query.idm.clone(), config.query.tenant_id.as_str(), - config.query.tenant_quota, + config.query.tenant_quota.clone(), ) .await?; RoleCacheManager::init()?; diff --git a/src/query/service/src/local/mod.rs b/src/query/service/src/local/mod.rs index 16d11f9650f4..42c5e6077d96 100644 --- a/src/query/service/src/local/mod.rs +++ b/src/query/service/src/local/mod.rs @@ -54,7 +54,7 @@ pub async fn query_local(query_sql: &str, output_format: &str) -> Result<()> { .await .unwrap(); - GlobalServices::init(conf.clone()).await?; + GlobalServices::init(&conf).await?; // init oss license manager OssLicenseManager::init(conf.query.tenant_id.clone()).unwrap(); diff --git a/src/query/service/src/test_kits/config.rs b/src/query/service/src/test_kits/config.rs index 9fa01e3d0928..5d5e4e05d1eb 100644 --- a/src/query/service/src/test_kits/config.rs +++ b/src/query/service/src/test_kits/config.rs @@ -17,7 +17,10 @@ use std::collections::HashMap; use common_base::base::GlobalUniqName; use common_config::InnerConfig; use common_meta_app::principal::AuthInfo; +use common_meta_app::storage::StorageFsConfig; +use common_meta_app::storage::StorageParams; use common_users::idm_config::IDMConfig; +use tempfile::TempDir; pub struct ConfigBuilder { conf: InnerConfig, @@ -36,6 +39,11 @@ impl ConfigBuilder { // set node_id to a unique value conf.query.node_id = GlobalUniqName::unique(); + // set storage to fs + let tmp_dir = TempDir::new().expect("create tmp dir failed"); + let root = tmp_dir.path().to_str().unwrap().to_string(); + conf.storage.params = StorageParams::Fs(StorageFsConfig { root }); + ConfigBuilder { conf } } diff --git a/src/query/service/src/test_kits/context.rs b/src/query/service/src/test_kits/context.rs deleted file mode 100644 index 1ae91a5d3af2..000000000000 --- a/src/query/service/src/test_kits/context.rs +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright 2021 Datafuse Labs -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::sync::Arc; - -use common_config::InnerConfig; -use common_config::DATABEND_COMMIT_VERSION; -use common_exception::Result; -use common_meta_app::principal::AuthInfo; -use common_meta_app::principal::GrantObject; -use common_meta_app::principal::PasswordHashMethod; -use common_meta_app::principal::UserInfo; -use common_meta_app::principal::UserPrivilegeSet; -use common_meta_types::NodeInfo; - -use crate::clusters::Cluster; -use crate::clusters::ClusterHelper; -use crate::sessions::QueryContext; -use crate::sessions::QueryContextShared; -use crate::sessions::Session; -use crate::sessions::SessionManager; -use crate::sessions::SessionType; -use crate::sessions::TableContext; -use crate::test_kits::ConfigBuilder; -use crate::test_kits::TestGlobalServices; -use crate::test_kits::TestGuard; - -pub async fn create_query_context() -> Result<(TestGuard, Arc)> { - create_query_context_with_session(SessionType::Dummy, None).await -} - -pub async fn create_query_context_with_session( - typ: SessionType, - guard: Option, -) -> Result<(TestGuard, Arc)> { - let guard = match guard { - None => TestGlobalServices::setup(&ConfigBuilder::create().build()).await?, - Some(g) => g, - }; - let dummy_session = SessionManager::instance().create_session(typ).await?; - - // Set user with all privileges - let mut user_info = UserInfo::new("root", "%", AuthInfo::Password { - hash_method: PasswordHashMethod::Sha256, - hash_value: Vec::from("pass"), - }); - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_global(), - ); - - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_stage(), - ); - - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_udf(), - ); - - dummy_session.set_authed_user(user_info, None).await?; - - let dummy_query_context = dummy_session.create_query_context().await?; - dummy_query_context.get_settings().set_max_threads(8)?; - dummy_query_context - .get_settings() - .set_max_memory_usage(16 * 1024 * 1024 * 1024)?; - Ok((guard, dummy_query_context)) -} - -pub async fn create_query_context_with_config( - config: InnerConfig, - current_user: Option, -) -> Result<(TestGuard, Arc)> { - let (g, c, _s) = create_query_context_with_config_new(config, current_user).await?; - Ok((g, c)) -} - -pub async fn create_query_context_with_config_new( - config: InnerConfig, - mut current_user: Option, -) -> Result<(TestGuard, Arc, Arc)> { - let guard = TestGlobalServices::setup(&config).await?; - - let dummy_session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; - - if current_user.is_none() { - let mut user_info = UserInfo::new("root", "%", AuthInfo::Password { - hash_method: PasswordHashMethod::Sha256, - hash_value: Vec::from("pass"), - }); - - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_global(), - ); - - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_stage(), - ); - - user_info.grants.grant_privileges( - &GrantObject::Global, - UserPrivilegeSet::available_privileges_on_udf(), - ); - - current_user = Some(user_info); - } - - dummy_session - .set_authed_user(current_user.unwrap(), None) - .await?; - let dummy_query_context = dummy_session.create_query_context().await?; - - dummy_query_context.get_settings().set_max_threads(8)?; - Ok((guard, dummy_query_context, dummy_session)) -} - -pub struct ClusterDescriptor { - local_node_id: String, - cluster_nodes_list: Vec>, -} - -impl ClusterDescriptor { - pub fn new() -> ClusterDescriptor { - ClusterDescriptor { - local_node_id: String::from(""), - cluster_nodes_list: vec![], - } - } - - pub fn with_node(self, id: impl Into, addr: impl Into) -> ClusterDescriptor { - let mut new_nodes = self.cluster_nodes_list.clone(); - new_nodes.push(Arc::new(NodeInfo::create( - id.into(), - 0, - addr.into(), - DATABEND_COMMIT_VERSION.to_string(), - ))); - ClusterDescriptor { - cluster_nodes_list: new_nodes, - local_node_id: self.local_node_id, - } - } - - pub fn with_local_id(self, id: impl Into) -> ClusterDescriptor { - ClusterDescriptor { - local_node_id: id.into(), - cluster_nodes_list: self.cluster_nodes_list, - } - } -} - -impl Default for ClusterDescriptor { - fn default() -> Self { - Self::new() - } -} - -#[allow(dead_code)] -pub async fn create_query_context_with_cluster( - desc: ClusterDescriptor, -) -> Result<(TestGuard, Arc)> { - let config = ConfigBuilder::create().build(); - let guard = TestGlobalServices::setup(&config).await?; - let dummy_session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; - let local_id = desc.local_node_id; - let nodes = desc.cluster_nodes_list; - - let dummy_query_context = QueryContext::create_from_shared(QueryContextShared::try_create( - dummy_session, - Cluster::create(nodes, local_id), - )?); - - dummy_query_context.get_settings().set_max_threads(8)?; - Ok((guard, dummy_query_context)) -} diff --git a/src/query/service/src/test_kits/table_test_fixture.rs b/src/query/service/src/test_kits/fixture.rs similarity index 85% rename from src/query/service/src/test_kits/table_test_fixture.rs rename to src/query/service/src/test_kits/fixture.rs index 374acce439ae..2bd84d6a332a 100644 --- a/src/query/service/src/test_kits/table_test_fixture.rs +++ b/src/query/service/src/test_kits/fixture.rs @@ -18,9 +18,11 @@ use std::sync::Arc; use common_ast::ast::Engine; use common_catalog::catalog_kind::CATALOG_DEFAULT; +use common_catalog::cluster_info::Cluster; use common_catalog::table::AppendMode; use common_config::GlobalConfig; use common_config::InnerConfig; +use common_config::DATABEND_COMMIT_VERSION; use common_exception::Result; use common_expression::block_debug::assert_blocks_sorted_eq_with_name; use common_expression::infer_table_schema; @@ -42,14 +44,16 @@ use common_expression::TableDataType; use common_expression::TableField; use common_expression::TableSchemaRef; use common_expression::TableSchemaRefExt; +use common_license::license_manager::LicenseManager; +use common_license::license_manager::OssLicenseManager; use common_meta_app::principal::AuthInfo; use common_meta_app::principal::GrantObject; use common_meta_app::principal::PasswordHashMethod; use common_meta_app::principal::UserInfo; use common_meta_app::principal::UserPrivilegeSet; use common_meta_app::schema::DatabaseMeta; -use common_meta_app::storage::StorageFsConfig; use common_meta_app::storage::StorageParams; +use common_meta_types::NodeInfo; use common_pipeline_core::processors::ProcessorPtr; use common_pipeline_sinks::EmptySink; use common_pipeline_sources::BlocksSource; @@ -64,16 +68,19 @@ use common_storages_fuse::FUSE_TBL_SEGMENT_PREFIX; use common_storages_fuse::FUSE_TBL_SNAPSHOT_PREFIX; use common_storages_fuse::FUSE_TBL_SNAPSHOT_STATISTICS_PREFIX; use common_storages_fuse::FUSE_TBL_XOR_BLOOM_INDEX_PREFIX; +use common_tracing::set_panic_hook; use futures::TryStreamExt; use jsonb::Number as JsonbNumber; use jsonb::Object as JsonbObject; use jsonb::Value as JsonbValue; +use log::info; use parking_lot::Mutex; use storages_common_table_meta::table::OPT_KEY_DATABASE_ID; -use tempfile::TempDir; use uuid::Uuid; use walkdir::WalkDir; +use crate::clusters::ClusterDiscovery; +use crate::clusters::ClusterHelper; use crate::interpreters::CreateTableInterpreter; use crate::interpreters::DeleteInterpreter; use crate::interpreters::Interpreter; @@ -84,6 +91,7 @@ use crate::pipelines::executor::PipelineCompleteExecutor; use crate::pipelines::PipelineBuildResult; use crate::pipelines::PipelineBuilder; use crate::sessions::QueryContext; +use crate::sessions::QueryContextShared; use crate::sessions::Session; use crate::sessions::SessionManager; use crate::sessions::SessionType; @@ -91,20 +99,38 @@ use crate::sessions::TableContext; use crate::sql::Planner; use crate::storages::Table; use crate::test_kits::ConfigBuilder; -use crate::test_kits::TestGlobalServices; -use crate::test_kits::TestGuard; +use crate::GlobalServices; pub struct TestFixture { default_ctx: Arc, default_session: Arc, conf: InnerConfig, prefix: String, + // Keep in the end. + // Session will drop first then the guard drop. _guard: TestGuard, } +pub struct TestGuard { + thread_name: String, +} + +impl TestGuard { + pub fn new(thread_name: String) -> Self { + Self { thread_name } + } +} + +impl Drop for TestGuard { + fn drop(&mut self) { + #[cfg(debug_assertions)] + common_base::base::GlobalInstance::drop_testing(&self.thread_name); + } +} + #[async_trait::async_trait] pub trait Setup { - async fn setup(&self) -> Result<(TestGuard, InnerConfig)>; + async fn setup(&self) -> Result; } struct OSSSetup { @@ -113,53 +139,30 @@ struct OSSSetup { #[async_trait::async_trait] impl Setup for OSSSetup { - async fn setup(&self) -> Result<(TestGuard, InnerConfig)> { - Ok(( - TestGlobalServices::setup(&self.config).await?, - self.config.clone(), - )) + async fn setup(&self) -> Result { + TestFixture::init_global_with_config(&self.config).await?; + Ok(self.config.clone()) } } impl TestFixture { - pub async fn new() -> Result { - let mut config = ConfigBuilder::create().config(); - let tmp_dir = TempDir::new().expect("create tmp dir failed"); - let root = tmp_dir.path().to_str().unwrap().to_string(); - config.storage.params = StorageParams::Fs(StorageFsConfig { root }); - Self::with_setup(OSSSetup { config }).await + /// Create a new TestFixture with default config. + pub async fn setup() -> Result { + let config = ConfigBuilder::create().config(); + Self::setup_with_custom(OSSSetup { config }).await } - pub async fn with_setup(setup: impl Setup) -> Result { - let (guard, conf) = setup.setup().await?; + /// Create a new TestFixture with setup impl. + pub async fn setup_with_custom(setup: impl Setup) -> Result { + let conf = setup.setup().await?; + + // This will use a max_active_sessions number. let default_session = Self::create_session(SessionType::Dummy).await?; let default_ctx = default_session.create_query_context().await?; let random_prefix: String = Uuid::new_v4().simple().to_string(); - - // prepare a randomly named default database - { - let tenant = default_ctx.get_tenant(); - let db_name = gen_db_name(&random_prefix); - let plan = CreateDatabasePlan { - catalog: "default".to_owned(), - tenant, - if_not_exists: false, - database: db_name, - meta: DatabaseMeta { - engine: "".to_string(), - ..Default::default() - }, - }; - - default_ctx - .get_catalog("default") - .await - .unwrap() - .create_database(plan.into()) - .await?; - } - + let thread_name = std::thread::current().name().unwrap().to_string(); + let guard = TestGuard::new(thread_name.clone()); Ok(Self { default_ctx, default_session, @@ -168,11 +171,12 @@ impl TestFixture { _guard: guard, }) } - pub async fn with_config(mut config: InnerConfig) -> Result { - let tmp_dir = TempDir::new().expect("create tmp dir failed"); - let root = tmp_dir.path().to_str().unwrap().to_string(); - config.storage.params = StorageParams::Fs(StorageFsConfig { root }); - Self::with_setup(OSSSetup { config }).await + + pub async fn setup_with_config(config: &InnerConfig) -> Result { + Self::setup_with_custom(OSSSetup { + config: config.clone(), + }) + .await } async fn create_session(session_type: SessionType) -> Result> { @@ -201,6 +205,38 @@ impl TestFixture { Ok(dummy_session) } + /// Setup the test environment. + /// Set the panic hook. + /// Set the unit test env. + /// Init the global instance. + /// Init the global services. + /// Init the license manager. + /// Register the cluster to the metastore. + async fn init_global_with_config(config: &InnerConfig) -> Result<()> { + set_panic_hook(); + std::env::set_var("UNIT_TEST", "TRUE"); + + let thread_name = std::thread::current().name().unwrap().to_string(); + #[cfg(debug_assertions)] + common_base::base::GlobalInstance::init_testing(&thread_name); + + GlobalServices::init_with(config).await?; + OssLicenseManager::init(config.query.tenant_id.clone())?; + + // Cluster register. + { + ClusterDiscovery::instance() + .register_to_metastore(config) + .await?; + info!( + "Databend query unit test setup registered:{:?} to metasrv:{:?}.", + config.query.cluster_id, config.meta.endpoints + ); + } + + Ok(()) + } + pub fn default_session(&self) -> Arc { self.default_session.clone() } @@ -210,20 +246,27 @@ impl TestFixture { self.default_session.create_query_context().await } - /// returns a new session, for test cases that need to tweak session setting, - /// please use this method. - pub async fn new_session(&self) -> Result> { - Self::create_session(SessionType::Dummy).await + /// returns new QueryContext of default session with cluster + pub async fn new_query_ctx_with_cluster( + &self, + desc: ClusterDescriptor, + ) -> Result> { + let local_id = desc.local_node_id; + let nodes = desc.cluster_nodes_list; + + let dummy_query_context = QueryContext::create_from_shared(QueryContextShared::try_create( + self.default_session.clone(), + Cluster::create(nodes, local_id), + )?); + + dummy_query_context.get_settings().set_max_threads(8)?; + Ok(dummy_query_context) } pub async fn new_session_with_type(&self, session_type: SessionType) -> Result> { Self::create_session(session_type).await } - pub fn conf(&self) -> &InnerConfig { - &self.conf - } - pub fn storage_root(&self) -> &str { match &self.conf.storage.params { StorageParams::Fs(fs) => &fs.root, @@ -415,6 +458,31 @@ impl TestFixture { Ok(()) } + /// Create database with prefix. + pub async fn create_default_database(&self) -> Result<()> { + let tenant = self.default_ctx.get_tenant(); + let db_name = gen_db_name(&self.prefix); + let plan = CreateDatabasePlan { + catalog: "default".to_owned(), + tenant, + if_not_exists: false, + database: db_name, + meta: DatabaseMeta { + engine: "".to_string(), + ..Default::default() + }, + }; + + self.default_ctx + .get_catalog("default") + .await + .unwrap() + .create_database(plan.into()) + .await?; + + Ok(()) + } + pub async fn create_computed_table(&self) -> Result<()> { let create_table_plan = self.computed_create_table_plan(); let interpreter = @@ -554,16 +622,6 @@ impl TestFixture { Box::pin(futures::stream::iter(blocks)) } - pub fn gen_variant_sample_blocks_stream_ex( - num_of_block: usize, - rows_perf_block: usize, - val_start_from: i32, - ) -> SendableDataBlockStream { - let (_, blocks) = - Self::gen_variant_sample_blocks_ex(num_of_block, rows_perf_block, val_start_from); - Box::pin(futures::stream::iter(blocks)) - } - pub fn gen_computed_sample_blocks( num_of_blocks: usize, start: i32, @@ -605,16 +663,6 @@ impl TestFixture { Box::pin(futures::stream::iter(blocks)) } - pub fn gen_computed_sample_blocks_stream_ex( - num_of_block: usize, - rows_perf_block: usize, - val_start_from: i32, - ) -> SendableDataBlockStream { - let (_, blocks) = - Self::gen_computed_sample_blocks_ex(num_of_block, rows_perf_block, val_start_from); - Box::pin(futures::stream::iter(blocks)) - } - pub async fn latest_default_table(&self) -> Result> { // table got from catalog is always fresh self.default_ctx @@ -965,3 +1013,44 @@ pub async fn history_should_have_item( ) .await } + +pub struct ClusterDescriptor { + local_node_id: String, + cluster_nodes_list: Vec>, +} + +impl ClusterDescriptor { + pub fn new() -> ClusterDescriptor { + ClusterDescriptor { + local_node_id: String::from(""), + cluster_nodes_list: vec![], + } + } + + pub fn with_node(self, id: impl Into, addr: impl Into) -> ClusterDescriptor { + let mut new_nodes = self.cluster_nodes_list.clone(); + new_nodes.push(Arc::new(NodeInfo::create( + id.into(), + 0, + addr.into(), + DATABEND_COMMIT_VERSION.to_string(), + ))); + ClusterDescriptor { + cluster_nodes_list: new_nodes, + local_node_id: self.local_node_id, + } + } + + pub fn with_local_id(self, id: impl Into) -> ClusterDescriptor { + ClusterDescriptor { + local_node_id: id.into(), + cluster_nodes_list: self.cluster_nodes_list, + } + } +} + +impl Default for ClusterDescriptor { + fn default() -> Self { + Self::new() + } +} diff --git a/src/query/service/src/test_kits/mod.rs b/src/query/service/src/test_kits/mod.rs index 19acc1258370..47cebad32dc1 100644 --- a/src/query/service/src/test_kits/mod.rs +++ b/src/query/service/src/test_kits/mod.rs @@ -16,19 +16,10 @@ pub mod block_writer; pub mod config; -pub mod context; -#[allow(dead_code)] -pub mod sessions; -pub mod table_test_fixture; +pub mod fixture; pub mod utils; pub use config::ConfigBuilder; -pub use context::create_query_context; -pub use context::create_query_context_with_cluster; -pub use context::create_query_context_with_config; -pub use context::create_query_context_with_session; -pub use context::ClusterDescriptor; -pub use sessions::TestGlobalServices; -pub use sessions::TestGuard; -pub use table_test_fixture::Setup; -pub use table_test_fixture::TestFixture; +pub use fixture::ClusterDescriptor; +pub use fixture::Setup; +pub use fixture::TestFixture; diff --git a/src/query/service/src/test_kits/sessions.rs b/src/query/service/src/test_kits/sessions.rs deleted file mode 100644 index 043e3e54ce37..000000000000 --- a/src/query/service/src/test_kits/sessions.rs +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright 2021 Datafuse Labs -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::borrow::Borrow; - -use common_config::InnerConfig; -use common_exception::Result; -use common_license::license_manager::LicenseManager; -use common_license::license_manager::OssLicenseManager; -use common_tracing::set_panic_hook; -use log::info; - -use crate::clusters::ClusterDiscovery; -use crate::GlobalServices; - -pub struct TestGlobalServices; - -unsafe impl Send for TestGlobalServices {} - -unsafe impl Sync for TestGlobalServices {} - -impl TestGlobalServices { - pub async fn setup(config: impl Borrow) -> Result { - set_panic_hook(); - std::env::set_var("UNIT_TEST", "TRUE"); - - let config = config.borrow(); - - let thread_name = match std::thread::current().name() { - None => panic!("thread name is none"), - Some(thread_name) => thread_name.to_string(), - }; - - #[cfg(debug_assertions)] - common_base::base::GlobalInstance::init_testing(&thread_name); - - GlobalServices::init_with(config.clone()).await?; - OssLicenseManager::init(config.query.tenant_id.clone())?; - - // Cluster register. - { - ClusterDiscovery::instance() - .register_to_metastore(config) - .await?; - info!( - "Databend query has been registered:{:?} to metasrv:{:?}.", - config.query.cluster_id, config.meta.endpoints - ); - } - - Ok(TestGuard { - thread_name: thread_name.to_string(), - }) - } -} - -pub struct TestGuard { - thread_name: String, -} - -impl TestGuard { - pub fn new(thread_name: String) -> Self { - Self { thread_name } - } -} - -impl Drop for TestGuard { - fn drop(&mut self) { - #[cfg(debug_assertions)] - common_base::base::GlobalInstance::drop_testing(&self.thread_name); - } -} diff --git a/src/query/service/tests/it/api/http/cluster.rs b/src/query/service/tests/it/api/http/cluster.rs index e99e2febea84..d2b6210e62fc 100644 --- a/src/query/service/tests/it/api/http/cluster.rs +++ b/src/query/service/tests/it/api/http/cluster.rs @@ -16,7 +16,7 @@ use common_base::base::tokio; use common_exception::Result; use common_meta_types::NodeInfo; use databend_query::api::http::v1::cluster::*; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use poem::get; use poem::http::header; use poem::http::Method; @@ -29,9 +29,9 @@ use pretty_assertions::assert_eq; #[tokio::test(flavor = "multi_thread")] async fn test_cluster() -> Result<()> { - let _guard = - TestGlobalServices::setup(databend_query::test_kits::ConfigBuilder::create().build()) - .await?; + // Setup. + let _fixture = TestFixture::setup().await?; + let cluster_router = Route::new().at("/v1/cluster/list", get(cluster_list_handler)); // List Node diff --git a/src/query/service/tests/it/api/http/config.rs b/src/query/service/tests/it/api/http/config.rs index 7ea5f0078080..addc68e13b07 100644 --- a/src/query/service/tests/it/api/http/config.rs +++ b/src/query/service/tests/it/api/http/config.rs @@ -14,7 +14,7 @@ use common_base::base::tokio; use databend_query::api::http::v1::config::config_handler; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use poem::get; use poem::http::Method; use poem::http::StatusCode; @@ -26,9 +26,8 @@ use pretty_assertions::assert_eq; // for `app.oneshot()` #[tokio::test(flavor = "multi_thread")] async fn test_config() -> common_exception::Result<()> { - let _guard = - TestGlobalServices::setup(databend_query::test_kits::ConfigBuilder::create().build()) - .await?; + let _fixture = TestFixture::setup().await?; + let cluster_router = Route::new().at("/v1/config", get(config_handler)); let response = cluster_router @@ -42,5 +41,6 @@ async fn test_config() -> common_exception::Result<()> { .unwrap(); assert_eq!(response.status(), StatusCode::OK); + Ok(()) } diff --git a/src/query/service/tests/it/api/http/logs.rs b/src/query/service/tests/it/api/http/logs.rs index 580180aadff3..51402c24bd90 100644 --- a/src/query/service/tests/it/api/http/logs.rs +++ b/src/query/service/tests/it/api/http/logs.rs @@ -15,7 +15,7 @@ use common_base::base::tokio; use common_exception::Result; use databend_query::api::http::v1::logs::logs_handler; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use poem::get; use poem::http::Method; use poem::http::StatusCode; @@ -27,9 +27,8 @@ use pretty_assertions::assert_eq; #[tokio::test(flavor = "multi_thread")] async fn test_logs() -> Result<()> { - let _guard = - TestGlobalServices::setup(databend_query::test_kits::ConfigBuilder::create().build()) - .await?; + // Setup. + let _fixture = TestFixture::setup().await?; let test_router = Route::new().at("/v1/logs", get(logs_handler)); { @@ -45,5 +44,6 @@ async fn test_logs() -> Result<()> { assert_eq!(response.status(), StatusCode::OK); } + Ok(()) } diff --git a/src/query/service/tests/it/api/http/status.rs b/src/query/service/tests/it/api/http/status.rs index b56cb7f655a4..4c003d90acc4 100644 --- a/src/query/service/tests/it/api/http/status.rs +++ b/src/query/service/tests/it/api/http/status.rs @@ -72,7 +72,7 @@ async fn run_query(query_ctx: &Arc) -> Result #[tokio::test(flavor = "multi_thread")] async fn test_status() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ep = Route::new().at("/v1/status", get(instance_status_handler)); let status = get_status(&ep).await; diff --git a/src/query/service/tests/it/auth.rs b/src/query/service/tests/it/auth.rs index ab740d520acc..f63f27e9e2fc 100644 --- a/src/query/service/tests/it/auth.rs +++ b/src/query/service/tests/it/auth.rs @@ -24,6 +24,8 @@ use common_users::UserApiProvider; use databend_query::auth::AuthMgr; use databend_query::auth::Credential; use databend_query::sessions::TableContext; +use databend_query::test_kits::ConfigBuilder; +use databend_query::test_kits::TestFixture; use jwt_simple::prelude::*; use p256::EncodedPoint; use wiremock::matchers::method; @@ -74,13 +76,15 @@ async fn test_auth_mgr_with_jwt_multi_sources() -> Result<()> { // Mounting the mock on the mock server - it's now effective! .mount(&server) .await; - let mut conf = databend_query::test_kits::ConfigBuilder::create().config(); + + let mut conf = ConfigBuilder::create().config(); let first_url = format!("http://{}{}", server.address(), json_path); let second_url = format!("http://{}{}", server.address(), second_path); conf.query.jwt_key_file = first_url.clone(); conf.query.jwt_key_files = vec![second_url]; - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; + let auth_mgr = AuthMgr::instance(); { let user_name = "test-user2"; @@ -187,6 +191,7 @@ async fn test_auth_mgr_with_jwt_multi_sources() -> Result<()> { .contains("could not decode token from all available jwt key stores") ); } + Ok(()) } @@ -213,10 +218,12 @@ async fn test_auth_mgr_with_jwt() -> Result<()> { .await; let jwks_url = format!("http://{}{}", server.address(), json_path); - let mut conf = databend_query::test_kits::ConfigBuilder::create().config(); + let mut conf = ConfigBuilder::create().config(); conf.query.jwt_key_file = jwks_url.clone(); - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; + let auth_mgr = AuthMgr::instance(); let user_name = "test"; @@ -416,10 +423,12 @@ async fn test_auth_mgr_with_jwt_es256() -> Result<()> { .await; let jwks_url = format!("http://{}{}", server.address(), json_path); - let mut conf = databend_query::test_kits::ConfigBuilder::create().config(); + let mut conf = ConfigBuilder::create().config(); conf.query.jwt_key_file = jwks_url.clone(); - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + + let fixture = TestFixture::setup_with_config(&conf).await?; + + let ctx = fixture.new_query_ctx().await?; let auth_mgr = AuthMgr::instance(); let user_name = "test"; @@ -615,12 +624,11 @@ async fn test_jwt_auth_mgr_with_management() -> Result<()> { .mount(&server) .await; - let mut conf = databend_query::test_kits::ConfigBuilder::create() - .with_management_mode() - .config(); + let mut conf = ConfigBuilder::create().with_management_mode().config(); conf.query.jwt_key_file = format!("http://{}{}", server.address(), json_path); - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; + let auth_mgr = AuthMgr::instance(); // with create user in other tenant @@ -643,7 +651,7 @@ async fn test_jwt_auth_mgr_with_management() -> Result<()> { let current_tenant = ctx.get_tenant(); assert_eq!(current_tenant, tenant.to_string()); assert_eq!(user_info.grants.roles().len(), 0); - - Ok(()) } + + Ok(()) } diff --git a/src/query/service/tests/it/clusters.rs b/src/query/service/tests/it/clusters.rs index 67db811688cf..d7e5115cd16d 100644 --- a/src/query/service/tests/it/clusters.rs +++ b/src/query/service/tests/it/clusters.rs @@ -17,13 +17,13 @@ use common_exception::Result; use databend_query::clusters::ClusterDiscovery; use databend_query::clusters::ClusterHelper; use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use pretty_assertions::assert_eq; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_single_cluster_discovery() -> Result<()> { let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; let discover_cluster = ClusterDiscovery::instance().discover(&config).await?; @@ -31,6 +31,7 @@ async fn test_single_cluster_discovery() -> Result<()> { assert_eq!(discover_cluster_nodes.len(), 1); assert!(discover_cluster.is_empty()); assert!(discover_cluster.is_local(&discover_cluster_nodes[0])); + Ok(()) } diff --git a/src/query/service/tests/it/frame/dataframe.rs b/src/query/service/tests/it/frame/dataframe.rs index 304eeb3d8f02..ef6156e27c6b 100644 --- a/src/query/service/tests/it/frame/dataframe.rs +++ b/src/query/service/tests/it/frame/dataframe.rs @@ -402,7 +402,7 @@ async fn test_box_display() { let mut mint = Mint::new("tests/it/frame/testdata"); let mut file = &mint.new_goldenfile("box_display.txt").unwrap(); - let fixture = TestFixture::new().await.unwrap(); + let fixture = TestFixture::setup().await.unwrap(); let ctx = fixture.new_query_ctx().await.unwrap(); let cases = vec![ diff --git a/src/query/service/tests/it/interpreters/union.rs b/src/query/service/tests/it/interpreters/union.rs index 23dcc9cf94a6..a13589b360de 100644 --- a/src/query/service/tests/it/interpreters/union.rs +++ b/src/query/service/tests/it/interpreters/union.rs @@ -58,7 +58,8 @@ async fn execute_plan(ctx: Arc, plan: &Plan) -> Result Result<()> { { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + let (_, schema) = get_interpreter( fixture.new_query_ctx().await?, "select 1 union all select 2.0::FLOAT64", @@ -74,6 +75,7 @@ async fn test_simple_union_output_type() -> Result<()> { "select 1.0::FLOAT64 union all select 2", ) .await?; + assert!(matches!( schema.field(0).data_type(), DataType::Number(NumberDataType::Float64), @@ -81,7 +83,8 @@ async fn test_simple_union_output_type() -> Result<()> { } { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + execute_sql(fixture.new_query_ctx().await?, "create table a (a int)").await?; execute_sql(fixture.new_query_ctx().await?, "create table b (b double)").await?; let (_, schema) = get_interpreter( @@ -159,7 +162,7 @@ fn create_all_types_table_sql(table_name: &str) -> String { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_union_output_type() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; // Prepare tables let sql1 = create_all_types_table_sql("t1"); diff --git a/src/query/service/tests/it/parquet_rs/utils.rs b/src/query/service/tests/it/parquet_rs/utils.rs index 06092ec7d7de..0289dd050ef8 100644 --- a/src/query/service/tests/it/parquet_rs/utils.rs +++ b/src/query/service/tests/it/parquet_rs/utils.rs @@ -26,7 +26,7 @@ use databend_query::test_kits::TestFixture; pub async fn create_parquet2_test_fixture() -> TestFixture { let mut conf = ConfigBuilder::create().config(); conf.storage.allow_insecure = true; - let test_fixture = TestFixture::with_config(conf).await.unwrap(); + let test_fixture = TestFixture::setup_with_config(&conf).await.unwrap(); test_fixture .default_session() .get_settings() diff --git a/src/query/service/tests/it/pipelines/executor/executor_graph.rs b/src/query/service/tests/it/pipelines/executor/executor_graph.rs index ed66dcb3ef48..67c4333498aa 100644 --- a/src/query/service/tests/it/pipelines/executor/executor_graph.rs +++ b/src/query/service/tests/it/pipelines/executor/executor_graph.rs @@ -31,11 +31,13 @@ use databend_query::pipelines::executor::RunningGraph; use databend_query::pipelines::processors::InputPort; use databend_query::pipelines::processors::OutputPort; use databend_query::sessions::QueryContext; -use databend_query::test_kits::create_query_context; +use databend_query::test_kits::TestFixture; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_create_simple_pipeline() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + assert_eq!( format!("{:?}", create_simple_pipeline(ctx)?), "digraph {\ @@ -52,7 +54,9 @@ async fn test_create_simple_pipeline() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_create_parallel_simple_pipeline() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + assert_eq!( format!("{:?}", create_parallel_simple_pipeline(ctx)?), "digraph {\ @@ -74,7 +78,9 @@ async fn test_create_parallel_simple_pipeline() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_create_resize_pipeline() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + assert_eq!( format!("{:?}", create_resize_pipeline(ctx)?), "digraph {\ @@ -104,7 +110,9 @@ async fn test_create_resize_pipeline() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_simple_pipeline_init_queue() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + unsafe { assert_eq!( format!("{:?}", create_simple_pipeline(ctx)?.init_schedule_queue(0)?), @@ -115,13 +123,16 @@ async fn test_simple_pipeline_init_queue() -> Result<()> { async_queue: [] \ }" ); - Ok(()) } + + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_parallel_simple_pipeline_init_queue() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + unsafe { assert_eq!( format!( @@ -136,13 +147,16 @@ async fn test_parallel_simple_pipeline_init_queue() -> Result<()> { async_queue: [] \ }" ); - Ok(()) } + + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_resize_pipeline_init_queue() -> Result<()> { - let (_guard, ctx) = create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + unsafe { assert_eq!( format!("{:?}", create_resize_pipeline(ctx)?.init_schedule_queue(0)?), @@ -154,9 +168,9 @@ async fn test_resize_pipeline_init_queue() -> Result<()> { async_queue: [] \ }" ); - - Ok(()) } + + Ok(()) } fn create_simple_pipeline(ctx: Arc) -> Result { diff --git a/src/query/service/tests/it/pipelines/executor/pipeline_executor.rs b/src/query/service/tests/it/pipelines/executor/pipeline_executor.rs index baf0aa66505f..5fd74967f70d 100644 --- a/src/query/service/tests/it/pipelines/executor/pipeline_executor.rs +++ b/src/query/service/tests/it/pipelines/executor/pipeline_executor.rs @@ -34,10 +34,12 @@ use common_pipeline_sources::SyncReceiverSource; use databend_query::pipelines::executor::ExecutorSettings; use databend_query::pipelines::executor::PipelineExecutor; use databend_query::sessions::QueryContext; -use databend_query::test_kits::create_query_context; +use databend_query::test_kits::TestFixture; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_always_call_on_finished() -> Result<()> { + let fixture = TestFixture::setup().await?; + let settings = ExecutorSettings { enable_profiling: false, query_id: Arc::new("".to_string()), @@ -60,7 +62,7 @@ async fn test_always_call_on_finished() -> Result<()> { } } - let (_guard, ctx) = create_query_context().await?; + let ctx = fixture.new_query_ctx().await?; { let (called_finished, mut pipeline) = create_pipeline(); let (_rx, sink_pipe) = create_sink_pipe(1)?; diff --git a/src/query/service/tests/it/servers/flight_sql/flight_sql_handler.rs b/src/query/service/tests/it/servers/flight_sql/flight_sql_handler.rs index d0d678008104..955ea383205e 100644 --- a/src/query/service/tests/it/servers/flight_sql/flight_sql_handler.rs +++ b/src/query/service/tests/it/servers/flight_sql/flight_sql_handler.rs @@ -30,7 +30,7 @@ use common_meta_app::principal::AuthInfo; use common_meta_app::principal::PasswordHashMethod; use databend_query::servers::flight_sql::flight_sql_service::FlightSqlServiceImpl; use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use futures::TryStreamExt; use goldenfile::Mint; use log::debug; @@ -90,7 +90,7 @@ fn prepare_config() -> InnerConfig { #[tokio::test] async fn test_query() -> Result<()> { - let _guard = TestGlobalServices::setup(prepare_config()).await?; + let _fixture = TestFixture::setup_with_config(&prepare_config()).await?; let file = NamedTempFile::new().unwrap(); let path = file.into_temp_path().to_str().unwrap().to_string(); diff --git a/src/query/service/tests/it/servers/http/clickhouse_handler.rs b/src/query/service/tests/it/servers/http/clickhouse_handler.rs index e89f5293caa9..e0aa1f4d8d97 100644 --- a/src/query/service/tests/it/servers/http/clickhouse_handler.rs +++ b/src/query/service/tests/it/servers/http/clickhouse_handler.rs @@ -21,8 +21,7 @@ use databend_query::servers::http::middleware::HTTPSessionMiddleware; use databend_query::servers::http::v1::clickhouse_router; use databend_query::servers::http::CLICKHOUSE_VERSION; use databend_query::servers::HttpHandlerKind; -use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use http::Uri; use poem::error::Result as PoemResult; use poem::http::Method; @@ -49,8 +48,8 @@ macro_rules! assert_ok { #[tokio::test(flavor = "multi_thread")] async fn test_select() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { @@ -91,13 +90,14 @@ async fn test_select() -> PoemResult<()> { assert_eq!(status, StatusCode::OK); assert_eq!(&body, "0\ta\n1\ta\n"); } + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_insert_values() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { let (status, body) = server.post("create table t1(a int, b string)", "").await; @@ -119,13 +119,14 @@ async fn test_insert_values() -> PoemResult<()> { assert_eq!(status, StatusCode::OK); assert_eq!(&body, "0\ta\n1\tb\n"); } + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_output_formats() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { let (status, body) = server @@ -161,13 +162,14 @@ async fn test_output_formats() -> PoemResult<()> { assert_ok!(status, body); assert_eq!(&body, exp); } + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_output_format_compress() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; let sql = "select 1 format TabSeparated"; let (status, body) = server @@ -182,13 +184,14 @@ async fn test_output_format_compress() -> PoemResult<()> { assert_ok!(status, body); let exp = "DE79CF087FB635049DB816DF195B016B820C0000000200000020310A"; assert_eq!(&body, exp); + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_insert_format_values() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { let (status, body) = server.post("create table t1(a int, b string)", "").await; @@ -210,13 +213,13 @@ async fn test_insert_format_values() -> PoemResult<()> { assert_eq!(status, StatusCode::OK, "{} {}", status, body); assert_eq!(&body, "0\ta\n1\tb\n"); } + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_insert_format_ndjson() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); let server = Server::new().await; { @@ -240,13 +243,14 @@ async fn test_insert_format_ndjson() -> PoemResult<()> { assert_ok!(status, body); assert_eq!(&body, "0\ta\n1\tb\n"); } + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_settings() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; // unknown setting @@ -300,8 +304,8 @@ async fn test_settings() -> PoemResult<()> { #[tokio::test(flavor = "multi_thread")] async fn test_multi_partition() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { let sql = "create table tb2(id int, c1 varchar) Engine=Fuse;"; @@ -333,8 +337,8 @@ async fn test_multi_partition() -> PoemResult<()> { #[tokio::test(flavor = "multi_thread")] async fn test_federated() -> PoemResult<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await.unwrap(); + let _fixture = TestFixture::setup().await.unwrap(); + let server = Server::new().await; { let sql = "select version();"; diff --git a/src/query/service/tests/it/servers/http/http_query_handlers.rs b/src/query/service/tests/it/servers/http/http_query_handlers.rs index b1c9d0e8c998..055fd9f4a11d 100644 --- a/src/query/service/tests/it/servers/http/http_query_handlers.rs +++ b/src/query/service/tests/it/servers/http/http_query_handlers.rs @@ -42,7 +42,7 @@ use databend_query::servers::HttpHandler; use databend_query::servers::HttpHandlerKind; use databend_query::sessions::QueryAffect; use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use headers::Header; use headers::HeaderMapExt; use http::HeaderMap; @@ -261,7 +261,7 @@ async fn check_final(ep: &EndpointType, final_uri: &str) -> Result<()> { #[tokio::test(flavor = "current_thread")] async fn test_simple_sql() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let sql = "select * from system.tables limit 10"; let ep = create_endpoint().await?; @@ -324,12 +324,13 @@ async fn test_simple_sql() -> Result<()> { assert!(result.error.is_none(), "{:?}", result); // has only one column assert_eq!(result.schema.len(), 1, "{:?}", result); + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_show_databases() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let sql = "show databases"; let (status, result) = post_sql(sql, 1).await?; @@ -344,12 +345,13 @@ async fn test_show_databases() -> Result<()> { assert!(result.error.is_none(), "{:?}", result); // has two fields: catalog, name assert_eq!(result.schema.len(), 2, "{:?}", result); + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_return_when_finish() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let wait_time_secs = 5; let sql = "create table t1(a int)"; @@ -378,12 +380,13 @@ async fn test_return_when_finish() -> Result<()> { msg() ); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_client_query_id() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let wait_time_secs = 5; let sql = "select * from numbers(1)"; @@ -394,6 +397,7 @@ async fn test_client_query_id() -> Result<()> { post_sql_to_endpoint_new_session(&ep, sql, wait_time_secs, headers).await?; assert_eq!(status, StatusCode::OK); assert_eq!(result.id, "test-query-id"); + Ok(()) } @@ -444,7 +448,8 @@ async fn test_client_query_id() -> Result<()> { #[tokio::test(flavor = "current_thread")] async fn test_wait_time_secs() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; + let ep = create_endpoint().await?; let sql = "select sleep(0.001)"; let json = serde_json::json!({"sql": sql.to_string(), "pagination": {"wait_time_secs": 0}}); @@ -483,6 +488,7 @@ async fn test_wait_time_secs() -> Result<()> { assert_eq!(result.state, ExecuteStateKind::Succeeded, "{:?}", result); assert!(result.schema.is_empty(), "{:?}", result); assert_eq!(num_row, 1, "{:?}", result); + return Ok(()); } } @@ -492,7 +498,7 @@ async fn test_wait_time_secs() -> Result<()> { #[tokio::test(flavor = "current_thread")] async fn test_buffer_size() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let rows = 100; let sql = format!("select * from numbers({})", rows); @@ -509,12 +515,13 @@ async fn test_buffer_size() -> Result<()> { ); assert_eq!(reply.last().0, StatusCode::OK, "{} {:?}", buf_size, reply); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_pagination() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let ep = create_endpoint().await?; let sql = "select * from numbers(10)"; @@ -560,12 +567,13 @@ async fn test_pagination() -> Result<()> { next_uri = result.next_uri.clone().unwrap(); } } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_http_session() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let ep = create_endpoint().await?; let json = @@ -597,13 +605,14 @@ async fn test_http_session() -> Result<()> { assert!(result.error.is_none(), "{:?}", result); assert_eq!(status, StatusCode::OK, "{:?}", result); assert_eq!(result.data.len(), 1, "{:?}", result); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_result_timeout() -> Result<()> { let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; let json = serde_json::json!({ "sql": "SELECT 1", "pagination": {"wait_time_secs": 1}, "session": { "settings": {"http_handler_result_timeout_secs": "1"}}}); let mut req = TestHttpQueryRequest::new(json); @@ -619,13 +628,14 @@ async fn test_result_timeout() -> Result<()> { let msg = format!("query id {} timeout on {}", query_id, config.query.node_id); let msg = json!({ "error": { "code": "404", "message": msg }}).to_string(); assert_eq!(body, msg, "{:?}", result); + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_system_tables() -> Result<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup().await?; + let session_middleware = HTTPSessionMiddleware::create(HttpHandlerKind::Query, AuthMgr::instance()); let ep = Route::new() @@ -672,12 +682,13 @@ async fn test_system_tables() -> Result<()> { assert!(result.next_uri.is_some(), "{:?}", result); assert!(!result.schema.is_empty(), "{:?}", result); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_insert() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let route = create_endpoint().await?; @@ -700,13 +711,13 @@ async fn test_insert() -> Result<()> { result ); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_query_log() -> Result<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup().await?; let session_middleware = HTTPSessionMiddleware::create(HttpHandlerKind::Query, AuthMgr::instance()); @@ -771,6 +782,7 @@ async fn test_query_log() -> Result<()> { "{:?}", result ); + Ok(()) } @@ -778,8 +790,7 @@ async fn test_query_log() -> Result<()> { #[tokio::test(flavor = "current_thread")] #[ignore] async fn test_query_log_killed() -> Result<()> { - let config = ConfigBuilder::create().build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup().await?; let session_middleware = HTTPSessionMiddleware::create(HttpHandlerKind::Query, AuthMgr::instance()); @@ -821,6 +832,7 @@ async fn test_query_log_killed() -> Result<()> { "{:?}", result ); + Ok(()) } @@ -945,7 +957,7 @@ async fn test_auth_jwt() -> Result<()> { let config = ConfigBuilder::create() .jwt_key_file(format!("http://{}{}", server.address(), json_path)) .build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; let session_middleware = HTTPSessionMiddleware::create(HttpHandlerKind::Query, AuthMgr::instance()); @@ -970,6 +982,7 @@ async fn test_auth_jwt() -> Result<()> { let token = key_pair.sign(claims)?; let bear = headers::Authorization::bearer(&token).unwrap(); assert_auth_failure(&ep, bear).await?; + Ok(()) } @@ -1135,7 +1148,7 @@ async fn test_auth_jwt_with_create_user() -> Result<()> { let config = ConfigBuilder::create() .jwt_key_file(format!("http://{}{}", server.address(), json_path)) .build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; let session_middleware = HTTPSessionMiddleware::create(HttpHandlerKind::Query, AuthMgr::instance()); @@ -1166,19 +1179,19 @@ async fn test_auth_jwt_with_create_user() -> Result<()> { assert_auth_current_role(&ep, "account_admin", bearer.clone()).await?; // assert_auth_current_role_with_restricted_role(&ep, "public", "public", bearer).await?; assert_auth_current_role_with_role(&ep, "public", "public", bearer).await?; + Ok(()) } // need to support local_addr, but axum_server do not have local_addr callback #[tokio::test(flavor = "current_thread")] async fn test_http_handler_tls_server() -> Result<()> { - let _guard = TestGlobalServices::setup( - ConfigBuilder::create() - .http_handler_tls_server_key(TEST_SERVER_KEY) - .http_handler_tls_server_cert(TEST_SERVER_CERT) - .build(), - ) - .await?; + let config = ConfigBuilder::create() + .http_handler_tls_server_key(TEST_SERVER_KEY) + .http_handler_tls_server_cert(TEST_SERVER_CERT) + .build(); + let _fixture = TestFixture::setup_with_config(&config).await?; + let address_str = format!("127.0.0.1:{}", get_free_tcp_port()); let mut srv = HttpHandler::create(HttpHandlerKind::Query); @@ -1211,6 +1224,7 @@ async fn test_http_handler_tls_server() -> Result<()> { assert!(res.is_ok(), "{:?}", res); let res = res.unwrap(); assert!(!res.data.is_empty(), "{:?}", res); + Ok(()) } @@ -1220,7 +1234,8 @@ async fn test_http_handler_tls_server_failed_case_1() -> Result<()> { .http_handler_tls_server_key(TEST_SERVER_KEY) .http_handler_tls_server_cert(TEST_SERVER_CERT) .build(); - let _guard = TestGlobalServices::setup(config).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; + let address_str = format!("127.0.0.1:{}", get_free_tcp_port()); let mut srv = HttpHandler::create(HttpHandlerKind::Query); @@ -1235,6 +1250,7 @@ async fn test_http_handler_tls_server_failed_case_1() -> Result<()> { let client = reqwest::Client::builder().build().unwrap(); let resp = client.post(&url).json(&json).send().await; assert!(resp.is_err(), "{:?}", resp.err()); + Ok(()) } @@ -1245,8 +1261,8 @@ async fn test_http_service_tls_server_mutual_tls() -> Result<()> { .http_handler_tls_server_cert(TEST_TLS_SERVER_CERT) .http_handler_tls_server_root_ca_cert(TEST_TLS_CA_CERT) .build(); + let _fixture = TestFixture::setup_with_config(&config).await?; - let _guard = TestGlobalServices::setup(config.clone()).await?; let address_str = format!("127.0.0.1:{}", get_free_tcp_port()); let mut srv = HttpHandler::create(HttpHandlerKind::Query); let listening = srv.start(address_str.parse()?).await?; @@ -1283,20 +1299,20 @@ async fn test_http_service_tls_server_mutual_tls() -> Result<()> { assert!(res.is_ok(), "{:?}", res); let res = res.unwrap(); assert!(!res.data.is_empty(), "{:?}", res); + Ok(()) } // cannot connect with server unless it have CA signed identity #[tokio::test(flavor = "current_thread")] async fn test_http_service_tls_server_mutual_tls_failed() -> Result<()> { - let _guard = TestGlobalServices::setup( - ConfigBuilder::create() - .http_handler_tls_server_key(TEST_TLS_SERVER_KEY) - .http_handler_tls_server_cert(TEST_TLS_SERVER_CERT) - .http_handler_tls_server_root_ca_cert(TEST_TLS_CA_CERT) - .build(), - ) - .await?; + let config = ConfigBuilder::create() + .http_handler_tls_server_key(TEST_TLS_SERVER_KEY) + .http_handler_tls_server_cert(TEST_TLS_SERVER_CERT) + .http_handler_tls_server_root_ca_cert(TEST_TLS_CA_CERT) + .build(); + let _fixture = TestFixture::setup_with_config(&config).await?; + let address_str = format!("127.0.0.1:{}", get_free_tcp_port()); let mut srv = HttpHandler::create(HttpHandlerKind::Query); @@ -1317,12 +1333,13 @@ async fn test_http_service_tls_server_mutual_tls_failed() -> Result<()> { .expect("preconfigured rustls tls"); let resp = client.post(&url).json(&json).send().await; assert!(resp.is_err(), "{:?}", resp.err()); + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_func_object_keys() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let sqls = vec![ ( @@ -1347,12 +1364,13 @@ async fn test_func_object_keys() -> Result<()> { assert!(result.error.is_none(), "{:?}", result.error); assert_eq!(reply.data().len(), data_len); } + Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_multi_partition() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let sqls = vec![ ("create table tb2(id int, c1 varchar) Engine=Fuse;", 0), @@ -1374,12 +1392,13 @@ async fn test_multi_partition() -> Result<()> { ); assert_eq!(reply.data().len(), data_len); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_affect() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let sqls = vec![ ( @@ -1462,12 +1481,13 @@ async fn test_affect() -> Result<()> { assert_eq!(result.1.affect, affect); assert_eq!(result.1.session, session_conf); } + Ok(()) } #[tokio::test(flavor = "current_thread")] async fn test_session_secondary_roles() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let route = create_endpoint().await?; @@ -1513,7 +1533,7 @@ async fn test_auth_configured_user() -> Result<()> { let config = ConfigBuilder::create() .add_user(user_name, auth_info) .build(); - let _guard = TestGlobalServices::setup(config.clone()).await?; + let _fixture = TestFixture::setup_with_config(&config).await?; let mut req = TestHttpQueryRequest::new(serde_json::json!({"sql": "select current_user()"})) .with_basic_auth(user_name, pass_word); @@ -1525,5 +1545,6 @@ async fn test_auth_configured_user() -> Result<()> { v[0][0], serde_json::Value::String(format!("'{}'@'%'", user_name)) ); + Ok(()) } diff --git a/src/query/service/tests/it/servers/mysql/mysql_handler.rs b/src/query/service/tests/it/servers/mysql/mysql_handler.rs index 0e9701aee8d9..47cdb3e658a2 100644 --- a/src/query/service/tests/it/servers/mysql/mysql_handler.rs +++ b/src/query/service/tests/it/servers/mysql/mysql_handler.rs @@ -27,7 +27,7 @@ use common_exception::ToErrorCode; use databend_query::servers::MySQLHandler; use databend_query::servers::MySQLTlsConfig; use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; use mysql_async::prelude::FromRow; use mysql_async::prelude::Queryable; use mysql_async::FromRowError; @@ -39,8 +39,7 @@ use crate::tests::tls_constants::*; #[tokio::test(flavor = "current_thread")] async fn test_generic_code_with_on_query() -> Result<()> { - // Setup - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let tcp_keepalive_timeout_secs = 120; let mut handler = MySQLHandler::create(tcp_keepalive_timeout_secs, MySQLTlsConfig::default())?; @@ -57,8 +56,7 @@ async fn test_generic_code_with_on_query() -> Result<()> { #[tokio::test(flavor = "current_thread")] async fn test_connect_with_tls() -> Result<()> { - // Setup - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build()).await?; + let _fixture = TestFixture::setup().await?; let tcp_keepalive_timeout_secs = 120; let tls_config = MySQLTlsConfig::new(TEST_SERVER_CERT.to_string(), TEST_SERVER_KEY.to_string()); @@ -77,8 +75,12 @@ async fn test_connect_with_tls() -> Result<()> { #[tokio::test(flavor = "current_thread")] async fn test_rejected_session_with_sequence() -> Result<()> { - let _guard = - TestGlobalServices::setup(ConfigBuilder::create().max_active_sessions(1).build()).await?; + // TestFixture will create a default session, so we should limit the max_active_sessions to 2. + let max_active_sessions = 2; + let conf = ConfigBuilder::create() + .max_active_sessions(max_active_sessions) + .build(); + let _fixture = TestFixture::setup_with_config(&conf).await?; let tcp_keepalive_timeout_secs = 120; let mut handler = MySQLHandler::create(tcp_keepalive_timeout_secs, MySQLTlsConfig::default())?; @@ -97,7 +99,7 @@ async fn test_rejected_session_with_sequence() -> Result<()> { assert_eq!(error.code(), 1067); assert_eq!( error.message(), - "Reject connection, cause: Server error: `ERROR HY000 (1815): Current active sessions (1) has exceeded the max_active_sessions limit (1)'" + "Reject connection, cause: Server error: `ERROR HY000 (1815): Current active sessions (2) has exceeded the max_active_sessions limit (2)'" ); } }; @@ -136,16 +138,19 @@ async fn test_rejected_session_with_parallel() -> Result<()> { assert_eq!(error.code(), 1067); assert_eq!( error.message(), - "Reject connection, cause: Server error: `ERROR HY000 (1815): Current active sessions (1) has exceeded the max_active_sessions limit (1)'" + "Reject connection, cause: Server error: `ERROR HY000 (1815): Current active sessions (2) has exceeded the max_active_sessions limit (2)'" ); CreateServerResult::Rejected } } } - // Setup - let _guard = - TestGlobalServices::setup(ConfigBuilder::create().max_active_sessions(1).build()).await?; + // TestFixture will create a default session, so we should limit the max_active_sessions to 2. + let max_active_sessions = 2; + let conf = ConfigBuilder::create() + .max_active_sessions(max_active_sessions) + .build(); + let _fixture = TestFixture::setup_with_config(&conf).await?; let tcp_keepalive_timeout_secs = 120; let mut handler = MySQLHandler::create(tcp_keepalive_timeout_secs, MySQLTlsConfig::default())?; diff --git a/src/query/service/tests/it/sessions/query_ctx.rs b/src/query/service/tests/it/sessions/query_ctx.rs index ab7782195729..d827f57de8c7 100644 --- a/src/query/service/tests/it/sessions/query_ctx.rs +++ b/src/query/service/tests/it/sessions/query_ctx.rs @@ -18,6 +18,8 @@ use common_meta_app::storage::StorageFsConfig; use common_meta_app::storage::StorageParams; use common_meta_app::storage::StorageS3Config; use databend_query::sessions::TableContext; +use databend_query::test_kits::ConfigBuilder; +use databend_query::test_kits::TestFixture; use wiremock::matchers::method; use wiremock::matchers::path; use wiremock::Mock; @@ -33,8 +35,7 @@ async fn test_get_storage_accessor_s3() -> Result<()> { .mount(&mock_server) .await; - let mut conf = databend_query::test_kits::ConfigBuilder::create().config(); - + let mut conf = ConfigBuilder::create().config(); conf.storage.params = StorageParams::S3(StorageS3Config { region: "us-east-2".to_string(), endpoint_url: mock_server.uri(), @@ -44,27 +45,23 @@ async fn test_get_storage_accessor_s3() -> Result<()> { disable_credential_loader: true, ..Default::default() }); + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; - let (_guard, qctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; - - let _ = qctx.get_data_operator()?; + let _ = ctx.get_data_operator()?; Ok(()) } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_get_storage_accessor_fs() -> Result<()> { - let mut conf = databend_query::test_kits::ConfigBuilder::create().config(); - + let mut conf = ConfigBuilder::create().config(); conf.storage.params = StorageParams::Fs(StorageFsConfig { root: "/tmp".to_string(), }); - - let (_guard, qctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; - - let _ = qctx.get_data_operator()?; + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; + let _ = ctx.get_data_operator()?; Ok(()) } diff --git a/src/query/service/tests/it/sessions/session.rs b/src/query/service/tests/it/sessions/session.rs index 930a5748f03f..58de2be888fe 100644 --- a/src/query/service/tests/it/sessions/session.rs +++ b/src/query/service/tests/it/sessions/session.rs @@ -14,17 +14,14 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::sessions::SessionManager; use databend_query::sessions::SessionType; use databend_query::test_kits::ConfigBuilder; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::TestFixture; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_session() -> Result<()> { - let _guard = TestGlobalServices::setup(ConfigBuilder::create().build().clone()).await?; - let session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; + let fixture = TestFixture::setup().await?; + let session = fixture.new_session_with_type(SessionType::Dummy).await?; // Tenant. { @@ -50,11 +47,9 @@ async fn test_session() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_session_in_management_mode() -> Result<()> { - let _guard = - TestGlobalServices::setup(ConfigBuilder::create().with_management_mode().build()).await?; - let session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; + let config = ConfigBuilder::create().with_management_mode().build(); + let fixture = TestFixture::setup_with_config(&config).await?; + let session = fixture.new_session_with_type(SessionType::Dummy).await?; // Tenant. { diff --git a/src/query/service/tests/it/sessions/session_setting.rs b/src/query/service/tests/it/sessions/session_setting.rs index 105567febb52..0cf73a9f1b22 100644 --- a/src/query/service/tests/it/sessions/session_setting.rs +++ b/src/query/service/tests/it/sessions/session_setting.rs @@ -14,18 +14,14 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::sessions::SessionManager; use databend_query::sessions::SessionType; -use databend_query::test_kits::TestGlobalServices; +use databend_query::test_kits::ConfigBuilder; +use databend_query::test_kits::TestFixture; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_session_setting() -> Result<()> { - let _guard = - TestGlobalServices::setup(databend_query::test_kits::ConfigBuilder::create().build()) - .await?; - let session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; + let fixture = TestFixture::setup().await?; + let session = fixture.new_session_with_type(SessionType::Dummy).await?; // Settings. { @@ -41,16 +37,13 @@ async fn test_session_setting() -> Result<()> { #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_session_setting_override() -> Result<()> { - let _guard = TestGlobalServices::setup( - databend_query::test_kits::ConfigBuilder::create() - .max_storage_io_requests(1000) - .parquet_fast_read_bytes(1000000) - .build(), - ) - .await?; - let session = SessionManager::instance() - .create_session(SessionType::Dummy) - .await?; + // Setup. + let config = ConfigBuilder::create() + .max_storage_io_requests(1000) + .parquet_fast_read_bytes(1000000) + .build(); + let fixture = TestFixture::setup_with_config(&config).await?; + let session = fixture.new_session_with_type(SessionType::Dummy).await?; // Settings. { diff --git a/src/query/service/tests/it/spillers/spiller.rs b/src/query/service/tests/it/spillers/spiller.rs index 8511ed3ee72d..5ffb38d1fd7f 100644 --- a/src/query/service/tests/it/spillers/spiller.rs +++ b/src/query/service/tests/it/spillers/spiller.rs @@ -31,7 +31,8 @@ use databend_query::test_kits::TestFixture; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_spill_with_partition() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; let tenant = ctx.get_tenant(); let spiller_config = SpillerConfig::create(query_spill_prefix(&tenant)); @@ -71,5 +72,6 @@ async fn test_spill_with_partition() -> Result<()> { } } } + Ok(()) } diff --git a/src/query/service/tests/it/sql/exec/get_table_bind_test.rs b/src/query/service/tests/it/sql/exec/get_table_bind_test.rs index 77c349ecbad3..a531d6b166b8 100644 --- a/src/query/service/tests/it/sql/exec/get_table_bind_test.rs +++ b/src/query/service/tests/it/sql/exec/get_table_bind_test.rs @@ -112,7 +112,7 @@ use common_storage::StageFileInfo; use common_users::GrantObjectVisibilityChecker; use dashmap::DashMap; use databend_query::sessions::QueryContext; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use parking_lot::Mutex; use parking_lot::RwLock; use storages_common_table_meta::meta::Location; @@ -702,7 +702,9 @@ impl TableContext for CtxDelegation { #[tokio::test(flavor = "multi_thread")] async fn test_get_same_table_once() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let query = format!( "select * from {}.{} join {}.{} as t2 join {}.{} as t3", fixture.default_db_name().as_str(), @@ -734,5 +736,6 @@ async fn test_get_same_table_once() -> Result<()> { .load(std::sync::atomic::Ordering::SeqCst), 2 ); + Ok(()) } diff --git a/src/query/service/tests/it/sql/exec/mod.rs b/src/query/service/tests/it/sql/exec/mod.rs index 05961dd5743b..d320c720051c 100644 --- a/src/query/service/tests/it/sql/exec/mod.rs +++ b/src/query/service/tests/it/sql/exec/mod.rs @@ -39,7 +39,9 @@ pub fn test_format_field_name() { #[tokio::test(flavor = "multi_thread")] pub async fn test_snapshot_consistency() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let ctx = fixture.new_query_ctx().await?; let tbl = fixture.default_table_name(); let db = fixture.default_db_name(); @@ -134,6 +136,7 @@ pub async fn test_snapshot_consistency() -> Result<()> { } else { return Err(ErrorCode::BadArguments("query bad plan")); } + Ok::<(), ErrorCode>(()) }; diff --git a/src/query/service/tests/it/sql/planner/optimizer/agg_index_query_rewrite.rs b/src/query/service/tests/it/sql/planner/optimizer/agg_index_query_rewrite.rs index bcb637d25ff5..dcb7b56d7abd 100644 --- a/src/query/service/tests/it/sql/planner/optimizer/agg_index_query_rewrite.rs +++ b/src/query/service/tests/it/sql/planner/optimizer/agg_index_query_rewrite.rs @@ -382,7 +382,8 @@ async fn test_query_rewrite() -> Result<()> { } async fn test_query_rewrite_impl(format: &str) -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; let create_table_plan = create_table_plan(&fixture, format); let interpreter = CreateTableInterpreter::try_create(ctx.clone(), create_table_plan)?; diff --git a/src/query/service/tests/it/storages/fuse/operations/alter_table.rs b/src/query/service/tests/it/storages/fuse/operations/alter_table.rs index 500a954df328..d75856cd1eac 100644 --- a/src/query/service/tests/it/storages/fuse/operations/alter_table.rs +++ b/src/query/service/tests/it/storages/fuse/operations/alter_table.rs @@ -38,7 +38,7 @@ use databend_query::interpreters::AddTableColumnInterpreter; use databend_query::interpreters::DropTableColumnInterpreter; use databend_query::interpreters::Interpreter; use databend_query::interpreters::InterpreterFactory; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures_util::TryStreamExt; use ordered_float::OrderedFloat; use storages_common_cache::LoadParams; @@ -131,7 +131,9 @@ async fn check_segment_column_ids( #[tokio::test(flavor = "multi_thread")] async fn test_fuse_table_optimize_alter_table() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let tbl_name = fixture.default_table_name(); let db_name = fixture.default_db_name(); let catalog_name = fixture.default_catalog_name(); diff --git a/src/query/service/tests/it/storages/fuse/operations/analyze.rs b/src/query/service/tests/it/storages/fuse/operations/analyze.rs index fba55832d82c..fcc9084d573f 100644 --- a/src/query/service/tests/it/storages/fuse/operations/analyze.rs +++ b/src/query/service/tests/it/storages/fuse/operations/analyze.rs @@ -19,15 +19,17 @@ use common_exception::Result; use common_storages_factory::Table; use common_storages_fuse::FuseTable; use common_storages_fuse::TableContext; -use databend_query::test_kits::table_test_fixture::analyze_table; -use databend_query::test_kits::table_test_fixture::check_data_dir; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::analyze_table; +use databend_query::test_kits::fixture::check_data_dir; +use databend_query::test_kits::fixture::TestFixture; use crate::storages::fuse::utils::do_insertions; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_snapshot_analyze() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let ctx = fixture.new_query_ctx().await?; let case_name = "analyze_statistic_optimize"; do_insertions(&fixture).await?; @@ -43,12 +45,16 @@ async fn test_fuse_snapshot_analyze() -> Result<()> { fuse_table .do_purge(&table_ctx, snapshot_files, None, true, false) .await?; - check_data_dir(&fixture, case_name, 1, 1, 1, 1, 1, Some(()), Some(())).await + check_data_dir(&fixture, case_name, 1, 1, 1, 1, 1, Some(()), Some(())).await?; + + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_fuse_snapshot_analyze_and_truncate() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); let case_name = "test_fuse_snapshot_analyze_and_truncate"; @@ -98,7 +104,9 @@ async fn test_fuse_snapshot_analyze_and_truncate() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_fuse_snapshot_analyze_purge() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let ctx = fixture.new_query_ctx().await?; let case_name = "analyze_statistic_purge"; do_insertions(&fixture).await?; @@ -117,5 +125,7 @@ async fn test_fuse_snapshot_analyze_purge() -> Result<()> { fuse_table .do_purge(&table_ctx, snapshot_files, None, true, false) .await?; - check_data_dir(&fixture, case_name, 1, 1, 1, 1, 1, Some(()), Some(())).await + check_data_dir(&fixture, case_name, 1, 1, 1, 1, 1, Some(()), Some(())).await?; + + Ok(()) } diff --git a/src/query/service/tests/it/storages/fuse/operations/clustering.rs b/src/query/service/tests/it/storages/fuse/operations/clustering.rs index 54358ab14833..586fea5b54e9 100644 --- a/src/query/service/tests/it/storages/fuse/operations/clustering.rs +++ b/src/query/service/tests/it/storages/fuse/operations/clustering.rs @@ -23,7 +23,7 @@ use databend_query::interpreters::AlterTableClusterKeyInterpreter; use databend_query::interpreters::CreateTableInterpreter; use databend_query::interpreters::DropTableClusterKeyInterpreter; use databend_query::interpreters::Interpreter; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use storages_common_cache::LoadParams; use storages_common_table_meta::meta::TableSnapshot; use storages_common_table_meta::meta::Versioned; @@ -32,7 +32,9 @@ use storages_common_table_meta::table::OPT_KEY_SNAPSHOT_LOCATION; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_alter_table_cluster_key() -> common_exception::Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let ctx = fixture.new_query_ctx().await?; let create_table_plan = CreateTablePlan { diff --git a/src/query/service/tests/it/storages/fuse/operations/commit.rs b/src/query/service/tests/it/storages/fuse/operations/commit.rs index 5aaaca362fdd..6c2ac4dd3558 100644 --- a/src/query/service/tests/it/storages/fuse/operations/commit.rs +++ b/src/query/service/tests/it/storages/fuse/operations/commit.rs @@ -112,7 +112,7 @@ use common_storages_fuse::FUSE_TBL_SNAPSHOT_PREFIX; use common_users::GrantObjectVisibilityChecker; use dashmap::DashMap; use databend_query::sessions::QueryContext; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures::TryStreamExt; use parking_lot::RwLock; use storages_common_table_meta::meta::Location; @@ -125,7 +125,9 @@ use walkdir::WalkDir; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_occ_retry() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; + let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); fixture.create_default_table().await?; @@ -182,7 +184,8 @@ async fn test_fuse_occ_retry() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_last_snapshot_hint() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; let table = fixture.latest_default_table().await?; @@ -228,8 +231,10 @@ async fn test_commit_to_meta_server() -> Result<()> { impl Case { async fn run(&self) -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; + let ctx = fixture.new_query_ctx().await?; let catalog = ctx.get_catalog("default").await?; @@ -296,6 +301,7 @@ async fn test_commit_to_meta_server() -> Result<()> { "case name {}", self.case_name ); + Ok(()) } } diff --git a/src/query/service/tests/it/storages/fuse/operations/gc.rs b/src/query/service/tests/it/storages/fuse/operations/gc.rs index 8e742485eb44..a8f8b850cc9a 100644 --- a/src/query/service/tests/it/storages/fuse/operations/gc.rs +++ b/src/query/service/tests/it/storages/fuse/operations/gc.rs @@ -21,9 +21,9 @@ use common_catalog::table_context::TableContext; use common_exception::Result; use common_storages_fuse::io::MetaWriter; use common_storages_fuse::FuseTable; -use databend_query::test_kits::table_test_fixture::append_sample_data; -use databend_query::test_kits::table_test_fixture::check_data_dir; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::append_sample_data; +use databend_query::test_kits::fixture::check_data_dir; +use databend_query::test_kits::fixture::TestFixture; use databend_query::test_kits::utils::generate_segments; use databend_query::test_kits::utils::generate_snapshot_with_segments; use databend_query::test_kits::utils::generate_snapshots; @@ -36,10 +36,12 @@ use crate::storages::fuse::operations::mutation::compact_segment; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_purge_normal_case() -> Result<()> { - let fixture = TestFixture::new().await?; - let ctx = fixture.new_query_ctx().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; + let ctx = fixture.new_query_ctx().await?; + // ingests some test data append_sample_data(1, &fixture).await?; @@ -66,15 +68,18 @@ async fn test_fuse_purge_normal_case() -> Result<()> { None, ) .await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_fuse_purge_normal_orphan_snapshot() -> Result<()> { - let fixture = TestFixture::new().await?; - let ctx = fixture.new_query_ctx().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; + let ctx = fixture.new_query_ctx().await?; + // ingests some test data append_sample_data(1, &fixture).await?; @@ -121,6 +126,7 @@ async fn test_fuse_purge_normal_orphan_snapshot() -> Result<()> { None, ) .await?; + Ok(()) } @@ -173,10 +179,12 @@ async fn test_fuse_purge_orphan_retention() -> Result<()> { // - 3 segments left: seg_c, seg_2, seg_1 // - 3 blocks left: block_c, block_2, block_1 - let fixture = TestFixture::new().await?; - let ctx = fixture.new_query_ctx().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; + let ctx = fixture.new_query_ctx().await?; + // 1. prepare `S_1` let number_of_block = 1; append_sample_data(number_of_block, &fixture).await?; @@ -241,13 +249,16 @@ async fn test_fuse_purge_orphan_retention() -> Result<()> { None, ) .await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_fuse_purge_older_version() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; fixture.create_normal_table().await?; + generate_snapshots(&fixture).await?; let ctx = fixture.new_query_ctx().await?; let table_ctx: Arc = ctx.clone(); diff --git a/src/query/service/tests/it/storages/fuse/operations/internal_column.rs b/src/query/service/tests/it/storages/fuse/operations/internal_column.rs index ff6bb54db630..47c73e53d404 100644 --- a/src/query/service/tests/it/storages/fuse/operations/internal_column.rs +++ b/src/query/service/tests/it/storages/fuse/operations/internal_column.rs @@ -26,7 +26,7 @@ use common_storages_fuse::io::MetaReaders; use common_storages_fuse::FusePartInfo; use common_storages_fuse::FuseTable; use databend_query::interpreters::InterpreterFactory; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures::TryStreamExt; use storages_common_cache::LoadParams; use storages_common_table_meta::meta::SegmentInfo; @@ -139,10 +139,11 @@ async fn check_partitions(parts: &Partitions, fixture: &TestFixture) -> Result<( #[tokio::test(flavor = "multi_thread")] async fn test_internal_column() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); let ctx = fixture.new_query_ctx().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; let internal_columns = vec![ diff --git a/src/query/service/tests/it/storages/fuse/operations/mutation/block_compact_mutator.rs b/src/query/service/tests/it/storages/fuse/operations/mutation/block_compact_mutator.rs index 83d238fae79b..451272cf38ad 100644 --- a/src/query/service/tests/it/storages/fuse/operations/mutation/block_compact_mutator.rs +++ b/src/query/service/tests/it/storages/fuse/operations/mutation/block_compact_mutator.rs @@ -30,8 +30,8 @@ use databend_query::pipelines::executor::PipelineCompleteExecutor; use databend_query::schedulers::build_query_pipeline_without_render_result_set; use databend_query::sessions::QueryContext; use databend_query::sessions::TableContext; -use databend_query::test_kits::table_test_fixture::expects_ok; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::expects_ok; +use databend_query::test_kits::fixture::TestFixture; use rand::thread_rng; use rand::Rng; use storages_common_table_meta::meta::SegmentInfo; @@ -43,11 +43,12 @@ use crate::storages::fuse::operations::mutation::segments_compact_mutator::Compa #[tokio::test(flavor = "multi_thread")] async fn test_compact() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let tbl_name = fixture.default_table_name(); let db_name = fixture.default_db_name(); + fixture.create_default_database().await?; fixture.create_normal_table().await?; // insert @@ -100,6 +101,7 @@ async fn test_compact() -> Result<()> { expected, ) .await?; + Ok(()) } @@ -140,7 +142,7 @@ async fn do_compact(ctx: Arc, table: Arc) -> Result Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let operator = ctx.get_data_operator()?.operator(); let settings = ctx.get_settings(); diff --git a/src/query/service/tests/it/storages/fuse/operations/mutation/deletion.rs b/src/query/service/tests/it/storages/fuse/operations/mutation/deletion.rs index e3e8bf230269..420e40b24823 100644 --- a/src/query/service/tests/it/storages/fuse/operations/mutation/deletion.rs +++ b/src/query/service/tests/it/storages/fuse/operations/mutation/deletion.rs @@ -14,15 +14,16 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::test_kits::table_test_fixture::expects_ok; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::expects_ok; +use databend_query::test_kits::fixture::TestFixture; #[tokio::test(flavor = "multi_thread")] async fn test_deletion_mutator_multiple_empty_segments() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let tbl_name = fixture.default_table_name(); let db_name = fixture.default_db_name(); + fixture.create_default_database().await?; fixture.create_normal_table().await?; // insert @@ -53,5 +54,6 @@ async fn test_deletion_mutator_multiple_empty_segments() -> Result<()> { expected, ) .await?; + Ok(()) } diff --git a/src/query/service/tests/it/storages/fuse/operations/mutation/recluster_mutator.rs b/src/query/service/tests/it/storages/fuse/operations/mutation/recluster_mutator.rs index a118ce2ddf99..d8e57f9165b3 100644 --- a/src/query/service/tests/it/storages/fuse/operations/mutation/recluster_mutator.rs +++ b/src/query/service/tests/it/storages/fuse/operations/mutation/recluster_mutator.rs @@ -34,7 +34,7 @@ use common_storages_fuse::statistics::reducers::reduce_block_metas; use common_storages_fuse::FusePartInfo; use common_storages_fuse::FuseTable; use databend_query::sessions::TableContext; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use rand::thread_rng; use rand::Rng; use storages_common_table_meta::meta; @@ -50,7 +50,7 @@ use crate::storages::fuse::operations::mutation::CompactSegmentTestFixture; #[tokio::test(flavor = "multi_thread")] async fn test_recluster_mutator_block_select() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let location_generator = TableMetaLocationGenerator::with_prefix("_prefix".to_owned()); @@ -154,7 +154,7 @@ async fn test_recluster_mutator_block_select() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_safety_for_recluster() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let operator = ctx.get_data_operator()?.operator(); diff --git a/src/query/service/tests/it/storages/fuse/operations/mutation/segments_compact_mutator.rs b/src/query/service/tests/it/storages/fuse/operations/mutation/segments_compact_mutator.rs index 904a3794c70f..6142f066c1c6 100644 --- a/src/query/service/tests/it/storages/fuse/operations/mutation/segments_compact_mutator.rs +++ b/src/query/service/tests/it/storages/fuse/operations/mutation/segments_compact_mutator.rs @@ -52,7 +52,7 @@ use common_storages_fuse::FuseStorageFormat; use common_storages_fuse::FuseTable; use databend_query::sessions::QueryContext; use databend_query::sessions::TableContext; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures_util::TryStreamExt; use rand::thread_rng; use rand::Rng; @@ -67,7 +67,7 @@ use storages_common_table_meta::meta::Versioned; #[tokio::test(flavor = "multi_thread")] async fn test_compact_segment_normal_case() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; // setup let qry = "create table t(c int) block_per_segment=10"; @@ -103,12 +103,13 @@ async fn test_compact_segment_normal_case() -> Result<()> { let qry = "select block_count as count from fuse_snapshot('default', 't') limit 1"; let stream = fixture.execute_query(qry).await?; assert_eq!(num_inserts as u64, check_count(stream).await?); + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_compact_segment_resolvable_conflict() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; // setup let create_tbl_command = "create table t(c int) block_per_segment=10"; fixture.execute_command(create_tbl_command).await?; @@ -165,7 +166,7 @@ async fn test_compact_segment_resolvable_conflict() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_compact_segment_unresolvable_conflict() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; // setup let create_tbl_command = "create table t(c int) block_per_segment=10"; @@ -279,7 +280,7 @@ async fn build_mutator( #[tokio::test(flavor = "multi_thread")] async fn test_segment_compactor() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; { @@ -962,7 +963,7 @@ async fn test_compact_segment_with_cluster() -> Result<()> { let cluster_key_id = 0; let chunk_size = 6; - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let location_gen = TableMetaLocationGenerator::with_prefix("test/".to_owned()); let data_accessor = ctx.get_data_operator()?.operator(); diff --git a/src/query/service/tests/it/storages/fuse/operations/navigate.rs b/src/query/service/tests/it/storages/fuse/operations/navigate.rs index d21bbb2d2476..ffa295435918 100644 --- a/src/query/service/tests/it/storages/fuse/operations/navigate.rs +++ b/src/query/service/tests/it/storages/fuse/operations/navigate.rs @@ -23,7 +23,7 @@ use common_storages_fuse::io::SnapshotHistoryReader; use databend_query::storages::fuse::io::MetaReaders; use databend_query::storages::fuse::io::TableMetaLocationGenerator; use databend_query::storages::fuse::FuseTable; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures::TryStreamExt; #[tokio::test(flavor = "multi_thread")] @@ -33,9 +33,11 @@ async fn test_fuse_navigate() -> Result<()> { // - navigate to the snapshot that generated before the first insertion should fail // 1. Setup - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); + + fixture.create_default_database().await?; fixture.create_default_table().await?; // 1.1 first commit @@ -120,15 +122,18 @@ async fn test_fuse_navigate() -> Result<()> { Ok(_) => panic!("historical data should not exist"), Err(e) => assert_eq!(e.code(), ErrorCode::TABLE_HISTORICAL_DATA_NOT_FOUND), }; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_navigate_for_purge() -> Result<()> { // 1. Setup - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); + + fixture.create_default_database().await?; fixture.create_default_table().await?; // 1.1 first commit diff --git a/src/query/service/tests/it/storages/fuse/operations/optimize.rs b/src/query/service/tests/it/storages/fuse/operations/optimize.rs index 1deaba1c5bae..c548038f55a8 100644 --- a/src/query/service/tests/it/storages/fuse/operations/optimize.rs +++ b/src/query/service/tests/it/storages/fuse/operations/optimize.rs @@ -17,7 +17,7 @@ use common_exception::Result; use common_sql::Planner; use common_storages_fuse::TableContext; use databend_query::interpreters::InterpreterFactory; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures_util::TryStreamExt; use crate::storages::fuse::utils::do_purge_test; @@ -34,11 +34,12 @@ async fn test_fuse_snapshot_optimize_all() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_fuse_table_optimize() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; let tbl_name = fixture.default_table_name(); let db_name = fixture.default_db_name(); + fixture.create_default_database().await?; fixture.create_normal_table().await?; // insert 5 times diff --git a/src/query/service/tests/it/storages/fuse/operations/purge_drop.rs b/src/query/service/tests/it/storages/fuse/operations/purge_drop.rs index e42a8b63fbfe..c90c15847b45 100644 --- a/src/query/service/tests/it/storages/fuse/operations/purge_drop.rs +++ b/src/query/service/tests/it/storages/fuse/operations/purge_drop.rs @@ -14,15 +14,17 @@ use common_base::base::tokio; use common_exception::Result; -use databend_query::test_kits::table_test_fixture::append_sample_data; -use databend_query::test_kits::table_test_fixture::check_data_dir; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::append_sample_data; +use databend_query::test_kits::fixture::check_data_dir; +use databend_query::test_kits::fixture::TestFixture; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_snapshot_truncate_in_drop_stmt() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); + + fixture.create_default_database().await?; fixture.create_default_table().await?; // ingests some test data @@ -35,9 +37,11 @@ async fn test_fuse_snapshot_truncate_in_drop_stmt() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_fuse_snapshot_truncate_in_drop_all_stmt() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); + + fixture.create_default_database().await?; fixture.create_default_table().await?; // ingests some test data @@ -58,5 +62,6 @@ async fn test_fuse_snapshot_truncate_in_drop_all_stmt() -> Result<()> { None, ) .await?; + Ok(()) } diff --git a/src/query/service/tests/it/storages/fuse/operations/read_plan.rs b/src/query/service/tests/it/storages/fuse/operations/read_plan.rs index d07ae8f0f09a..0cb17a84270d 100644 --- a/src/query/service/tests/it/storages/fuse/operations/read_plan.rs +++ b/src/query/service/tests/it/storages/fuse/operations/read_plan.rs @@ -30,7 +30,7 @@ use common_storage::ColumnNode; use common_storage::ColumnNodes; use common_storages_fuse::FusePartInfo; use databend_query::storages::fuse::FuseTable; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures::TryStreamExt; use storages_common_table_meta::meta; use storages_common_table_meta::meta::BlockMeta; @@ -150,9 +150,10 @@ fn test_to_partitions() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_fuse_table_exact_statistic() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; let mut table = fixture.latest_default_table().await?; @@ -187,5 +188,6 @@ async fn test_fuse_table_exact_statistic() -> Result<()> { let fuse_part = FusePartInfo::from_part(&part)?; assert!(fuse_part.create_on.is_some()) } + Ok(()) } diff --git a/src/query/service/tests/it/storages/fuse/operations/table_analyze.rs b/src/query/service/tests/it/storages/fuse/operations/table_analyze.rs index 988fd094feb1..ff4035d9f637 100644 --- a/src/query/service/tests/it/storages/fuse/operations/table_analyze.rs +++ b/src/query/service/tests/it/storages/fuse/operations/table_analyze.rs @@ -27,11 +27,11 @@ use databend_query::sessions::QueryContext; use databend_query::sessions::TableContext; use databend_query::sql::plans::Plan; use databend_query::sql::Planner; -use databend_query::test_kits::table_test_fixture::analyze_table; -use databend_query::test_kits::table_test_fixture::do_deletion; -use databend_query::test_kits::table_test_fixture::do_update; -use databend_query::test_kits::table_test_fixture::execute_command; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::analyze_table; +use databend_query::test_kits::fixture::do_deletion; +use databend_query::test_kits::fixture::do_update; +use databend_query::test_kits::fixture::execute_command; +use databend_query::test_kits::fixture::TestFixture; use databend_query::test_kits::utils::query_count; use storages_common_cache::LoadParams; use storages_common_table_meta::meta::SegmentInfo; @@ -39,7 +39,7 @@ use storages_common_table_meta::meta::Statistics; #[tokio::test(flavor = "multi_thread")] async fn test_table_modify_column_ndv_statistics() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; // setup @@ -99,10 +99,10 @@ async fn test_table_modify_column_ndv_statistics() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_table_update_analyze_statistics() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; - // create table + fixture.create_default_database().await?; fixture.create_default_table().await?; let db_name = fixture.default_db_name(); let tb_name = fixture.default_table_name(); diff --git a/src/query/service/tests/it/storages/fuse/operations/truncate.rs b/src/query/service/tests/it/storages/fuse/operations/truncate.rs index 192fe464c840..92f3775a291c 100644 --- a/src/query/service/tests/it/storages/fuse/operations/truncate.rs +++ b/src/query/service/tests/it/storages/fuse/operations/truncate.rs @@ -19,14 +19,15 @@ use common_catalog::table::Table; use common_exception::ErrorCode; use common_expression::DataBlock; use common_sql::executor::table_read_plan::ToReadDataSourcePlan; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures_util::TryStreamExt; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_table_truncate() -> common_exception::Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; let table = fixture.latest_default_table().await?; @@ -106,8 +107,10 @@ async fn test_fuse_table_truncate_appending_concurrently() -> common_exception:: // // s3 should be a valid snapshot,full-scan should work as expected - let fixture = Arc::new(TestFixture::new().await?); + let fixture = Arc::new(TestFixture::setup().await?); let ctx = fixture.new_query_ctx().await?; + + fixture.create_default_database().await?; fixture.create_default_table().await?; let init_table = fixture.latest_default_table().await?; diff --git a/src/query/service/tests/it/storages/fuse/pruning.rs b/src/query/service/tests/it/storages/fuse/pruning.rs index 6d423a847c81..b878f6730691 100644 --- a/src/query/service/tests/it/storages/fuse/pruning.rs +++ b/src/query/service/tests/it/storages/fuse/pruning.rs @@ -42,7 +42,7 @@ use databend_query::sessions::TableContext; use databend_query::storages::fuse::io::MetaReaders; use databend_query::storages::fuse::FUSE_OPT_KEY_BLOCK_PER_SEGMENT; use databend_query::storages::fuse::FUSE_OPT_KEY_ROW_PER_BLOCK; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use opendal::Operator; use storages_common_cache::LoadParams; use storages_common_table_meta::meta::BlockMeta; @@ -70,9 +70,11 @@ async fn apply_block_pruning( #[tokio::test(flavor = "multi_thread")] async fn test_block_pruner() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; + fixture.create_default_database().await?; + let test_tbl_name = "test_index_helper"; let test_schema = TableSchemaRefExt::create(vec![ TableField::new("a", TableDataType::Number(NumberDataType::UInt64)), diff --git a/src/query/service/tests/it/storages/fuse/statistics.rs b/src/query/service/tests/it/storages/fuse/statistics.rs index 42ca236d6a00..ba515c53b1d7 100644 --- a/src/query/service/tests/it/storages/fuse/statistics.rs +++ b/src/query/service/tests/it/storages/fuse/statistics.rs @@ -49,7 +49,7 @@ use databend_query::storages::fuse::statistics::reducers; use databend_query::storages::fuse::statistics::ClusterStatsGenerator; use databend_query::storages::fuse::statistics::StatisticsAccumulator; use databend_query::test_kits::block_writer::BlockWriter; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use opendal::Operator; use rand::Rng; use storages_common_table_meta::meta::BlockMeta; diff --git a/src/query/service/tests/it/storages/fuse/table.rs b/src/query/service/tests/it/storages/fuse/table.rs index dac525b5fd57..a96fb734af20 100644 --- a/src/query/service/tests/it/storages/fuse/table.rs +++ b/src/query/service/tests/it/storages/fuse/table.rs @@ -20,15 +20,16 @@ use common_meta_app::schema::TableInfo; use common_sql::executor::table_read_plan::ToReadDataSourcePlan; use databend_query::storages::fuse::FuseTable; use databend_query::stream::ReadDataBlockStream; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::TestFixture; use futures::TryStreamExt; use storages_common_table_meta::table::OPT_KEY_DATABASE_ID; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_table_normal_case() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let ctx = fixture.new_query_ctx().await?; + fixture.create_default_database().await?; fixture.create_default_table().await?; let mut table = fixture.latest_default_table().await?; diff --git a/src/query/service/tests/it/storages/fuse/table_functions/clustering_information_table.rs b/src/query/service/tests/it/storages/fuse/table_functions/clustering_information_table.rs index 331933bf21b9..7366dced8bb8 100644 --- a/src/query/service/tests/it/storages/fuse/table_functions/clustering_information_table.rs +++ b/src/query/service/tests/it/storages/fuse/table_functions/clustering_information_table.rs @@ -27,17 +27,18 @@ use common_sql::executor::table_read_plan::ToReadDataSourcePlan; use common_storages_fuse::table_functions::ClusteringInformationTable; use databend_query::sessions::QueryContext; use databend_query::stream::ReadDataBlockStream; -use databend_query::test_kits::table_test_fixture::*; +use databend_query::test_kits::fixture::*; use tokio_stream::StreamExt; #[tokio::test(flavor = "multi_thread")] async fn test_clustering_information_table_read() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); let ctx = fixture.new_query_ctx().await?; // test db & table + fixture.create_default_database().await?; fixture.create_default_table().await?; // func args diff --git a/src/query/service/tests/it/storages/fuse/table_functions/fuse_block_table.rs b/src/query/service/tests/it/storages/fuse/table_functions/fuse_block_table.rs index 944c9c1f8eba..eee5351a28a4 100644 --- a/src/query/service/tests/it/storages/fuse/table_functions/fuse_block_table.rs +++ b/src/query/service/tests/it/storages/fuse/table_functions/fuse_block_table.rs @@ -16,17 +16,18 @@ use common_base::base::tokio; use common_exception::ErrorCode; use common_exception::Result; use common_expression::DataBlock; -use databend_query::test_kits::table_test_fixture::*; +use databend_query::test_kits::fixture::*; use tokio_stream::StreamExt; #[tokio::test(flavor = "multi_thread")] async fn test_fuse_block_table() -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; let db = fixture.default_db_name(); let tbl = fixture.default_table_name(); let ctx = fixture.new_query_ctx().await?; // test db & table + fixture.create_default_database().await?; fixture.create_default_table().await?; { diff --git a/src/query/service/tests/it/storages/fuse/utils.rs b/src/query/service/tests/it/storages/fuse/utils.rs index 04566433fa9d..2b8193df964e 100644 --- a/src/query/service/tests/it/storages/fuse/utils.rs +++ b/src/query/service/tests/it/storages/fuse/utils.rs @@ -18,11 +18,11 @@ use std::sync::Arc; use common_exception::Result; use common_storages_fuse::FuseTable; use common_storages_fuse::TableContext; -use databend_query::test_kits::table_test_fixture::append_sample_data; -use databend_query::test_kits::table_test_fixture::append_sample_data_overwrite; -use databend_query::test_kits::table_test_fixture::check_data_dir; -use databend_query::test_kits::table_test_fixture::history_should_have_item; -use databend_query::test_kits::table_test_fixture::TestFixture; +use databend_query::test_kits::fixture::append_sample_data; +use databend_query::test_kits::fixture::append_sample_data_overwrite; +use databend_query::test_kits::fixture::check_data_dir; +use databend_query::test_kits::fixture::history_should_have_item; +use databend_query::test_kits::fixture::TestFixture; pub async fn do_insertions(fixture: &TestFixture) -> Result<()> { fixture.create_default_table().await?; @@ -41,7 +41,8 @@ pub async fn do_purge_test( block_count: u32, index_count: u32, ) -> Result<()> { - let fixture = TestFixture::new().await?; + let fixture = TestFixture::setup().await?; + fixture.create_default_database().await?; // insert, and then insert overwrite (1 snapshot, 1 segment, 1 data block, 1 index block for each insertion); do_insertions(&fixture).await?; @@ -70,5 +71,7 @@ pub async fn do_purge_test( None, ) .await?; - history_should_have_item(&fixture, case_name, snapshot_count).await + history_should_have_item(&fixture, case_name, snapshot_count).await?; + + Ok(()) } diff --git a/src/query/service/tests/it/storages/null.rs b/src/query/service/tests/it/storages/null.rs index fe5494af29f4..c0ea8de63e5f 100644 --- a/src/query/service/tests/it/storages/null.rs +++ b/src/query/service/tests/it/storages/null.rs @@ -24,11 +24,13 @@ use common_sql::executor::table_read_plan::ToReadDataSourcePlan; use common_sql::plans::TableOptions; use common_storages_null::NullTable; use databend_query::stream::ReadDataBlockStream; +use databend_query::test_kits::TestFixture; use futures::TryStreamExt; #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_null_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; let table = NullTable::try_create(TableInfo { desc: "'default'.'a'".into(), diff --git a/src/query/service/tests/it/storages/system.rs b/src/query/service/tests/it/storages/system.rs index 857bcce5614d..18d7317a374e 100644 --- a/src/query/service/tests/it/storages/system.rs +++ b/src/query/service/tests/it/storages/system.rs @@ -27,6 +27,7 @@ use common_meta_app::principal::UserGrantSet; use common_meta_app::principal::UserInfo; use common_meta_app::principal::UserOption; use common_meta_app::principal::UserQuota; +use common_meta_app::storage::StorageFsConfig; use common_meta_app::storage::StorageParams; use common_meta_app::storage::StorageS3Config; use common_sql::executor::table_read_plan::ToReadDataSourcePlan; @@ -51,6 +52,8 @@ use databend_query::sessions::QueryContext; use databend_query::sessions::TableContext; use databend_query::stream::ReadDataBlockStream; use databend_query::test_kits::ClusterDescriptor; +use databend_query::test_kits::ConfigBuilder; +use databend_query::test_kits::TestFixture; use futures::TryStreamExt; use goldenfile::Mint; use wiremock::matchers::method; @@ -97,7 +100,8 @@ async fn run_table_tests( #[tokio::test(flavor = "multi_thread")] async fn test_build_options_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; let table = BuildOptionsTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; @@ -107,26 +111,29 @@ async fn test_build_options_table() -> Result<()> { let block = &result[0]; assert_eq!(block.num_columns(), 2); assert!(block.num_rows() > 0); + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_columns_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("columns_table.txt").unwrap(); let table = ColumnsTable::create(1); - run_table_tests(file, ctx, table).await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_clusters_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; - let table = ClustersTable::create(1); + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = ClustersTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; let stream = table.read_data_block_stream(ctx, &source_plan).await?; @@ -139,17 +146,19 @@ async fn test_clusters_table() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_configs_table_basic() -> Result<()> { - let mut mint = Mint::new("tests/it/storages/testdata"); - let file = &mut mint.new_goldenfile("configs_table_basic.txt").unwrap(); + let mut config = ConfigBuilder::create().build(); + config.storage.params = StorageParams::Fs(StorageFsConfig::default()); + let fixture = TestFixture::setup_with_config(&config).await?; - let conf = databend_query::test_kits::ConfigBuilder::create().config(); - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + let ctx = fixture.new_query_ctx().await?; ctx.get_settings().set_max_threads(8)?; - let table = ConfigsTable::create(1); + let mut mint = Mint::new("tests/it/storages/testdata"); + let file = &mut mint.new_goldenfile("configs_table_basic.txt").unwrap(); + let table = ConfigsTable::create(1); run_table_tests(file, ctx, table).await?; + Ok(()) } @@ -174,9 +183,8 @@ async fn test_configs_table_redact() -> Result<()> { secret_access_key: "secret_access_key".to_string(), ..Default::default() }); - - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_config(conf, None).await?; + let fixture = TestFixture::setup_with_config(&conf).await?; + let ctx = fixture.new_query_ctx().await?; ctx.get_settings().set_max_threads(8)?; let table = ConfigsTable::create(1); @@ -194,7 +202,9 @@ async fn test_configs_table_redact() -> Result<()> { #[tokio::test(flavor = "multi_thread")] async fn test_contributors_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = ContributorsTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; @@ -202,12 +212,15 @@ async fn test_contributors_table() -> Result<()> { let result = stream.try_collect::>().await?; let block = &result[0]; assert_eq!(block.num_columns(), 1); + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_credits_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = CreditsTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; @@ -215,6 +228,7 @@ async fn test_credits_table() -> Result<()> { let result = stream.try_collect::>().await?; let block = &result[0]; assert_eq!(block.num_columns(), 3); + Ok(()) } @@ -223,40 +237,49 @@ async fn test_catalogs_table() -> Result<()> { let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("catalogs_table.txt").unwrap(); - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; - let table = CatalogsTable::create(1); + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = CatalogsTable::create(1); run_table_tests(file, ctx, table).await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_databases_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let mut config = ConfigBuilder::create().build(); + config.storage.params = StorageParams::Fs(StorageFsConfig::default()); + let fixture = TestFixture::setup_with_config(&config).await?; + let ctx = fixture.new_query_ctx().await?; + let table = DatabasesTable::create(1); let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("databases_table.txt").unwrap(); - run_table_tests(file, ctx, table).await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_engines_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; - let table = EnginesTable::create(1); + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = EnginesTable::create(1); let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("engines_table.txt").unwrap(); - run_table_tests(file, ctx, table).await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_functions_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = FunctionsTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; @@ -264,12 +287,15 @@ async fn test_functions_table() -> Result<()> { let result = stream.try_collect::>().await?; let block = &result[0]; assert_eq!(block.num_columns(), 8); + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_metrics_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table = MetricsTable::create(1); let source_plan = table.read_plan(ctx.clone(), None, true).await?; let counter1 = common_metrics::register_counter("test_metrics_table_count"); @@ -304,10 +330,12 @@ async fn test_roles_table() -> Result<()> { let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("roles_table.txt").unwrap(); - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; - let tenant = ctx.get_tenant(); + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; ctx.get_settings().set_max_threads(2)?; + let tenant = ctx.get_tenant(); + { let role_info = RoleInfo::new("test"); UserApiProvider::instance() @@ -323,8 +351,8 @@ async fn test_roles_table() -> Result<()> { .await?; } let table = RolesTable::create(1); - run_table_tests(file, ctx, table).await?; + Ok(()) } @@ -333,19 +361,22 @@ async fn test_settings_table() -> Result<()> { let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("settings_table.txt").unwrap(); - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; ctx.get_settings().set_max_threads(2)?; ctx.get_settings().set_max_memory_usage(1073741824)?; let table = SettingsTable::create(1); - run_table_tests(file, ctx, table).await?; + Ok(()) } #[tokio::test(flavor = "multi_thread")] async fn test_tracing_table() -> Result<()> { - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let table: Arc = Arc::new(TracingTable::create(1)); let source_plan = table.read_plan(ctx.clone(), None, true).await?; @@ -363,9 +394,11 @@ async fn test_users_table() -> Result<()> { let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("users_table.txt").unwrap(); - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; - let tenant = ctx.get_tenant(); + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; ctx.get_settings().set_max_threads(2)?; + + let tenant = ctx.get_tenant(); let auth_data = AuthInfo::None; UserApiProvider::instance() .add_user( @@ -399,8 +432,8 @@ async fn test_users_table() -> Result<()> { .await?; let table = UsersTable::create(1); - run_table_tests(file, ctx, table).await?; + Ok(()) } @@ -409,12 +442,14 @@ async fn test_caches_table() -> Result<()> { let mut mint = Mint::new("tests/it/storages/testdata"); let file = &mut mint.new_goldenfile("caches_table.txt").unwrap(); + let mut config = ConfigBuilder::create().build(); + config.storage.params = StorageParams::Fs(StorageFsConfig::default()); + let fixture = TestFixture::setup_with_config(&config).await?; let cluster_desc = ClusterDescriptor::new().with_local_id("test-node"); - let (_guard, ctx) = - databend_query::test_kits::create_query_context_with_cluster(cluster_desc).await?; + let ctx = fixture.new_query_ctx_with_cluster(cluster_desc).await?; let table = CachesTable::create(1); - run_table_tests(file, ctx, table).await?; + Ok(()) } diff --git a/src/query/service/tests/it/table_functions/numbers_table.rs b/src/query/service/tests/it/table_functions/numbers_table.rs index d2c01a75c199..b1f848a87b3d 100644 --- a/src/query/service/tests/it/table_functions/numbers_table.rs +++ b/src/query/service/tests/it/table_functions/numbers_table.rs @@ -23,13 +23,16 @@ use databend_query::stream::ReadDataBlockStream; use databend_query::table_functions::generate_numbers_parts; use databend_query::table_functions::NumbersPartInfo; use databend_query::table_functions::NumbersTable; +use databend_query::test_kits::TestFixture; use futures::TryStreamExt; use pretty_assertions::assert_eq; #[tokio::test(flavor = "multi_thread")] async fn test_number_table() -> Result<()> { + let fixture = TestFixture::setup().await?; + let ctx = fixture.new_query_ctx().await?; + let tbl_args = TableArgs::new_positioned(vec![Scalar::from(8u64)]); - let (_guard, ctx) = databend_query::test_kits::create_query_context().await?; let table = NumbersTable::create("system", "numbers_mt", 1, tbl_args)?; let source_plan = table diff --git a/src/query/sql/tests/location.rs b/src/query/sql/tests/location.rs index 6efe76af387c..ab3355441370 100644 --- a/src/query/sql/tests/location.rs +++ b/src/query/sql/tests/location.rs @@ -46,7 +46,7 @@ async fn test_parse_uri_location() -> Result<()> { .expect("thread should has a name"); GlobalInstance::init_testing(&thread_name); - GlobalConfig::init(InnerConfig::default())?; + GlobalConfig::init(&InnerConfig::default())?; let cases = vec![ (