Skip to content

Commit

Permalink
test: Migrate tests to github.com/go-quicktest/qt
Browse files Browse the repository at this point in the history
Fixes: #1251
Signed-off-by: Tam Mach <[email protected]>
  • Loading branch information
sayboras committed Dec 6, 2023
1 parent c739d15 commit 8442057
Show file tree
Hide file tree
Showing 43 changed files with 536 additions and 571 deletions.
38 changes: 18 additions & 20 deletions asm/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"math"
"testing"

qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"
)

var test64bitImmProg = []byte{
Expand Down Expand Up @@ -110,23 +110,23 @@ func TestSignedJump(t *testing.T) {
func TestInstructionRewriteMapConstant(t *testing.T) {
ins := LoadMapValue(R0, 123, 321)

qt.Assert(t, ins.MapPtr(), qt.Equals, 123)
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(321))
qt.Assert(t, qt.Equals(ins.MapPtr(), 123))
qt.Assert(t, qt.Equals(ins.mapOffset(), 321))

qt.Assert(t, ins.RewriteMapPtr(-1), qt.IsNil)
qt.Assert(t, ins.MapPtr(), qt.Equals, -1)
qt.Assert(t, qt.IsNil(ins.RewriteMapPtr(-1)))
qt.Assert(t, qt.Equals(ins.MapPtr(), -1))

qt.Assert(t, ins.RewriteMapPtr(1), qt.IsNil)
qt.Assert(t, ins.MapPtr(), qt.Equals, 1)
qt.Assert(t, qt.IsNil(ins.RewriteMapPtr(1)))
qt.Assert(t, qt.Equals(ins.MapPtr(), 1))

// mapOffset should be unchanged after rewriting the pointer.
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(321))
qt.Assert(t, qt.Equals(ins.mapOffset(), 321))

qt.Assert(t, ins.RewriteMapOffset(123), qt.IsNil)
qt.Assert(t, ins.mapOffset(), qt.Equals, uint32(123))
qt.Assert(t, qt.IsNil(ins.RewriteMapOffset(123)))
qt.Assert(t, qt.Equals(ins.mapOffset(), 123))

// MapPtr should be unchanged.
qt.Assert(t, ins.MapPtr(), qt.Equals, 1)
qt.Assert(t, qt.Equals(ins.MapPtr(), 1))

ins = Mov.Imm(R1, 32)
if err := ins.RewriteMapPtr(1); err == nil {
Expand Down Expand Up @@ -304,34 +304,32 @@ func TestInstructionIterator(t *testing.T) {
}

func TestMetadataCopyOnWrite(t *testing.T) {
c := qt.New(t)

// Setting metadata should copy Instruction and modify the metadata pointer
// of the new object without touching the old Instruction.

// Reference
ins := Ja.Label("my_func")
ins2 := ins.WithReference("my_func2")

c.Assert(ins.Reference(), qt.Equals, "my_func", qt.Commentf("WithReference updated ins"))
c.Assert(ins2.Reference(), qt.Equals, "my_func2", qt.Commentf("WithReference didn't update ins2"))
qt.Assert(t, qt.Equals(ins.Reference(), "my_func"), qt.Commentf("WithReference updated ins"))
qt.Assert(t, qt.Equals(ins2.Reference(), "my_func2"), qt.Commentf("WithReference didn't update ins2"))

// Symbol
ins = Ja.Label("").WithSymbol("my_sym")
ins2 = ins.WithSymbol("my_sym2")

c.Assert(ins.Symbol(), qt.Equals, "my_sym", qt.Commentf("WithSymbol updated ins"))
c.Assert(ins2.Symbol(), qt.Equals, "my_sym2", qt.Commentf("WithSymbol didn't update ins2"))
qt.Assert(t, qt.Equals(ins.Symbol(), "my_sym"), qt.Commentf("WithSymbol updated ins"))
qt.Assert(t, qt.Equals(ins2.Symbol(), "my_sym2"), qt.Commentf("WithSymbol didn't update ins2"))

// Map
ins = LoadMapPtr(R1, 0)
ins2 = ins

testMap := testFDer(1)
c.Assert(ins2.AssociateMap(testMap), qt.IsNil, qt.Commentf("failed to associate map with ins2"))
qt.Assert(t, qt.IsNil(ins2.AssociateMap(testMap)), qt.Commentf("failed to associate map with ins2"))

c.Assert(ins.Map(), qt.IsNil, qt.Commentf("AssociateMap updated ins"))
c.Assert(ins2.Map(), qt.Equals, testMap, qt.Commentf("AssociateMap didn't update ins2"))
qt.Assert(t, qt.IsNil(ins.Map()), qt.Commentf("AssociateMap updated ins"))
qt.Assert(t, qt.Equals[FDer](ins2.Map(), testMap), qt.Commentf("AssociateMap didn't update ins2"))
}

type testFDer int
Expand Down
30 changes: 15 additions & 15 deletions asm/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@ import (
"testing"
"unsafe"

qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"
)

func TestMetadata(t *testing.T) {
var m Metadata

// Metadata should be the size of a pointer.
qt.Assert(t, unsafe.Sizeof(m), qt.Equals, unsafe.Sizeof(uintptr(0)))
qt.Assert(t, qt.Equals(unsafe.Sizeof(m), unsafe.Sizeof(uintptr(0))))

// A lookup in a nil meta should return nil.
qt.Assert(t, m.Get(bool(false)), qt.IsNil)
qt.Assert(t, qt.IsNil(m.Get(bool(false))))

// We can look up anything we inserted.
m.Set(bool(false), int(0))
m.Set(int(1), int(1))
qt.Assert(t, m.Get(bool(false)), qt.Equals, int(0))
qt.Assert(t, m.Get(int(1)), qt.Equals, int(1))
qt.Assert(t, qt.Equals(m.Get(bool(false)), 0))
qt.Assert(t, qt.Equals(m.Get(1), 1))

// We have copy on write semantics
old := m
m.Set(bool(false), int(1))
qt.Assert(t, m.Get(bool(false)), qt.Equals, int(1))
qt.Assert(t, m.Get(int(1)), qt.Equals, int(1))
qt.Assert(t, old.Get(bool(false)), qt.Equals, int(0))
qt.Assert(t, old.Get(int(1)), qt.Equals, int(1))
qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
qt.Assert(t, qt.Equals(old.Get(bool(false)), 0))
qt.Assert(t, qt.Equals(old.Get(int(1)), 1))

// Newtypes are handled distinctly.
type b bool
m.Set(b(false), int(42))
qt.Assert(t, m.Get(bool(false)), qt.Equals, int(1))
qt.Assert(t, m.Get(int(1)), qt.Equals, int(1))
qt.Assert(t, m.Get(b(false)), qt.Equals, int(42))
qt.Assert(t, qt.Equals(m.Get(bool(false)), 1))
qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
qt.Assert(t, qt.Equals(m.Get(b(false)), 42))

// Setting nil removes a key.
m.Set(bool(false), nil)
qt.Assert(t, m.Get(bool(false)), qt.IsNil)
qt.Assert(t, m.Get(int(1)), qt.Equals, int(1))
qt.Assert(t, m.Get(b(false)), qt.Equals, int(42))
qt.Assert(t, qt.IsNil(m.Get(bool(false))))
qt.Assert(t, qt.Equals(m.Get(int(1)), 1))
qt.Assert(t, qt.Equals(m.Get(b(false)), 42))
}

func BenchmarkMetadata(b *testing.B) {
Expand Down
10 changes: 5 additions & 5 deletions asm/opcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"
)

func TestGetSetJumpOp(t *testing.T) {
Expand All @@ -13,11 +13,11 @@ func TestGetSetJumpOp(t *testing.T) {
opcode := OpCode(class).SetJumpOp(op)

if valid {
qt.Assert(t, opcode, qt.Not(qt.Equals), InvalidOpCode)
qt.Assert(t, opcode.JumpOp(), qt.Equals, op)
qt.Assert(t, qt.Not(qt.Equals(opcode, InvalidOpCode)))
qt.Assert(t, qt.Equals(opcode.JumpOp(), op))
} else {
qt.Assert(t, opcode, qt.Equals, InvalidOpCode)
qt.Assert(t, opcode.JumpOp(), qt.Equals, InvalidJumpOp)
qt.Assert(t, qt.Equals(opcode, InvalidOpCode))
qt.Assert(t, qt.Equals(opcode.JumpOp(), InvalidJumpOp))
}
})
}
Expand Down
33 changes: 17 additions & 16 deletions btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"os"
"testing"

"github.com/go-quicktest/qt"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
qt "github.com/frankban/quicktest"
)

