Skip to content

Commit

Permalink
Upgrade testcontainers from 0.12 to 0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
ikmckenz committed Feb 19, 2024
1 parent 82e22f0 commit 35a64ee
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 244 deletions.
43 changes: 4 additions & 39 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion monero-harness/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ futures = "0.3"
monero-rpc = { path = "../monero-rpc" }
rand = "0.7"
spectral = "0.6"
testcontainers = "0.12"
testcontainers = "0.14"
tokio = { version = "1", default-features = false, features = [ "rt-multi-thread", "time", "macros" ] }
tracing = "0.1"
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "tracing-log" ] }
95 changes: 31 additions & 64 deletions monero-harness/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;
use testcontainers::core::{Container, Docker, WaitForMessage};
use testcontainers::Image;
use testcontainers::{core::WaitFor, Image, ImageArgs};

pub const MONEROD_DAEMON_CONTAINER_NAME: &str = "monerod";
pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
Expand All @@ -14,42 +12,24 @@ pub const MONEROD_DEFAULT_NETWORK: &str = "monero_network";
pub const RPC_PORT: u16 = 18081;

#[derive(Debug, Default)]
#[allow(dead_code)]
pub struct Monerod {
args: MonerodArgs,
}

impl Image for Monerod {
type Args = MonerodArgs;
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;

fn descriptor(&self) -> String {
"rinocommunity/monero:v0.18.1.2".to_owned()
fn name(&self) -> String {
"rinocommunity/monero".into()
}

fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stdout
.wait_for_message("RPC server started ok")
.unwrap();
fn tag(&self) -> String {
"v0.18.1.2".into()
}

fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}

fn volumes(&self) -> Self::Volumes {
HashMap::new()
}

fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
}

fn with_args(self, args: <Self as Image>::Args) -> Self {
Self { args }
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("RPC server started ok")]
}

