Skip to content

Commit

Permalink
test for retrieving exit code
Browse files Browse the repository at this point in the history
Signed-off-by: Haiyu Zuo <958474674@qq.com>
  • Loading branch information
zhy76 committed Mar 29, 2023
1 parent 2c9dc45 commit 5ba87f3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions contrib/tester-progs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ nop
sigkill-unprivileged-user-ns-tester
exit-leader
exit-tester
exit-code
libuprobe.so
uprobe-test-1
uprobe-test-2
7 changes: 7 additions & 0 deletions contrib/tester-progs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PROGS = sigkill-tester \
nop \
exit-leader \
exit-tester \
exit-code \
uprobe-test-1 \
uprobe-test-2

Expand All @@ -28,9 +29,15 @@ exit-tester: exit-tester.c
sigkill-unprivileged-user-ns-tester: sigkill-unprivileged-user-ns-tester.c
$(GCC) -Wall $< -o $@ -lcap

nop: nop.c
$(GCC) -Wall $< -o $@ -lpthread

exit-leader: exit-leader.c
$(GCC) -Wall $< -o $@ -lpthread

exit-code: exit-code.c
$(GCC) -Wall $< -o $@ -lpthread

libuprobe.so: uprobe-lib.c
$(GCC) -Wall -fPIC $< -o $@ -shared

Expand Down
4 changes: 4 additions & 0 deletions contrib/tester-progs/exit-code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int main(int argc, char **argv)
{
return 6;
}
67 changes: 67 additions & 0 deletions pkg/sensors/exec/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,70 @@ func TestExitZombie(t *testing.T) {
err = jsonchecker.JsonTestCheck(t, checker)
assert.NoError(t, err)
}

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

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

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

testExitCode := testutils.RepoRootPath("contrib/tester-progs/exit-code")

var exitCode, realCode uint32

if err := exec.Command(testExitCode).Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
// handle ExitError
realCode = uint32(exitErr.ExitCode())
} else {
t.Fatalf("Failed to execute test binary: %s\n", err)
}
}

// The test executes 'exit-code' benary which return a exit code 6
nextCheck := func(event ec.Event, l *logrus.Logger) (bool, error) {
switch ev := event.(type) {
case *tetragon.ProcessExit:
if ev.Process.Binary == testExitCode {
exitCode = ev.Status
}
return false, nil
}
return false, nil
}

finalCheck := func(l *logrus.Logger) error {
fmt.Printf("exitCode %v\n", exitCode)

if exitCode == realCode {
return nil
}
return fmt.Errorf("tetragon returns the exit code of the process uncorrectly")
}

checker := &ec.FnEventChecker{
NextCheckFn: nextCheck,
FinalCheckFn: finalCheck,
}

if err := jsonchecker.JsonTestCheck(t, checker); err != nil {
t.Logf("error: %s", err)
t.Fail()
}
}

0 comments on commit 5ba87f3

Please sign in to comment.