Skip to content

Commit

Permalink
tetragon: Add throttle exec/fork event test
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Olsa <[email protected]>
  • Loading branch information
olsajiri committed Mar 25, 2024
1 parent c6ebc31 commit c0d9fb9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
18 changes: 15 additions & 3 deletions pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ var (
)

type testObserverOptions struct {
crd bool
config string
lib string
crd bool
config string
lib string
cgroupRate string
}

type testExporterOptions struct {
Expand Down Expand Up @@ -124,6 +125,12 @@ func WithLib(lib string) TestOption {
}
}

func WithCgroupRate(rate string) TestOption {
return func(o *TestOptions) {
o.observer.cgroupRate = rate
}
}

func testDone(tb testing.TB, obs *observer.Observer) {
if tb.Failed() {
bugtoolFname := "/tmp/tetragon-bugtool.tar.gz"
Expand All @@ -138,6 +145,7 @@ func testDone(tb testing.TB, obs *observer.Observer) {

obs.PrintStats()
obs.Remove()
os.RemoveAll(option.Config.BpfDir)
}

// saveInitInfo saves initial info for subsequent use in bugtool
Expand Down Expand Up @@ -226,6 +234,10 @@ func getDefaultObserver(tb testing.TB, ctx context.Context, base *sensors.Sensor
option.Config.ProcFS = procfs
}

if o.observer.cgroupRate != "" {
option.Config.CgroupRate = option.ParseCgroupRate(o.observer.cgroupRate)
}

obs := newDefaultObserver(&o.observer)
if testing.Verbose() {
option.Config.Verbosity = 1
Expand Down
4 changes: 2 additions & 2 deletions pkg/option/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func ReadAndSetFlags() error {

Config.ExposeKernelAddresses = viper.GetBool(KeyExposeKernelAddresses)

Config.CgroupRate = parseCgroupRate(viper.GetString(KeyCgroupRate))
Config.CgroupRate = ParseCgroupRate(viper.GetString(KeyCgroupRate))
return nil
}

Expand All @@ -192,7 +192,7 @@ type CgroupRate struct {
Interval uint64
}

func parseCgroupRate(rate string) CgroupRate {
func ParseCgroupRate(rate string) CgroupRate {
empty := CgroupRate{}

if rate == "" {
Expand Down
48 changes: 48 additions & 0 deletions pkg/sensors/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1436,3 +1436,51 @@ 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()

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

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

// create the load 10 fork/exec per sec for 2 seconds
// to get THROTTLE START
for cnt := 0; cnt < 20; cnt++ {
if err := exec.Command("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(2 * time.Second)

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

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

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

0 comments on commit c0d9fb9

Please sign in to comment.