zombienet_configuration/
utils.rs

1use std::env;
2
3use support::constants::ZOMBIE_NODE_SPAWN_TIMEOUT_SECONDS;
4
5use crate::types::{Chain, Command, Duration};
6
7pub(crate) fn is_true(value: &bool) -> bool {
8    *value
9}
10
11pub(crate) fn is_false(value: &bool) -> bool {
12    !(*value)
13}
14
15pub(crate) fn default_as_true() -> bool {
16    true
17}
18
19pub(crate) fn default_as_false() -> bool {
20    false
21}
22
23pub(crate) fn default_initial_balance() -> crate::types::U128 {
24    2_000_000_000_000.into()
25}
26
27/// Default timeout for spawning a node (10mins)
28pub(crate) fn default_node_spawn_timeout() -> Duration {
29    env::var(ZOMBIE_NODE_SPAWN_TIMEOUT_SECONDS)
30        .ok()
31        .and_then(|s| s.parse::<u32>().ok())
32        .unwrap_or(600)
33}
34
35/// Default timeout for spawning the whole network (1hr)
36pub(crate) fn default_timeout() -> Duration {
37    3600
38}
39
40pub(crate) fn default_command_polkadot() -> Option<Command> {
41    TryInto::<Command>::try_into("polkadot").ok()
42}
43
44pub(crate) fn default_relaychain_chain() -> Chain {
45    TryInto::<Chain>::try_into("rococo-local").expect("'rococo-local' should be a valid chain")
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn default_node_spawn_timeout_works_before_and_after_env_is_set() {
54        // The default should be 600 seconds if the env var is not set
55        assert_eq!(default_node_spawn_timeout(), 600);
56
57        // If env var is set to a valid number, it should return that number
58        env::set_var(ZOMBIE_NODE_SPAWN_TIMEOUT_SECONDS, "123");
59        assert_eq!(default_node_spawn_timeout(), 123);
60
61        // If env var is set to a NOT valid number, it should return 600
62        env::set_var(ZOMBIE_NODE_SPAWN_TIMEOUT_SECONDS, "NOT_A_NUMBER");
63        assert_eq!(default_node_spawn_timeout(), 600);
64    }
65}