|
| 1 | +package pin |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/go-quicktest/qt" |
| 8 | + |
| 9 | + "github.com/cilium/ebpf" |
| 10 | + "github.com/cilium/ebpf/asm" |
| 11 | + "github.com/cilium/ebpf/internal/testutils" |
| 12 | +) |
| 13 | + |
| 14 | +func mustPinnedProgram(t *testing.T, path string) *ebpf.Program { |
| 15 | + t.Helper() |
| 16 | + |
| 17 | + spec := &ebpf.ProgramSpec{ |
| 18 | + Name: "test", |
| 19 | + Type: ebpf.SocketFilter, |
| 20 | + Instructions: asm.Instructions{ |
| 21 | + asm.LoadImm(asm.R0, 2, asm.DWord), |
| 22 | + asm.Return(), |
| 23 | + }, |
| 24 | + License: "MIT", |
| 25 | + } |
| 26 | + |
| 27 | + p, err := ebpf.NewProgram(spec) |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + t.Cleanup(func() { p.Close() }) |
| 32 | + |
| 33 | + if err := p.Pin(path); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + |
| 37 | + return p |
| 38 | +} |
| 39 | + |
| 40 | +func mustPinnedMap(t *testing.T, path string) *ebpf.Map { |
| 41 | + t.Helper() |
| 42 | + |
| 43 | + spec := &ebpf.MapSpec{ |
| 44 | + Name: "test", |
| 45 | + Type: ebpf.Array, |
| 46 | + KeySize: 4, |
| 47 | + ValueSize: 4, |
| 48 | + MaxEntries: 1, |
| 49 | + } |
| 50 | + |
| 51 | + m, err := ebpf.NewMap(spec) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + t.Cleanup(func() { m.Close() }) |
| 56 | + |
| 57 | + if err := m.Pin(path); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + |
| 61 | + return m |
| 62 | +} |
| 63 | + |
| 64 | +func TestLoad(t *testing.T) { |
| 65 | + testutils.SkipOnOldKernel(t, "4.10", "reading program fdinfo") |
| 66 | + |
| 67 | + tmp := testutils.TempBPFFS(t) |
| 68 | + |
| 69 | + mpath := filepath.Join(tmp, "map") |
| 70 | + ppath := filepath.Join(tmp, "prog") |
| 71 | + |
| 72 | + mustPinnedMap(t, mpath) |
| 73 | + mustPinnedProgram(t, ppath) |
| 74 | + |
| 75 | + _, err := Load(tmp, nil) |
| 76 | + qt.Assert(t, qt.IsNotNil(err)) |
| 77 | + |
| 78 | + m, err := Load(mpath, nil) |
| 79 | + qt.Assert(t, qt.IsNil(err)) |
| 80 | + qt.Assert(t, qt.Satisfies(m, testutils.Contains[*ebpf.Map])) |
| 81 | + |
| 82 | + p, err := Load(ppath, nil) |
| 83 | + qt.Assert(t, qt.IsNil(err)) |
| 84 | + qt.Assert(t, qt.Satisfies(p, testutils.Contains[*ebpf.Program])) |
| 85 | +} |
0 commit comments