From be0021b9dda1c1b9f5d8b96948c8f3f5e8c1bed3 Mon Sep 17 00:00:00 2001 From: ngutech21 Date: Mon, 18 Dec 2023 14:50:19 +0100 Subject: [PATCH] fix: run postgresdb in itest --- Cargo.lock | 97 +++++++++++++++++++++++++++++++-- integrationtests/Cargo.toml | 1 + integrationtests/tests/tests.rs | 55 ++++++++++++++++++- 3 files changed, 145 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index af7d1cb2..325f2f0c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -562,6 +562,16 @@ dependencies = [ "zeroize", ] +[[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 = "build-target" version = "0.4.0" @@ -800,14 +810,38 @@ dependencies = [ "typenum", ] +[[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.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ - "darling_core", - "darling_macro", + "darling_core 0.20.3", + "darling_macro 0.20.3", +] + +[[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", + "syn 1.0.109", ] [[package]] @@ -824,13 +858,24 @@ dependencies = [ "syn 2.0.41", ] +[[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.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ - "darling_core", + "darling_core 0.20.3", "quote", "syn 2.0.41", ] @@ -2662,7 +2707,7 @@ dependencies = [ "secp256k1 0.28.0", "serde", "serde_json", - "serde_with", + "serde_with 3.4.0", "thiserror", "url", "utoipa", @@ -2711,6 +2756,7 @@ dependencies = [ "serde_derive", "serde_json", "tempfile", + "testcontainers", "tokio", "tracing", ] @@ -3819,6 +3865,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 = "3.4.0" @@ -3832,17 +3888,29 @@ dependencies = [ "indexmap 2.1.0", "serde", "serde_json", - "serde_with_macros", + "serde_with_macros 3.4.0", "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 = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93634eb5f75a2323b16de4748022ac4297f9e76b6dced2be287a099f41b5e788" dependencies = [ - "darling", + "darling 0.20.3", "proc-macro2", "quote", "syn 2.0.41", @@ -4355,6 +4423,23 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "testcontainers" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d2931d7f521af5bae989f716c3fa43a6af9af7ec7a5e21b59ae40878cec00" +dependencies = [ + "bollard-stubs", + "futures", + "hex", + "hmac", + "log", + "rand", + "serde", + "serde_json", + "sha2", +] + [[package]] name = "thiserror" version = "1.0.51" diff --git a/integrationtests/Cargo.toml b/integrationtests/Cargo.toml index a2e0db51..554154f3 100644 --- a/integrationtests/Cargo.toml +++ b/integrationtests/Cargo.toml @@ -33,6 +33,7 @@ hyper = { version = "0.14", features = ["full"] } bitcoin_hashes = { version = "0.11.0", default-features = false } secp256k1 = { version = "0.24.0", default-features = false, features = ["recovery", "alloc", "rand"] } tracing = "0.1.40" +testcontainers = "0.15.0" [target.'cfg(not(target_family="wasm"))'.dependencies] tokio = { version = "1.35.0", features = ["sync", "rt"] } diff --git a/integrationtests/tests/tests.rs b/integrationtests/tests/tests.rs index d971fb9b..22001e4a 100644 --- a/integrationtests/tests/tests.rs +++ b/integrationtests/tests/tests.rs @@ -6,14 +6,21 @@ use moksha_wallet::wallet::WalletBuilder; use mokshamint::lightning::{LightningType, LnbitsLightningSettings}; use mokshamint::mint::Mint; use reqwest::Url; +use std::collections::HashMap; use std::thread; use std::time::Duration; +use testcontainers::core::WaitFor; +use testcontainers::{clients, Image}; use tokio::runtime::Runtime; use tokio::time::{sleep_until, Instant}; #[test] #[cfg(feature = "integration-tests")] pub fn test_integration() -> anyhow::Result<()> { + let docker = clients::Cli::default(); + let node = docker.run(Postgres::default()); + let host_port = node.get_host_port_ipv4(5432); + // start lnbits let _lnbits_thread = thread::spawn(|| { let rt = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime"); @@ -23,12 +30,18 @@ pub fn test_integration() -> anyhow::Result<()> { }); // Create a channel to signal when the server has started - let _server_thread = thread::spawn(|| { + let _server_thread = thread::spawn(move || { let rt = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime"); rt.block_on(async { let mint = Mint::builder() .with_private_key("my_private_key".to_string()) - .with_db("postgres://postgres:postgres@127.0.0.1/moksha-mint".to_owned()) + .with_db( + format!( + "postgres://postgres:postgres@127.0.0.1:{}/moksha-mint", + host_port + ) + .to_owned(), + ) .with_lightning(LightningType::Lnbits(LnbitsLightningSettings::new( "my_admin_key", "http://127.0.0.1:6100", @@ -130,3 +143,41 @@ fn read_fixture(name: &str) -> anyhow::Result { let raw_token = std::fs::read_to_string(format!("{base_dir}/tests/fixtures/{name}"))?; Ok(raw_token.trim().to_string()) } + +#[derive(Debug)] +pub struct Postgres { + env_vars: HashMap, +} + +impl Default for Postgres { + fn default() -> Self { + let mut env_vars = HashMap::new(); + env_vars.insert("POSTGRES_DB".to_owned(), "postgres".to_owned()); + env_vars.insert("POSTGRES_HOST_AUTH_METHOD".into(), "trust".into()); + env_vars.insert("POSTGRES_DB".into(), "moksha-mint".into()); + + Self { env_vars } + } +} + +impl Image for Postgres { + type Args = (); + + fn name(&self) -> String { + "postgres".to_owned() + } + + fn tag(&self) -> String { + "15.3".to_owned() + } + + fn ready_conditions(&self) -> Vec { + vec![WaitFor::message_on_stderr( + "database system is ready to accept connections", + )] + } + + fn env_vars(&self) -> Box + '_> { + Box::new(self.env_vars.iter()) + } +}