Skip to content

Commit

Permalink
test: replace lo interface with dummy interface
Browse files Browse the repository at this point in the history
Fixes: aya-rs#422

Signed-off-by: murex971 <nupur202000@gmail.com>
  • Loading branch information
murex971 committed Mar 29, 2023
1 parent b13070a commit 43df857
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
25 changes: 19 additions & 6 deletions test/integration-test/src/tests/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use aya::{
};
use log::warn;

use crate::tests::kernel_version;
use crate::tests::{kernel_version, util::DummyInterface};

use super::{integration_test, IntegrationTest};

Expand All @@ -28,7 +28,10 @@ fn long_name() {
.try_into()
.unwrap();
name_prog.load().unwrap();
name_prog.attach("lo", XdpFlags::default()).unwrap();
let _iface = DummyInterface::new().unwrap();
name_prog
.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();

// We used to be able to assert with bpftool that the program name was short.
// It seem though that it now uses the name from the ELF symbol table instead.
Expand Down Expand Up @@ -94,7 +97,10 @@ fn unload_xdp() {
.unwrap();
prog.load().unwrap();
assert_loaded!("test_unload_xdp", true);
let link = prog.attach("lo", XdpFlags::default()).unwrap();
let _iface = DummyInterface::new().unwrap();
let link = prog
.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();
{
let _link_owned = prog.take_link(link).unwrap();
prog.unload().unwrap();
Expand All @@ -105,7 +111,8 @@ fn unload_xdp() {
prog.load().unwrap();

assert_loaded!("test_unload_xdp", true);
prog.attach("lo", XdpFlags::default()).unwrap();
prog.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();

assert_loaded!("test_unload_xdp", true);
prog.unload().unwrap();
Expand Down Expand Up @@ -158,7 +165,10 @@ fn pin_link() {
.try_into()
.unwrap();
prog.load().unwrap();
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
let _iface = DummyInterface::new().unwrap();
let link_id = prog
.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();
let link = prog.take_link(link_id).unwrap();
assert_loaded!("test_unload_xdp", true);

Expand Down Expand Up @@ -207,9 +217,12 @@ fn pin_lifecycle() {
assert_loaded!("pass", true);

// 3. Load program from bpffs and attach
let _iface = DummyInterface::new().unwrap();
{
let mut prog = Xdp::from_pin("/sys/fs/bpf/aya-xdp-test-prog").unwrap();
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
let link_id = prog
.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();
let link = prog.take_link(link_id).unwrap();
let fd_link: FdLink = link.try_into().unwrap();
fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap();
Expand Down
1 change: 1 addition & 0 deletions test/integration-test/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod load;
pub mod rbpf;
pub mod relocations;
pub mod smoke;
mod util;

pub use integration_test_macros::integration_test;
#[derive(Debug)]
Expand Down
11 changes: 8 additions & 3 deletions test/integration-test/src/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ use aya::{
};
use log::info;

use super::{integration_test, kernel_version, IntegrationTest};
use super::{integration_test, kernel_version, util::DummyInterface, IntegrationTest};

#[integration_test]
fn xdp() {
let bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/pass");
let mut bpf = Bpf::load(bytes).unwrap();
let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
dispatcher.load().unwrap();
dispatcher.attach("lo", XdpFlags::default()).unwrap();
let _iface = DummyInterface::new().unwrap();
dispatcher
.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();
}

#[integration_test]
Expand All @@ -32,7 +35,9 @@ fn extension() {
let mut bpf = Bpf::load(main_bytes).unwrap();
let pass: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
pass.load().unwrap();
pass.attach("lo", XdpFlags::default()).unwrap();
let _iface = DummyInterface::new().unwrap();
pass.attach(DummyInterface::TEST_DUMMY, XdpFlags::default())
.unwrap();

let ext_bytes = include_bytes_aligned!("../../../../target/bpfel-unknown-none/debug/ext.bpf.o");
let mut bpf = BpfLoader::new().extension("drop").load(ext_bytes).unwrap();
Expand Down
30 changes: 30 additions & 0 deletions test/integration-test/src/tests/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::process::Command;

pub(crate) struct DummyInterface;

impl DummyInterface {
pub const TEST_DUMMY: &str = "aya-dummy";

pub fn new() -> Option<Self> {
let output = Command::new("ip")
.args(["link", "add", Self::TEST_DUMMY, "type", "dummy"])
.output()
.expect("failed to run ip command");

if output.status.success() {
None
} else {
Some(Self)
}
}
}

impl Drop for DummyInterface {
fn drop(&mut self) {
let output = Command::new("ip")
.args(["link", "del", Self::TEST_DUMMY])
.output()
.expect("failed to run ip command");
assert!(output.status.success())
}
}

0 comments on commit 43df857

Please sign in to comment.