chore: move the interop objects into separate modules

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-09-07 17:13:47 +02:00
parent b545c4022b
commit 0e3afd4382
2 changed files with 92 additions and 93 deletions

View File

@ -1,110 +1,106 @@
#[cfg(feature = "test_go_interop")]
use libp2p::{Multiaddr, PeerId};
#[cfg(feature = "test_go_interop")]
use rand::prelude::*;
#[cfg(feature = "test_go_interop")]
use serde::Deserialize;
#[cfg(feature = "test_go_interop")]
use std::time::Duration;
#[cfg(feature = "test_go_interop")]
use std::{
env, fs,
path::PathBuf,
process::{Child, Command, Stdio},
thread,
};
pub mod go {
use libp2p::{Multiaddr, PeerId};
use rand::prelude::*;
use serde::Deserialize;
use std::time::Duration;
use std::{
env, fs,
path::PathBuf,
process::{Child, Command, Stdio},
thread,
};
#[cfg(feature = "test_go_interop")]
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct GoNodeId {
#[serde(rename = "ID")]
pub id: String,
#[serde(skip)]
pub public_key: String,
pub addresses: Vec<String>,
#[serde(skip)]
agent_version: String,
#[serde(skip)]
protocol_version: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct GoNodeId {
#[serde(rename = "ID")]
pub id: String,
#[serde(skip)]
pub public_key: String,
pub addresses: Vec<String>,
#[serde(skip)]
agent_version: String,
#[serde(skip)]
protocol_version: String,
}
#[cfg(feature = "test_go_interop")]
pub struct ForeignNode {
dir: PathBuf,
daemon: Child,
pub id: PeerId,
pub addrs: Vec<Multiaddr>,
}
pub struct ForeignNode {
dir: PathBuf,
daemon: Child,
pub id: PeerId,
pub addrs: Vec<Multiaddr>,
}
impl ForeignNode {
#[cfg(feature = "test_go_interop")]
pub fn new() -> ForeignNode {
// GO_IPFS_PATH should point to the location of the go-ipfs binary
let go_ipfs_path = env::vars()
.find(|(key, _val)| key == "GO_IPFS_PATH")
.expect("the GO_IPFS_PATH environment variable was not found")
.1;
impl ForeignNode {
pub fn new() -> ForeignNode {
// GO_IPFS_PATH should point to the location of the go-ipfs binary
let go_ipfs_path = env::vars()
.find(|(key, _val)| key == "GO_IPFS_PATH")
.expect("the GO_IPFS_PATH environment variable was not found")
.1;
let mut tmp_dir = env::temp_dir();
let mut rng = rand::thread_rng();
tmp_dir.push(&format!("ipfs_test_{}", rng.gen::<u64>()));
let _ = fs::create_dir(&tmp_dir);
let mut tmp_dir = env::temp_dir();
let mut rng = rand::thread_rng();
tmp_dir.push(&format!("ipfs_test_{}", rng.gen::<u64>()));
let _ = fs::create_dir(&tmp_dir);
Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("init")
.arg("-p")
.arg("test")
.arg("--bits")
.arg("2048")
.stdout(Stdio::null())
.status()
.unwrap();
Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("init")
.arg("-p")
.arg("test")
.arg("--bits")
.arg("2048")
.stdout(Stdio::null())
.status()
.unwrap();
let daemon = Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("daemon")
.stdout(Stdio::null())
.spawn()
.unwrap();
let daemon = Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("daemon")
.stdout(Stdio::null())
.spawn()
.unwrap();
// give the go-ipfs daemon a little bit of time to start
thread::sleep(Duration::from_secs(1));
// give the go-ipfs daemon a little bit of time to start
thread::sleep(Duration::from_secs(1));
let go_id = Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("id")
.output()
.unwrap()
.stdout;
let go_id = Command::new(&go_ipfs_path)
.env("IPFS_PATH", &tmp_dir)
.arg("id")
.output()
.unwrap()
.stdout;
let go_id_stdout = String::from_utf8_lossy(&go_id);
let go_id: GoNodeId = serde_json::de::from_str(&go_id_stdout).unwrap();
let go_id_stdout = String::from_utf8_lossy(&go_id);
let go_id: GoNodeId = serde_json::de::from_str(&go_id_stdout).unwrap();
let id = go_id.id.parse().unwrap();
let addrs = go_id
.addresses
.into_iter()
.map(|a| a.parse().unwrap())
.collect();
let id = go_id.id.parse().unwrap();
let addrs = go_id
.addresses
.into_iter()
.map(|a| a.parse().unwrap())
.collect();
ForeignNode {
dir: tmp_dir,
daemon,
id,
addrs,
ForeignNode {
dir: tmp_dir,
daemon,
id,
addrs,
}
}
}
impl Drop for ForeignNode {
fn drop(&mut self) {
let _ = self.daemon.kill();
let _ = fs::remove_dir_all(&self.dir);
}
}
}
#[cfg(not(feature = "test_go_interop"))]
pub struct ForeignNode;
#[cfg(feature = "test_go_interop")]
impl Drop for ForeignNode {
fn drop(&mut self) {
let _ = self.daemon.kill();
let _ = fs::remove_dir_all(&self.dir);
}
pub mod none {
pub struct ForeignNode;
}

View File

@ -7,7 +7,10 @@ use tokio::time::timeout;
use std::{convert::TryInto, time::Duration};
mod common;
use common::interop::ForeignNode;
#[cfg(feature = "test_go_interop")]
use common::interop::go::ForeignNode;
#[cfg(not(feature = "test_go_interop"))]
use common::interop::none::ForeignNode;
fn strip_peer_id(addr: Multiaddr) -> Multiaddr {
let MultiaddrWithPeerId { multiaddr, .. } = addr.try_into().unwrap();