Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add docs for the ganachecli module #190

Merged
merged 10 commits into from
Aug 31, 2024
51 changes: 47 additions & 4 deletions src/trufflesuite_ganachecli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
use std::borrow::Cow;

use testcontainers::{core::WaitFor, Image};
use testcontainers::{
core::{ContainerPort, WaitFor},
Image,
};

const NAME: &str = "trufflesuite/ganache-cli";
const TAG: &str = "v6.1.3";

/// Port that the [`Ganache CLI`] container has internally.
/// Can be rebound externally via [`testcontainers::core::ImageExt::with_mapped_port`]
///
/// [Ganache CLI]: https://github.com/trufflesuite/ganache
pub const GANACHE_CLI_PORT: ContainerPort = ContainerPort::Tcp(8545);

/// # Module to work with the [`Ganache CLI`] inside of tests.
///
/// Starts an instance of Meilisearch.
/// This module is based on the official [`trufflesuite/ganache-cli` docker image] documented in the [documentation].
///
/// # Example
/// ```
/// use testcontainers_modules::{
/// testcontainers::runners::SyncRunner, trufflesuite_ganachecli,
/// trufflesuite_ganachecli::GANACHE_CLI_PORT,
/// };
///
/// let instance = trufflesuite_ganachecli::GanacheCli::default()
/// .start()
/// .unwrap();
/// let url = format!(
/// "http://{host_ip}:{host_port}",
/// host_ip = instance.get_host().unwrap(),
/// host_port = instance.get_host_port_ipv4(GANACHE_CLI_PORT).unwrap()
DDtKey marked this conversation as resolved.
Show resolved Hide resolved
/// );
/// // do something with the started GanacheCli instance..
/// ```
///
/// [Ganache CLI]: https://github.com/trufflesuite/ganache
/// [documentation]: https://github.com/trufflesuite/ganache?tab=readme-ov-file#documentation
/// [`trufflesuite/ganache-cli` docker image]: https://hub.docker.com/r/trufflesuite/ganache-cli/
#[derive(Debug, Default, Clone)]
pub struct GanacheCli {
cmd: GanacheCliCmd,
}

/// Options to pass to the `ganache-cli` command
#[derive(Debug, Clone)]
pub struct GanacheCliCmd {
/// Specify the network id ganache-core will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured)
pub network_id: u32,
/// Specify the number of accounts to generate at startup
pub number_of_accounts: u32,
/// Use a bip39 mnemonic phrase for generating a PRNG seed, which is in turn used for hierarchical deterministic (HD) account generation.
pub mnemonic: String,
}

Expand Down Expand Up @@ -57,6 +96,10 @@ impl Image for GanacheCli {
TAG
}

fn expose_ports(&self) -> &[ContainerPort] {
&[GANACHE_CLI_PORT]
}

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Listening on localhost:")]
}
Expand All @@ -70,14 +113,14 @@ impl Image for GanacheCli {
mod tests {
use testcontainers::runners::SyncRunner;

use crate::trufflesuite_ganachecli;
use super::*;

#[test]
fn trufflesuite_ganachecli_listaccounts() -> Result<(), Box<dyn std::error::Error + 'static>> {
let _ = pretty_env_logger::try_init();
let node = trufflesuite_ganachecli::GanacheCli::default().start()?;
let node = GanacheCli::default().start()?;
let host_ip = node.get_host()?;
let host_port = node.get_host_port_ipv4(8545)?;
let host_port = node.get_host_port_ipv4(GANACHE_CLI_PORT)?;

let response = reqwest::blocking::Client::new()
.post(format!("http://{host_ip}:{host_port}"))
Expand Down
Loading