Deduplicate test helpers

reviewable/pr942/r1
Tamir Duberstein 6 months ago
parent 58e154e1bc
commit 7e1666fb83

@ -86,41 +86,20 @@ mod tests {
use super::*; use super::*;
use crate::{ use crate::{
bpf_map_def,
generated::{ generated::{
bpf_cmd, bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_BLOOM_FILTER, BPF_MAP_TYPE_PERF_EVENT_ARRAY}, bpf_map_type::{BPF_MAP_TYPE_BLOOM_FILTER, BPF_MAP_TYPE_PERF_EVENT_ARRAY},
}, },
maps::Map, maps::{
obj::{self, maps::LegacyMap, EbpfSectionKind}, test_utils::{self, new_map},
Map,
},
obj,
sys::{override_syscall, SysResult, Syscall}, sys::{override_syscall, SysResult, Syscall},
}; };
fn new_obj_map() -> obj::Map { fn new_obj_map() -> obj::Map {
obj::Map::Legacy(LegacyMap { test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_BLOOM_FILTER)
def: bpf_map_def {
map_type: BPF_MAP_TYPE_BLOOM_FILTER as u32,
key_size: 4,
value_size: 4,
max_entries: 1024,
..Default::default()
},
section_index: 0,
section_kind: EbpfSectionKind::Maps,
symbol_index: None,
data: Vec::new(),
})
}
fn new_map(obj: obj::Map) -> MapData {
override_syscall(|call| match call {
Syscall::Ebpf {
cmd: bpf_cmd::BPF_MAP_CREATE,
..
} => Ok(1337),
call => panic!("unexpected syscall {:?}", call),
});
MapData::create(obj, "foo", None).unwrap()
} }
fn sys_error(value: i32) -> SysResult<c_long> { fn sys_error(value: i32) -> SysResult<c_long> {
@ -141,20 +120,9 @@ mod tests {
#[test] #[test]
fn test_try_from_wrong_map() { fn test_try_from_wrong_map() {
let map = new_map(obj::Map::Legacy(LegacyMap { let map = new_map(test_utils::new_obj_map::<u32>(
def: bpf_map_def { BPF_MAP_TYPE_PERF_EVENT_ARRAY,
map_type: BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32, ));
key_size: 4,
value_size: 4,
max_entries: 1024,
..Default::default()
},
section_index: 0,
section_kind: EbpfSectionKind::Maps,
symbol_index: None,
data: Vec::new(),
}));
let map = Map::PerfEventArray(map); let map = Map::PerfEventArray(map);
assert_matches!( assert_matches!(

@ -108,22 +108,22 @@ mod tests {
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{EFAULT, ENOENT};
use super::{ use super::*;
super::test_utils::{self, new_map},
*,
};
use crate::{ use crate::{
generated::{ generated::{
bpf_attr, bpf_cmd, bpf_attr, bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH}, bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH},
}, },
maps::Map, maps::{
test_utils::{self, new_map},
Map,
},
obj, obj,
sys::{override_syscall, SysResult, Syscall}, sys::{override_syscall, SysResult, Syscall},
}; };
fn new_obj_map() -> obj::Map { fn new_obj_map() -> obj::Map {
test_utils::new_obj_map(BPF_MAP_TYPE_HASH) test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_HASH)
} }
fn sys_error(value: i32) -> SysResult<c_long> { fn sys_error(value: i32) -> SysResult<c_long> {
@ -192,7 +192,7 @@ mod tests {
#[test] #[test]
fn test_try_from_ok_lru() { fn test_try_from_ok_lru() {
let map_data = || new_map(test_utils::new_obj_map(BPF_MAP_TYPE_LRU_HASH)); let map_data = || new_map(test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_LRU_HASH));
let map = Map::HashMap(map_data()); let map = Map::HashMap(map_data());
assert!(HashMap::<_, u32, u32>::try_from(&map).is_ok()); assert!(HashMap::<_, u32, u32>::try_from(&map).is_ok());
let map = Map::LruHashMap(map_data()); let map = Map::LruHashMap(map_data());

@ -43,41 +43,3 @@ pub(crate) fn remove<K: Pod>(map: &MapData, key: &K) -> Result<(), MapError> {
.into() .into()
}) })
} }
#[cfg(test)]
mod test_utils {
use crate::{
bpf_map_def,
generated::{bpf_cmd, bpf_map_type},
maps::MapData,
obj::{self, maps::LegacyMap, EbpfSectionKind},
sys::{override_syscall, Syscall},
};
pub(super) fn new_map(obj: obj::Map) -> MapData {
override_syscall(|call| match call {
Syscall::Ebpf {
cmd: bpf_cmd::BPF_MAP_CREATE,
..
} => Ok(1337),
call => panic!("unexpected syscall {:?}", call),
});
MapData::create(obj, "foo", None).unwrap()
}
pub(super) fn new_obj_map(map_type: bpf_map_type) -> obj::Map {
obj::Map::Legacy(LegacyMap {
def: bpf_map_def {
map_type: map_type as u32,
key_size: 4,
value_size: 4,
max_entries: 1024,
..Default::default()
},
section_index: 0,
section_kind: EbpfSectionKind::Maps,
data: Vec::new(),
symbol_index: None,
})
}
}

@ -150,15 +150,15 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>>
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{super::test_utils, *}; use super::*;
use crate::{ use crate::{
generated::bpf_map_type::{BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH}, generated::bpf_map_type::{BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH},
maps::Map, maps::{test_utils, Map},
}; };
#[test] #[test]
fn test_try_from_ok() { fn test_try_from_ok() {
let map = Map::PerCpuHashMap(test_utils::new_map(test_utils::new_obj_map( let map = Map::PerCpuHashMap(test_utils::new_map(test_utils::new_obj_map::<u32>(
BPF_MAP_TYPE_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH,
))); )));
assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok()) assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok())
@ -166,7 +166,7 @@ mod tests {
#[test] #[test]
fn test_try_from_ok_lru() { fn test_try_from_ok_lru() {
let map_data = let map_data =
|| test_utils::new_map(test_utils::new_obj_map(BPF_MAP_TYPE_LRU_PERCPU_HASH)); || test_utils::new_map(test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_LRU_PERCPU_HASH));
let map = Map::PerCpuHashMap(map_data()); let map = Map::PerCpuHashMap(map_data());
assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok()); assert!(PerCpuHashMap::<_, u32, u32>::try_from(&map).is_ok());
let map = Map::PerCpuLruHashMap(map_data()); let map = Map::PerCpuLruHashMap(map_data());

@ -196,48 +196,27 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<Key<K>, V> for LpmTrie<T, K
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::{ffi::c_long, io, mem, net::Ipv4Addr}; use std::{ffi::c_long, io, net::Ipv4Addr};
use assert_matches::assert_matches; use assert_matches::assert_matches;
use libc::{EFAULT, ENOENT}; use libc::{EFAULT, ENOENT};
use super::*; use super::*;
use crate::{ use crate::{
bpf_map_def,
generated::{ generated::{
bpf_cmd, bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_LPM_TRIE, BPF_MAP_TYPE_PERF_EVENT_ARRAY}, bpf_map_type::{BPF_MAP_TYPE_LPM_TRIE, BPF_MAP_TYPE_PERF_EVENT_ARRAY},
}, },
maps::Map, maps::{
obj::{self, maps::LegacyMap, EbpfSectionKind}, test_utils::{self, new_map},
Map,
},
obj,
sys::{override_syscall, SysResult, Syscall}, sys::{override_syscall, SysResult, Syscall},
}; };
fn new_obj_map() -> obj::Map { fn new_obj_map() -> obj::Map {
obj::Map::Legacy(LegacyMap { test_utils::new_obj_map::<Key<u32>>(BPF_MAP_TYPE_LPM_TRIE)
def: bpf_map_def {
map_type: BPF_MAP_TYPE_LPM_TRIE as u32,
key_size: mem::size_of::<Key<u32>>() as u32,
value_size: 4,
max_entries: 1024,
..Default::default()
},
section_index: 0,
section_kind: EbpfSectionKind::Maps,
symbol_index: None,
data: Vec::new(),
})
}
fn new_map(obj: obj::Map) -> MapData {
override_syscall(|call| match call {
Syscall::Ebpf {
cmd: bpf_cmd::BPF_MAP_CREATE,
..
} => Ok(1337),
call => panic!("unexpected syscall {:?}", call),
});
MapData::create(obj, "foo", None).unwrap()
} }
fn sys_error(value: i32) -> SysResult<c_long> { fn sys_error(value: i32) -> SysResult<c_long> {
@ -270,20 +249,9 @@ mod tests {
#[test] #[test]
fn test_try_from_wrong_map() { fn test_try_from_wrong_map() {
let map = new_map(obj::Map::Legacy(LegacyMap { let map = new_map(test_utils::new_obj_map::<u32>(
def: bpf_map_def { BPF_MAP_TYPE_PERF_EVENT_ARRAY,
map_type: BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32, ));
key_size: 4,
value_size: 4,
max_entries: 1024,
..Default::default()
},
section_index: 0,
section_kind: EbpfSectionKind::Maps,
symbol_index: None,
data: Vec::new(),
}));
let map = Map::PerfEventArray(map); let map = Map::PerfEventArray(map);
assert_matches!( assert_matches!(

@ -1021,35 +1021,59 @@ pub fn loaded_maps() -> impl Iterator<Item = Result<MapInfo, MapError>> {
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod test_utils {
use std::os::fd::AsRawFd as _;
use assert_matches::assert_matches;
use libc::{c_char, EFAULT};
use super::*;
use crate::{ use crate::{
bpf_map_def, bpf_map_def,
generated::{bpf_cmd, bpf_map_type::BPF_MAP_TYPE_HASH}, generated::{bpf_cmd, bpf_map_type},
obj::maps::LegacyMap, maps::MapData,
obj::{self, maps::LegacyMap, EbpfSectionKind},
sys::{override_syscall, Syscall}, sys::{override_syscall, Syscall},
}; };
fn new_obj_map() -> obj::Map { pub(super) fn new_map(obj: obj::Map) -> MapData {
override_syscall(|call| match call {
Syscall::Ebpf {
cmd: bpf_cmd::BPF_MAP_CREATE,
..
} => Ok(1337),
call => panic!("unexpected syscall {:?}", call),
});
MapData::create(obj, "foo", None).unwrap()
}
pub(super) fn new_obj_map<K>(map_type: bpf_map_type) -> obj::Map {
obj::Map::Legacy(LegacyMap { obj::Map::Legacy(LegacyMap {
def: bpf_map_def { def: bpf_map_def {
map_type: BPF_MAP_TYPE_HASH as u32, map_type: map_type as u32,
key_size: 4, key_size: std::mem::size_of::<K>() as u32,
value_size: 4, value_size: 4,
max_entries: 1024, max_entries: 1024,
..Default::default() ..Default::default()
}, },
section_index: 0, section_index: 0,
section_kind: EbpfSectionKind::Maps, section_kind: EbpfSectionKind::Maps,
symbol_index: Some(0),
data: Vec::new(), data: Vec::new(),
symbol_index: None,
}) })
} }
}
#[cfg(test)]
mod tests {
use std::os::fd::AsRawFd as _;
use assert_matches::assert_matches;
use libc::{c_char, EFAULT};
fn new_obj_map() -> obj::Map {
test_utils::new_obj_map::<u32>(crate::generated::bpf_map_type::BPF_MAP_TYPE_HASH)
}
use super::*;
use crate::{
generated::bpf_cmd,
sys::{override_syscall, Syscall},
};
#[test] #[test]
fn test_from_map_id() { fn test_from_map_id() {

Loading…
Cancel
Save