| 1 |
package testutils |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"math/rand" |
| 6 |
"path/filepath" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-quicktest/qt" |
| 12 |
|
| 13 |
"github.com/cilium/ebpf/internal/efw" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
func TempBPFFS(tb testing.TB) string { |
| 18 |
tb.Helper() |
| 19 |
|
| 20 |
path := filepath.Join("ebpf-go-test", strconv.Itoa(rand.Int())) |
| 21 |
path, err := efw.EbpfCanonicalizePinPath(path) |
| 22 |
qt.Assert(tb, qt.IsNil(err)) |
| 23 |
|
| 24 |
tb.Cleanup(func() { |
| 25 |
tb.Helper() |
| 26 |
|
| 27 |
cursor := path |
| 28 |
for { |
| 29 |
next, _, err := efw.EbpfGetNextPinnedObjectPath(cursor, efw.EBPF_OBJECT_UNKNOWN) |
| 30 |
if errors.Is(err, efw.EBPF_NO_MORE_KEYS) { |
| 31 |
break |
| 32 |
} |
| 33 |
qt.Assert(tb, qt.IsNil(err)) |
| 34 |
|
| 35 |
if !strings.HasPrefix(next, path) { |
| 36 |
break |
| 37 |
} |
| 38 |
|
| 39 |
if err := efw.EbpfObjectUnpin(next); err != nil { |
| 40 |
tb.Errorf("Failed to unpin %s: %s", next, err) |
| 41 |
} |
| 42 |
|
| 43 |
cursor = next |
| 44 |
} |
| 45 |
}) |
| 46 |
|
| 47 |
return path |
| 48 |
} |