WIP: filebeat: auto-build test binary in Go integration tests via TestMain#49583
WIP: filebeat: auto-build test binary in Go integration tests via TestMain#49583AndersonQ wants to merge 5 commits intoelastic:mainfrom
Conversation
🤖 GitHub commentsJust comment with:
|
There was a problem hiding this comment.
Pull request overview
This PR aims to make Filebeat Go integration tests runnable via go test -tags integration without requiring a prior mage buildSystemTestBinary, by moving the filebeat.test build step into TestMain and removing the explicit mage dependency.
Changes:
- Added a
BuildSystemTestBinary(binPath, packagePath)helper to the Go integration test framework. - Added
TestMaininfilebeat/tests/integrationandx-pack/filebeat/tests/integrationto auto-buildfilebeat.test. - Removed system test binary build steps from Filebeat mage Go integration test targets.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
libbeat/tests/integration/framework.go |
Adds a helper to build the system test binary via go test -c. |
filebeat/tests/integration/integration_test.go |
Adds TestMain to build filebeat.test before running tests in this package. |
x-pack/filebeat/tests/integration/integration_test.go |
Adds TestMain to build the x-pack filebeat.test before running tests in this package. |
filebeat/magefile.go |
Removes BuildSystemTestBinary wrapper and drops binary build dependency from Go integ mage targets. |
x-pack/filebeat/magefile.go |
Drops binary build step from Go integ mage targets. |
Comments suppressed due to low confidence (1)
x-pack/filebeat/magefile.go:172
- GoIntegTest/GoFIPSOnlyIntegTest no longer build the system test binary before running Go integration tests. Because
go testruns packages in parallel and multiple x-pack/filebeat integration packages require ../../filebeat.test to exist, this will likely fail unless binary creation is coordinated elsewhere (e.g. a shared EnsureSystemTestBinary with locking). Consider restoring the build dependency here or ensuring the new TestMain-based build is reliable across all packages.
// GoIntegTest starts the docker containers and executes the Go integration tests.
func GoIntegTest(ctx context.Context) error {
args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
// ES_USER must be admin in order for the Go Integration tests to function because they require
// indices:data/read/search
args.Env["ES_USER"] = args.Env["ES_SUPERUSER_USER"]
args.Env["ES_PASS"] = args.Env["ES_SUPERUSER_PASS"]
return devtools.GoIntegTestFromHost(ctx, args)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func TestMain(m *testing.M) { | ||
| binPath, err := filepath.Abs("../../filebeat.test") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| packagePath, err := filepath.Abs("../../") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err) | ||
| os.Exit(1) | ||
| } |
There was a problem hiding this comment.
Building ../../filebeat.test in TestMain for this single package doesn’t guarantee the binary exists before other integration-tagged packages run. go test -tags integration ./... executes packages in parallel, and several other packages (e.g. filebeat/input/filestream, filebeat/tests/integration/* is not the only user) call integration.NewBeat with "../../filebeat.test" and currently fail fast if it’s missing. Consider moving the auto-build into a shared helper (e.g. integration.NewBeat or a dedicated EnsureSystemTestBinary with cross-process locking) so every package that needs the binary can reliably trigger/await the build, or keep a pre-step in the mage GoIntegTest targets.
| binPath, err := filepath.Abs("../../filebeat.test") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| packagePath, err := filepath.Abs("../../") | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err) | ||
| os.Exit(1) | ||
| } | ||
| if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil { | ||
| fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err) | ||
| os.Exit(1) | ||
| } |
There was a problem hiding this comment.
Same concurrency issue as in OSS filebeat: building ../../filebeat.test only from this package’s TestMain is racy under go test -tags integration ./... because other integration packages (e.g. x-pack/filebeat/input/* integration tests) may start in parallel and require the binary to already exist. Prefer a shared EnsureSystemTestBinary that can be called by all packages (or from integration.NewBeat) and coordinates via a file lock / sync.Once.
| binPath, err := filepath.Abs("../../filebeat.test") | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err) | |
| os.Exit(1) | |
| } | |
| packagePath, err := filepath.Abs("../../") | |
| if err != nil { | |
| fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err) | |
| os.Exit(1) | |
| } | |
| if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil { | |
| fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err) | |
| os.Exit(1) | |
| } | |
| if err := integration.EnsureSystemTestBinary(); err != nil { | |
| fmt.Fprintf(os.Stderr, "failed to ensure filebeat test binary: %s\n", err) | |
| os.Exit(1) | |
| } |
| args = append(args, packagePath) | ||
|
|
||
| cmd := exec.Command("go", args...) |
There was a problem hiding this comment.
When TEST_COVERAGE=true, -coverpkg ./... is evaluated relative to the current working directory. Since this helper can be invoked from nested test package directories, coverage instrumentation will likely target the wrong package set compared to the mage build (which runs from the beat root). Set cmd.Dir (e.g. to packagePath or the module root) and/or make -coverpkg refer to the intended module path so coverage results are consistent.
| args = append(args, packagePath) | |
| cmd := exec.Command("go", args...) | |
| // Ensure that coverage patterns like ./... are evaluated relative to packagePath. | |
| absPackagePath, err := filepath.Abs(packagePath) | |
| if err != nil { | |
| return err | |
| } | |
| // Run "go test" in the target package directory, addressing it as ".". | |
| args = append(args, ".") | |
| cmd := exec.Command("go", args...) | |
| cmd.Dir = absPackagePath |
| // GoIntegTest starts the docker containers and executes the Go integration tests. | ||
| func GoIntegTest(ctx context.Context) error { | ||
| mg.Deps(BuildSystemTestBinary) | ||
| return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs(ctx)) | ||
| } |
There was a problem hiding this comment.
GoIntegTest no longer ensures the system test binary exists before running go test -tags integration ./.... Given packages run in parallel and many integration tests require ../../filebeat.test to exist (integration.NewBeat requires it), this can make mage GoIntegTest flaky/fail unless the binary is built elsewhere in a coordinated way. Either keep a pre-build dependency here or introduce a robust shared auto-build mechanism used by all packages before starting tests.
Move the filebeat.test binary build step from the mage targets into TestMain so that `go test -tags integration` works without requiring a prior `mage buildSystemTestBinary`. This removes a manual prerequisite that tripped up developers running integration tests locally. - Add BuildSystemTestBinary helper to libbeat/tests/integration/framework.go - Add TestMain in filebeat and x-pack/filebeat integration test packages - Remove BuildSystemTestBinary from mage GoIntegTest/GoFIPSOnlyIntegTest - Python integration tests are unchanged (they still use devtools directly) Assisted by Claude
70a227f to
4a597bb
Compare
Proposed commit message
Checklist
stresstest.shscript to run them under stress conditions and race detector to verify their stability../changelog/fragmentsusing the changelog tool.Disruptive User Impact
Author's Checklist
How to test this PR locally
Related issues
Use cases
Screenshots
Logs