File: walk_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (58 lines) | stat: -rw-r--r-- 1,116 bytes parent folder | download
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
package pin

import (
	"io/fs"
	"os"
	"path/filepath"
	"testing"

	"github.com/go-quicktest/qt"

	"github.com/cilium/ebpf"
	"github.com/cilium/ebpf/internal/testutils"
)

func TestWalkDir(t *testing.T) {
	testutils.SkipOnOldKernel(t, "4.10", "reading program fdinfo")

	tmp := testutils.TempBPFFS(t)
	dir := filepath.Join(tmp, "dir")
	qt.Assert(t, qt.IsNil(os.Mkdir(dir, 0755)))

	mustPinnedProgram(t, filepath.Join(tmp, "pinned_prog"))
	mustPinnedMap(t, filepath.Join(dir, "pinned_map"))

	entries := make(map[string]string)

	bpffn := func(path string, d fs.DirEntry, obj Pinner, err error) error {
		qt.Assert(t, qt.IsNil(err))

		if obj != nil {
			defer obj.Close()
		}

		if path == "." {
			return nil
		}

		switch obj.(type) {
		case *ebpf.Program:
			entries[path] = "prog"
		case *ebpf.Map:
			entries[path] = "map"
		default:
			entries[path] = ""
		}

		return nil
	}
	qt.Assert(t, qt.IsNil(WalkDir(tmp, bpffn)))

	qt.Assert(t, qt.DeepEquals(entries, map[string]string{
		"pinned_prog":    "prog",
		"dir":            "",
		"dir/pinned_map": "map",
	}))

	qt.Assert(t, qt.IsNotNil(WalkDir("/", nil)))
}