From 7dabd46aa24546678401d310750535a18c52f038 Mon Sep 17 00:00:00 2001 From: lonelyhentxi Date: Mon, 4 Mar 2024 00:05:00 +0800 Subject: [PATCH] feat: add testcontainers --- Cargo.lock | 99 ++++++++++++++++++- crates/recorder/Cargo.toml | 24 +++-- crates/recorder/src/downloaders/qbitorrent.rs | 49 ++++++++- 3 files changed, 156 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd3869f..06fe14f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -546,6 +546,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bollard-stubs" +version = "1.42.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed59b5c00048f48d7af971b71f800fdf23e858844a6f9e4d32ca72e9399e7864" +dependencies = [ + "serde", + "serde_with 1.14.0", +] + [[package]] name = "borsh" version = "1.3.1" @@ -1122,6 +1132,16 @@ dependencies = [ "syn 2.0.50", ] +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core 0.13.4", + "darling_macro 0.13.4", +] + [[package]] name = "darling" version = "0.14.4" @@ -1142,6 +1162,20 @@ dependencies = [ "darling_macro 0.20.8", ] +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.10.0", + "syn 1.0.109", +] + [[package]] name = "darling_core" version = "0.14.4" @@ -1170,6 +1204,17 @@ dependencies = [ "syn 2.0.50", ] +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core 0.13.4", + "quote", + "syn 1.0.109", +] + [[package]] name = "darling_macro" version = "0.14.4" @@ -3406,7 +3451,7 @@ dependencies = [ "serde-value", "serde_json", "serde_repr", - "serde_with", + "serde_with 2.3.3", "tap", "thiserror", "tracing", @@ -3537,6 +3582,8 @@ dependencies = [ "serde", "serde_json", "serial_test", + "testcontainers", + "testcontainers-modules", "thiserror", "tl", "tokio", @@ -4421,6 +4468,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff" +dependencies = [ + "serde", + "serde_with_macros 1.5.2", +] + [[package]] name = "serde_with" version = "2.3.3" @@ -4433,10 +4490,22 @@ dependencies = [ "indexmap 1.9.3", "serde", "serde_json", - "serde_with_macros", + "serde_with_macros 2.3.3", "time", ] +[[package]] +name = "serde_with_macros" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" +dependencies = [ + "darling 0.13.4", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "serde_with_macros" version = "2.3.3" @@ -5122,6 +5191,32 @@ dependencies = [ "winapi", ] +[[package]] +name = "testcontainers" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d2931d7f521af5bae989f716c3fa43a6af9af7ec7a5e21b59ae40878cec00" +dependencies = [ + "bollard-stubs", + "futures", + "hex 0.4.3", + "hmac", + "log", + "rand", + "serde", + "serde_json", + "sha2", +] + +[[package]] +name = "testcontainers-modules" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d0334776e1e8ee7c504a922c5236daf865ffe413aa630d84ae91dcce0b10bc3" +dependencies = [ + "testcontainers", +] + [[package]] name = "textwrap" version = "0.15.2" diff --git a/crates/recorder/Cargo.toml b/crates/recorder/Cargo.toml index 7bb9a3d..cf46f3f 100644 --- a/crates/recorder/Cargo.toml +++ b/crates/recorder/Cargo.toml @@ -4,9 +4,20 @@ version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "recorder" +path = "src/lib.rs" + +[[bin]] +name = "recorder_cli" +path = "src/bin/main.rs" +required-features = [] + +[features] +default = [] +testcontainers = [] [dependencies] - loco-rs = { version = "0.3.1" } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -49,17 +60,10 @@ html-escape = "0.2.13" opendal = "0.45.0" librqbit-core = "3.5.0" -[lib] -name = "recorder" -path = "src/lib.rs" - -[[bin]] -name = "recorder_cli" -path = "src/bin/main.rs" -required-features = [] - [dev-dependencies] serial_test = "2.0.0" rstest = "0.18.2" loco-rs = { version = "0.3.1", features = ["testing"] } insta = { version = "1.34.0", features = ["redactions", "yaml", "filters"] } +testcontainers = { version = "0.15.0" } +testcontainers-modules = { version = "0.3.5" } \ No newline at end of file diff --git a/crates/recorder/src/downloaders/qbitorrent.rs b/crates/recorder/src/downloaders/qbitorrent.rs index 4cad980..52a0270 100644 --- a/crates/recorder/src/downloaders/qbitorrent.rs +++ b/crates/recorder/src/downloaders/qbitorrent.rs @@ -411,8 +411,9 @@ impl Debug for QBittorrentDownloader { } #[cfg(test)] -mod tests { +pub mod tests { use itertools::Itertools; + use testcontainers::core::ExecCommand; use super::*; @@ -424,8 +425,48 @@ mod tests { } } + #[cfg(feature = "testcontainers")] + pub fn create_qbit_testcontainer() -> testcontainers::RunnableImage + { + let image = testcontainers::RunnableImage::from( + testcontainers::GenericImage::new("linuxserver/qbittorrent", "latest") + .with_wait_for(testcontainers::core::WaitFor::message_on_stderr( + "Connection to localhost", + )) + .with_env_var("WEBUI_PORT", "8080") + .with_env_var("TZ", "Asia/Singapore") + .with_env_var("TORRENTING_PORT", "6881") + .with_exposed_port(8080) + .with_exposed_port(6881), + ); + + image + } + + #[cfg(not(feature = "testcontainers"))] #[tokio::test] - async fn test_add_torrents_and_get_info() { + async fn test_qbittorrent_downloader() { + test_qbittorrent_downloader_impl().await; + } + + // @TODO: not support now, testcontainers crate not support to read logs to get + // password + #[cfg(feature = "testcontainers")] + #[tokio::test] + async fn test_qbittorrent_downloader() { + let docker = testcontainers::clients::Cli::default(); + let image = create_qbit_testcontainer(); + + let container = docker.run(image); + + let mut exec = ExecCommand::default(); + + container.exec(exec); + + test_qbittorrent_downloader_impl().await; + } + + async fn test_qbittorrent_downloader_impl() { let base_save_path = VFSSubPath::new(get_tmp_qbit_test_folder()); let downloader = QBittorrentDownloader::from_downloader_model(downloaders::Model { @@ -433,7 +474,7 @@ mod tests { updated_at: Default::default(), id: 0, category: DownloaderCategory::QBittorrent, - endpoint: "http://127.0.0.1:8080".to_string(), + endpoint: "http://localhost:8080".to_string(), password: "".to_string(), username: "".to_string(), subscriber_id: 0, @@ -453,7 +494,7 @@ mod tests { "https://mikanani.me/Download/20240301/47ee2d69e7f19af783ad896541a07b012676f858.torrent" ).await.unwrap(); - let mut save_path = base_save_path.join(format!( + let save_path = base_save_path.join(format!( "test_add_torrents_{}", chrono::Utc::now().timestamp() ));