Merge pull request #20 from comit-network/monero-harness-ports

Use lib to ensure ports are free
This commit is contained in:
Philipp Hoenisch 2020-10-23 11:52:29 +11:00 committed by GitHub
commit da5b4bef75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 20 deletions

View File

@ -7,6 +7,7 @@ edition = "2018"
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
futures = "0.3" futures = "0.3"
port_check = "0.1"
rand = "0.7" rand = "0.7"
reqwest = { version = "0.10", default-features = false, features = ["json", "native-tls"] } reqwest = { version = "0.10", default-features = false, features = ["json", "native-tls"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -24,8 +24,7 @@
pub mod image; pub mod image;
pub mod rpc; pub mod rpc;
use anyhow::Result; use anyhow::{anyhow, Result};
use rand::Rng;
use serde::Deserialize; use serde::Deserialize;
use std::time::Duration; use std::time::Duration;
use testcontainers::{clients::Cli, core::Port, Container, Docker}; use testcontainers::{clients::Cli, core::Port, Container, Docker};
@ -58,12 +57,15 @@ pub struct Monero {
impl<'c> Monero { impl<'c> Monero {
/// Starts a new regtest monero container. /// Starts a new regtest monero container.
pub fn new(cli: &'c Cli) -> (Self, Container<'c, Cli, image::Monero>) { pub fn new(cli: &'c Cli) -> Result<(Self, Container<'c, Cli, image::Monero>)> {
let mut rng = rand::thread_rng(); let monerod_rpc_port: u16 =
let monerod_rpc_port: u16 = rng.gen_range(1024, u16::MAX); port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let miner_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX); let miner_wallet_rpc_port: u16 =
let alice_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX); port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let bob_wallet_rpc_port: u16 = rng.gen_range(1024, u16::MAX); let alice_wallet_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let bob_wallet_rpc_port: u16 =
port_check::free_local_port().ok_or_else(|| anyhow!("Could not retrieve free port"))?;
let image = image::Monero::default() let image = image::Monero::default()
.with_mapped_port(Port { .with_mapped_port(Port {
@ -90,7 +92,7 @@ impl<'c> Monero {
let docker = cli.run(image); let docker = cli.run(image);
println!("image ran"); println!("image ran");
( Ok((
Self { Self {
monerod_rpc_port, monerod_rpc_port,
miner_wallet_rpc_port, miner_wallet_rpc_port,
@ -98,7 +100,7 @@ impl<'c> Monero {
bob_wallet_rpc_port, bob_wallet_rpc_port,
}, },
docker, docker,
) ))
} }
pub fn miner_wallet_rpc_client(&self) -> wallet::Client { pub fn miner_wallet_rpc_client(&self) -> wallet::Client {

View File

@ -8,7 +8,7 @@ const BOB_FUND_AMOUNT: u64 = 0;
#[tokio::test] #[tokio::test]
async fn init_accounts_for_alice_and_bob() { async fn init_accounts_for_alice_and_bob() {
let tc = Cli::default(); let tc = Cli::default();
let (monero, _container) = Monero::new(&tc); let (monero, _container) = Monero::new(&tc).unwrap();
monero monero
.init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT) .init(ALICE_FUND_AMOUNT, BOB_FUND_AMOUNT)
.await .await

View File

@ -9,7 +9,7 @@ fn init_cli() -> Cli {
#[tokio::test] #[tokio::test]
async fn connect_to_monerod() { async fn connect_to_monerod() {
let tc = init_cli(); let tc = init_cli();
let (monero, _container) = Monero::new(&tc); let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.monerod_rpc_client(); let cli = monero.monerod_rpc_client();
let header = cli let header = cli

View File

@ -5,7 +5,7 @@ use testcontainers::clients::Cli;
#[tokio::test] #[tokio::test]
async fn wallet_and_accounts() { async fn wallet_and_accounts() {
let tc = Cli::default(); let tc = Cli::default();
let (monero, _container) = Monero::new(&tc); let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.miner_wallet_rpc_client(); let cli = monero.miner_wallet_rpc_client();
println!("creating wallet ..."); println!("creating wallet ...");
@ -24,7 +24,7 @@ async fn wallet_and_accounts() {
#[tokio::test] #[tokio::test]
async fn create_account_and_retrieve_it() { async fn create_account_and_retrieve_it() {
let tc = Cli::default(); let tc = Cli::default();
let (monero, _container) = Monero::new(&tc); let (monero, _container) = Monero::new(&tc).unwrap();
let cli = monero.miner_wallet_rpc_client(); let cli = monero.miner_wallet_rpc_client();
let label = "Iron Man"; // This is intentionally _not_ Alice or Bob. let label = "Iron Man"; // This is intentionally _not_ Alice or Bob.
@ -58,7 +58,7 @@ async fn transfer_and_check_tx_key() {
let fund_bob = 0; let fund_bob = 0;
let tc = Cli::default(); let tc = Cli::default();
let (monero, _container) = Monero::new(&tc); let (monero, _container) = Monero::new(&tc).unwrap();
let _ = monero.init(fund_alice, fund_bob).await; let _ = monero.init(fund_alice, fund_bob).await;
let address_bob = monero let address_bob = monero

View File

@ -34,7 +34,7 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli); let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (
@ -108,7 +108,7 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli); let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (
@ -184,7 +184,7 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli); let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let ( let (
@ -249,7 +249,7 @@ mod tests {
.set_default(); .set_default();
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli); let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let alice_db_dir = tempfile::tempdir().unwrap(); let alice_db_dir = tempfile::tempdir().unwrap();

View File

@ -164,7 +164,7 @@ async fn swap_as_bob(
#[tokio::test] #[tokio::test]
async fn on_chain_happy_path() { async fn on_chain_happy_path() {
let cli = Cli::default(); let cli = Cli::default();
let (monero, _container) = Monero::new(&cli); let (monero, _container) = Monero::new(&cli).unwrap();
let bitcoind = init_bitcoind(&cli).await; let bitcoind = init_bitcoind(&cli).await;
let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) = let (alice_state0, bob_state0, mut alice_node, mut bob_node, initial_balances, swap_amounts) =