-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_table_test.go
62 lines (52 loc) · 1.45 KB
/
array_table_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package wbpf
import (
"net/netip"
"testing"
"github.com/cilium/ebpf"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestArrayTable(t *testing.T) {
tbl, err := NewArrayTable[netip.Addr](createArray(t))
require.NoError(t, err, "Failed to new array table: %v", err)
addr, err := netip.ParseAddr("127.0.0.1")
t.Run("TEST SUCCESS: set value as netip.Addr", func(t *testing.T) {
require.NoError(t, err, "Failed to parse addr: %v", err)
err = tbl.Set(0, addr, UpdateAny)
require.NoError(t, err, "Failed to update value: %v", err)
})
t.Run("TEST SUCCESS: get value as netip.Addr", func(t *testing.T) {
var actual netip.Addr
err = tbl.Get(0, &actual)
require.NoError(t, err, "Failed to ret value at index 0: %v", err)
assert.Equal(t, addr.String(), actual.String())
})
t.Run("TEST FAILURE: index out bound", func(t *testing.T) {
var zero netip.Addr
err = tbl.Get(2, &zero)
require.Error(t, err, "No error raised")
assert.ErrorContains(t, err, "key does not exis", "Incorrect error")
})
}
func createArray(t *testing.T) *Table {
t.Helper()
m, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
})
if err != nil {
require.NoError(t, err, "Failed to new array map: %v", err)
}
t.Cleanup(func() { m.Close() })
return &Table{
Map: m,
info: &ebpf.MapInfo{
Type: ebpf.Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
}
}