Skip to content

Commit 18b49da

Browse files
authored
agave-validator: add args tests for set-public-address (#4922)
* agave-validator: add args tests for set-public-address * SetPublicAddressArg -> SetPublicAddressArgs * remove unused default args
1 parent 131bc18 commit 18b49da

File tree

2 files changed

+99
-19
lines changed

2 files changed

+99
-19
lines changed

validator/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn app<'a>(version: &'a str, default_args: &'a DefaultArgs) -> App<'a, 'a> {
7979
.subcommand(commands::set_log_filter::command())
8080
.subcommand(commands::staked_nodes_overrides::command(default_args))
8181
.subcommand(commands::wait_for_restart_window::command())
82-
.subcommand(commands::set_public_address::command(default_args));
82+
.subcommand(commands::set_public_address::command());
8383

8484
commands::run::add_args(app, default_args)
8585
.args(&thread_args(&default_args.thread_args))

validator/src/commands/set_public_address/mod.rs

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,40 @@
11
use {
2-
crate::{admin_rpc_service, cli::DefaultArgs},
2+
crate::{admin_rpc_service, commands::FromClapArgMatches},
33
clap::{App, Arg, ArgGroup, ArgMatches, SubCommand},
44
std::{net::SocketAddr, path::Path},
55
};
66

7-
pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> {
8-
SubCommand::with_name("set-public-address")
7+
const COMMAND: &str = "set-public-address";
8+
9+
#[derive(Debug, PartialEq)]
10+
pub struct SetPublicAddressArgs {
11+
pub tpu_addr: Option<SocketAddr>,
12+
pub tpu_forwards_addr: Option<SocketAddr>,
13+
}
14+
15+
impl FromClapArgMatches for SetPublicAddressArgs {
16+
fn from_clap_arg_match(matches: &ArgMatches) -> Result<Self, String> {
17+
let parse_arg_addr = |arg_name: &str,
18+
arg_long: &str|
19+
-> Result<Option<SocketAddr>, String> {
20+
matches.value_of(arg_name).map(|host_port| {
21+
solana_net_utils::parse_host_port(host_port).map_err(|err| {
22+
format!(
23+
"failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
24+
)
25+
})
26+
})
27+
.transpose()
28+
};
29+
Ok(SetPublicAddressArgs {
30+
tpu_addr: parse_arg_addr("tpu_addr", "tpu")?,
31+
tpu_forwards_addr: parse_arg_addr("tpu_forwards_addr", "tpu-forwards")?,
32+
})
33+
}
34+
}
35+
36+
pub fn command<'a>() -> App<'a, 'a> {
37+
SubCommand::with_name(COMMAND)
938
.about("Specify addresses to advertise in gossip")
1039
.arg(
1140
Arg::with_name("tpu_addr")
@@ -33,18 +62,7 @@ pub fn command(_default_args: &DefaultArgs) -> App<'_, '_> {
3362
}
3463

3564
pub fn execute(matches: &ArgMatches, ledger_path: &Path) -> Result<(), String> {
36-
let parse_arg_addr = |arg_name: &str, arg_long: &str| -> Result<Option<SocketAddr>, String> {
37-
matches.value_of(arg_name).map(|host_port| {
38-
solana_net_utils::parse_host_port(host_port).map_err(|err| {
39-
format!(
40-
"failed to parse --{arg_long} address. It must be in the HOST:PORT format. {err}"
41-
)
42-
})
43-
})
44-
.transpose()
45-
};
46-
let tpu_addr = parse_arg_addr("tpu_addr", "tpu")?;
47-
let tpu_forwards_addr = parse_arg_addr("tpu_forwards_addr", "tpu-forwards")?;
65+
let set_public_address_args = SetPublicAddressArgs::from_clap_arg_match(matches)?;
4866

4967
macro_rules! set_public_address {
5068
($public_addr:expr, $set_public_address:ident, $request:literal) => {
@@ -60,10 +78,72 @@ pub fn execute(matches: &ArgMatches, ledger_path: &Path) -> Result<(), String> {
6078
}
6179
};
6280
}
63-
set_public_address!(tpu_addr, set_public_tpu_address, "set public tpu address")?;
6481
set_public_address!(
65-
tpu_forwards_addr,
82+
set_public_address_args.tpu_addr,
83+
set_public_tpu_address,
84+
"setPublicTpuAddress"
85+
)?;
86+
set_public_address!(
87+
set_public_address_args.tpu_forwards_addr,
6688
set_public_tpu_forwards_address,
6789
"set public tpu forwards address"
68-
)
90+
)?;
91+
Ok(())
92+
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use {
97+
super::*,
98+
crate::commands::tests::{
99+
verify_args_struct_by_command, verify_args_struct_by_command_is_error,
100+
},
101+
};
102+
103+
#[test]
104+
fn verify_args_struct_by_command_set_public_default() {
105+
verify_args_struct_by_command_is_error::<SetPublicAddressArgs>(command(), vec![COMMAND]);
106+
}
107+
108+
#[test]
109+
fn verify_args_struct_by_command_set_public_address_tpu() {
110+
verify_args_struct_by_command(
111+
command(),
112+
vec![COMMAND, "--tpu", "127.0.0.1:8080"],
113+
SetPublicAddressArgs {
114+
tpu_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8080))),
115+
tpu_forwards_addr: None,
116+
},
117+
);
118+
}
119+
120+
#[test]
121+
fn verify_args_struct_by_command_set_public_address_tpu_forwards() {
122+
verify_args_struct_by_command(
123+
command(),
124+
vec![COMMAND, "--tpu-forwards", "127.0.0.1:8081"],
125+
SetPublicAddressArgs {
126+
tpu_addr: None,
127+
tpu_forwards_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8081))),
128+
},
129+
);
130+
}
131+
132+
#[test]
133+
fn verify_args_struct_by_command_set_public_address_tpu_and_tpu_forwards() {
134+
verify_args_struct_by_command(
135+
command(),
136+
vec![
137+
COMMAND,
138+
"--tpu",
139+
"127.0.0.1:8080",
140+
"--tpu-forwards",
141+
"127.0.0.1:8081",
142+
],
143+
SetPublicAddressArgs {
144+
tpu_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8080))),
145+
tpu_forwards_addr: Some(SocketAddr::from(([127, 0, 0, 1], 8081))),
146+
},
147+
);
148+
}
69149
}

0 commit comments

Comments
 (0)