testcontainers_modules/parity_parity/
mod.rs

1use std::borrow::Cow;
2
3use testcontainers::{core::WaitFor, Image};
4
5const NAME: &str = "parity/parity";
6const TAG: &str = "v2.5.0";
7
8/// Module to work with [`Parity Ethereum`] inside of tests.
9///
10/// Starts an instance of Parity Ethereum based on the official [`Parity docker image`].
11///
12/// Parity Ethereum is a fast and feature-rich multi-network Ethereum client.
13/// This module provides a local Ethereum node for testing purposes with JSON-RPC API enabled.
14/// The container exposes port `8545` for HTTP JSON-RPC connections by default.
15///
16/// The node is configured with development settings including:
17/// - Development chain configuration
18/// - All JSON-RPC APIs enabled
19/// - Unsafe expose mode (for testing)
20/// - Tracing enabled
21///
22/// # Example
23/// ```
24/// use testcontainers_modules::{
25///     parity_parity::ParityEthereum, testcontainers::runners::SyncRunner,
26/// };
27///
28/// let ethereum_instance = ParityEthereum::default().start().unwrap();
29/// let host = ethereum_instance.get_host().unwrap();
30/// let port = ethereum_instance.get_host_port_ipv4(8545).unwrap();
31///
32/// // Use the JSON-RPC endpoint at http://{host}:{port}
33/// ```
34///
35/// [`Parity Ethereum`]: https://www.parity.io/
36/// [`Parity docker image`]: https://hub.docker.com/r/parity/parity
37#[derive(Debug, Default, Clone)]
38pub struct ParityEthereum {
39    /// (remove if there is another variable)
40    /// Field is included to prevent this struct to be a unit struct.
41    /// This allows extending functionality (and thus further variables) without breaking changes
42    _priv: (),
43}
44
45impl Image for ParityEthereum {
46    fn name(&self) -> &str {
47        NAME
48    }
49
50    fn tag(&self) -> &str {
51        TAG
52    }
53
54    fn ready_conditions(&self) -> Vec<WaitFor> {
55        vec![WaitFor::message_on_stderr("Public node URL:")]
56    }
57
58    fn cmd(&self) -> impl IntoIterator<Item = impl Into<Cow<'_, str>>> {
59        [
60            "--config=dev",
61            "--jsonrpc-apis=all",
62            "--unsafe-expose",
63            "--tracing=on",
64        ]
65    }
66}
67
68#[cfg(test)]
69mod tests {
70    use testcontainers::runners::SyncRunner;
71
72    use crate::parity_parity;
73
74    #[test]
75    fn parity_parity_net_version() -> Result<(), Box<dyn std::error::Error + 'static>> {
76        let _ = pretty_env_logger::try_init();
77        let node = parity_parity::ParityEthereum::default().start()?;
78        let host_ip = node.get_host()?;
79        let host_port = node.get_host_port_ipv4(8545)?;
80
81        let response = reqwest::blocking::Client::new()
82            .post(format!("http://{host_ip}:{host_port}"))
83            .body(
84                serde_json::json!({
85                    "jsonrpc": "2.0",
86                    "method": "net_version",
87                    "params": [],
88                    "id": 1
89                })
90                .to_string(),
91            )
92            .header("content-type", "application/json")
93            .send()
94            .unwrap();
95
96        let response = response.text().unwrap();
97        let response: serde_json::Value = serde_json::from_str(&response).unwrap();
98
99        assert_eq!(response["result"], "17");
100        Ok(())
101    }
102}