|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +use crate::tests::{exec_cmd, ip_rs_exec_cmd}; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test_link_show_veth() { |
| 7 | + let ifname = "tveth0"; |
| 8 | + let peer = "tveth0_peer"; |
| 9 | + |
| 10 | + with_veth_iface(ifname, peer, || { |
| 11 | + let expected_output = exec_cmd(&["ip", "link", "show", ifname]); |
| 12 | + |
| 13 | + let our_output = ip_rs_exec_cmd(&["link", "show", ifname]); |
| 14 | + |
| 15 | + pretty_assertions::assert_eq!(expected_output, our_output); |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +#[test] |
| 20 | +fn test_link_detailed_show_veth() { |
| 21 | + let ifname = "tveth1"; |
| 22 | + let peer = "tveth1_peer"; |
| 23 | + |
| 24 | + with_veth_iface(ifname, peer, || { |
| 25 | + let expected_output = exec_cmd(&["ip", "-d", "link", "show", ifname]); |
| 26 | + |
| 27 | + let our_output = ip_rs_exec_cmd(&["-d", "link", "show", ifname]); |
| 28 | + |
| 29 | + pretty_assertions::assert_eq!(expected_output, our_output); |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +#[test] |
| 34 | +fn test_link_show_veth_json() { |
| 35 | + let ifname = "tveth2"; |
| 36 | + let peer = "tveth2_peer"; |
| 37 | + |
| 38 | + with_veth_iface(ifname, peer, || { |
| 39 | + let expected_output = exec_cmd(&["ip", "-j", "link", "show", ifname]); |
| 40 | + |
| 41 | + let our_output = ip_rs_exec_cmd(&["-j", "link", "show", ifname]); |
| 42 | + |
| 43 | + pretty_assertions::assert_eq!(expected_output, our_output); |
| 44 | + }) |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn test_link_detailed_show_veth_json() { |
| 49 | + let ifname = "tveth3"; |
| 50 | + let peer = "tveth3_peer"; |
| 51 | + |
| 52 | + with_veth_iface(ifname, peer, || { |
| 53 | + let expected_output = |
| 54 | + exec_cmd(&["ip", "-d", "-j", "link", "show", ifname]); |
| 55 | + |
| 56 | + let our_output = ip_rs_exec_cmd(&["-d", "-j", "link", "show", ifname]); |
| 57 | + |
| 58 | + pretty_assertions::assert_eq!(expected_output, our_output); |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +fn with_veth_iface<T>(name: &str, peer: &str, test: T) |
| 63 | +where |
| 64 | + T: FnOnce() + std::panic::UnwindSafe, |
| 65 | +{ |
| 66 | + ip_rs_exec_cmd(&["link", "add", name, "type", "veth", "peer", peer]); |
| 67 | + exec_cmd(&["ip", "link", "set", name, "up"]); |
| 68 | + exec_cmd(&["ip", "link", "set", peer, "up"]); |
| 69 | + |
| 70 | + let result = std::panic::catch_unwind(|| { |
| 71 | + test(); |
| 72 | + }); |
| 73 | + |
| 74 | + // clean up (deleting veth removes both ends) |
| 75 | + let _ = exec_cmd(&["ip", "link", "del", name]); |
| 76 | + |
| 77 | + if let Err(e) = result { |
| 78 | + std::panic::resume_unwind(e); |
| 79 | + } |
| 80 | +} |
0 commit comments