fn entrypoint(&self) -> Option<String> {
Expand All @@ -58,43 +38,25 @@ impl Image for Monerod {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
#[allow(dead_code)]
pub struct MoneroWalletRpc {
args: MoneroWalletRpcArgs,
}

impl Image for MoneroWalletRpc {
type Args = MoneroWalletRpcArgs;
type EnvVars = HashMap<String, String>;
type Volumes = HashMap<String, String>;
type EntryPoint = str;

fn descriptor(&self) -> String {
"rinocommunity/monero:v0.18.1.2".to_owned()
}

fn wait_until_ready<D: Docker>(&self, container: &Container<'_, D, Self>) {
container
.logs()
.stdout
.wait_for_message("Run server thread name: RPC")
.unwrap();
}

fn args(&self) -> <Self as Image>::Args {
self.args.clone()
}

fn volumes(&self) -> Self::Volumes {
HashMap::new()
fn name(&self) -> String {
"rinocommunity/monero".into()
}

fn env_vars(&self) -> Self::EnvVars {
HashMap::new()
fn tag(&self) -> String {
"v0.18.1.2".into()
}

fn with_args(self, args: <Self as Image>::Args) -> Self {
Self { args }
fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Run server thread name: RPC")]
}

fn entrypoint(&self) -> Option<String> {
Expand All @@ -104,10 +66,9 @@ impl Image for MoneroWalletRpc {
}

impl MoneroWalletRpc {
pub fn new(name: &str, daemon_address: String) -> Self {
Self {
args: MoneroWalletRpcArgs::new(name, daemon_address),
}
pub fn new(name: &str, daemon_address: String) -> (Self, MoneroWalletRpcArgs) {
let args = MoneroWalletRpcArgs::new(name, daemon_address);
(Self { args: args.clone() }, args)
}
}

Expand Down Expand Up @@ -191,6 +152,12 @@ impl IntoIterator for MonerodArgs {
}
}

impl ImageArgs for MonerodArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}

#[derive(Debug, Clone)]
pub struct MoneroWalletRpcArgs {
pub disable_rpc_login: bool,
Expand All @@ -200,12 +167,6 @@ pub struct MoneroWalletRpcArgs {
pub daemon_address: String,
}

impl Default for MoneroWalletRpcArgs {
fn default() -> Self {
unimplemented!("A default instance for `MoneroWalletRpc` doesn't make sense because we always need to connect to a node.")
}
}

impl MoneroWalletRpcArgs {
pub fn new(wallet_name: &str, daemon_address: String) -> Self {
Self {
Expand Down Expand Up @@ -247,3 +208,9 @@ impl IntoIterator for MoneroWalletRpcArgs {
args.into_iter()
}
}

impl ImageArgs for MoneroWalletRpcArgs {
fn into_iterator(self) -> Box<dyn Iterator<Item = String>> {
Box::new(self.into_iter())
}
}
50 changes: 24 additions & 26 deletions monero-harness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
//! every BLOCK_TIME_SECS seconds.
//!
//! Also provides standalone JSON RPC clients for monerod and monero-wallet-rpc.
pub mod image;
use std::time::Duration;

use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};
use anyhow::{anyhow, bail, Context, Result};
use testcontainers::clients::Cli;
use testcontainers::{Container, RunnableImage};
use tokio::time;

use monero_rpc::monerod;
use monero_rpc::monerod::MonerodRpc as _;
use monero_rpc::wallet::{self, GetAddress, MoneroWalletRpc as _, Refreshed, Transfer};
use std::time::Duration;
use testcontainers::clients::Cli;
use testcontainers::{Container, Docker, RunArgs};
use tokio::time;

use crate::image::{MONEROD_DAEMON_CONTAINER_NAME, MONEROD_DEFAULT_NETWORK, RPC_PORT};

pub mod image;

/// How often we mine a block.
const BLOCK_TIME_SECS: u64 = 1;
Expand All @@ -56,8 +59,8 @@ impl<'c> Monero {
additional_wallets: Vec<&'static str>,
) -> Result<(
Self,
Container<'c, Cli, image::Monerod>,
Vec<Container<'c, Cli, image::MoneroWalletRpc>>,
Container<'c, image::Monerod>,
Vec<Container<'c, image::MoneroWalletRpc>>,
)> {
let prefix = format!("{}_", random_prefix());
let monerod_name = format!("{}{}", prefix, MONEROD_DAEMON_CONTAINER_NAME);
Expand Down Expand Up @@ -221,15 +224,14 @@ impl<'c> Monerod {
cli: &'c Cli,
name: String,
network: String,
) -> Result<(Self, Container<'c, Cli, image::Monerod>)> {
) -> Result<(Self, Container<'c, image::Monerod>)> {
let image = image::Monerod::default();
let run_args = RunArgs::default()
.with_name(name.clone())
let image: RunnableImage<image::Monerod> = RunnableImage::from(image)
.with_container_name(name.clone())
.with_network(network.clone());
let container = cli.run_with_args(image, run_args);
let monerod_rpc_port = container
.get_host_port(RPC_PORT)
.context("port not exposed")?;

let container = cli.run(image);
let monerod_rpc_port = container.get_host_port_ipv4(RPC_PORT);

Ok((
Self {
Expand Down Expand Up @@ -262,19 +264,15 @@ impl<'c> MoneroWalletRpc {
name: &str,
monerod: &Monerod,
prefix: String,
) -> Result<(Self, Container<'c, Cli, image::MoneroWalletRpc>)> {
) -> Result<(Self, Container<'c, image::MoneroWalletRpc>)> {
let daemon_address = format!("{}:{}", monerod.name, RPC_PORT);
let image = image::MoneroWalletRpc::new(name, daemon_address);
let (image, args) = image::MoneroWalletRpc::new(name, daemon_address);
let image = RunnableImage::from((image, args))
.with_container_name(format!("{}{}", prefix, name))
.with_network(monerod.network.clone());

let network = monerod.network.clone();
let run_args = RunArgs::default()
// prefix the container name so we can run multiple tests
.with_name(format!("{}{}", prefix, name))
.with_network(network.clone());
let container = cli.run_with_args(image, run_args);
let wallet_rpc_port = container
.get_host_port(RPC_PORT)
.context("port not exposed")?;
let container = cli.run(image);
let wallet_rpc_port = container.get_host_port_ipv4(RPC_PORT);

let client = wallet::Client::localhost(wallet_rpc_port)?;

Expand Down
2 changes: 1 addition & 1 deletion monero-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ rand = "0.7"
curve25519-dalek = "3"
monero-harness = { path = "../monero-harness" }
rand = "0.7"
testcontainers = "0.12"
testcontainers = "0.14"
tokio = { version = "1", features = [ "rt-multi-thread", "time", "macros", "sync", "process", "fs" ] }
tracing-subscriber = { version = "0.2", default-features = false, features = [ "fmt", "ansi", "env-filter", "chrono", "tracing-log" ] }
3 changes: 1 addition & 2 deletions monero-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ mod tests {
use monero_harness::image::Monerod;
use monero_rpc::monerod::{Client, GetOutputsOut};
use testcontainers::clients::Cli;
use testcontainers::Docker;

#[tokio::test]
async fn get_outs_for_key_offsets() {
let cli = Cli::default();
let container = cli.run(Monerod::default());
let rpc_client = Client::localhost(container.get_host_port(18081).unwrap()).unwrap();
let rpc_client = Client::localhost(container.get_host_port_ipv4(18081)).unwrap();
rpc_client.generateblocks(150, "498AVruCDWgP9Az9LjMm89VWjrBrSZ2W2K3HFBiyzzrRjUJWUcCVxvY1iitfuKoek2FdX6MKGAD9Qb1G1P8QgR5jPmmt3Vj".to_owned()).await.unwrap();
let wallet = Wallet {
client: rpc_client.clone(),
Expand Down
2 changes: 1 addition & 1 deletion swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ serde_cbor = "0.11"
serial_test = "3.0"
spectral = "0.6"
tempfile = "3"
testcontainers = "0.12"
testcontainers = "0.14"

[build-dependencies]
anyhow = "1"
Expand Down
Loading

0 comments on commit 35a64ee

Please sign in to comment.