Skip to content

Commit

Permalink
test: process exit signal
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chen <[email protected]>
  • Loading branch information
justin0u0 authored and mtardy committed Oct 28, 2024
1 parent 7b04416 commit f12a340
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions contrib/tester-progs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ drop-privileges
change-capabilities
direct-write-tester
user-stacktrace
pause
3 changes: 2 additions & 1 deletion contrib/tester-progs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ PROGS = sigkill-tester \
getcpu \
direct-write-tester \
change-capabilities \
user-stacktrace
user-stacktrace \
pause


all: $(PROGS)
Expand Down
7 changes: 7 additions & 0 deletions contrib/tester-progs/pause.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <unistd.h>

int main(int argc, char** argv)
{
pause();
return 0;
}
56 changes: 56 additions & 0 deletions pkg/sensors/exec/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os/exec"
"sync"
"syscall"
"testing"
"time"

Expand All @@ -20,6 +21,7 @@ import (
tus "github.com/cilium/tetragon/pkg/testutils/sensors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)

func TestExit(t *testing.T) {
Expand Down Expand Up @@ -241,3 +243,57 @@ func TestExitCode(t *testing.T) {
err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}

// TestExitSignal tests whether we properly return the exit signal of the process.
// see: tester-progs/pause.c for the program we use to test this.
//
// The program will:
// - Pause until it receives a signal
//
// In our test we check whether the observed exit signal equals the real exit signal.
func TestExitSignal(t *testing.T) {
var doneWG, readyWG sync.WaitGroup
defer doneWG.Wait()

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

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

checker := ec.NewOrderedEventChecker()
testExitSignalBinary := testutils.RepoRootPath("contrib/tester-progs/pause")

for sig := 1; sig <= 15; sig++ {
signal := syscall.Signal(sig)
expectedSignal := unix.SignalName(signal)
cmd := exec.CommandContext(ctx, testExitSignalBinary)

if err := cmd.Start(); err != nil {
t.Fatalf("failed to execute the test binary with signal %q: %s", expectedSignal, err)
}

if err := cmd.Process.Signal(signal); err != nil {
t.Fatalf("failed to send signal %q to the test binary: %s", expectedSignal, err)
}

if err := cmd.Wait(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if got := exitErr.Sys().(syscall.WaitStatus).Signal(); got != signal {
t.Errorf("unexpected: wanted signal %q, execution returned %q", signal, got)
}
}
}

checker.AddChecks(
ec.NewProcessExitChecker(fmt.Sprintf("exitSignal=%s", expectedSignal)).WithSignal(sm.Full(expectedSignal)),
)
}

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

0 comments on commit f12a340

Please sign in to comment.