From 910aba2e93263412435efde128255ccefeed03a1 Mon Sep 17 00:00:00 2001 From: Michael Wolf Date: Wed, 17 Jan 2024 16:25:05 -0800 Subject: [PATCH] Pass DB by reference in tests --- .../add_session_metadata.go | 3 +++ .../add_session_metadata/processdb/db_test.go | 4 ++-- .../processdb/entry_leader_test.go | 24 ++++++++++++------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/x-pack/auditbeat/processors/add_session_metadata/add_session_metadata.go b/x-pack/auditbeat/processors/add_session_metadata/add_session_metadata.go index 31aa881b692..9ad2e721c90 100644 --- a/x-pack/auditbeat/processors/add_session_metadata/add_session_metadata.go +++ b/x-pack/auditbeat/processors/add_session_metadata/add_session_metadata.go @@ -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 ( diff --git a/x-pack/auditbeat/processors/add_session_metadata/processdb/db_test.go b/x-pack/auditbeat/processors/add_session_metadata/processdb/db_test.go index 2450eb7ee7b..59074b153b9 100644 --- a/x-pack/auditbeat/processors/add_session_metadata/processdb/db_test.go +++ b/x-pack/auditbeat/processors/add_session_metadata/processdb/db_test.go @@ -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 } diff --git a/x-pack/auditbeat/processors/add_session_metadata/processdb/entry_leader_test.go b/x-pack/auditbeat/processors/add_session_metadata/processdb/entry_leader_test.go index b8b5348df6a..b6ae1b2d85d 100644 --- a/x-pack/auditbeat/processors/add_session_metadata/processdb/entry_leader_test.go +++ b/x-pack/auditbeat/processors/add_session_metadata/processdb/entry_leader_test.go @@ -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) ) @@ -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) @@ -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) @@ -102,12 +105,14 @@ 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) @@ -115,7 +120,8 @@ func requireGroupLeader(t *testing.T, db DB, pid uint32, pgid uint32) { 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) @@ -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)