Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: rewrite pkg/sensors/exec:TestExitCode #1015

Merged
merged 1 commit into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 21 additions & 39 deletions pkg/sensors/exec/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,10 @@ func TestExitCode(t *testing.T) {
observer.LoopEvents(ctx, t, &doneWG, &readyWG, obs)
readyWG.Wait()

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

var exitCode, realCode uint32
testExitCodeBinary := testutils.RepoRootPath("contrib/tester-progs/exit-code")

// Test different exit codes
testCases := []int{
testCases := []int8{
// Test some usual exit code
0, // Generic success code.
1, // Generic failure or unspecified error.
Expand All @@ -220,45 +218,29 @@ func TestExitCode(t *testing.T) {
-34, // -ERANGE(Math result not representable)
}

for _, tc := range testCases {
// The test executes 'exit-code' benary which return a exit code
if err := exec.Command(testExitCode, fmt.Sprintf("%d", tc)).Run(); err != nil {
realCode = 0
if exitErr, ok := err.(*exec.ExitError); ok {
// handle ExitError
realCode = uint32(exitErr.ExitCode())
} else {
t.Fatalf("Failed to execute test binary: %s\n", err)
}
}
checker := ec.NewUnorderedEventChecker()
testerBinaryCheck := ec.NewProcessChecker().WithBinary(sm.Suffix("tester-progs/exit-code"))

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 {
t.Logf("exitCode %v\n", exitCode)
for _, testCaseCode := range testCases {
// convert the code to an unsigned 8 bits integer (example: -30 -> 226)
// since Linux will return an unsigned integer after execution
expectedCode := uint8(testCaseCode)

if exitCode == realCode {
return nil
if err := exec.Command(testExitCodeBinary, fmt.Sprint(testCaseCode)).Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
if uint8(exitErr.ExitCode()) != expectedCode {
t.Errorf("unexpected: wanted exit code %d, execution returned %d", expectedCode, exitErr.ExitCode())
}
} else {
t.Fatalf("failed to execute the test binary %q with exit code %d", err, expectedCode)
}
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()
}
checker.AddChecks(
ec.NewProcessExitChecker(fmt.Sprintf("exitCode%d", expectedCode)).WithProcess(testerBinaryCheck).WithStatus(uint32(expectedCode)),
)
}

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