-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENG-578: Refactor tests. Remove testnet
- Loading branch information
Showing
5 changed files
with
122 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,71 @@ | ||
// Copyright 2022-2024 Protocol Labs | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use std::{env::current_dir, time::Duration}; | ||
use std::{env::current_dir, path::PathBuf, pin::Pin, time::Duration}; | ||
|
||
use fendermint_testing_materializer::{docker::DockerMaterializer, testnet::Testnet}; | ||
use anyhow::Context; | ||
use fendermint_testing_materializer::{ | ||
docker::{DockerMaterializer, DockerMaterials}, | ||
manifest::Manifest, | ||
testnet::Testnet, | ||
TestnetName, | ||
}; | ||
use futures::Future; | ||
|
||
mod manifests { | ||
use fendermint_testing_materializer::manifest::Manifest; | ||
|
||
pub const ROOT_ONLY: &str = include_str!("../manifests/root-only.yaml"); | ||
|
||
pub fn parse_yaml(yaml: &str) -> Manifest { | ||
serde_yaml::from_str(yaml).expect("failed to parse manifest") | ||
} | ||
/// Want to keep the testnet artifacts in the `tests/testnets` directory. | ||
fn materializer_dir() -> PathBuf { | ||
let dir = current_dir().unwrap(); | ||
debug_assert!( | ||
dir.ends_with("materializer"), | ||
"expected the current directory to be the crate" | ||
); | ||
dir | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_root_only() { | ||
let manifest = manifests::parse_yaml(manifests::ROOT_ONLY); | ||
/// Parse a manifest file in the `manifests` directory, clean up any corresponding | ||
/// testnet resources, then materialize a testnet and run some tests. | ||
async fn with_testnet<F>(manifest_name: &str, f: F) -> anyhow::Result<()> | ||
where | ||
F: FnOnce( | ||
Testnet<DockerMaterials, DockerMaterializer>, | ||
Manifest, | ||
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>>>>, | ||
{ | ||
let root = materializer_dir(); | ||
let manifest = root.join("manifests").join(format!("{manifest_name}.yaml")); | ||
let manifest = std::fs::read_to_string(manifest).context("failed to read manifest")?; | ||
let manifest = serde_yaml::from_str(&manifest).context("failed to parse manifest")?; | ||
|
||
let testnet_name = TestnetName::new(manifest_name); | ||
|
||
// The current directory should be this crate. | ||
let root_dir = current_dir().unwrap().join("tests"); | ||
let mut materializer = DockerMaterializer::new(&root_dir, 0).unwrap(); | ||
let mut materializer = DockerMaterializer::new(&root, 0).unwrap(); | ||
materializer | ||
.remove(&testnet_name) | ||
.await | ||
.context("failed to remove testnet")?; | ||
|
||
let testnet = Testnet::setup(&mut materializer, "test-root-only", &manifest) | ||
let testnet = Testnet::setup(&mut materializer, &testnet_name, &manifest) | ||
.await | ||
.unwrap(); | ||
.context("failed to set up testnet")?; | ||
|
||
let node1 = testnet.root().node("node-1"); | ||
let _dnode1 = testnet.node(&node1).unwrap(); | ||
let res = f(testnet, manifest).await; | ||
|
||
tokio::time::sleep(Duration::from_secs(60)).await; | ||
// Allow some time for resources to be freed. | ||
// TODO: Remove the runtime handle becuase as it goes out of scope it causes panics. | ||
tokio::time::sleep(Duration::from_secs(1)).await; | ||
|
||
res | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_root_only() { | ||
with_testnet("root-only", |testnet, _manifest| { | ||
Box::pin(async move { | ||
let node1 = testnet.root().node("node-1"); | ||
let _dnode1 = testnet.node(&node1)?; | ||
Ok(()) | ||
}) | ||
}) | ||
.await | ||
.unwrap() | ||
} |