Skip to content

Commit e63897c

Browse files
committed
refactor: removed redundant code
1 parent fa3d009 commit e63897c

File tree

5 files changed

+219
-260
lines changed

5 files changed

+219
-260
lines changed

substrate/zombienet/zombienet-sdk/tests/utils.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
// Copyright (C) Parity Technologies (UK) Ltd.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use std::time::Duration;
5+
6+
use zombienet_orchestrator::network::node::LogLineCountOptions;
47
use zombienet_sdk::{LocalFileSystem, Network, NetworkConfig};
58

69
pub const BEST_BLOCK_METRIC: &str = "block_height{status=\"best\"}";
710
pub const FINALIZED_BLOCK_METRIC: &str = "substrate_block_height{status=\"finalized\"}";
811
pub const BEEFY_BEST_BLOCK_METRIC: &str = "substrate_beefy_best_block";
912
pub const DEFAULT_SUBSTRATE_IMAGE: &str = "docker.io/paritypr/substrate:latest";
13+
pub const NODE_ROLE_METRIC: &str = "node_roles";
14+
pub const PEER_COUNT_METRIC: &str = "substrate_sub_libp2p_peers_count";
1015

1116
pub const DEFAULT_DB_SNAPSHOT_URL: &str =
1217
"https://storage.googleapis.com/zombienet-db-snaps/substrate/0001-basic-warp-sync/chains-0bb3f0be2ce41b5615b224215bcc8363aa0416a6.tgz";
13-
pub const DEFAULT_CHAIN_SPEC: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/chain-spec.json");
18+
pub const DEFAULT_CHAIN_SPEC: &str =
19+
"https://storage.googleapis.com/zombienet-db-snaps/substrate/chain-spec.json";
20+
21+
pub const FULLNODE_ROLE_VALUE: f64 = 1.0;
22+
pub const VALIDATOR_ROLE_VALUE: f64 = 4.0;
23+
24+
pub const INTEGRATION_IMAGE_ENV: &str = "ZOMBIENET_INTEGRATION_TEST_IMAGE";
25+
pub const DB_SNAPSHOT_ENV: &str = "DB_SNAPSHOT";
26+
pub const CHAIN_SPEC_ENV: &str = "WARP_CHAIN_SPEC_PATH";
27+
pub const DB_BLOCK_HEIGHT_ENV: &str = "DB_BLOCK_HEIGHT";
1428

