Skip to content

Commit

Permalink
tetragon: Add throttle exec/fork event test
Browse files Browse the repository at this point in the history
Adding throttle exec/fork event test that sets cgroup rate
limit and spawns bit more processes per second and triggers
throttle start event, then wait for throttle stop event.

Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed May 6, 2024
1 parent 416215c commit 058967b
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/sensors/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,3 +1449,55 @@ func TestExecDeletedBinary(t *testing.T) {
err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}

func testThrottle(t *testing.T) {
var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

throttleStartChecker := ec.NewProcessThrottleChecker("THROTTLE").
WithType(tetragon.ThrottleType_THROTTLE_START)

throttleStopChecker := ec.NewProcessThrottleChecker("THROTTLE").
WithType(tetragon.ThrottleType_THROTTLE_STOP)

checker := ec.NewUnorderedEventChecker(throttleStartChecker, throttleStopChecker)

ctx, cancel := context.WithTimeout(context.Background(), tus.Conf().CmdWaitTime)
defer cancel()

option.Config.CgroupRate = option.ParseCgroupRate("10,2s")
t.Cleanup(func() {
option.Config.CgroupRate = option.CgroupRate{}
})

obs, err := observertesthelper.GetDefaultObserver(t, ctx, tus.Conf().TetragonLib)
if err != nil {
t.Fatalf("Failed to run observer: %s", err)
}

observertesthelper.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

// create the load 40 fork/exec per sec for 4 seconds
// to get THROTTLE START
for cnt := 0; cnt < 40; cnt++ {
if err := exec.Command("taskset", "-c", "1", "sleep", "0.1s").Run(); err != nil {
t.Fatalf("Failed to execute test binary: %s\n", err)
}
}

// and calm down to get THROTTLE STOP
time.Sleep(8 * time.Second)

err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}

func TestThrottle1(t *testing.T) {
testThrottle(t)
}

// Run throttle twice to test the CgroupRate setup code
func TestThrottle2(t *testing.T) {
testThrottle(t)
}

0 comments on commit 058967b

Please sign in to comment.