Use dockertest for the newly added serai-client-serai test

This commit is contained in:
Luke Parker
2025-11-07 02:06:34 -05:00
parent ce08fad931
commit 4653ef4a61
12 changed files with 144 additions and 33 deletions

View 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
View 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/>.

View 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");
}