Debian Codegraph @

internal/testutils/feature.go

raw symbols SCIP
1 package testutils
2
3 import (
4 "encoding/binary"
5 "errors"
6 "os"
7 "runtime"
8 "strings"
9 "testing"
10
11 "github.com/go-quicktest/qt"
12
13 "github.com/cilium/ebpf/internal"
14 "github.com/cilium/ebpf/internal/platform"
15 )
16
17 const (
18 ignoreVersionEnvVar = "EBPF_TEST_IGNORE_VERSION"
19 )
20
21 func CheckFeatureTest(t *testing.T, fn func() error) {
22 t.Helper()
23
24 checkFeatureTestError(t, fn())
25 }
26
27 func checkFeatureTestError(t *testing.T, err error) {
28 t.Helper()
29
30 if err == nil {
31 return
32 }
33
34 if errors.Is(err, internal.ErrNotSupportedOnOS) {
35 t.Skip(err)
36 }
37
38 var ufe *internal.UnsupportedFeatureError
39 if errors.As(err, &ufe) {
40 checkVersion(t, ufe)
41 } else {
42 t.Error("Feature test failed:", err)
43 }
44 }
45
46 func CheckFeatureMatrix[K comparable](t *testing.T, fm internal.FeatureMatrix[K]) {
47 t.Helper()
48
49 for key, ft := range fm {
50 t.Run(ft.Name, func(t *testing.T) {
51 checkFeatureTestError(t, fm.Result(key))
52 })
53 }
54 }
55
56 func SkipIfNotSupported(tb testing.TB, err error) {
57 tb.Helper()
58
59 if err == internal.ErrNotSupported {
60 tb.Fatal("Unwrapped ErrNotSupported")
61 }
62
63 var ufe *internal.UnsupportedFeatureError
64 if errors.As(err, &ufe) {
65 checkVersion(tb, ufe)
66 tb.Skip(ufe.Error())
67 }
68 if errors.Is(err, internal.ErrNotSupported) {
69 tb.Skip(err.Error())
70 }
71 }
72
73 func SkipIfNotSupportedOnOS(tb testing.TB, err error) {
74 tb.Helper()
75
76 if err == internal.ErrNotSupportedOnOS {
77 tb.Fatal("Unwrapped ErrNotSupportedOnOS")
78 }
79
80 if errors.Is(err, internal.ErrNotSupportedOnOS) {
81 tb.Skip(err.Error())
82 }
83 }
84
85 func checkVersion(tb testing.TB, ufe *internal.UnsupportedFeatureError) {
86 if ufe.MinimumVersion.Unspecified() {
87 return
88 }
89
90 tb.Helper()
91
92 if ignoreVersionCheck(tb.Name()) {
93 tb.Logf("Ignoring error due to %s: %s", ignoreVersionEnvVar, ufe.Error())
94 return
95 }
96
97 if !isPlatformVersionLessThan(tb, ufe.MinimumVersion, platformVersion(tb)) {
98 tb.Fatalf("Feature '%s' isn't supported even though kernel is newer than %s",
99 ufe.Name, ufe.MinimumVersion)
100 }
101 }
102
103 // Skip a test based on the Linux version we are running on.
104 //
105 // Warning: this function does not have an effect on platforms other than Linux.
106 func SkipOnOldKernel(tb testing.TB, minVersion, feature string) {
107 tb.Helper()
108
109 if !platform.IsLinux {
110 tb.Logf("Ignoring version constraint %s for %s on %s", minVersion, feature, runtime.GOOS)
111 return
112 }
113
114 if IsVersionLessThan(tb, minVersion) {
115 tb.Skipf("Test requires at least kernel %s (due to missing %s)", minVersion, feature)
116 }
117 }
118
119 // Check whether the current runtime version is less than some minimum.
120 func IsVersionLessThan(tb testing.TB, minVersions ...string) bool {
121 tb.Helper()
122
123 version, err := platform.SelectVersion(minVersions)
124 qt.Assert(tb, qt.IsNil(err))
125
126 if version == "" {
127 // No matching version means that the platform
128 // doesn't support whatever feature.
129 return true
130 }
131
132 minv, err := internal.NewVersion(version)
133 if err != nil {
134 tb.Fatalf("Invalid version %s: %s", version, err)
135 }
136
137 return isPlatformVersionLessThan(tb, minv, platformVersion(tb))
138 }
139
140 func isPlatformVersionLessThan(tb testing.TB, minv, runv internal.Version) bool {
141 tb.Helper()
142
143 key := "CI_MAX_KERNEL_VERSION"
144 if platform.IsWindows {
145 key = "CI_MAX_EFW_VERSION"
146 }
147
148 if max := os.Getenv(key); max != "" {
149 maxv, err := internal.NewVersion(max)
150 if err != nil {
151 tb.Fatalf("Invalid version %q in %s: %s", max, key, err)
152 }
153
154 if maxv.Less(minv) {
155 tb.Fatalf("Test for %s will never execute on CI since %s is the most recent runtime", minv, maxv)
156 }
157 }
158
159 return runv.Less(minv)
160 }
161
162 // ignoreVersionCheck checks whether to omit the version check for a test.
163 //
164 // It reads a comma separated list of test names from an environment variable.
165 //
166 // For example:
167 //
168 // EBPF_TEST_IGNORE_VERSION=TestABC,TestXYZ go test ...
169 func ignoreVersionCheck(tName string) bool {
170 tNames := os.Getenv(ignoreVersionEnvVar)
171 if tNames == "" {
172 return false
173 }
174
175 ignored := strings.SplitSeq(tNames, ",")
176 for n := range ignored {
177 if strings.TrimSpace(n) == tName {
178 return true
179 }
180 }
181 return false
182 }
183
184 // SkipNonNativeEndian skips the test or benchmark if bo doesn't match the
185 // host's native endianness.
186 func SkipNonNativeEndian(tb testing.TB, bo binary.ByteOrder) {
187 tb.Helper()
188
189 if bo != internal.NativeEndian {
190 tb.Skip("Skipping due to non-native endianness")
191 }
192 }