Use random ports in the acceptance tests
This change avoids errors when tests are cancelled and re-run within a short period of time, for example, using `cargo watch`. It introduces a slight risk of port conflicts between the endpoint tests, and with (ephemeral) ports used by other services. The risk of conflicts across 2 tests is very low, and tests should be run in an isolated environment on busy servers.
This commit is contained in:
parent
5f8891df88
commit
1b6bf7f105
|
|
@ -23,7 +23,7 @@ use color_eyre::eyre::Result;
|
||||||
use eyre::WrapErr;
|
use eyre::WrapErr;
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
|
|
||||||
use std::{env, fs, io::Write, path::Path, path::PathBuf, time::Duration};
|
use std::{convert::TryInto, env, fs, io::Write, path::Path, path::PathBuf, time::Duration};
|
||||||
|
|
||||||
use zebra_chain::{
|
use zebra_chain::{
|
||||||
block::Height,
|
block::Height,
|
||||||
|
|
@ -739,15 +739,27 @@ fn sync_past_sapling_testnet() {
|
||||||
sync_past_sapling(network).unwrap();
|
sync_past_sapling(network).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a random port
|
||||||
|
///
|
||||||
|
/// Does not check if the port is already in use. It's impossible to do this
|
||||||
|
/// check in a reliable, cross-platform way.
|
||||||
|
fn random_port() -> u16 {
|
||||||
|
// Use the intersection of the IANA ephemeral port range, and the Linux
|
||||||
|
// ephemeral port range:
|
||||||
|
// https://en.wikipedia.org/wiki/Ephemeral_port#Range
|
||||||
|
rand::thread_rng().gen_range(49152, 60999)
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn metrics_endpoint() -> Result<()> {
|
async fn metrics_endpoint() -> Result<()> {
|
||||||
use hyper::{Client, Uri};
|
use hyper::Client;
|
||||||
|
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
||||||
// [Note on port conflict](#Note on port conflict)
|
// [Note on port conflict](#Note on port conflict)
|
||||||
let endpoint = "127.0.0.1:50001";
|
let port = random_port();
|
||||||
let url = "http://127.0.0.1:50001";
|
let endpoint = format!("127.0.0.1:{}", port);
|
||||||
|
let url = format!("http://{}", endpoint);
|
||||||
|
|
||||||
// Write a configuration that has metrics endpoint_addr set
|
// Write a configuration that has metrics endpoint_addr set
|
||||||
let mut config = default_test_config()?;
|
let mut config = default_test_config()?;
|
||||||
|
|
@ -766,7 +778,7 @@ async fn metrics_endpoint() -> Result<()> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
// Test metrics endpoint
|
// Test metrics endpoint
|
||||||
let res = client.get(Uri::from_static(url)).await?;
|
let res = client.get(url.try_into().expect("url is valid")).await?;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
let body = hyper::body::to_bytes(res).await?;
|
let body = hyper::body::to_bytes(res).await?;
|
||||||
assert!(
|
assert!(
|
||||||
|
|
@ -794,14 +806,15 @@ async fn metrics_endpoint() -> Result<()> {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn tracing_endpoint() -> Result<()> {
|
async fn tracing_endpoint() -> Result<()> {
|
||||||
use hyper::{Body, Client, Request, Uri};
|
use hyper::{Body, Client, Request};
|
||||||
|
|
||||||
zebra_test::init();
|
zebra_test::init();
|
||||||
|
|
||||||
// [Note on port conflict](#Note on port conflict)
|
// [Note on port conflict](#Note on port conflict)
|
||||||
let endpoint = "127.0.0.1:50002";
|
let port = random_port();
|
||||||
let url_default = "http://127.0.0.1:50002";
|
let endpoint = format!("127.0.0.1:{}", port);
|
||||||
let url_filter = "http://127.0.0.1:50002/filter";
|
let url_default = format!("http://{}", endpoint);
|
||||||
|
let url_filter = format!("{}/filter", url_default);
|
||||||
|
|
||||||
// Write a configuration that has tracing endpoint_addr option set
|
// Write a configuration that has tracing endpoint_addr option set
|
||||||
let mut config = default_test_config()?;
|
let mut config = default_test_config()?;
|
||||||
|
|
@ -820,7 +833,9 @@ async fn tracing_endpoint() -> Result<()> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
// Test tracing endpoint
|
// Test tracing endpoint
|
||||||
let res = client.get(Uri::from_static(url_default)).await?;
|
let res = client
|
||||||
|
.get(url_default.try_into().expect("url_default is valid"))
|
||||||
|
.await?;
|
||||||
assert!(res.status().is_success());
|
assert!(res.status().is_success());
|
||||||
let body = hyper::body::to_bytes(res).await?;
|
let body = hyper::body::to_bytes(res).await?;
|
||||||
assert!(std::str::from_utf8(&body).unwrap().contains(
|
assert!(std::str::from_utf8(&body).unwrap().contains(
|
||||||
|
|
@ -828,12 +843,14 @@ async fn tracing_endpoint() -> Result<()> {
|
||||||
));
|
));
|
||||||
|
|
||||||
// Set a filter and make sure it was changed
|
// Set a filter and make sure it was changed
|
||||||
let request = Request::post(url_filter)
|
let request = Request::post(url_filter.clone())
|
||||||
.body(Body::from("zebrad=debug"))
|
.body(Body::from("zebrad=debug"))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let _post = client.request(request).await?;
|
let _post = client.request(request).await?;
|
||||||
|
|
||||||
let tracing_res = client.get(Uri::from_static(url_filter)).await?;
|
let tracing_res = client
|
||||||
|
.get(url_filter.try_into().expect("url_filter is valid"))
|
||||||
|
.await?;
|
||||||
assert!(tracing_res.status().is_success());
|
assert!(tracing_res.status().is_success());
|
||||||
let tracing_body = hyper::body::to_bytes(tracing_res).await?;
|
let tracing_body = hyper::body::to_bytes(tracing_res).await?;
|
||||||
assert!(std::str::from_utf8(&tracing_body)
|
assert!(std::str::from_utf8(&tracing_body)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue