From 8dd8389639386c4c3314453161adcc62fa9a46c5 Mon Sep 17 00:00:00 2001 From: Takeru Hayasaka Date: Fri, 13 Sep 2024 05:45:24 +0900 Subject: [PATCH] chore: test refactor --- lib/sys/ebpf/map.pm | 6 +++ t/03_ebpf_crud_map.t | 105 +++++++++++++++++++++++-------------------- 2 files changed, 63 insertions(+), 48 deletions(-) diff --git a/lib/sys/ebpf/map.pm b/lib/sys/ebpf/map.pm index 5e94b57..c465cea 100644 --- a/lib/sys/ebpf/map.pm +++ b/lib/sys/ebpf/map.pm @@ -129,6 +129,12 @@ sub syscall_bpf_map_elem { my $result = syscall( sys::ebpf::syscall::SYS_bpf(), $cmd, $attr, length($attr) ); + if ( $result < 0 ) { + my $errno = $!; + warn sprintf( + "syscall_bpf_map_elem failed with errno %d (%s). Command: %d, Map FD: %d, Flags: %d", + $errno, $!, $cmd, $map_fd, $flags ); + } return $result; } diff --git a/t/03_ebpf_crud_map.t b/t/03_ebpf_crud_map.t index b89ff6e..c3c6fb9 100644 --- a/t/03_ebpf_crud_map.t +++ b/t/03_ebpf_crud_map.t @@ -2,25 +2,19 @@ use strict; use warnings; use utf8; -use Test::More import => [qw( done_testing is_deeply ok plan )]; -use Data::Dumper (); +use Test::More import => [qw( done_testing is_deeply ok plan subtest )]; +use Time::HiRes qw( usleep ); -# Load the module you're testing use lib '../lib'; -use sys::ebpf::asm; use sys::ebpf::map; - -use sys::ebpf::constants::bpf_map_type qw( BPF_MAP_TYPE_HASH ); +use sys::ebpf::constants::bpf_map_type qw(BPF_MAP_TYPE_HASH); use sys::ebpf::constants::bpf_map_create_flags qw(BPF_F_NO_PREALLOC); -use sys::ebpf::constants::bpf_prog_type (); use sys::ebpf::constants::bpf_map_update_flags qw(BPF_ANY BPF_NOEXIST BPF_EXIST); plan skip_all => "This test must be run as root" if $> != 0; -my $map_instance; - -sub setup { +sub create_map { my %map_attr = ( map_name => "ebpf_crud_map", map_type => BPF_MAP_TYPE_HASH, @@ -40,62 +34,77 @@ sub setup { map_flags => BPF_F_NO_PREALLOC, ); - $map_instance = sys::ebpf::map->create( \%map_attr ); + my $map = sys::ebpf::map->create( \%map_attr ); + usleep(10000); # 10ms wait after creation + return $map; } -sub teardown { - if ($map_instance) { - $map_instance->close(); - undef $map_instance; - - # sleep 1; - } -} +subtest 'test_map_creation' => sub { + my $map = create_map(); + ok( $map->{map_fd} > 0, "Created map fd is " . $map->{map_fd} ); + ok( $map->{map_flags} == BPF_F_NO_PREALLOC, "Map flags are correct" ); + ok( $map->{key_size} == 20, "Key size is correct" ); + ok( $map->{value_size} == 20, "Value size is correct" ); + $map->close(); + usleep(10000); # 10ms wait after closure +}; +subtest 'test_map_update_and_lookup' => sub { + my $map = create_map(); + my $key = { + uint8_id => [ 1, 0, 2, 0 ], + uint16_id => [ 1, 2 ], + uint32_id => 1, + uint64_id => 1 + }; + my $value = { + uint8_value => [ 1, 2, 3, 4 ], + uint16_value => [ 1, 2 ], + uint32_value => 1, + uint64_value => 1 + }; -sub run_test { - setup(); + my $res = $map->update( $key, $value, BPF_ANY() ); + ok( $res == 0, "Updated map: $res" ); - # テストコード - my $map_fd = $map_instance->{map_fd}; - ok( $map_fd > 0, "Created map fd is $map_fd" ); - ok( $map_instance->{map_flags} == BPF_F_NO_PREALLOC, - "Map flags are correct", - ); - ok( $map_instance->{key_size} == 20, "Key size is correct" ); - ok( $map_instance->{value_size} == 20, "Value size is correct" ); + my $lookup_value = $map->lookup($key); + ok( defined $lookup_value, "Found value" ); + is_deeply( $lookup_value, $value, "Value is correct" ); - my $origin_key = { - uint8_id => 1, + $map->close(); + usleep(10000); # 10ms wait after closure +}; +subtest 'test_map_delete' => sub { + my $map = create_map(); + my $key = { + uint8_id => [ 1, 0, 2, 0 ], uint16_id => [ 1, 2 ], uint32_id => 1, uint64_id => 1 }; - my $origin_value = { + my $value = { uint8_value => [ 1, 2, 3, 4 ], uint16_value => [ 1, 2 ], uint32_value => 1, uint64_value => 1 }; - my $res = $map_instance->update( $origin_key, $origin_value, BPF_ANY() ); - - ok( $res >= 0, "Updated map: $res" ); - my $value = $map_instance->lookup($origin_key); - ok( defined $value, "Found value" ); - is_deeply( $value, $origin_value, "Value is correct" ); + # Update the map and verify the write + my $update_res = $map->update( $key, $value, BPF_ANY() ); + ok( $update_res == 0, "Updated map: $update_res" ); - $res = $map_instance->delete($origin_key); - ok( $res == 0, "Deleted map: $res" ); + my $verify_value = $map->lookup($key); + ok( defined $verify_value, "Value found after update" ); + is_deeply( $verify_value, $value, "Written value is correct" ); - $value = $map_instance->lookup($origin_key); - ok( !defined $value, "Value not found after deletion" ); + # Now proceed with deletion test + my $res = $map->delete($key); + ok( $res == 0, "Deleted map entry: $res" ); - teardown(); -} + my $lookup_value = $map->lookup($key); + ok( !defined $lookup_value, "Value not found after deletion" ); -run_test(); + $map->close(); + usleep(10000); # 10ms wait after closure +}; -END { - teardown(); -} done_testing();