refactor(config): unify runtime configuration management via ConfigRpc (#1397)

* refactor(config): unify runtime configuration management via ConfigRpc
* feat(tests): add config patch test and fix problem
This commit is contained in:
Mg Pig
2025-10-01 00:32:28 +08:00
committed by GitHub
parent 4d91582fd8
commit 020bf04ec4
16 changed files with 772 additions and 280 deletions

View File

@@ -147,6 +147,21 @@ fn check_route(ipv4: &str, dst_peer_id: PeerId, routes: Vec<crate::proto::cli::R
);
}
fn check_route_ex(
routes: Vec<crate::proto::cli::Route>,
peer_id: PeerId,
checker: impl Fn(&crate::proto::cli::Route) -> bool,
) {
let mut found = false;
for r in routes.iter() {
if r.peer_id == peer_id {
found = true;
assert!(checker(r), "{:?}", routes);
}
}
assert!(found, "routes: {:?}, dst_peer_id: {}", routes, peer_id);
}
async fn wait_proxy_route_appear(
mgr: &std::sync::Arc<PeerManager>,
ipv4: &str,

View File

@@ -2094,3 +2094,96 @@ pub async fn acl_group_based_test(
drop_insts(insts).await;
}
#[rstest::rstest]
#[tokio::test]
#[serial_test::serial]
pub async fn config_patch_test() {
use crate::proto::{
common::{PortForwardConfigPb, SocketType},
config::{ConfigPatchAction, InstanceConfigPatch, PortForwardPatch, ProxyNetworkPatch},
};
use crate::tunnel::common::tests::_tunnel_pingpong_netns_with_timeout;
let insts = init_three_node("udp").await;
check_route(
"10.144.144.2/24",
insts[1].peer_id(),
insts[0].get_peer_manager().list_routes().await,
);
check_route(
"10.144.144.3/24",
insts[2].peer_id(),
insts[0].get_peer_manager().list_routes().await,
);
// 测试1 修改hostname、ip、子网代理
let patch = InstanceConfigPatch {
hostname: Some("new_inst1".to_string()),
ipv4: Some("10.144.144.22/24".parse().unwrap()),
proxy_networks: vec![ProxyNetworkPatch {
action: ConfigPatchAction::Add as i32,
cidr: Some("10.144.145.0/24".parse().unwrap()),
mapped_cidr: None,
}],
..Default::default()
};
insts[1]
.get_config_patcher()
.apply_patch(patch)
.await
.unwrap();
assert_eq!(insts[1].get_global_ctx().get_hostname(), "new_inst1");
assert_eq!(
insts[1].get_global_ctx().get_ipv4().unwrap(),
"10.144.144.22/24".parse().unwrap()
);
tokio::time::sleep(Duration::from_secs(1)).await;
check_route_ex(
insts[0].get_peer_manager().list_routes().await,
insts[1].peer_id(),
|r| {
assert_eq!(r.hostname, "new_inst1");
assert_eq!(r.ipv4_addr, Some("10.144.144.22/24".parse().unwrap()));
assert_eq!(r.proxy_cidrs[0], "10.144.145.0/24");
true
},
);
// 测试2: 端口转发
let patch = InstanceConfigPatch {
port_forwards: vec![PortForwardPatch {
action: ConfigPatchAction::Add as i32,
cfg: Some(PortForwardConfigPb {
bind_addr: Some("0.0.0.0:23458".parse::<SocketAddr>().unwrap().into()),
dst_addr: Some("10.144.144.3:23457".parse::<SocketAddr>().unwrap().into()),
socket_type: SocketType::Tcp as i32,
}),
}],
..Default::default()
};
insts[0]
.get_config_patcher()
.apply_patch(patch)
.await
.unwrap();
let mut buf = vec![0; 32];
rand::thread_rng().fill(&mut buf[..]);
let tcp_listener = TcpTunnelListener::new("tcp://0.0.0.0:23457".parse().unwrap());
let tcp_connector = TcpTunnelConnector::new("tcp://127.0.0.1:23458".parse().unwrap());
let result = _tunnel_pingpong_netns_with_timeout(
tcp_listener,
tcp_connector,
NetNS::new(Some("net_c".into())),
NetNS::new(Some("net_a".into())),
buf.clone(),
std::time::Duration::from_millis(30000),
)
.await;
assert!(result.is_ok(), "Port forward pingpong should succeed");
drop_insts(insts).await;
}