func vmlinuxSpec(tb testing.TB) *Spec {
Expand Down Expand Up @@ -339,10 +340,10 @@ func TestSpecCopy(t *testing.T) {
cpy := spec.Copy()

have := typesFromSpec(t, spec)
qt.Assert(t, len(spec.types) > 0, qt.IsTrue)
qt.Assert(t, qt.IsTrue(len(spec.types) > 0))

want := typesFromSpec(t, cpy)
qt.Assert(t, want, qt.HasLen, len(have))
qt.Assert(t, qt.HasLen(want, len(have)))

for i := range want {
if _, ok := have[i].(*Void); ok {
Expand All @@ -362,10 +363,10 @@ func TestSpecTypeByID(t *testing.T) {
spec := specFromTypes(t, nil)

_, err := spec.TypeByID(0)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, qt.IsNil(err))

_, err = spec.TypeByID(1)
qt.Assert(t, err, qt.ErrorIs, ErrNotFound)
qt.Assert(t, qt.ErrorIs(err, ErrNotFound))
}

func ExampleSpec_TypeByName() {
Expand Down Expand Up @@ -409,7 +410,7 @@ func TestTypesIterator(t *testing.T) {
t.Fatal("Iterator ended early at item", i)
}

qt.Assert(t, iter.Type, qt.DeepEquals, typ)
qt.Assert(t, qt.DeepEquals(iter.Type, typ))
}

if iter.Next() {
Expand Down Expand Up @@ -441,8 +442,8 @@ func TestLoadSplitSpecFromReader(t *testing.T) {
}

typeByID, err := splitSpec.TypeByID(typeID)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, typeByID, qt.Equals, typ)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(typeByID, typ))

fnType := typ.(*Func)
fnProto := fnType.Type.(*FuncProto)
Expand Down Expand Up @@ -492,15 +493,15 @@ func TestFixupDatasecLayout(t *testing.T) {
},
}

qt.Assert(t, fixupDatasecLayout(ds), qt.IsNil)
qt.Assert(t, qt.IsNil(fixupDatasecLayout(ds)))

qt.Assert(t, ds.Size, qt.Equals, uint32(40))
qt.Assert(t, ds.Vars[0].Offset, qt.Equals, uint32(0))
qt.Assert(t, ds.Vars[1].Offset, qt.Equals, uint32(4))
qt.Assert(t, ds.Vars[2].Offset, qt.Equals, uint32(5))
qt.Assert(t, ds.Vars[3].Offset, qt.Equals, uint32(6))
qt.Assert(t, ds.Vars[4].Offset, qt.Equals, uint32(16))
qt.Assert(t, ds.Vars[5].Offset, qt.Equals, uint32(32))
qt.Assert(t, qt.Equals(ds.Size, 40))
qt.Assert(t, qt.Equals(ds.Vars[0].Offset, 0))
qt.Assert(t, qt.Equals(ds.Vars[1].Offset, 4))
qt.Assert(t, qt.Equals(ds.Vars[2].Offset, 5))
qt.Assert(t, qt.Equals(ds.Vars[3].Offset, 6))
qt.Assert(t, qt.Equals(ds.Vars[4].Offset, 16))
qt.Assert(t, qt.Equals(ds.Vars[5].Offset, 32))
}

