Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/ip/link/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::collections::HashMap;
use futures_util::TryStreamExt;
use iproute_rs::{CliError, parse_mac_str};
use rtnetlink::{
LinkDummy, LinkMessageBuilder,
LinkDummy, LinkMessageBuilder, LinkNlmon,
packet_route::link::{InfoKind, LinkMessage},
};

Expand Down Expand Up @@ -45,6 +45,9 @@ impl LinkAddCommand {
InfoKind::Dummy => {
base_conf.apply(LinkDummy::new(&base_conf.name))?
}
InfoKind::Nlmon => {
base_conf.apply(LinkNlmon::new(&base_conf.name))?
}
InfoKind::Vlan => {
base_conf.apply(base_conf.apply_vlan(&handle).await?)?
}
Expand Down
1 change: 1 addition & 0 deletions src/ip/link/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ mod bridge;
mod color;
mod dummy;
mod loopback;
mod nlmon;
mod vlan;
mod vxlan;
72 changes: 72 additions & 0 deletions src/ip/link/tests/nlmon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT

use crate::tests::{exec_cmd, ip_rs_exec_cmd};

#[test]
fn test_link_show_nlmon() {
let ifname = "tnlm0";

with_nlmon_iface(ifname, || {
let expected_output = exec_cmd(&["ip", "link", "show", ifname]);

let our_output = ip_rs_exec_cmd(&["link", "show", ifname]);

pretty_assertions::assert_eq!(expected_output, our_output);
})
}

#[test]
fn test_link_detailed_show_nlmon() {
let ifname = "tnlm1";

with_nlmon_iface(ifname, || {
let expected_output = exec_cmd(&["ip", "-d", "link", "show", ifname]);

let our_output = ip_rs_exec_cmd(&["-d", "link", "show", ifname]);

pretty_assertions::assert_eq!(expected_output, our_output);
})
}

#[test]
fn test_link_show_nlmon_json() {
let ifname = "tnlm2";

with_nlmon_iface(ifname, || {
let expected_output = exec_cmd(&["ip", "-j", "link", "show", ifname]);

let our_output = ip_rs_exec_cmd(&["-j", "link", "show", ifname]);

pretty_assertions::assert_eq!(expected_output, our_output);
})
}

#[test]
fn test_link_detailed_show_nlmon_json() {
let ifname = "tnlm3";

with_nlmon_iface(ifname, || {
let expected_output =
exec_cmd(&["ip", "-d", "-j", "link", "show", ifname]);

let our_output = ip_rs_exec_cmd(&["-d", "-j", "link", "show", ifname]);

pretty_assertions::assert_eq!(expected_output, our_output);
})
}

fn with_nlmon_iface<T>(name: &str, test: T)
where
T: FnOnce() + std::panic::UnwindSafe,
{
ip_rs_exec_cmd(&["link", "add", name, "type", "nlmon"]);
exec_cmd(&["ip", "link", "set", name, "up"]);

let result = std::panic::catch_unwind(|| {
test();
});

// clean up
exec_cmd(&["ip", "link", "del", name]);
assert!(result.is_ok())
Comment on lines +65 to +71

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of with_nlmon_iface uses assert!(result.is_ok()) after catching a panic. This is suboptimal because it discards the original panic message and stack trace, making it difficult to debug test failures. Using std::panic::resume_unwind is preferred as it preserves the original panic information. Additionally, the cleanup step should ideally be robust against failures (e.g., if the interface was already removed by the test itself).

    let result = std::panic::catch_unwind(test);

    // clean up
    let _ = exec_cmd(&["ip", "link", "del", name]);

    if let Err(e) = result {
        std::panic::resume_unwind(e);
    }

}
10 changes: 9 additions & 1 deletion src/ip/link/tests/vlan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,15 @@ where
let parent_name = format!("p{vlan_name}");

// create parent dummy interface using ip-rs
ip_rs_exec_cmd(&["link", "add", &parent_name, "type", "dummy"]);
ip_rs_exec_cmd(&[
"link",
"add",
&parent_name,
"address",
"0e:d1:49:08:27:84",
"type",
"dummy",
]);
exec_cmd(&["ip", "link", "set", &parent_name, "up"]);

let mut args = vec![
Expand Down
Loading