Skip to content

Commit

Permalink
Some fixes and adding a bond connection unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Nov 24, 2023
1 parent 49dba10 commit aeed3ab
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
6 changes: 4 additions & 2 deletions rust/agama-dbus-server/src/network/nm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use std::collections::HashMap;

use super::dbus::{
connection_from_dbus, connection_to_dbus, controller_from_dbus, merge_dbus_connections,
cleanup_dbus_connection, connection_from_dbus, connection_to_dbus, controller_from_dbus,
merge_dbus_connections,
};
use super::model::NmDeviceType;
use super::proxies::{ConnectionProxy, DeviceProxy, NetworkManagerProxy, SettingsProxy};
Expand Down Expand Up @@ -103,7 +104,7 @@ impl<'a> NetworkManagerClient<'a> {
///
/// * `conn`: connection to add or update.
pub async fn add_or_update_connection(&self, conn: &Connection) -> Result<(), ServiceError> {
let new_conn = connection_to_dbus(conn);
let mut new_conn = connection_to_dbus(conn);

let path = if let Ok(proxy) = self.get_connection_proxy(conn.uuid()).await {
let original = proxy.get_settings().await?;
Expand All @@ -112,6 +113,7 @@ impl<'a> NetworkManagerClient<'a> {
OwnedObjectPath::from(proxy.path().to_owned())
} else {
let proxy = SettingsProxy::new(&self.connection).await?;
cleanup_dbus_connection(&mut new_conn);
proxy.add_connection(new_conn).await?
};

Expand Down
3 changes: 1 addition & 2 deletions rust/agama-dbus-server/src/network/nm/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub fn connection_to_dbus(conn: &Connection) -> NestedHash {
}

result.insert("connection", connection_dbus);
cleanup_dbus_connection(&mut result);
result
}

Expand Down Expand Up @@ -117,7 +116,7 @@ pub fn merge_dbus_connections<'a>(
/// replaced with "address-data". However, if "addresses" is present, it takes precedence.
///
/// * `conn`: connection represented as a NestedHash.
fn cleanup_dbus_connection(conn: &mut NestedHash) {
pub fn cleanup_dbus_connection(conn: &mut NestedHash) {
if let Some(connection) = conn.get_mut("connection") {
if connection
.get("interface-name")
Expand Down
2 changes: 0 additions & 2 deletions rust/agama-dbus-server/src/network/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ impl<T: Adapter> NetworkSystem<T> {
if let Some(conn) = self.state.get_connection(id) {
tx.send(Ok(conn.clone())).unwrap();
}

dbg!(&self.state.connections);
}
Action::RemoveConnection(id) => {
self.tree.remove_connection(&id).await?;
Expand Down
43 changes: 41 additions & 2 deletions rust/agama-dbus-server/tests/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ mod common;
use self::common::{async_retry, DBusServer};
use agama_dbus_server::network::{
self,
model::{self, Ipv4Method, Ipv6Method},
model::{self, BondConfig, Ipv4Method, Ipv6Method},
Adapter, NetworkService, NetworkState,
};
use agama_lib::network::{settings, types::DeviceType, NetworkClient};
use agama_lib::network::{
settings::{self, BondSettings},
types::DeviceType,
NetworkClient,
};
use cidr::IpInet;
use std::error::Error;
use tokio::test;
Expand Down Expand Up @@ -86,6 +90,41 @@ async fn test_add_connection() -> Result<(), Box<dyn Error>> {
assert_eq!(method4, &Ipv4Method::Auto.to_string());
let method6 = conn.method6.as_ref().unwrap();
assert_eq!(method6, &Ipv6Method::Disabled.to_string());

Ok(())
}

#[test]
async fn test_add_bond_connection() -> Result<(), Box<dyn Error>> {
let mut server = DBusServer::new().start().await?;

let adapter = NetworkTestAdapter(NetworkState::default());

let _service = NetworkService::start(&server.connection(), adapter).await?;
server.request_name().await?;

let client = NetworkClient::new(server.connection().clone()).await?;
let bond0 = settings::NetworkConnection {
id: "bond0".to_string(),
method4: Some("auto".to_string()),
method6: Some("disabled".to_string()),
interface: Some("bond0".to_string()),
bond: Some(settings::BondSettings {
ports: vec!["eth0".to_string(), "eth1".to_string()],
options: "mode=active-backup primary=eth1".to_string(),
}),
..Default::default()
};

client.add_or_update_connection(&bond0).await?;
println!("FETCHING CONNECTIONS");
let conns = async_retry(|| client.connections()).await?;
assert_eq!(conns.len(), 1);

let conn = conns.iter().find(|c| c.id == "bond0".to_string()).unwrap();
assert_eq!(conn.id, "bond0");
assert_eq!(conn.device_type(), DeviceType::Bond);

Ok(())
}

Expand Down
20 changes: 20 additions & 0 deletions rust/agama-lib/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ impl<'a> NetworkClient<'a> {
for path in connection_paths {
let mut connection = self.connection_from(path.as_str()).await?;

if let Ok(bond) = self.bond_from(path.as_str()).await {
connection.bond = Some(bond);
}
if let Ok(wireless) = self.wireless_from(path.as_str()).await {
connection.wireless = Some(wireless);
}
Expand Down Expand Up @@ -130,6 +133,21 @@ impl<'a> NetworkClient<'a> {
})
}

/// Returns the [bond settings][BondSettings] for the given connection
///
/// * `path`: the connections path to get the wireless config from
async fn bond_from(&self, path: &str) -> Result<BondSettings, ServiceError> {
let bond_proxy = BondProxy::builder(&self.connection)
.path(path)?
.build()
.await?;
let bond = BondSettings {
options: bond_proxy.options().await?,
ports: bond_proxy.ports().await?,
};

Ok(bond)
}
/// Returns the [wireless settings][WirelessSettings] for the given connection
///
/// * `path`: the connections path to get the wireless config from
Expand Down Expand Up @@ -179,6 +197,7 @@ impl<'a> NetworkClient<'a> {
Ok(path) => path,
Err(_) => self.add_connection(conn).await?,
};

self.update_connection(&path, conn).await?;
Ok(())
}
Expand Down Expand Up @@ -297,6 +316,7 @@ impl<'a> NetworkClient<'a> {
let ports: Vec<_> = bond.ports.iter().map(String::as_ref).collect();
proxy.set_ports(ports.as_slice()).await?;
proxy.set_options(bond.options.to_string().as_str()).await?;

Ok(())
}
/// Updates the wireless settings for network connection.
Expand Down

0 comments on commit aeed3ab

Please sign in to comment.