Skip to content

Commit

Permalink
Pass DB by reference in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjwolf committed Jan 18, 2024
1 parent bf38e89 commit 910aba2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build linux
// +build linux

package add_session_metadata

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ var logger = logp.NewLogger("processdb")
var reader = procfs.NewMockReader()

// glue function to fit the return type required by these tests
func newDBIntf(reader procfs.Reader) DB {
func newDBIntf(reader procfs.Reader) *DB {
ret := NewDB(reader, *logger)
_ = ret.ScrapeProcfs()
return *ret
return ret
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (

"github.com/elastic/beats/v7/x-pack/auditbeat/processors/add_session_metadata/procfs"
"github.com/elastic/beats/v7/x-pack/auditbeat/processors/add_session_metadata/types"
// "github.com/elastic/elastic-agent-libs/logp"
)

type (
createDBFn func(procfs.Reader) DB
createDBFn func(procfs.Reader) *DB
testFn func(*testing.T)
)

Expand Down Expand Up @@ -69,7 +68,8 @@ const (
// These tests should effectively serve as the spec for how we assign entry
// leaders. When further entry meta types or cases are added, tests should be

func requireProcess(t *testing.T, db DB, pid uint32, processPath string) {
func requireProcess(t *testing.T, db *DB, pid uint32, processPath string) {
t.Helper()
process, err := db.GetProcess(pid)
require.Nil(t, err)
require.Equal(t, pid, process.PID)
Expand All @@ -81,19 +81,22 @@ func requireProcess(t *testing.T, db DB, pid uint32, processPath string) {
}
}

func requireParent(t *testing.T, db DB, pid uint32, ppid uint32) {
func requireParent(t *testing.T, db *DB, pid uint32, ppid uint32) {
t.Helper()
process, err := db.GetProcess(pid)
require.Nil(t, err)
require.Equal(t, ppid, process.Parent.PID)
}

func requireParentUnset(t *testing.T, process types.Process) {
t.Helper()
require.Equal(t, "", process.Parent.EntityID)
require.Equal(t, uint32(0), process.Parent.PID)
require.Nil(t, process.Parent.Start)
}

func requireSessionLeader(t *testing.T, db DB, pid uint32, sid uint32) {
func requireSessionLeader(t *testing.T, db *DB, pid uint32, sid uint32) {
t.Helper()
process, err := db.GetProcess(pid)
require.Nil(t, err)
require.Equal(t, sid, process.SessionLeader.PID)
Expand All @@ -102,20 +105,23 @@ func requireSessionLeader(t *testing.T, db DB, pid uint32, sid uint32) {
}

func requireSessionLeaderUnset(t *testing.T, process types.Process) {
t.Helper()
require.Equal(t, "", process.SessionLeader.EntityID)
require.Equal(t, uint32(0), process.SessionLeader.PID)
require.Nil(t, process.SessionLeader.Start)
}

func requireGroupLeader(t *testing.T, db DB, pid uint32, pgid uint32) {
func requireGroupLeader(t *testing.T, db *DB, pid uint32, pgid uint32) {
t.Helper()
process, err := db.GetProcess(pid)
require.Nil(t, err)
require.Equal(t, pgid, process.GroupLeader.PID)
require.NotNil(t, process.GroupLeader.SameAsProcess)
require.Equal(t, pid == pgid, *process.GroupLeader.SameAsProcess)
}

func requireEntryLeader(t *testing.T, db DB, pid uint32, entryPid uint32, expectedEntryType EntryType) {
func requireEntryLeader(t *testing.T, db *DB, pid uint32, entryPid uint32, expectedEntryType EntryType) {
t.Helper()
process, err := db.GetProcess(pid)
require.Nil(t, err)
require.Equal(t, entryPid, process.EntryLeader.PID)
Expand All @@ -128,13 +134,15 @@ func requireEntryLeader(t *testing.T, db DB, pid uint32, entryPid uint32, expect
}

func requireEntryLeaderUnset(t *testing.T, process types.Process) {
t.Helper()
require.Equal(t, "", process.EntryLeader.EntityID)
require.Equal(t, uint32(0), process.EntryLeader.PID)
require.Nil(t, process.EntryLeader.Start)
}

// tries to construct fork event from what's in the db
func insertForkAndExec(t *testing.T, db DB, exec types.ProcessExecEvent) {
func insertForkAndExec(t *testing.T, db *DB, exec types.ProcessExecEvent) {
t.Helper()
var fork types.ProcessForkEvent
fork.ChildPids = exec.Pids
parent, err := db.GetProcess(exec.Pids.Ppid)
Expand Down

0 comments on commit 910aba2

Please sign in to comment.