Skip to content

Commit

Permalink
chore(test): enhance tests using t.Parallel()
Browse files Browse the repository at this point in the history
Utilize t.Parallel() in unit tests to enable concurrent execution. This
takes advantage of multiple CPU cores, reducing the overall test suite
runtime.

Critically, this practice elevates the detection of insidious race
conditions and state leakages—issues that often remain concealed during
serial test execution.
  • Loading branch information
geyslan authored and rafaeldtinoco committed Oct 3, 2023
1 parent 96e29d5 commit 68aa961
Show file tree
Hide file tree
Showing 91 changed files with 950 additions and 93 deletions.
2 changes: 2 additions & 0 deletions cmd/tracee-bench/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestParseQueryResString(t *testing.T) {
t.Parallel()

testCases := []struct {
resultString string
expectedResult float64
Expand Down
4 changes: 4 additions & 0 deletions cmd/tracee-rules/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func Test_listSigs(t *testing.T) {
t.Parallel()

fakeSigs := []*signature.FakeSignature{
{
FakeGetMetadata: func() (detect.SignatureMetadata, error) {
Expand Down Expand Up @@ -54,6 +56,8 @@ BAR-1 bar signature 4.5.6 bar signature helps with
}

func Test_listEvents(t *testing.T) {
t.Parallel()

fakeSigs := []*signature.FakeSignature{
{
FakeGetSelectedEvents: func() ([]detect.SignatureEventSelector, error) {
Expand Down
14 changes: 14 additions & 0 deletions cmd/tracee-rules/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
)

func Test_setupOutput(t *testing.T) {
t.Parallel()

var testCases = []struct {
name string
inputEvent protocol.Event
Expand Down Expand Up @@ -125,6 +127,8 @@ func checkOutput(t *testing.T, testName string, actualOutput *SyncBuffer, expect
}

func Test_sendToWebhook(t *testing.T) {
t.Parallel()

var testCases = []struct {
name string
inputTemplateFile string
Expand Down Expand Up @@ -170,7 +174,11 @@ func Test_sendToWebhook(t *testing.T) {
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

ts := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
got, err := io.ReadAll(request.Body)
require.NoError(t, err)
Expand Down Expand Up @@ -209,6 +217,8 @@ func Test_sendToWebhook(t *testing.T) {
}

func TestOutputTemplates(t *testing.T) {
t.Parallel()

testCases := []struct {
testName string
finding detect.Finding
Expand Down Expand Up @@ -272,7 +282,11 @@ func TestOutputTemplates(t *testing.T) {
require.NoError(t, err)

for _, tc := range testCases {
tc := tc

t.Run(tc.testName, func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
err := jsonTemplate.Execute(&buf, tc.finding)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/bucketscache/bucketscache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestBucketsCache(t *testing.T) {
t.Parallel()

var cache bucketscache.BucketsCache
cache.Init(5)
cache.AddBucketItem(1, 32)
Expand Down
42 changes: 42 additions & 0 deletions pkg/bufferdecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

func TestDecodeContext(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
ctxExpected := Context{
Ts: 11,
Expand Down Expand Up @@ -49,6 +51,8 @@ func TestDecodeContext(t *testing.T) {
}

func TestDecodeUint8(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint8 = 42
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -69,6 +73,8 @@ func TestDecodeUint8(t *testing.T) {
}

func TestDecodeInt8(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected int8 = -42
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -89,6 +95,8 @@ func TestDecodeInt8(t *testing.T) {
}

func TestDecodeUint16(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint16 = 5555
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -109,6 +117,8 @@ func TestDecodeUint16(t *testing.T) {
}

func TestDecodeUint16BigEndian(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint16 = 5555
err := binary.Write(buf, binary.BigEndian, expected)
Expand All @@ -128,6 +138,8 @@ func TestDecodeUint16BigEndian(t *testing.T) {
assert.Equal(t, 2, cursorAfter-cursorBefore) // cursor should move 2 byte
}
func TestDecodeInt16(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected int16 = -3456
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -148,6 +160,8 @@ func TestDecodeInt16(t *testing.T) {
}

func TestDecodeUint32(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint32 = 5555
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -168,6 +182,8 @@ func TestDecodeUint32(t *testing.T) {
}

func TestDecodeUint32BigEndian(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint32 = 5555
err := binary.Write(buf, binary.BigEndian, expected)
Expand All @@ -187,6 +203,8 @@ func TestDecodeUint32BigEndian(t *testing.T) {
assert.Equal(t, cursorAfter-cursorBefore, 4) // cursor should move 4 byte
}
func TestDecodeInt32(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected int32 = -3456
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -207,6 +225,8 @@ func TestDecodeInt32(t *testing.T) {
}

func TestDecodeUint64(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected uint64 = 5555
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -227,6 +247,8 @@ func TestDecodeUint64(t *testing.T) {
}

func TestDecodeInt64(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
var expected int64 = -3456
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -247,6 +269,8 @@ func TestDecodeInt64(t *testing.T) {
}

func TestDecodeBoolTrue(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := true
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -267,6 +291,8 @@ func TestDecodeBoolTrue(t *testing.T) {
}

func TestDecodeBoolFalse(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := false
err := binary.Write(buf, binary.LittleEndian, expected)
Expand All @@ -288,6 +314,8 @@ func TestDecodeBoolFalse(t *testing.T) {

// TODO DecodeBytes and DecodeIntArray
func TestDecodeBytes(t *testing.T) {
t.Parallel()

type JustAStruct struct {
A1 uint32
A2 uint64
Expand All @@ -313,6 +341,8 @@ func TestDecodeBytes(t *testing.T) {
}

func TestDecodeIntArray(t *testing.T) {
t.Parallel()

var raw []byte
raw = append(raw, 1, 2, 3, 4, 5, 6, 7, 8)
decoder := New(raw)
Expand All @@ -329,6 +359,8 @@ func TestDecodeIntArray(t *testing.T) {
}

func TestDecodeSlimCred(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := SlimCred{
Uid: 43,
Expand Down Expand Up @@ -358,6 +390,8 @@ func TestDecodeSlimCred(t *testing.T) {
}

func TestDecodeChunkMeta(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := ChunkMeta{
BinType: 54,
Expand All @@ -377,6 +411,8 @@ func TestDecodeChunkMeta(t *testing.T) {
}

func TestDecodeVfsWriteMeta(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := VfsFileMeta{
DevID: 54,
Expand All @@ -395,6 +431,8 @@ func TestDecodeVfsWriteMeta(t *testing.T) {
}

func TestDecodeKernelModuleMeta(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := KernelModuleMeta{
DevID: 7489,
Expand All @@ -413,6 +451,8 @@ func TestDecodeKernelModuleMeta(t *testing.T) {
}

func TestDecodeBpfObjectMeta(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := BpfObjectMeta{
Name: [16]byte{80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80},
Expand All @@ -431,6 +471,8 @@ func TestDecodeBpfObjectMeta(t *testing.T) {
}

func TestDecodeMprotectWriteMeta(t *testing.T) {
t.Parallel()

buf := new(bytes.Buffer)
expected := MprotectWriteMeta{
Pid: 12,
Expand Down
36 changes: 24 additions & 12 deletions pkg/bufferdecoder/eventsreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestReadArgFromBuff(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
input []byte
Expand Down Expand Up @@ -165,22 +167,30 @@ func TestReadArgFromBuff(t *testing.T) {
}

for _, tc := range testCases {
decoder := New(tc.input)
_, actual, err := readArgFromBuff(0, decoder, tc.params)

if tc.expectedError != nil {
assert.ErrorContains(t, err, tc.expectedError.Error())
}
assert.Equal(t, tc.expectedArg, actual.Value)

if tc.name == "unknown" {
continue
}
assert.Empty(t, decoder.BuffLen()-decoder.ReadAmountBytes(), tc.name) // passed in buffer should be emptied out
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

decoder := New(tc.input)
_, actual, err := readArgFromBuff(0, decoder, tc.params)

if tc.expectedError != nil {
assert.ErrorContains(t, err, tc.expectedError.Error())
}
assert.Equal(t, tc.expectedArg, actual.Value)

if tc.name == "unknown" {
return
}
assert.Empty(t, decoder.BuffLen()-decoder.ReadAmountBytes(), tc.name) // passed in buffer should be emptied out
})
}
}

func TestPrintUint32IP(t *testing.T) {
t.Parallel()

var input uint32 = 3232238339
ip := PrintUint32IP(input)

Expand All @@ -189,6 +199,8 @@ func TestPrintUint32IP(t *testing.T) {
}

func TestPrint16BytesSliceIP(t *testing.T) {
t.Parallel()

input := []byte{32, 1, 13, 184, 133, 163, 0, 0, 0, 0, 138, 46, 3, 112, 115, 52}
ip := Print16BytesSliceIP(input)

Expand Down
12 changes: 12 additions & 0 deletions pkg/bufferdecoder/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,47 @@ import (
// If so, then this means you need to update the decoder functions and GetSizeBytes functions.

func TestContextSize(t *testing.T) {
t.Parallel()

var v Context
size := int(unsafe.Sizeof(v))
assert.Equal(t, size, int(v.GetSizeBytes()))
}
func TestChunkMetaSize(t *testing.T) {
t.Parallel()

var v ChunkMeta
size := int(unsafe.Sizeof(v))
assert.Equal(t, size-7, int(v.GetSizeBytes()))
}

func TestVfsWriteMetaSize(t *testing.T) {
t.Parallel()

var v VfsFileMeta
size := int(unsafe.Sizeof(v))
assert.Equal(t, size-4, int(v.GetSizeBytes()))
}

func TestKernelModuleMetaSize(t *testing.T) {
t.Parallel()

var v KernelModuleMeta
size := int(unsafe.Sizeof(v))
assert.Equal(t, size-4, int(v.GetSizeBytes()))
}

func TestBpfObjectMetaSize(t *testing.T) {
t.Parallel()

var v BpfObjectMeta
size := int(unsafe.Sizeof(v))
assert.Equal(t, size, int(v.GetSizeBytes()))
}

func TestMprotectWriteMetaSize(t *testing.T) {
t.Parallel()

var v MprotectWriteMeta
size := int(unsafe.Sizeof(v))
assert.Equal(t, size-4, int(v.GetSizeBytes()))
Expand Down
2 changes: 2 additions & 0 deletions pkg/changelog/changelog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestChangelog(t *testing.T) {
t.Parallel()

cl := NewChangelog[int]()

// Test GetCurrent on an empty changelog
Expand Down
Loading

0 comments on commit 68aa961

Please sign in to comment.