From 61f56f4f0d1876b2b92e84456e7019bf37de1210 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:04:30 -0400 Subject: [PATCH] Only publish necessary ports for emulator container also make sure to randomize host port so that tests don't clobber each other when ran in parallel --- .../tests/test/emulator_tests.rs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs index 228fc44090..6f177c936e 100644 --- a/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs +++ b/cmd/crates/stellar-ledger/tests/test/emulator_tests.rs @@ -4,6 +4,7 @@ use soroban_env_host::xdr::{self, Operation, OperationBody, Uint256}; use soroban_env_host::xdr::{Hash, Transaction}; use std::vec; +use std::net::TcpListener; use stellar_ledger::hd_path::HdPath; use stellar_ledger::{Blob, Error, LedgerSigner}; @@ -14,7 +15,7 @@ use stellar_xdr::curr::{ Memo, MuxedAccount, PaymentOp, Preconditions, SequenceNumber, TransactionExt, }; -use testcontainers::clients; +use testcontainers::{clients, core::Port, RunnableImage}; use tokio::time::sleep; pub const TEST_NETWORK_PASSPHRASE: &[u8] = b"Test SDF Network ; September 2015"; @@ -73,6 +74,18 @@ async fn test_get_public_key(ledger_device_model: String) { node.stop(); } +fn get_available_ports() -> (u16, u16) { + let listener1 = TcpListener::bind("127.0.0.1:0").unwrap(); + let listener2 = TcpListener::bind("127.0.0.1:0").unwrap(); + println!("listener1: {:?}", listener1); + let port_1 = listener1.local_addr().unwrap().port(); + let port_2 = listener2.local_addr().unwrap().port(); + drop(listener1); + drop(listener2); + + (port_1, port_2) +} + #[test_case("nanos".to_string() ; "when the device is NanoS")] #[test_case("nanox".to_string() ; "when the device is NanoX")] #[test_case("nanosp".to_string() ; "when the device is NanoS Plus")] @@ -82,7 +95,21 @@ async fn test_get_app_configuration(ledger_device_model: String) { ledger_device_model, }; let docker = clients::Cli::default(); - let node = docker.run((Speculos::new(), args)); + let runnable_image: RunnableImage = (Speculos::new(), args.clone()).into(); + + // doing this to randomize the ports on the host so that parallel tests don't clobber each other + let (tcp_port_1, tcp_port_2) = get_available_ports(); + let runnable_image = runnable_image + .with_mapped_port(Port { + local: tcp_port_1, + internal: 9998, + }) + .with_mapped_port(Port { + local: tcp_port_2, + internal: 5000, + }); + + let node = docker.run(runnable_image); let host_port = node.get_host_port_ipv4(9998); let ui_host_port: u16 = node.get_host_port_ipv4(5000); wait_for_emulator_start_text(ui_host_port).await;