Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agave-validator: add args tests for set-public-address #4922

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion validator/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
.subcommand(commands::set_log_filter::command(default_args))
.subcommand(commands::staked_nodes_overrides::command(default_args))
.subcommand(commands::wait_for_restart_window::command())
.subcommand(commands::set_public_address::command(default_args));
.subcommand(commands::set_public_address::command());

commands::run::add_args(app, default_args)
.args(&thread_args(&default_args.thread_args))
Expand Down
116 changes: 98 additions & 18 deletions validator/src/commands/set_public_address/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
use {
crate::{admin_rpc_service, cli::DefaultArgs},
crate::{admin_rpc_service, commands::FromClapArgMatches},
clap::{App, Arg, ArgGroup, ArgMatches, SubCommand},
std::{net::SocketAddr, path::Path},
};

pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> {
SubCommand::with_name("set-public-address")
const COMMAND: &str = "set-public-address";

#[derive(Debug, PartialEq)]
pub struct SetPublicAddressArgs {
pub tpu_addr: Option<SocketAddr>,
pub tpu_forwards_addr: Option<SocketAddr>,
}

impl FromClapArgMatches for SetPublicAddressArgs {
fn from_clap_arg_match(matches: &ArgMatches) -> Result<Self, String> {
let parse_arg_addr = |arg_name: &str,
arg_long: &str|
-> Result<Option<SocketAddr>, String> {
matches.value_of(arg_name).map(|host_port| {
solana_net_utils::parse_host_port(host_port).map_err(|err| {
format!(
"failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
)
})
})
.transpose()
};
Ok(SetPublicAddressArgs {
tpu_addr: parse_arg_addr("tpu_addr", "tpu")?,
tpu_forwards_addr: parse_arg_addr("tpu_forwards_addr", "tpu-forwards")?,
})
}
}

pub fn command<'a>() -> App<'a, 'a> {
SubCommand::with_name(COMMAND)
.about("Specify addresses to advertise in gossip")
.arg(
Arg::with_name("tpu_addr")
Expand Down Expand Up @@ -33,18 +62,7 @@ pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> {
}

pub fn execute(matches: &ArgMatches, ledger_path: &Path) -> Result<(), String> {
let parse_arg_addr = |arg_name: &str, arg_long: &str| -> Result<Option<SocketAddr>, String> {
matches.value_of(arg_name).map(|host_port| {
solana_net_utils::parse_host_port(host_port).map_err(|err| {
format!(
"failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
)
})
})
.transpose()
};
let tpu_addr = parse_arg_addr("tpu_addr", "tpu")?;
let tpu_forwards_addr = parse_arg_addr("tpu_forwards_addr", "tpu-forwards")?;
let set_public_address_args = SetPublicAddressArgs::from_clap_arg_match(matches)?;

macro_rules! set_public_address {
($public_addr:expr, $set_public_address:ident, $request:literal) => {
Expand All @@ -60,10 +78,72 @@ pub fn execute(matches: &ArgMatches, ledger_path: &Path) -> Result<(), String> {
}
};
}
set_public_address!(tpu_addr, set_public_tpu_address, "set public tpu address")?;
set_public_address!(
tpu_forwards_addr,
set_public_address_args.tpu_addr,
set_public_tpu_address,
"setPublicTpuAddress"
)?;
set_public_address!(
set_public_address_args.tpu_forwards_addr,
set_public_tpu_forwards_address,
"set public tpu forwards address"
)
)?;
Ok(())
}

#[cfg(test)]
mod tests {
use {
super::*,
crate::commands::tests::{
verify_args_struct_by_command, verify_args_struct_by_command_is_error,
},
};

#[test]
fn verify_args_struct_by_command_set_public_default() {
verify_args_struct_by_command_is_error::<SetPublicAddressArgs>(command(), vec![COMMAND]);
}

#[test]
fn verify_args_struct_by_command_set_public_address_tpu() {
verify_args_struct_by_command(
command(),
vec![COMMAND, "--tpu", "127.0.0.1:8080"],
SetPublicAddressArgs {
tpu_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8080))),
tpu_forwards_addr: None,
},
);
}

#[test]
fn verify_args_struct_by_command_set_public_address_tpu_forwards() {
verify_args_struct_by_command(
command(),
vec![COMMAND, "--tpu-forwards", "127.0.0.1:8081"],
SetPublicAddressArgs {
tpu_addr: None,
tpu_forwards_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8081))),
},
);
}

#[test]
fn verify_args_struct_by_command_set_public_address_tpu_and_tpu_forwards() {
verify_args_struct_by_command(
command(),
vec![
COMMAND,
"--tpu",
"127.0.0.1:8080",
"--tpu-forwards",
"127.0.0.1:8081",
],
SetPublicAddressArgs {
tpu_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8080))),
tpu_forwards_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8081))),
},
);
}
}
Loading