|
1 | 1 | package pin
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "io/fs" |
| 4 | + "iter" |
5 | 5 | "os"
|
6 | 6 | "path/filepath"
|
| 7 | + "reflect" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "github.com/go-quicktest/qt"
|
10 | 11 |
|
11 | 12 | "github.com/cilium/ebpf"
|
| 13 | + "github.com/cilium/ebpf/internal/platform" |
12 | 14 | "github.com/cilium/ebpf/internal/testutils"
|
13 | 15 | )
|
14 | 16 |
|
15 | 17 | func TestWalkDir(t *testing.T) {
|
16 | 18 | testutils.SkipOnOldKernel(t, "4.10", "reading program fdinfo")
|
17 | 19 |
|
18 | 20 | tmp := testutils.TempBPFFS(t)
|
| 21 | + |
19 | 22 | dir := filepath.Join(tmp, "dir")
|
20 |
| - qt.Assert(t, qt.IsNil(os.Mkdir(dir, 0755))) |
| 23 | + if !platform.IsWindows { |
| 24 | + // Windows doesn't have a BPF file system, so mkdir below fails. |
| 25 | + qt.Assert(t, qt.IsNil(os.Mkdir(dir, 0755))) |
| 26 | + } |
21 | 27 |
|
22 |
| - mustPinnedProgram(t, filepath.Join(tmp, "pinned_prog")) |
23 |
| - mustPinnedMap(t, filepath.Join(dir, "pinned_map")) |
| 28 | + progPath := filepath.Join(tmp, "pinned_prog") |
| 29 | + mustPinnedProgram(t, progPath) |
| 30 | + mapPath := filepath.Join(dir, "pinned_map") |
| 31 | + mustPinnedMap(t, mapPath) |
24 | 32 |
|
25 |
| - entries := make(map[string]string) |
| 33 | + next, stop := iter.Pull2(WalkDir(tmp, nil)) |
| 34 | + defer stop() |
26 | 35 |
|
27 |
| - bpffn := func(path string, d fs.DirEntry, obj Pinner, err error) error { |
28 |
| - qt.Assert(t, qt.IsNil(err)) |
| 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)) |
29 | 41 |
|
30 |
| - if obj != nil { |
31 |
| - defer obj.Close() |
32 |
| - } |
| 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)) |
33 | 47 |
|
34 |
| - if path == "." { |
35 |
| - return nil |
36 |
| - } |
| 48 | + _, _, ok = next() |
| 49 | + qt.Assert(t, qt.IsFalse(ok)) |
37 | 50 |
|
38 |
| - switch obj.(type) { |
39 |
| - case *ebpf.Program: |
40 |
| - entries[path] = "prog" |
41 |
| - case *ebpf.Map: |
42 |
| - entries[path] = "map" |
43 |
| - default: |
44 |
| - entries[path] = "" |
| 51 | + t.Run("Not BPFFS", func(t *testing.T) { |
| 52 | + if platform.IsWindows { |
| 53 | + t.Skip("Windows does not have BPFFS") |
45 | 54 | }
|
46 | 55 |
|
47 |
| - return nil |
48 |
| - } |
49 |
| - qt.Assert(t, qt.IsNil(WalkDir(tmp, bpffn))) |
| 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)) |
50 | 62 |
|
51 |
| - qt.Assert(t, qt.DeepEquals(entries, map[string]string{ |
52 |
| - "pinned_prog": "prog", |
53 |
| - "dir": "", |
54 |
| - "dir/pinned_map": "map", |
55 |
| - })) |
| 63 | + _, _, ok = next() |
| 64 | + qt.Assert(t, qt.IsFalse(ok)) |
| 65 | + }) |
56 | 66 |
|
57 |
| - qt.Assert(t, qt.IsNotNil(WalkDir("/", nil))) |
58 | 67 | }
|
0 commit comments