Skip to content

Commit

Permalink
chore: test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
takehaya committed Sep 12, 2024
1 parent 821f576 commit 8dd8389
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
6 changes: 6 additions & 0 deletions lib/sys/ebpf/map.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.14

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.18

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.16

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.40

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.34

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.36

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.38

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.22

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.20

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.30

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.32

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.26

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.28

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.10

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.12

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0

Check failure on line 134 in lib/sys/ebpf/map.pm

View workflow job for this annotation

GitHub Actions / Perl 5.24

syscall_bpf_map_elem failed with errno 2 (No such file or directory). Command: 1, Map FD: 3, Flags: 0
"syscall_bpf_map_elem failed with errno %d (%s). Command: %d, Map FD: %d, Flags: %d",
$errno, $!, $cmd, $map_fd, $flags );
}
return $result;
}

Expand Down
105 changes: 57 additions & 48 deletions t/03_ebpf_crud_map.t
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

0 comments on commit 8dd8389

Please sign in to comment.