| 1 |
package pin |
| 2 |
|
| 3 |
import ( |
| 4 |
"iter" |
| 5 |
"os" |
| 6 |
"path/filepath" |
| 7 |
"reflect" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/go-quicktest/qt" |
| 11 |
|
| 12 |
"github.com/cilium/ebpf" |
| 13 |
"github.com/cilium/ebpf/internal/platform" |
| 14 |
"github.com/cilium/ebpf/internal/testutils" |
| 15 |
) |
| 16 |
|
| 17 |
func TestWalkDir(t *testing.T) { |
| 18 |
testutils.SkipOnOldKernel(t, "4.13", "reading program objinfo") |
| 19 |
|
| 20 |
tmp := testutils.TempBPFFS(t) |
| 21 |
|
| 22 |
dir := filepath.Join(tmp, "dir") |
| 23 |
if !platform.IsWindows { |
| 24 |
|
| 25 |
qt.Assert(t, qt.IsNil(os.Mkdir(dir, 0755))) |
| 26 |
} |
| 27 |
|
| 28 |
progPath := filepath.Join(tmp, "pinned_prog") |
| 29 |
mustPinnedProgram(t, progPath) |
| 30 |
mapPath := filepath.Join(dir, "pinned_map") |
| 31 |
mustPinnedMap(t, mapPath) |
| 32 |
|
| 33 |
next, stop := iter.Pull2(WalkDir(tmp, nil)) |
| 34 |
defer stop() |
| 35 |
|
| 36 |
pin, err, ok := next() |
| 37 |
qt.Assert(t, qt.IsTrue(ok)) |
| 38 |
qt.Assert(t, qt.IsNil(err)) |
| 39 |
qt.Assert(t, qt.Equals(reflect.TypeOf(pin.Object), reflect.TypeFor[*ebpf.Map]())) |
| 40 |
qt.Assert(t, qt.Equals(pin.Path, mapPath)) |
| 41 |
|
| 42 |
pin, err, ok = next() |
| 43 |
qt.Assert(t, qt.IsTrue(ok)) |
| 44 |
qt.Assert(t, qt.IsNil(err)) |
| 45 |
qt.Assert(t, qt.Equals(reflect.TypeOf(pin.Object), reflect.TypeFor[*ebpf.Program]())) |
| 46 |
qt.Assert(t, qt.Equals(pin.Path, progPath)) |
| 47 |
|
| 48 |
_, _, ok = next() |
| 49 |
qt.Assert(t, qt.IsFalse(ok)) |
| 50 |
|
| 51 |
t.Run("Not BPFFS", func(t *testing.T) { |
| 52 |
if platform.IsWindows { |
| 53 |
t.Skip("Windows does not have BPFFS") |
| 54 |
} |
| 55 |
|
| 56 |
next, stop := iter.Pull2(WalkDir("/", nil)) |
| 57 |
defer stop() |
| 58 |
|
| 59 |
_, err, ok = next() |
| 60 |
qt.Assert(t, qt.IsTrue(ok)) |
| 61 |
qt.Assert(t, qt.IsNotNil(err)) |
| 62 |
|
| 63 |
_, _, ok = next() |
| 64 |
qt.Assert(t, qt.IsFalse(ok)) |
| 65 |
}) |
| 66 |
|
| 67 |
} |