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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
package pin
import (
"path/filepath"
"testing"
"github.com/go-quicktest/qt"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
)
func mustPinnedProgram(t *testing.T, path string) *ebpf.Program {
t.Helper()
spec := &ebpf.ProgramSpec{
Name: "test",
Type: ebpf.SocketFilter,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 2, asm.DWord),
asm.Return(),
},
License: "MIT",
}
p, err := ebpf.NewProgram(spec)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { p.Close() })
if err := p.Pin(path); err != nil {
t.Fatal(err)
}
return p
}
func mustPinnedMap(t *testing.T, path string) *ebpf.Map {
t.Helper()
spec := &ebpf.MapSpec{
Name: "test",
Type: ebpf.Array,
KeySize: 4,
ValueSize: 4,
MaxEntries: 1,
}
m, err := ebpf.NewMap(spec)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.Close() })
if err := m.Pin(path); err != nil {
t.Fatal(err)
}
return m
}
func TestLoad(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.10", "reading program fdinfo")
tmp := testutils.TempBPFFS(t)
mpath := filepath.Join(tmp, "map")
ppath := filepath.Join(tmp, "prog")
mustPinnedMap(t, mpath)
mustPinnedProgram(t, ppath)
_, err := Load(tmp, nil)
qt.Assert(t, qt.IsNotNil(err))
m, err := Load(mpath, nil)
qt.Assert(t, qt.IsNil(err))
defer m.Close()
qt.Assert(t, qt.Satisfies(m, testutils.Contains[*ebpf.Map]))
p, err := Load(ppath, nil)
qt.Assert(t, qt.IsNil(err))
defer p.Close()
qt.Assert(t, qt.Satisfies(p, testutils.Contains[*ebpf.Program]))
}
func TestMain(m *testing.M) {
fdtrace.TestMain(m)
}
|