From ee4376d4308fe78da42b62347875afaddbd845b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geyslan=20Greg=C3=B3rio?= Date: Thu, 27 Jun 2024 13:59:47 -0300 Subject: [PATCH] fix(tests): refactor Test_TraceeCapture 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). --- tests/integration/capture_test.go | 47 +++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/tests/integration/capture_test.go b/tests/integration/capture_test.go index 134f6acecf67..6c1a642bbef4 100644 --- a/tests/integration/capture_test.go +++ b/tests/integration/capture_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/goleak" - "gotest.tools/assert" "github.com/aquasecurity/tracee/tests/testutils" ) @@ -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() + } }) } } @@ -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 @@ -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 }