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 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package internal
import (
"errors"
"strings"
"testing"
"github.com/cilium/ebpf/internal/testutils/fdtrace"
)
func TestMain(m *testing.M) {
fdtrace.TestMain(m)
}
func TestFeatureTest(t *testing.T) {
var called bool
fn := NewFeatureTest("foo", "1.0", func() error {
called = true
return nil
})
if called {
t.Error("Function was called too early")
}
err := fn()
if !called {
t.Error("Function wasn't called")
}
if err != nil {
t.Error("Unexpected negative result:", err)
}
fn = NewFeatureTest("bar", "2.1.1", func() error {
return ErrNotSupported
})
err = fn()
if err == nil {
t.Fatal("Unexpected positive result")
}
fte, ok := err.(*UnsupportedFeatureError)
if !ok {
t.Fatal("Result is not a *UnsupportedFeatureError")
}
if !strings.Contains(fte.Error(), "2.1.1") {
t.Error("UnsupportedFeatureError.Error doesn't contain version")
}
if !errors.Is(err, ErrNotSupported) {
t.Error("UnsupportedFeatureError is not ErrNotSupported")
}
err2 := fn()
if err != err2 {
t.Error("Didn't cache an error wrapping ErrNotSupported")
}
fn = NewFeatureTest("bar", "2.1.1", func() error {
return errors.New("foo")
})
err1, err2 := fn(), fn()
if err1 == err2 {
t.Error("Cached result of unsuccessful execution")
}
}
|