Skip to content

Commit

Permalink
fix: run postgresdb in itest
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 18, 2023
1 parent a8d791a commit be0021b
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 8 deletions.
97 changes: 91 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integrationtests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
55 changes: 53 additions & 2 deletions integrationtests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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:[email protected]/moksha-mint".to_owned())
.with_db(
format!(
"postgres://postgres:[email protected]:{}/moksha-mint",
host_port
)
.to_owned(),
)
.with_lightning(LightningType::Lnbits(LnbitsLightningSettings::new(
"my_admin_key",
"http://127.0.0.1:6100",
Expand Down Expand Up @@ -130,3 +143,41 @@ fn read_fixture(name: &str) -> anyhow::Result<String> {
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<String, String>,
}

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<WaitFor> {
vec![WaitFor::message_on_stderr(
"database system is ready to accept connections",
)]
}

fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
Box::new(self.env_vars.iter())
}
}

0 comments on commit be0021b

Please sign in to comment.