Skip to content

Commit

Permalink
fix(tests): refactor Test_TraceeCapture
Browse files Browse the repository at this point in the history
This test was failing when other tests were run before it and failed,
tracee running was not stopped and some garbage was left in the system.

Two major changes were made:

1. Do not fail the test before stopping tracee.
2. Always remove capture files after the test (do cleanup).
  • Loading branch information
geyslan committed Jun 28, 2024
1 parent d5d1ca7 commit ee4376d
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions tests/integration/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"gotest.tools/assert"

"github.com/aquasecurity/tracee/tests/testutils"
)
Expand Down Expand Up @@ -91,16 +90,33 @@ func Test_TraceeCapture(t *testing.T) {
t.Fatal("tracee is already running")
}

var failed bool

captureDir := tc.directory + "/out/host/"
err := tc.test(t, captureDir, homeDir)
if err != nil {
t.Errorf("test %s failed: %v", tc.name, err)
cmdErrs := running.Stop() // stop tracee
require.Empty(t, cmdErrs)
t.Fail()
failed = true
t.Logf("test %s failed: %v", tc.name, err)
}
defer func() {
t.Logf("removing directory %s", tc.directory)
err := os.RemoveAll(tc.directory)
if err != nil {
t.Logf("failed to remove directory %s: %v", tc.directory, err)
}
}()

cmdErrs := running.Stop() // stop tracee
require.Empty(t, cmdErrs)
if len(cmdErrs) > 0 {
failed = true
t.Logf("failed to stop tracee: %v", cmdErrs)
} else {
t.Logf(" --- stopped tracee ---")
}

if failed {
t.Fail()
}
})
}
}
Expand Down Expand Up @@ -256,10 +272,12 @@ func readWritePipe(t *testing.T, captureDir string, workingDir string) error {
}()

finfo, err := pipe.Stat()
require.NoError(t, err)
if err != nil {
return err
}
statInfo, ok := finfo.Sys().(*syscall.Stat_t)
if !ok {
t.Logf("type assertion failed: expected *syscall.Stat_t")
return fmt.Errorf("type assertion failed: expected *syscall.Stat_t")
}
inode := statInfo.Ino

Expand Down Expand Up @@ -323,10 +341,17 @@ func assertEntries(t *testing.T, captureDir string, input string, readOut string
}
}
// Found 2 entries
assert.Equal(t, found, 2)
if found != 2 {
return fmt.Errorf("expected 2 entries in capture dir, found %d", found)
}

// Compare captured data to expected data
assert.Equal(t, input, string(writeCaptureFile))
assert.Equal(t, readOut, string(readCaptureFile))
if string(writeCaptureFile) != input {
return fmt.Errorf("expected write capture file %s, got %s", input, writeCaptureFile)
}
if string(readCaptureFile) != readOut {
return fmt.Errorf("expected read capture file %s, got %s", readOut, readCaptureFile)
}

return nil
}

0 comments on commit ee4376d

Please sign in to comment.