From d8629b29402f16205bb13a2ce063be8e7fbc0b8c Mon Sep 17 00:00:00 2001 From: qima Date: Mon, 13 May 2024 22:05:02 +0800 Subject: [PATCH] chore: use const for default user or owner --- sn_node_manager/src/bin/cli/main.rs | 5 +++-- sn_node_manager/src/lib.rs | 3 +++ sn_node_manager/src/local.rs | 11 +++++++---- sn_node_manager/tests/daemon.rs | 6 ++---- sn_node_manager/tests/e2e.rs | 5 ++--- sn_node_manager/tests/upgrades.rs | 11 +++++------ sn_transfers/src/lib.rs | 3 +++ 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/sn_node_manager/src/bin/cli/main.rs b/sn_node_manager/src/bin/cli/main.rs index 295bad912f..8a5ae405dc 100644 --- a/sn_node_manager/src/bin/cli/main.rs +++ b/sn_node_manager/src/bin/cli/main.rs @@ -13,6 +13,7 @@ use sn_node_manager::{ cmd, VerbosityLevel, }; use sn_peers_acquisition::PeersArgs; +use sn_transfers::DEFAULT_NODE_OWNER; use std::{net::Ipv4Addr, path::PathBuf}; const DEFAULT_NODE_COUNT: u16 = 25; @@ -733,7 +734,7 @@ async fn main() -> Result<()> { skip_validation: _, owner, } => { - let owner = owner.unwrap_or("maidsafe_test".to_string()); + let owner = owner.unwrap_or(DEFAULT_NODE_OWNER.to_string()); cmd::local::join( build, count, @@ -762,7 +763,7 @@ async fn main() -> Result<()> { skip_validation: _, owner, } => { - let owner = owner.unwrap_or("maidsafe_test".to_string()); + let owner = owner.unwrap_or(DEFAULT_NODE_OWNER.to_string()); cmd::local::run( build, clean, diff --git a/sn_node_manager/src/lib.rs b/sn_node_manager/src/lib.rs index 128044a362..0c9937b3bf 100644 --- a/sn_node_manager/src/lib.rs +++ b/sn_node_manager/src/lib.rs @@ -50,6 +50,9 @@ use tracing::debug; pub const DAEMON_DEFAULT_PORT: u16 = 12500; pub const DAEMON_SERVICE_NAME: &str = "safenodemand"; + +pub const DEFAULT_CI_USER: &str = "runner"; + const RPC_START_UP_DELAY_MS: u64 = 3000; pub struct ServiceManager { diff --git a/sn_node_manager/src/local.rs b/sn_node_manager/src/local.rs index dc109aa0f8..d8fd5a1aa1 100644 --- a/sn_node_manager/src/local.rs +++ b/sn_node_manager/src/local.rs @@ -6,7 +6,10 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use crate::helpers::{get_bin_version, get_username}; +use crate::{ + helpers::{get_bin_version, get_username}, + DEFAULT_CI_USER, +}; use color_eyre::eyre::OptionExt; use color_eyre::{eyre::eyre, Result}; use colored::Colorize; @@ -18,7 +21,7 @@ use sn_service_management::{ rpc::{RpcActions, RpcClient}, FaucetServiceData, NodeRegistry, NodeServiceData, ServiceStatus, }; -use sn_transfers::get_faucet_data_dir; +use sn_transfers::{get_faucet_data_dir, DEFAULT_NODE_OWNER}; use std::{ net::{IpAddr, Ipv4Addr, SocketAddr}, path::PathBuf, @@ -299,8 +302,8 @@ pub async fn run_node( // We shall use the input or env parsed username, // as long as they are not default ones for tests. // Input user_name is prioritized than parsed env. - let user = match (get_username(), run_options.owner == *"maidsafe_test") { - (Ok(user), true) if user == *"runner" => { + let user = match (get_username(), run_options.owner == *DEFAULT_NODE_OWNER) { + (Ok(user), true) if user == *DEFAULT_CI_USER => { format!("node_{}", run_options.number) } (_, false) | (Err(_), true) => run_options.owner.clone(), diff --git a/sn_node_manager/tests/daemon.rs b/sn_node_manager/tests/daemon.rs index f6d3ed6f1d..91ea81292e 100644 --- a/sn_node_manager/tests/daemon.rs +++ b/sn_node_manager/tests/daemon.rs @@ -10,7 +10,7 @@ mod utils; use assert_cmd::Command; use color_eyre::eyre::{bail, eyre, OptionExt, Result}; -use sn_node_manager::DAEMON_DEFAULT_PORT; +use sn_node_manager::{DAEMON_DEFAULT_PORT, DEFAULT_CI_USER}; use sn_service_management::safenode_manager_proto::{ safe_node_manager_client::SafeNodeManagerClient, NodeServiceRestartRequest, }; @@ -24,8 +24,6 @@ use std::{ use tonic::Request; use utils::get_service_status; -const CI_USER: &str = "runner"; - /// These tests need to execute as the root user. /// /// They are intended to run on a CI-based environment with a fresh build agent because they will @@ -66,7 +64,7 @@ async fn restart_node() -> Result<()> { let mut cmd = Command::cargo_bin("safenode-manager")?; cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") diff --git a/sn_node_manager/tests/e2e.rs b/sn_node_manager/tests/e2e.rs index 017289243c..545dcf2b0f 100644 --- a/sn_node_manager/tests/e2e.rs +++ b/sn_node_manager/tests/e2e.rs @@ -8,6 +8,7 @@ use assert_cmd::Command; use libp2p_identity::PeerId; +use sn_node_manager::DEFAULT_CI_USER; use sn_service_management::{ServiceStatus, StatusSummary}; /// These tests need to execute as the root user. @@ -17,8 +18,6 @@ use sn_service_management::{ServiceStatus, StatusSummary}; /// /// If you run them on your own dev machine, do so at your own risk! -const CI_USER: &str = "runner"; - /// The default behaviour is for the service to run as the `safe` user, which gets created during /// the process. However, there seems to be some sort of issue with adding user accounts on the GHA /// build agent, so we will just tell it to use the `runner` user, which is the account for the @@ -30,7 +29,7 @@ fn cross_platform_service_install_and_control() { let mut cmd = Command::cargo_bin("safenode-manager").unwrap(); cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") diff --git a/sn_node_manager/tests/upgrades.rs b/sn_node_manager/tests/upgrades.rs index 5691c0b5fc..701952c434 100644 --- a/sn_node_manager/tests/upgrades.rs +++ b/sn_node_manager/tests/upgrades.rs @@ -10,11 +10,10 @@ mod utils; use assert_cmd::Command; use color_eyre::Result; +use sn_node_manager::DEFAULT_CI_USER; use sn_releases::{ReleaseType, SafeReleaseRepoActions}; use utils::get_service_status; -const CI_USER: &str = "runner"; - /// These tests need to execute as the root user. /// /// They are intended to run on a CI-based environment with a fresh build agent because they will @@ -29,7 +28,7 @@ async fn upgrade_to_latest_version() -> Result<()> { let mut cmd = Command::cargo_bin("safenode-manager")?; cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") @@ -85,7 +84,7 @@ async fn force_upgrade_when_two_binaries_have_the_same_version() -> Result<()> { let mut cmd = Command::cargo_bin("safenode-manager")?; cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") @@ -145,7 +144,7 @@ async fn force_downgrade_to_a_previous_version() -> Result<()> { let mut cmd = Command::cargo_bin("safenode-manager")?; cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") @@ -205,7 +204,7 @@ async fn upgrade_from_older_version_to_specific_version() -> Result<()> { let mut cmd = Command::cargo_bin("safenode-manager")?; cmd.arg("add") .arg("--user") - .arg(CI_USER) + .arg(DEFAULT_CI_USER) .arg("--count") .arg("3") .arg("--peer") diff --git a/sn_transfers/src/lib.rs b/sn_transfers/src/lib.rs index 91b392684f..aa752ea37a 100644 --- a/sn_transfers/src/lib.rs +++ b/sn_transfers/src/lib.rs @@ -17,6 +17,9 @@ mod wallet; pub(crate) use cashnotes::{Input, TransactionBuilder}; +/// Default value as a node owner +pub const DEFAULT_NODE_OWNER: &str = "maidsafe_test"; + /// Types used in the public API pub use cashnotes::{ CashNote, DerivationIndex, DerivedSecretKey, Hash, MainPubkey, MainSecretKey, NanoTokens,