1529
pub async fn initialize_network(
1630
config: NetworkConfig,
@@ -27,3 +41,46 @@ pub async fn initialize_network(
2741

2842
Ok(network)
2943
}
44+
45+
pub fn ensure_env_default(var: &str, default: &str) {
46+
if std::env::var(var).is_err() {
47+
std::env::set_var(var, default);
48+
}
49+
}
50+
51+
pub fn ensure_env_defaults(defaults: &[(&str, &str)]) {
52+
for &(var, default_value) in defaults {
53+
ensure_env_default(var, default_value);
54+
}
55+
}
56+
57+
pub fn db_snapshot_height_override_from_env() -> Option<f64> {
58+
std::env::var(DB_BLOCK_HEIGHT_ENV)
59+
.ok()
60+
.and_then(|value| value.parse::<f64>().ok())
61+
}
62+
63+
pub async fn resolve_db_snapshot_height(
64+
network: &Network<LocalFileSystem>,
65+
node_name: &str,
66+
) -> anyhow::Result<f64> {
67+
if let Some(override_height) = db_snapshot_height_override_from_env() {
68+
return Ok(override_height);
69+
}
70+
71+
let node = network.get_node(node_name)?;
72+
let height = node.reports(BEST_BLOCK_METRIC).await?;
73+
Ok(height)
74+
}
75+
76+
pub fn log_line_at_least_once(timeout_secs: u64) -> LogLineCountOptions {
77+
LogLineCountOptions::new(|count| count >= 1, Duration::from_secs(timeout_secs), false)
78+
}
79+
80+
pub fn log_line_exactly_once(timeout_secs: u64) -> LogLineCountOptions {
81+
LogLineCountOptions::new(|count| count == 1, Duration::from_secs(timeout_secs), false)
82+
}
83+
84+
pub fn log_line_absent(timeout_secs: u64) -> LogLineCountOptions {
85+
LogLineCountOptions::no_occurences_within_timeout(Duration::from_secs(timeout_secs))
86+
}

substrate/zombienet/zombienet-sdk/tests/zombie_ci/basic_warp_sync.rs

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
// Copyright (C) Parity Technologies (UK) Ltd.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::time::Duration;
5-
64
use crate::utils::{
7-
initialize_network, BEST_BLOCK_METRIC, DEFAULT_CHAIN_SPEC, DEFAULT_DB_SNAPSHOT_URL,
5+
ensure_env_defaults, initialize_network, log_line_absent, log_line_at_least_once,
6+
resolve_db_snapshot_height, BEST_BLOCK_METRIC, CHAIN_SPEC_ENV, DB_SNAPSHOT_ENV,
7+
DEFAULT_CHAIN_SPEC, DEFAULT_DB_SNAPSHOT_URL, DEFAULT_SUBSTRATE_IMAGE, FULLNODE_ROLE_VALUE,
8+
INTEGRATION_IMAGE_ENV, NODE_ROLE_METRIC, PEER_COUNT_METRIC,
89
};
9-
use anyhow::{anyhow, Result};
10+
use anyhow::{anyhow, Context, Result};
1011
use env_logger::Env;
11-
use zombienet_orchestrator::network::node::LogLineCountOptions;
1212
use zombienet_sdk::{NetworkConfig, NetworkConfigBuilder, NetworkNode};
1313

1414
const BEST_BLOCK_THRESHOLD: f64 = 1.0;
1515
const PEERS_THRESHOLD: f64 = 3.0;
16-
const VALIDATOR_ROLE_VALUE: f64 = 1.0;
1716

1817
const NETWORK_READY_TIMEOUT_SECS: u64 = 180;
1918
const ROLE_TIMEOUT_SECS: u64 = 60;
@@ -22,35 +21,36 @@ const METRIC_TIMEOUT_SECS: u64 = 60;
2221
const LOG_TIMEOUT_LONG_SECS: u64 = 60;
2322
const LOG_TIMEOUT_SHORT_SECS: u64 = 10;
2423
const LOG_ERROR_TIMEOUT_SECS: u64 = 10;
25-
const NODE_ROLE_METRIC: &str = "node_roles";
26-
const PEER_COUNT_METRIC: &str = "substrate_sub_libp2p_peers_count";
27-
const NODE_NAMES: [&str; 4] = ["alice", "bob", "charlie", "dave"];
2824
const SNAPSHOT_NODES: [&str; 3] = ["alice", "bob", "charlie"];
29-
const INTEGRATION_IMAGE_ENV: &str = "ZOMBIENET_INTEGRATION_TEST_IMAGE";
30-
const DB_SNAPSHOT_ENV: &str = "DB_SNAPSHOT";
31-
const CHAIN_SPEC_ENV: &str = "WARP_CHAIN_SPEC_PATH";
32-
const DB_BLOCK_HEIGHT_ENV: &str = "DB_BLOCK_HEIGHT";
33-
const DEFAULT_SUBSTRATE_IMAGE: &str = "docker.io/paritypr/substrate:latest";
34-
25+
const NODE_ROLE_EXPECTATIONS: [(&str, f64); 4] = [
26+
("alice", FULLNODE_ROLE_VALUE),
27+
("bob", FULLNODE_ROLE_VALUE),
28+
("charlie", FULLNODE_ROLE_VALUE),
29+
("dave", FULLNODE_ROLE_VALUE),
30+
];
3531
#[tokio::test(flavor = "multi_thread")]
3632
async fn basic_warp_sync() -> Result<()> {
3733
let _ = env_logger::Builder::from_env(Env::default().default_filter_or("info")).try_init();
3834

39-
ensure_env_defaults();
35+
ensure_env_defaults(&[
36+
(INTEGRATION_IMAGE_ENV, DEFAULT_SUBSTRATE_IMAGE),
37+
(DB_SNAPSHOT_ENV, DEFAULT_DB_SNAPSHOT_URL),
38+
(CHAIN_SPEC_ENV, DEFAULT_CHAIN_SPEC),
39+
]);
4040

4141
log::info!("Spawning network");
4242
let config = build_network_config()?;
4343
let network = initialize_network(config).await?;
4444

4545
network.wait_until_is_up(NETWORK_READY_TIMEOUT_SECS).await?;
4646

47-
for node_name in NODE_NAMES {
47+
for &(node_name, expected_role) in NODE_ROLE_EXPECTATIONS.iter() {
4848
let node = network.get_node(node_name)?;
49-
assert_node_roles(node).await?;
49+
assert_node_roles(expected_role, node).await?;
5050
assert_peers_count(node).await?;
5151
}
5252

53-
let db_snapshot_height = resolve_db_snapshot_height(&network).await?;
53+
let db_snapshot_height = resolve_db_snapshot_height(&network, "alice").await?;
5454

5555
for node_name in SNAPSHOT_NODES {
5656
network
@@ -85,36 +85,6 @@ async fn basic_warp_sync() -> Result<()> {
8585
Ok(())
8686
}
8787

88-
fn ensure_env_defaults() {
89-
if std::env::var(INTEGRATION_IMAGE_ENV).is_err() {
90-
std::env::set_var(INTEGRATION_IMAGE_ENV, DEFAULT_SUBSTRATE_IMAGE);
91-
}
92-
if std::env::var(DB_SNAPSHOT_ENV).is_err() {
93-
std::env::set_var(DB_SNAPSHOT_ENV, DEFAULT_DB_SNAPSHOT_URL);
94-
}
95-
if std::env::var(CHAIN_SPEC_ENV).is_err() {
96-
std::env::set_var(CHAIN_SPEC_ENV, DEFAULT_CHAIN_SPEC);
97-
}
98-
}
99-
100-
fn db_snapshot_height_override() -> Option<f64> {
101-
std::env::var(DB_BLOCK_HEIGHT_ENV)
102-
.ok()
103-
.and_then(|value| value.parse::<f64>().ok())
104-
}
105-
106-
async fn resolve_db_snapshot_height(
107-
network: &zombienet_sdk::Network<zombienet_sdk::LocalFileSystem>,
108-
) -> Result<f64> {
109-
if let Some(override_height) = db_snapshot_height_override() {
110-
return Ok(override_height);
111-
}
112-
113-
let alice = network.get_node("alice")?;
114-
let height = alice.reports(BEST_BLOCK_METRIC).await?;
115-
Ok(height)
116-
}
117-
11888
fn build_network_config() -> Result<NetworkConfig> {
11989
let integration_image = std::env::var(INTEGRATION_IMAGE_ENV)
12090
.unwrap_or_else(|_| DEFAULT_SUBSTRATE_IMAGE.to_string());
@@ -159,13 +129,20 @@ fn build_network_config() -> Result<NetworkConfig> {
159129
})
160130
}
161131

162-
async fn assert_node_roles(node: &NetworkNode) -> Result<()> {
132+
async fn assert_node_roles(expected_role: f64, node: &NetworkNode) -> Result<()> {
133+
let node_name = node.name();
134+
163135
node.wait_metric_with_timeout(
164136
NODE_ROLE_METRIC,
165-
|role| (role - VALIDATOR_ROLE_VALUE).abs() < f64::EPSILON,
137+
|role| role == expected_role,
166138
ROLE_TIMEOUT_SECS,
167139
)
168-
.await?;
140+
.await
141+
.with_context(|| {
142+
format!(
143+
"node {node_name} did not expose expected role {expected_role} on metric {NODE_ROLE_METRIC}"
144+
)
145+
})?;
169146

170147
Ok(())
171148
}
@@ -182,53 +159,41 @@ async fn assert_peers_count(node: &NetworkNode) -> Result<()> {
182159
}
183160

184161
async fn wait_for_warp_logs(node: &NetworkNode) -> Result<()> {
185-
let at_least_once = |timeout_secs| {
186-
LogLineCountOptions::new(|count| count >= 1, Duration::from_secs(timeout_secs), false)
187-
};
188-
189162
node.wait_log_line_count_with_timeout(
190163
"Warp sync is complete",
191164
false,
192-
at_least_once(LOG_TIMEOUT_LONG_SECS),
165+
log_line_at_least_once(LOG_TIMEOUT_LONG_SECS),
193166
)
194167
.await?;
195168
node.wait_log_line_count_with_timeout(
196169
r"Checking for displaced leaves after finalization\. leaves=\[0xc5e7b4cfd23932bb930e859865430a35f6741b4732d677822d492ca64cc8d059\]",
197170
false,
198-
at_least_once(LOG_TIMEOUT_SHORT_SECS),
171+
log_line_at_least_once(LOG_TIMEOUT_SHORT_SECS),
199172
)
200173
.await?;
201174
node.wait_log_line_count_with_timeout(
202175
"State sync is complete",
203176
false,
204-
at_least_once(LOG_TIMEOUT_LONG_SECS),
177+
log_line_at_least_once(LOG_TIMEOUT_LONG_SECS),
205178
)
206179
.await?;
207180
node.wait_log_line_count_with_timeout(
208181
"Block history download is complete",
209182
false,
210-
at_least_once(LOG_TIMEOUT_SHORT_SECS),
183+
log_line_at_least_once(LOG_TIMEOUT_SHORT_SECS),
211184
)
212185
.await?;
213186

214187
Ok(())
215188
}
216189

217190
async fn wait_for_absence_of_errors(node: &NetworkNode) -> Result<()> {
218-
node.wait_log_line_count_with_timeout(
219-
"error",
220-
false,
221-
LogLineCountOptions::no_occurences_within_timeout(Duration::from_secs(
222-
LOG_ERROR_TIMEOUT_SECS,
223-
)),
224-
)
225-
.await?;
191+
node.wait_log_line_count_with_timeout("error", false, log_line_absent(LOG_ERROR_TIMEOUT_SECS))
192+
.await?;
226193
node.wait_log_line_count_with_timeout(
227194
"verification failed",
228195
false,
229-
LogLineCountOptions::no_occurences_within_timeout(Duration::from_secs(
230-
LOG_ERROR_TIMEOUT_SECS,
231-
)),
196+
log_line_absent(LOG_ERROR_TIMEOUT_SECS),
232197
)
233198
.await?;
234199

substrate/zombienet/zombienet-sdk/tests/zombie_ci/block_building.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,38 @@
33

44
use std::time::Duration;
55

6-
use crate::utils::{initialize_network, DEFAULT_CHAIN_SPEC, DEFAULT_SUBSTRATE_IMAGE};
6+
use crate::utils::{
7+
ensure_env_defaults, initialize_network, log_line_absent, BEST_BLOCK_METRIC, CHAIN_SPEC_ENV,
8+
DEFAULT_CHAIN_SPEC, DEFAULT_SUBSTRATE_IMAGE, INTEGRATION_IMAGE_ENV, NODE_ROLE_METRIC,
9+
PEER_COUNT_METRIC,
10+
};
711
use anyhow::{anyhow, Result};
812
use subxt::{config::substrate::SubstrateConfig, dynamic::tx, OnlineClient};
913
use subxt_signer::sr25519::dev;
10-
use zombienet_orchestrator::network::node::LogLineCountOptions;
1114
use zombienet_sdk::{NetworkConfig, NetworkConfigBuilder, NetworkNode};
1215

1316
const NODE_NAMES: [&str; 2] = ["alice", "bob"];
1417

15-
const NODE_ROLE_METRIC: &str = "node_roles";
16-
const PEER_COUNT_METRIC: &str = "substrate_sub_libp2p_peers_count";
17-
const BEST_BLOCK_METRIC: &str = "block_height{status=\"best\"}";
18-
1918
const ROLE_VALIDATOR_VALUE: f64 = 4.0;
2019
const PEER_MIN_THRESHOLD: f64 = 1.0;
2120
const BLOCK_TARGET: f64 = 5.0;
2221

2322
const NETWORK_READY_TIMEOUT_SECS: u64 = 60;
2423
const METRIC_TIMEOUT_SECS: u64 = 20;
2524
const LOG_TIMEOUT_SECS: u64 = 2;
26-
const SCRIPT_TIMEOUT_SECS: u64 = 30;
25+
const TRANSACTION_TIMEOUT_SECS: u64 = 30;
2726

2827
const REMARK_PAYLOAD: &[u8] = b"block-building-test";
29-
const INTEGRATION_IMAGE_ENV: &str = "ZOMBIENET_INTEGRATION_TEST_IMAGE";
30-
const CHAIN_SPEC_ENV: &str = "WARP_CHAIN_SPEC_PATH";
31-
3228
#[tokio::test(flavor = "multi_thread")]
3329
async fn block_building_test() -> Result<()> {
3430
let _ = env_logger::try_init_from_env(
3531
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
3632
);
3733

38-
ensure_env_defaults();
34+
ensure_env_defaults(&[
35+
(INTEGRATION_IMAGE_ENV, DEFAULT_SUBSTRATE_IMAGE),
36+
(CHAIN_SPEC_ENV, DEFAULT_CHAIN_SPEC),
37+
]);
3938

4039
log::info!("Spawning network");
4140
let config = build_network_config()?;
@@ -56,15 +55,6 @@ async fn block_building_test() -> Result<()> {
5655
Ok(())
5756
}
5857

59-
fn ensure_env_defaults() {
60-
if std::env::var(INTEGRATION_IMAGE_ENV).is_err() {
61-
std::env::set_var(INTEGRATION_IMAGE_ENV, DEFAULT_SUBSTRATE_IMAGE);
62-
}
63-
if std::env::var(CHAIN_SPEC_ENV).is_err() {
64-
std::env::set_var(CHAIN_SPEC_ENV, DEFAULT_CHAIN_SPEC);
65-
}
66-
}
67-
6858
fn build_network_config() -> Result<NetworkConfig> {
6959
let integration_image = std::env::var(INTEGRATION_IMAGE_ENV)
7060
.unwrap_or_else(|_| DEFAULT_SUBSTRATE_IMAGE.to_string());
@@ -101,7 +91,7 @@ async fn assert_node_health(node: &NetworkNode) -> Result<()> {
10191

10292
node.wait_metric_with_timeout(
10393
NODE_ROLE_METRIC,
104-
|role| (role - ROLE_VALIDATOR_VALUE).abs() < f64::EPSILON,
94+
|role| role == ROLE_VALIDATOR_VALUE,
10595
METRIC_TIMEOUT_SECS,
10696
)
10797
.await?;
@@ -120,12 +110,8 @@ async fn assert_node_health(node: &NetworkNode) -> Result<()> {
120110
)
121111
.await?;
122112

123-
node.wait_log_line_count_with_timeout(
124-
"error",
125-
false,
126-
LogLineCountOptions::no_occurences_within_timeout(Duration::from_secs(LOG_TIMEOUT_SECS)),
127-
)
128-
.await?;
113+
node.wait_log_line_count_with_timeout("error", false, log_line_absent(LOG_TIMEOUT_SECS))
114+
.await?;
129115

130116
Ok(())
131117
}
@@ -137,7 +123,7 @@ async fn submit_transaction_and_wait_finalization(node: &NetworkNode) -> Result<
137123
let remark_call =
138124
tx("System", "remark", vec![subxt::dynamic::Value::from_bytes(REMARK_PAYLOAD)]);
139125

140-
tokio::time::timeout(Duration::from_secs(SCRIPT_TIMEOUT_SECS), async {
126+
tokio::time::timeout(Duration::from_secs(TRANSACTION_TIMEOUT_SECS), async {
141127
client
142128
.tx()
143129
.sign_and_submit_then_watch_default(&remark_call, &signer)

0 commit comments

Comments
 (0)