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

runtime and agent test improvements #2014

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions ee/agent/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@ func TestMain(m *testing.M) {
fmt.Printf("failed to make temp dir for test osquery binary: %v", err)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}
defer os.RemoveAll(downloadDir)

target := packaging.Target{}
if err := target.PlatformFromString(runtime.GOOS); err != nil {
fmt.Printf("error parsing platform %s: %v", runtime.GOOS, err)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
os.RemoveAll(downloadDir) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}
target.Arch = packaging.ArchFlavor(runtime.GOARCH)
if runtime.GOOS == "darwin" {
target.Arch = packaging.Universal
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

dlPath, err := packaging.FetchBinary(ctx, downloadDir, "osqueryd", target.PlatformBinaryName("osqueryd"), "nightly", target)
if err != nil {
fmt.Printf("error fetching binary osqueryd binary: %v", err)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
cancel() // explicit cancel as defer will not run when os.Exit is called
os.RemoveAll(downloadDir) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}

testOsqueryBinary = filepath.Join(downloadDir, filepath.Base(dlPath))
Expand All @@ -63,12 +64,17 @@ func TestMain(m *testing.M) {

if err := fsutil.CopyFile(dlPath, testOsqueryBinary); err != nil {
fmt.Printf("error copying osqueryd binary: %v", err)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
cancel() // explicit cancel as defer will not run when os.Exit is called
os.RemoveAll(downloadDir) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}

// Run the tests
retCode := m.Run()
os.Exit(retCode) //nolint:forbidigo // Fine to use os.Exit inside tests

cancel() // explicit cancel as defer will not run when os.Exit is called
os.RemoveAll(downloadDir) // explicit removal as defer will not run when os.Exit is called
os.Exit(retCode) //nolint:forbidigo // Fine to use os.Exit inside tests
}

func TestDetectAndRemediateHardwareChange(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/osquery/runtime/osqueryinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestCreateOsqueryCommand(t *testing.T) {
extensionAutoloadPath: "/foo/bar/osquery.autoload",
}

osquerydPath := testOsqueryBinaryDirectory
osquerydPath := testOsqueryBinary

k := typesMocks.NewKnapsack(t)
k.On("WatchdogEnabled").Return(true)
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestCreateOsqueryCommandWithFlags(t *testing.T) {
i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

cmd, err := i.createOsquerydCommand(
testOsqueryBinaryDirectory,
testOsqueryBinary,
&osqueryFilePaths{},
)
require.NoError(t, err)
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestCreateOsqueryCommand_SetsEnabledWatchdogSettingsAppropriately(t *testin
i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

cmd, err := i.createOsquerydCommand(
testOsqueryBinaryDirectory,
testOsqueryBinary,
&osqueryFilePaths{},
)
require.NoError(t, err)
Expand Down Expand Up @@ -170,7 +170,7 @@ func TestCreateOsqueryCommand_SetsDisabledWatchdogSettingsAppropriately(t *testi
i := newInstance(types.DefaultRegistrationID, k, mockServiceClient())

cmd, err := i.createOsquerydCommand(
testOsqueryBinaryDirectory,
testOsqueryBinary,
&osqueryFilePaths{},
)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/runtime/osqueryinstance_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestCreateOsqueryCommandEnvVars(t *testing.T) {
t.Parallel()

osquerydPath := testOsqueryBinaryDirectory
osquerydPath := testOsqueryBinary

k := typesMocks.NewKnapsack(t)
k.On("WatchdogEnabled").Return(true)
Expand Down
4 changes: 2 additions & 2 deletions pkg/osquery/runtime/runtime_posix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestOsquerySlowStart(t *testing.T) {
k.On("OsqueryFlags").Return([]string{}).Maybe()
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("LoggingInterval").Return(5 * time.Minute).Maybe()
k.On("LogMaxBytesPerBatch").Return(0).Maybe()
k.On("Transport").Return("jsonrpc").Maybe()
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestExtensionSocketPath(t *testing.T) {
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryVerbose").Return(true).Maybe()
k.On("OsqueryFlags").Return([]string{}).Maybe()
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("LoggingInterval").Return(5 * time.Minute).Maybe()
k.On("LogMaxBytesPerBatch").Return(0).Maybe()
k.On("Transport").Return("jsonrpc").Maybe()
Expand Down
37 changes: 22 additions & 15 deletions pkg/osquery/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"github.com/stretchr/testify/require"
)

var testOsqueryBinaryDirectory string
var testOsqueryBinary string

// TestMain overrides the default test main function. This allows us to share setup/teardown.
func TestMain(m *testing.M) {
Expand All @@ -54,30 +54,37 @@ func TestMain(m *testing.M) {
fmt.Println("Failed to make temp dir for test binaries")
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
}
defer os.Remove(binDirectory)

s, err := storageci.NewStore(nil, multislogger.NewNopLogger(), storage.OsqueryHistoryInstanceStore.String())
if err != nil {
fmt.Println("Failed to make new store")
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
os.Remove(binDirectory) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
}
if err := history.InitHistory(s); err != nil {
fmt.Println("Failed to init history")
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
os.Remove(binDirectory) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
}

testOsqueryBinaryDirectory = filepath.Join(binDirectory, "osqueryd")
testOsqueryBinary = filepath.Join(binDirectory, "osqueryd")
if runtime.GOOS == "windows" {
testOsqueryBinary += ".exe"
}

thrift.ServerConnectivityCheckInterval = 100 * time.Millisecond

if err := downloadOsqueryInBinDir(binDirectory); err != nil {
fmt.Printf("Failed to download osquery: %v\n", err)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
os.Remove(binDirectory) // explicit removal as defer will not run when os.Exit is called
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit in tests
}

// Run the tests!
retCode := m.Run()
os.Exit(retCode) //nolint:forbidigo // Fine to use os.Exit in tests

os.Remove(binDirectory) // explicit removal as defer will not run when os.Exit is called
os.Exit(retCode) //nolint:forbidigo // Fine to use os.Exit in tests
}

// downloadOsqueryInBinDir downloads osqueryd. This allows the test
Expand Down Expand Up @@ -163,7 +170,7 @@ func TestWithOsqueryFlags(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{"verbose=false"})
k.On("OsqueryVerbose").Return(false)
Expand Down Expand Up @@ -197,7 +204,7 @@ func TestFlagsChanged(t *testing.T) {
k.On("WatchdogDelaySec").Return(120)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{"verbose=false"})
k.On("OsqueryVerbose").Return(false)
Expand Down Expand Up @@ -328,7 +335,7 @@ func TestSimplePath(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{})
k.On("OsqueryVerbose").Return(true)
Expand Down Expand Up @@ -364,7 +371,7 @@ func TestMultipleInstances(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{})
k.On("OsqueryVerbose").Return(true)
Expand Down Expand Up @@ -423,7 +430,7 @@ func TestRunnerHandlesImmediateShutdownWithMultipleInstances(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{})
k.On("OsqueryVerbose").Return(true)
Expand Down Expand Up @@ -474,7 +481,7 @@ func TestMultipleShutdowns(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{})
k.On("OsqueryVerbose").Return(true)
Expand Down Expand Up @@ -506,7 +513,7 @@ func TestOsqueryDies(t *testing.T) {
k.On("WatchdogEnabled").Return(false)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything)
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory)
k.On("OsqueryFlags").Return([]string{})
k.On("OsqueryVerbose").Return(true)
Expand Down Expand Up @@ -608,7 +615,7 @@ func setupOsqueryInstanceForTests(t *testing.T) (runner *Runner, logBytes *threa
k.On("WatchdogDelaySec").Return(120)
k.On("RegisterChangeObserver", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Maybe()
k.On("Slogger").Return(slogger)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinaryDirectory)
k.On("LatestOsquerydPath", mock.Anything).Return(testOsqueryBinary)
k.On("RootDirectory").Return(rootDirectory).Maybe()
k.On("OsqueryFlags").Return([]string{}).Maybe()
k.On("OsqueryVerbose").Return(true).Maybe()
Expand Down
Loading