Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions detector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (
}
return m
}()

defaultMinDurationFilter = 100 * time.Millisecond
zeroDurationFilter = time.Duration(0)
)

type testProcess struct {
Expand All @@ -54,13 +57,14 @@ func (p *testProcess) stop() {
}

type testCase struct {
name string
envVarsForExec map[string]string
envVarsToAssert map[string]string
exePath string
args []string
shouldDetect bool
expectedEvents []ProcessEventType
name string
envVarsForExec map[string]string
envVarsToAssert map[string]string
exePath string
args []string
shouldDetect bool
expectedEvents []ProcessEventType
minDurationFilter *time.Duration
}

func TestDetector(t *testing.T) {
Expand Down Expand Up @@ -124,12 +128,24 @@ func TestDetector(t *testing.T) {
shouldDetect: false, // Should be filtered out by environment variable filter
},
{
name: "short lived process",
name: "short lived process with duration filter",
envVarsForExec: map[string]string{"USER_ENV": "value"},
exePath: filepath.Join(testDir, "short_lived"),
args: []string{testFile},
shouldDetect: false, // Should be filtered out by duration filter
},
{
name: "short lived process with zero duration filter",
envVarsForExec: map[string]string{"USER_ENV": "value"},
exePath: filepath.Join(testDir, "short_lived"),
args: []string{testFile},
shouldDetect: true,
minDurationFilter: &zeroDurationFilter,
expectedEvents: []ProcessEventType{
ProcessExecEvent,
ProcessExitEvent,
},
},
{
name: "filtered process by env prefix",
envVarsForExec: map[string]string{},
Expand Down Expand Up @@ -159,13 +175,18 @@ func TestDetector(t *testing.T) {
}

opts := []DetectorOption{
WithMinDuration(100 * time.Millisecond),
WithExePathsToFilter("/usr/bin/bash"),
WithEnvironments("USER_ENV"),
WithEnvPrefixFilter("USER_E"),
WithFilesOpenTrigger(testFile, testFile2),
}

duration := defaultMinDurationFilter
if tc.minDurationFilter != nil {
duration = *tc.minDurationFilter
}
opts = append(opts, WithMinDuration(duration))

d, err := NewDetector(events, opts...)
require.NoError(t, err)

Expand Down
Loading