func BenchmarkSpecCopy(b *testing.B) {
Expand Down
39 changes: 20 additions & 19 deletions btf/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"strings"
"testing"

"github.com/google/go-cmp/cmp"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/google/go-cmp/cmp"

qt "github.com/frankban/quicktest"
"github.com/go-quicktest/qt"
"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -256,9 +257,9 @@ func TestCOREFindEnumValue(t *testing.T) {
for _, test := range valid {
t.Run(test.name, func(t *testing.T) {
local, target, err := coreFindEnumValue(test.local, test.acc, test.target)
qt.Assert(t, err, qt.IsNil)
qt.Check(t, local.Value, qt.Equals, test.localValue)
qt.Check(t, target.Value, qt.Equals, test.targetValue)
qt.Assert(t, qt.IsNil(err))
qt.Check(t, qt.Equals(local.Value, test.localValue))
qt.Check(t, qt.Equals(target.Value, test.targetValue))
})
}
}
Expand Down Expand Up @@ -523,7 +524,7 @@ func TestCOREFindField(t *testing.T) {
for _, test := range valid {
t.Run(test.name, func(t *testing.T) {
localField, targetField, err := coreFindField(test.local, test.acc, test.target)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, qt.IsNil(err))
checkCOREField(t, "local", localField, test.localField)
checkCOREField(t, "target", targetField, test.targetField)
})
Expand Down Expand Up @@ -632,8 +633,8 @@ func TestCORECopyWithoutQualifiers(t *testing.T) {
root.Type = test.fn(root)

cycle, ok := Copy(root, UnderlyingType).(*cycle)
qt.Assert(t, ok, qt.IsTrue)
qt.Assert(t, cycle.root, qt.Equals, root)
qt.Assert(t, qt.IsTrue(ok))
qt.Assert(t, qt.Equals[Type](cycle.root, root))
})
}

