From 390873e14d6f2d437564d65e3d00adce6b4eaa21 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 30 Apr 2024 18:28:17 -0400 Subject: [PATCH] Deduplicate test helpers --- aya/src/maps/bloom_filter.rs | 30 +++----------- aya/src/maps/hash_map/hash_map.rs | 10 ++--- aya/src/maps/hash_map/mod.rs | 38 ------------------ aya/src/maps/hash_map/per_cpu_hash_map.rs | 4 +- aya/src/maps/lpm_trie.rs | 32 +++------------ aya/src/maps/mod.rs | 48 +++++++++++++++++------ 6 files changed, 54 insertions(+), 108 deletions(-) diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 817c785c..d2769285 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -91,36 +91,16 @@ mod tests { bpf_cmd, bpf_map_type::{BPF_MAP_TYPE_BLOOM_FILTER, BPF_MAP_TYPE_PERF_EVENT_ARRAY}, }, - maps::Map, + maps::{ + test_utils::{self, new_map}, + Map, + }, obj::{self, maps::LegacyMap, EbpfSectionKind}, sys::{override_syscall, SysResult, Syscall}, }; fn new_obj_map() -> obj::Map { - obj::Map::Legacy(LegacyMap { - 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() + test_utils::new_obj_map(BPF_MAP_TYPE_BLOOM_FILTER) } fn sys_error(value: i32) -> SysResult { diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index f1f8fceb..bd7e18ac 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -108,16 +108,16 @@ mod tests { use assert_matches::assert_matches; use libc::{EFAULT, ENOENT}; - use super::{ - super::test_utils::{self, new_map}, - *, - }; + use super::*; use crate::{ generated::{ bpf_attr, bpf_cmd, bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH}, }, - maps::Map, + maps::{ + test_utils::{self, new_map}, + Map, + }, obj, sys::{override_syscall, SysResult, Syscall}, }; diff --git a/aya/src/maps/hash_map/mod.rs b/aya/src/maps/hash_map/mod.rs index 93f345bd..33f5292b 100644 --- a/aya/src/maps/hash_map/mod.rs +++ b/aya/src/maps/hash_map/mod.rs @@ -43,41 +43,3 @@ pub(crate) fn remove(map: &MapData, key: &K) -> Result<(), MapError> { .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, - }) - } -} diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index 6dd3ee19..9d1046df 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -150,10 +150,10 @@ impl, K: Pod, V: Pod> IterableMap> #[cfg(test)] mod tests { - use super::{super::test_utils, *}; + use super::*; use crate::{ generated::bpf_map_type::{BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH}, - maps::Map, + maps::{test_utils, Map}, }; #[test] diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index c3dd528a..84d97be9 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -196,7 +196,7 @@ impl, K: Pod, V: Pod> IterableMap, V> for LpmTrie obj::Map { - obj::Map::Legacy(LegacyMap { - def: bpf_map_def { - map_type: BPF_MAP_TYPE_LPM_TRIE as u32, - key_size: mem::size_of::>() 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() + test_utils::new_obj_map(BPF_MAP_TYPE_LPM_TRIE) } fn sys_error(value: i32) -> SysResult { diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 7c1d2a84..c53b5f6b 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -1021,24 +1021,30 @@ pub fn loaded_maps() -> impl Iterator> { } #[cfg(test)] -mod tests { - use std::os::fd::AsRawFd as _; - - use assert_matches::assert_matches; - use libc::{c_char, EFAULT}; - - use super::*; +mod test_utils { use crate::{ bpf_map_def, - generated::{bpf_cmd, bpf_map_type::BPF_MAP_TYPE_HASH}, - obj::maps::LegacyMap, + generated::{bpf_cmd, bpf_map_type}, + maps::MapData, + obj::{self, maps::LegacyMap, EbpfSectionKind}, 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(map_type: bpf_map_type) -> obj::Map { obj::Map::Legacy(LegacyMap { def: bpf_map_def { - map_type: BPF_MAP_TYPE_HASH as u32, + map_type: map_type as u32, key_size: 4, value_size: 4, max_entries: 1024, @@ -1046,10 +1052,28 @@ mod tests { }, section_index: 0, section_kind: EbpfSectionKind::Maps, - symbol_index: Some(0), 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(crate::generated::bpf_map_type::BPF_MAP_TYPE_HASH) + } + + use super::*; + use crate::{ + generated::bpf_cmd, + sys::{override_syscall, Syscall}, + }; #[test] fn test_from_map_id() {