mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Use dockertest for the newly added serai-client-serai test
This commit is contained in:
25
tests/substrate/Cargo.toml
Normal file
25
tests/substrate/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "serai-substrate-tests"
|
||||
version = "0.1.0"
|
||||
description = "Tests for Serai's node"
|
||||
license = "AGPL-3.0-only"
|
||||
repository = "https://github.com/serai-dex/serai/tree/develop/tests/substrate"
|
||||
authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
||||
keywords = []
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
rustdoc-args = ["--cfg", "docsrs"]
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
serai-client-serai = { path = "../../substrate/client/serai" }
|
||||
|
||||
tokio = { version = "1", features = ["time"] }
|
||||
|
||||
dockertest = "0.5"
|
||||
serai-docker-tests = { path = "../docker" }
|
||||
15
tests/substrate/LICENSE
Normal file
15
tests/substrate/LICENSE
Normal file
@@ -0,0 +1,15 @@
|
||||
AGPL-3.0-only license
|
||||
|
||||
Copyright (c) 2023-2025 Luke Parker
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License Version 3 as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
42
tests/substrate/src/lib.rs
Normal file
42
tests/substrate/src/lib.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use serai_client_serai::Serai;
|
||||
|
||||
use dockertest::{StartPolicy, PullPolicy, Image, TestBodySpecification, DockerOperations};
|
||||
|
||||
pub struct Handle(String);
|
||||
|
||||
pub fn composition(name: &str, logs_path: String) -> (TestBodySpecification, Handle) {
|
||||
let handle = serai_docker_tests::handle(&format!("serai-{name}"));
|
||||
serai_docker_tests::build("serai".to_string());
|
||||
(
|
||||
TestBodySpecification::with_image(
|
||||
Image::with_repository("serai-dev-serai").pull_policy(PullPolicy::Never),
|
||||
)
|
||||
.replace_env(
|
||||
[("SERAI_NAME".to_string(), name.to_lowercase()), ("KEY".to_string(), " ".to_string())]
|
||||
.into(),
|
||||
)
|
||||
.set_start_policy(StartPolicy::Strict)
|
||||
.set_publish_all_ports(true)
|
||||
.set_handle(handle.clone())
|
||||
.set_log_options(Some(serai_docker_tests::log_options(logs_path))),
|
||||
Handle(handle),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn rpc(ops: &DockerOperations, handle: Handle) -> Serai {
|
||||
let serai_rpc = ops.handle(&handle.0).host_port(9944).unwrap();
|
||||
let serai_rpc = format!("http://{}:{}", serai_rpc.0, serai_rpc.1);
|
||||
|
||||
// If the RPC server has yet to start, sleep for up to 60s until it does
|
||||
let client = Serai::new(serai_rpc.clone()).unwrap();
|
||||
for _ in 0 .. 180 {
|
||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||
if client.block_by_number(0).await.is_err() {
|
||||
continue;
|
||||
}
|
||||
return client;
|
||||
}
|
||||
panic!("serai RPC server wasn't available after 60s");
|
||||
}
|
||||
Reference in New Issue
Block a user