Expand All @@ -644,7 +645,7 @@ func TestCORECopyWithoutQualifiers(t *testing.T) {
want := &Pointer{Target: &Int{Name: "z"}}

got := Copy(v, UnderlyingType)
qt.Assert(t, got, qt.DeepEquals, want)
qt.Assert(t, qt.DeepEquals[Type](got, want))
})
}
}
Expand All @@ -660,7 +661,7 @@ func TestCORECopyWithoutQualifiers(t *testing.T) {
}

got := Copy(v, UnderlyingType)
qt.Assert(t, got, qt.DeepEquals, root)
qt.Assert(t, qt.DeepEquals[Type](got, root))
})
}

Expand All @@ -671,8 +672,8 @@ func TestCOREReloFieldSigned(t *testing.T) {
typ, coreAccessor{0}, reloFieldSigned, 0,
}
fixup, err := coreCalculateFixup(relo, &Void{}, 0, internal.NativeEndian)
qt.Assert(t, fixup.poison, qt.IsTrue)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, qt.IsTrue(fixup.poison))
qt.Assert(t, qt.IsNil(err))
})
}

Expand All @@ -681,7 +682,7 @@ func TestCOREReloFieldSigned(t *testing.T) {
&Array{}, coreAccessor{0}, reloFieldSigned, 0,
}
_, err := coreCalculateFixup(relo, &Array{}, 0, internal.NativeEndian)
qt.Assert(t, err, qt.ErrorIs, errNoSignedness)
qt.Assert(t, qt.ErrorIs(err, errNoSignedness))
})
}

Expand All @@ -698,7 +699,7 @@ func TestCOREReloFieldShiftU64(t *testing.T) {
} {
t.Run(relo.kind.String(), func(t *testing.T) {
_, err := coreCalculateFixup(relo, typ, 1, internal.NativeEndian)
qt.Assert(t, err, qt.ErrorIs, errUnsizedType)
qt.Assert(t, qt.ErrorIs(err, errUnsizedType))
})
}
}
Expand All @@ -708,22 +709,22 @@ func BenchmarkCORESkBuff(b *testing.B) {

var skb *Struct
err := spec.TypeByName("sk_buff", &skb)
qt.Assert(b, err, qt.IsNil)
qt.Assert(b, qt.IsNil(err))

skbID, err := spec.TypeID(skb)
qt.Assert(b, err, qt.IsNil)
qt.Assert(b, qt.IsNil(err))

lenIndex := slices.IndexFunc(skb.Members, func(m Member) bool {
return m.Name == "len"
})
qt.Assert(b, lenIndex, qt.Not(qt.Equals), -1)
qt.Assert(b, qt.Not(qt.Equals(lenIndex, -1)))

var pktHashTypes *Enum
err = spec.TypeByName("pkt_hash_types", &pktHashTypes)
qt.Assert(b, err, qt.IsNil)
qt.Assert(b, qt.IsNil(err))

pktHashTypesID, err := spec.TypeID(pktHashTypes)
qt.Assert(b, err, qt.IsNil)
qt.Assert(b, qt.IsNil(err))

for _, relo := range []*CORERelocation{
{skb, coreAccessor{0, lenIndex}, reloFieldByteOffset, skbID},
Expand Down
Loading

0 comments on commit 8442057

Please sign in to comment.