testutils

package
v0.24.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2025 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SIGTERM = "-15"
	SIGKILL = "-9"
)
View Source
const CPUForTests = 0 // CPU to pin test processes to
View Source
const (
	TraceeDefaultStartupTimeout = 15 * time.Second
)

Variables

View Source
var (
	TraceeBinary   = "../../dist/tracee"
	TraceeHostname = "localhost"
	TraceePort     = 3369
)

Functions

func AssureIsRoot added in v0.24.0

func AssureIsRoot(t *testing.T)

AssureIsRoot skips the test if it is not run as root

func BuildPoliciesFromEvents added in v0.22.0

func BuildPoliciesFromEvents(eventsToChoose []events.ID) []*policy.Policy

BuildPoliciesFromEvents create a Policies instance with a single policy, which chooses the given events without filters or scopes

func DiscoverChildProcesses

func DiscoverChildProcesses(pid int) ([]int, error)

DiscoverChildProcesses discovers all child processes of a given PID.

func EnableTestLogger added in v0.24.0

func EnableTestLogger(t *testing.T, level logger.Level) func()

EnableTestLogger configures the logger to output directly to the test's log output. This is useful for debugging tests as all logger output will be visible in test results. Call the returned function to restore the original logger.

Example usage:

func TestSomething(t *testing.T) {
    defer testutils.EnableTestLogger(t, logger.DebugLevel)()
    // ... your test code ...
}

func ExecCmdBgWithSudoAndCtx

func ExecCmdBgWithSudoAndCtx(ctx context.Context, command string) (int, chan error, error)

ExecCmdBgWithSudoAndCtx executes a command with sudo in the background, and returns the PID of the process and a channel to wait for the command to exit (Check RunningTracee object about how to use this). The function will return an error if the command execution fails

func ExecPinnedCmdWithTimeout

func ExecPinnedCmdWithTimeout(command string, timeout time.Duration) (int, error)

ExecPinnedCmdWithTimeout executes a cmd with a timeout and returns the PID of the process.

func GetProcNS added in v0.24.0

func GetProcNS(nsName string) string

func IsSudoCmdAvailableForThisUser

func IsSudoCmdAvailableForThisUser() bool

IsSudoCmdAvailableForThisUser checks if the sudo command is available for the current user.

func NewPolicies added in v0.22.0

func NewPolicies(polsFilesID []PolicyFileWithID) []*policy.Policy

NewPolicies creates a slice of policies setting the ID of each policy to the given ID.

func ParseCmd

func ParseCmd(fullCmd string) (string, []string, error)

ParseCmd parses a command string into a command and arguments.

func PinProccessToCPU

func PinProccessToCPU(id ...int) error

PinProccessToCPU pins the current process to a specific CPU

func PrepareCapture added in v0.24.0

func PrepareCapture() *config.CaptureConfig

prepareCapture prepares a capture config for tracee

func SetTestLogger added in v0.22.0

func SetTestLogger(t *testing.T, l logger.Level) (loggerOutput <-chan []byte, restoreLogger func())

SetTestLogger create a logger which prints the logs to the returned channel. This function is meant to be used by tests to check logs, and by that test the flow of Tracee from outside.

func StartTracee added in v0.24.0

func StartTracee(ctx context.Context, t *testing.T, cfg config.Config, output *config.OutputConfig, capture *config.CaptureConfig) (*tracee.Tracee, error)

load tracee into memory with args

func SudoKillProcess

func SudoKillProcess(pid int, force bool) error

SudoKillProcess kills a process with sudo.

func TestLogs added in v0.22.0

func TestLogs(
	t *testing.T,
	logsToSearch []string,
	logsChan <-chan []byte,
	done <-chan struct{},
) <-chan bool

TestLogs searches for the given logs and test when input channel closes if all logs were received. It also returns a channel with the result of the test - whether all logs were found.

func WaitForTraceeOutputEvents added in v0.24.0

func WaitForTraceeOutputEvents(t *testing.T, waitFor time.Duration, actual *EventBuffer, expectedEvts int, failOnTimeout bool) error

wait for tracee buffer to fill up with expected number of events (or timeout) in case of timeout, the test will fail

func WaitForTraceeStart added in v0.24.0

func WaitForTraceeStart(trc *tracee.Tracee) error

wait for tracee to start (or timeout) in case of timeout, the test will fail

func WaitForTraceeStop added in v0.24.0

func WaitForTraceeStop(trc *tracee.Tracee) error

wait for tracee to stop (or timeout) in case of timeout, the test will continue since all tests already passed

Types

type EventBuffer added in v0.24.0

type EventBuffer struct {
	// contains filtered or unexported fields
}

EventBuffer is a thread-safe buffer for tracee events

func NewEventBuffer added in v0.24.0

func NewEventBuffer() *EventBuffer

func (*EventBuffer) AddEvent added in v0.24.0

func (b *EventBuffer) AddEvent(evt trace.Event)

AddEvent adds an event to the EventBuffer

func (*EventBuffer) Clear added in v0.24.0

func (b *EventBuffer) Clear()

Clear clears the EventBuffer

func (*EventBuffer) GetCopy added in v0.24.0

func (b *EventBuffer) GetCopy() []trace.Event

getCopy returns a copy of the eventBuffer events

type PolicyFileWithID added in v0.22.0

type PolicyFileWithID struct {
	PolicyFile v1beta1.PolicyFile
	Id         int
}

type RunningTracee

type RunningTracee struct {
	// contains filtered or unexported fields
}

RunningTracee is a wrapper for a running tracee process as a regular process.

func NewRunningTracee

func NewRunningTracee(givenCtx context.Context, cmdLine string) *RunningTracee

NewRunningTracee creates a new RunningTracee instance.

func (*RunningTracee) IsReady

func (r *RunningTracee) IsReady() bool

IsReady checks if the tracee process is ready.

func (*RunningTracee) Start

func (r *RunningTracee) Start(timeout time.Duration) (<-chan TraceeStatus, error)

Start starts the tracee process.

func (*RunningTracee) Stop

func (r *RunningTracee) Stop() []error

Stop stops the tracee process.

type TraceeStatus

type TraceeStatus int
const (
	TraceeStarted TraceeStatus = iota
	TraceeFailed
	TraceeTimedout
	TraceeAlreadyRunning
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL