Skip to content

Commit b393417

Browse files
test: add tests and examples for ErrNotPermitted
Add TestErrNotPermitted to verify: - ErrNotPermitted is distinct from ErrNotSupported - Wrapped errors can be matched with errors.Is() Update DocDetectXDP example to show how to handle ErrNotPermitted in addition to ErrNotSupported. Signed-off-by: Javier Cardona <jcardona@meta.com>
1 parent 98943f0 commit b393417

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

docs/examples/features_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ func DocDetectXDP() {
1616
fmt.Println("XDP program type is not supported")
1717
return
1818
}
19+
if errors.Is(err, ebpf.ErrNotPermitted) {
20+
fmt.Println("XDP program type is supported but permission denied")
21+
return
22+
}
1923
if err != nil {
2024
// Feature detection was inconclusive.
2125
//
2226
// Note: always log and investigate these errors! These can be caused
23-
// by a lack of permissions, verifier errors, etc. Unless stated
24-
// otherwise, probes are expected to be conclusive. Please file
25-
// an issue if this is not the case in your environment.
27+
// by verifier errors, etc. Unless stated otherwise, probes are
28+
// expected to be conclusive. Please file an issue if this is not the
29+
// case in your environment.
2630
panic(err)
2731
}
2832

internal/feature_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package internal
22

33
import (
44
"errors"
5+
"fmt"
56
"runtime"
67
"strings"
78
"testing"
@@ -88,3 +89,14 @@ func TestFeatureTestNotSupportedOnOS(t *testing.T) {
8889
qt.Assert(t, qt.ErrorIs(NewFeatureTest("foo", fn, "1.0")(), sentinel))
8990
}
9091
}
92+
93+
func TestErrNotPermitted(t *testing.T) {
94+
// ErrNotPermitted should be distinct from ErrNotSupported
95+
qt.Assert(t, qt.Not(qt.ErrorIs(ErrNotPermitted, ErrNotSupported)))
96+
qt.Assert(t, qt.Not(qt.ErrorIs(ErrNotSupported, ErrNotPermitted)))
97+
98+
// Wrapped errors should be matchable
99+
wrapped := fmt.Errorf("%w: some details", ErrNotPermitted)
100+
qt.Assert(t, qt.ErrorIs(wrapped, ErrNotPermitted))
101+
qt.Assert(t, qt.Not(qt.ErrorIs(wrapped, ErrNotSupported)))
102+
}

0 commit comments

Comments
 (0)