Skip to content

Commit

Permalink
Make TestContainerCreatedAt stable
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Mar 4, 2025
1 parent 41cf327 commit 8f57d30
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkg/security/tests/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestContainerCreatedAt(t *testing.T) {
},
{
ID: "test_container_created_at_delay",
Expression: `container.id != "" && container.created_at > 3s && open.file.path == "{{.Root}}/test-open-delay"`,
Expression: `container.id != "" && container.created_at > 3s && open.file.path in [ "{{.Root}}/test-open-delay1", "{{.Root}}/test-open-delay2" ]`,
},
}
test, err := newTestModule(t, nil, ruleDefs)
Expand All @@ -49,7 +49,12 @@ func TestContainerCreatedAt(t *testing.T) {
t.Fatal(err)
}

testFileDelay, _, err := test.Path("test-open-delay")
testFileDelay1, _, err := test.Path("test-open-delay1")
if err != nil {
t.Fatal(err)
}

testFileDelay2, _, err := test.Path("test-open-delay2")
if err != nil {
t.Fatal(err)
}
Expand All @@ -76,16 +81,24 @@ func TestContainerCreatedAt(t *testing.T) {

dockerWrapper.Run(t, "container-created-at-delay", func(t *testing.T, _ wrapperType, cmdFunc func(cmd string, args []string, envs []string) *exec.Cmd) {
test.WaitSignal(t, func() error {
cmd := cmdFunc("touch", []string{testFileDelay}, nil) // shouldn't trigger an event
cmd := cmdFunc("touch", []string{testFileDelay1}, nil) // shouldn't trigger an event
if err := cmd.Run(); err != nil {
return err
}

time.Sleep(3 * time.Second)
cmd = cmdFunc("touch", []string{testFileDelay}, nil)
return cmd.Run()

for i := 0; i < 3; i++ {
cmd = cmdFunc("touch", []string{testFileDelay2}, nil)
if err := cmd.Run(); err != nil {
return err
}
time.Sleep(100 * time.Millisecond)
}
return nil
}, func(event *model.Event, rule *rules.Rule) {
assertTriggeredRule(t, rule, "test_container_created_at_delay")
assertFieldEqual(t, event, "open.file.path", testFileDelay)
assertFieldEqual(t, event, "open.file.path", testFileDelay2)
assertFieldNotEmpty(t, event, "container.id", "container id shouldn't be empty")
assert.Equal(t, event.CGroupContext.CGroupFlags, containerutils.CGroupFlags(containerutils.CGroupManagerDocker))
createdAtNano, _ := event.GetFieldValue("container.created_at")
Expand Down

0 comments on commit 8f57d30

Please sign in to comment.